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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/30ff0bf5-ae19-78a5-2c53-d19ddce00ec5.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/9e6f96a8-b4a8-fd67-379c-09c285a31aaf.png)

## 2. Download & Put SDL
公式サイトからダウンロードしたSDLを解凍。
解凍後のファイルをコピー。
SDL2-2.0.12\include
↓
$(SolutionDir)\include

SDL2-2.0.12\lib\x64
↓
$(SolutionDir)\lib\x64

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/954bb534-ef61-7778-b16d-fdc9ef84b82b.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/e3099002-633a-d735-3e73-b89b3659cf10.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/6cce1116-6bdb-6061-c611-e981f1459dd2.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/81b3b17e-925e-fe76-13d3-3abd1b8082d7.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/2f41d5d8-aa6a-17e3-5656-0f029f8da1f4.png)

## 4. Code Hello World sample
新規ソースファイルのmain.cppで下記のソースコードを実装。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/180af3fe-61c7-7381-cd3c-091c2df904e9.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/6c53bbcc-5ef2-aaed-6244-0e77179bd844.png)

## 6. Display Hello World

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/674299/ed35a236-65fd-09b4-7814-4d3e617e1034.png)