youtube:cpp-intro-033
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
youtube:cpp-intro-033 [2024/02/28 18:26] – 削除 - 外部編集 (不明な日付) 127.0.0.1 | youtube:cpp-intro-033 [2024/02/29 11:52] (現在) – freemikan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== Tetris Take 1 ====== | ||
+ | |||
+ | 作成日: 2023-07-21 (金) | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== main.cpp ===== | ||
+ | |||
+ | <file cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | void draw_rectangle(SDL_Renderer *renderer, SDL_Rect const &rect, SDL_Color const &fc, SDL_Color const &oc) { | ||
+ | SDL_SetRenderDrawColor(renderer, | ||
+ | SDL_RenderFillRect(renderer, | ||
+ | SDL_SetRenderDrawColor(renderer, | ||
+ | SDL_RenderDrawRect(renderer, | ||
+ | } | ||
+ | |||
+ | void draw_fence(SDL_Renderer *renderer, int a_width, int a_height, int c_width, int c_height) { | ||
+ | SDL_Rect rect{0, 0, c_width, c_height}; | ||
+ | SDL_Color color{128, 128, 128, 255}; | ||
+ | SDL_Color outline_color{255, | ||
+ | |||
+ | // draw left wall | ||
+ | for (int i = 0; i < a_height; ++i) { | ||
+ | rect.x = 0; | ||
+ | rect.y = i * c_height; | ||
+ | draw_rectangle(renderer, | ||
+ | } | ||
+ | |||
+ | // draw right wall | ||
+ | for (int i = 0; i < a_height; ++i) { | ||
+ | rect.x = (a_width + 1) * c_width; | ||
+ | rect.y = i * c_height; | ||
+ | draw_rectangle(renderer, | ||
+ | } | ||
+ | |||
+ | // draw floor | ||
+ | for (int i = 0; i < a_width + 2; ++i) { | ||
+ | rect.x = i * c_width; | ||
+ | rect.y = a_height * c_height; | ||
+ | draw_rectangle(renderer, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main(int argc, char **argv) { | ||
+ | int const ScreenWidth = 400; // px | ||
+ | int const ScreenHeight = 510; // px | ||
+ | int const ArenaWidth = 10; // cell count | ||
+ | int const ArenaHeight = 20; // cell count | ||
+ | int const CellWidth = 24; // px | ||
+ | int const CellHeight = 24; // px | ||
+ | |||
+ | if (SDL_Init(SDL_INIT_VIDEO) < 0) { | ||
+ | std::cerr << " | ||
+ | std:: | ||
+ | } | ||
+ | | ||
+ | SDL_Window *window = SDL_CreateWindow( | ||
+ | " | ||
+ | SDL_WINDOWPOS_CENTERED, | ||
+ | SDL_WINDOWPOS_CENTERED, | ||
+ | ScreenWidth, | ||
+ | ScreenHeight, | ||
+ | 0); | ||
+ | if (window == nullptr) { | ||
+ | std::cerr << " | ||
+ | std:: | ||
+ | } | ||
+ | | ||
+ | SDL_Renderer *renderer = SDL_CreateRenderer(window, | ||
+ | if (renderer == nullptr) { | ||
+ | std::cerr << " | ||
+ | std:: | ||
+ | } | ||
+ | | ||
+ | bool running = true; | ||
+ | while (running) { | ||
+ | SDL_Event event; | ||
+ | while (SDL_PollEvent(& | ||
+ | if (event.type == SDL_QUIT) { | ||
+ | running = false; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | SDL_SetRenderDrawColor(renderer, | ||
+ | SDL_RenderClear(renderer); | ||
+ | |||
+ | draw_fence(renderer, | ||
+ | | ||
+ | SDL_RenderPresent(renderer); | ||
+ | | ||
+ | SDL_Delay(1); | ||
+ | } | ||
+ | | ||
+ | SDL_DestroyRenderer(renderer); | ||
+ | SDL_DestroyWindow(window); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Makefile ===== | ||
+ | |||
+ | <file makefile> | ||
+ | SRCS = \ | ||
+ | main.cpp | ||
+ | OBJS = ${SRCS: | ||
+ | DEPS = ${SRCS: | ||
+ | EXECUTABLE = tetris.exe | ||
+ | CXXFLAGS = -IC: | ||
+ | LDFLAGS = -LC: | ||
+ | LDLIBS = -lmingw32 -lSDL2main -lSDL2 | ||
+ | |||
+ | .PHONY: all | ||
+ | all: ${EXECUTABLE} | ||
+ | |||
+ | ${EXECUTABLE}: | ||
+ | ${CXX} -o $@ $^ ${LDFLAGS} ${LDLIBS} | ||
+ | |||
+ | %.o: %.cpp | ||
+ | ${CXX} -c -o $@ $< -MMD -MP ${CXXFLAGS} | ||
+ | |||
+ | .PHONY: clean | ||
+ | clean: | ||
+ | ${RM} ${EXECUTABLE} ${OBJS} ${DEPS} | ||
+ | |||
+ | -include ${DEPS} | ||
+ | </ | ||
+ | |||
+ | '' | ||