3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SDL2 × C++ でHello Worldをやってみた

Posted at

image.png

0. Prepare Today's environment

Windows 10
Microsoft Visual Studio Community 2019 Version 16.7.5
SDL version 2.0.12
→Simple DirectMedia Layerの略。

1. Create a new project

空のプロジェクトを作成。

image.png

2. Download & Put SDL

公式サイトからダウンロードしたSDLを解凍。
解凍後のファイルをコピー。
SDL2-2.0.12\include

$(SolutionDir)\include

SDL2-2.0.12\lib\x64

$(SolutionDir)\lib\x64

image.png

3. Set SDL location into properties

プロパティの「追加のインクルードディレクトリ」で下記のパスを設定。
$(SolutionDir)include;%(AdditionalIncludeDirectories)

image.png

プロパティの「追加のライブラリディレクトリ」で下記のパスを設定。
$(SolutionDir)lib\$(PlatformTarget);%(AdditionalLibraryDirectories)

image.png

プロパティの「追加の依存ファイル」で下記のファイルを設定。
SDL2.lib;SDL2main.lib;%(AdditionalDependencies)

image.png

プロパティの「ビルド後のイベント」で下記のコマンドを設定。
xcopy /y /d "$(SolutionDir)lib\$(PlatformTarget)\SDL2.dll" "$(OutDir)"

image.png

4. Code Hello World sample

新規ソースファイルのmain.cppで下記のソースコードを実装。

image.png

#include <iostream>
#include "SDL.h"
using namespace std;

int main(int argc, char* argv[])
{
    const int SCREEN_WIDTH = 800;
    const int SCREEN_HEIGHT = 600;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        cout << "SDL init failed." << endl;
        return 1;
    }

    // initialize screen
    SDL_Window* window = SDL_CreateWindow("Hello world !!", SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    if (window == NULL) {
        SDL_Quit();
        return 2;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
    SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
        SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);

    if (renderer == NULL) {
        cout << "Could not create renderer" << endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 3;
    }

    if (texture == NULL) {
        cout << "Could not create texture" << endl;
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 4;
    }

    // prepare memory
    Uint32* buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];

    memset(buffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(Uint32));

    // set every 4bytes as color into memory
    for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
        buffer[i] = 0x008080FF;
    }

    // update screen
    SDL_UpdateTexture(texture, NULL, buffer, SCREEN_WIDTH * sizeof(Uint32));
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    bool quit = false;
    SDL_Event event;

    while (!quit) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            };
        }
    }

    // finalize screen
    delete[] buffer;
    SDL_DestroyRenderer(renderer);
    SDL_DestroyTexture(texture);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

5. Run Local Debugger

実行(ローカルWindowsデバッガー)ボタンを押下。

image.png

6. Display Hello World

タイトルバー「Hello World!」となった緑っぽい(0x008080)画面が表示。

image.png

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?