Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@tkc1009

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

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

2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?