ユーザ用ツール

サイト用ツール


youtube:cpp-intro-031

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
youtube:cpp-intro-031 [2024/02/28 18:26] – 削除 - 外部編集 (不明な日付) 127.0.0.1youtube:cpp-intro-031 [2024/02/29 11:52] (現在) freemikan
行 1: 行 1:
 +====== HelloSDL-window ======
 +
 +作成日: 2023-07-21 (金)
 +
 +[[https://youtu.be/x2xPsXAY50o|初心者によるC++入門 #31 SDLのウィンドウを表示する]]
 +
 +{{:youtube:hellosdl-window.png?400|}}
 +
 +===== main.cpp =====
 +
 +<file cpp>
 +#include <SDL2/SDL.h>
 +#include <iostream>
 +
 +int main(int argc, char **argv) {
 +    int const ScreenWidth = 400;
 +    int const ScreenHeight = 400;
 +
 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
 +        std::cerr << "Error: init SDL\n";
 +        std::exit(1);
 +    }
 +    
 +    SDL_Window *window = SDL_CreateWindow(
 +            "HelloSDL window",
 +            SDL_WINDOWPOS_CENTERED,
 +            SDL_WINDOWPOS_CENTERED,
 +            ScreenWidth,
 +            ScreenHeight,
 +            0);
 +    if (window == nullptr) {
 +        std::cerr << "Error: create window\n";
 +        std::exit(1);
 +    }
 +    
 +    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
 +    if (renderer == nullptr) {
 +        std::cerr << "Error: create renderer\n";
 +        std::exit(1);
 +    }
 +    
 +    bool running = true;
 +    while (running) {
 +        SDL_Event event;
 +        while (SDL_PollEvent(&event)) {
 +            if (event.type == SDL_QUIT) {
 +                running = false;
 +            }
 +        }
 +        
 +        SDL_SetRenderDrawColor(renderer, 100, 100, 255, 255);
 +        SDL_RenderClear(renderer);
 +        
 +        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
 +        SDL_Rect rect{100, 150, 150, 100};
 +        SDL_RenderFillRect(renderer, &rect);
 +        
 +        SDL_RenderPresent(renderer);
 +        
 +        SDL_Delay(1);
 +    }
 +    
 +    SDL_DestroyRenderer(renderer);
 +    SDL_DestroyWindow(window);
 +
 +    return 0;
 +}
 +</file>
 +
 +===== Makefile =====
 +
 +<file makefile>
 +SRCS = \
 + main.cpp
 +OBJS = ${SRCS:%.cpp=%.o}
 +DEPS = ${SRCS:%.cpp=%.d}
 +EXECUTABLE = HelloSDL-window.exe
 +CXXFLAGS = -IC:\SDL2\include
 +LDFLAGS = -LC:\SDL2\lib
 +LDLIBS = -lmingw32 -lSDL2main -lSDL2
 +
 +.PHONY: all
 +all: ${EXECUTABLE}
 +
 +${EXECUTABLE}: ${OBJS}
 + ${CXX} -o $@ $^ ${LDFLAGS} ${LDLIBS}
 +
 +%.o: %.cpp
 + ${CXX} -c -o $@ $< -MMD -MP ${CXXFLAGS}
 +
 +.PHONY: clean
 +clean:
 + ${RM} ${EXECUTABLE} ${OBJS} ${DEPS}
 +
 +-include ${DEPS}
 +</file>
 +
 +''C:\SDL2''の部分は、SDLをインストールした場所を指定します。
  

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki