目次

Tetris Take 1

作成日: 2023-07-21 (金)

初心者によるC++入門 #33 壁と床を描く

main.cpp

#include <SDL2/SDL.h>
#include <iostream>

void draw_rectangle(SDL_Renderer *renderer, SDL_Rect const &rect, SDL_Color const &fc, SDL_Color const &oc) {
    SDL_SetRenderDrawColor(renderer, fc.r, fc.g, fc.b, fc.a);
    SDL_RenderFillRect(renderer, &rect);
    SDL_SetRenderDrawColor(renderer, oc.r, oc.g, oc.b, oc.a);
    SDL_RenderDrawRect(renderer, &rect);
}

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, 255, 255, 255};

        // draw left wall
        for (int i = 0; i < a_height; ++i) {
            rect.x = 0;
            rect.y = i * c_height;
            draw_rectangle(renderer, rect, color, outline_color);
        }

        // 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, rect, color, outline_color);
        }

        // 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, rect, color, outline_color);
        }
}

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 << "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);

        draw_fence(renderer, ArenaWidth, ArenaHeight, CellWidth, CellHeight);
        
        SDL_RenderPresent(renderer);
        
        SDL_Delay(1);
    }
    
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    return 0;
}

Makefile

SRCS = \
	main.cpp
OBJS = ${SRCS:%.cpp=%.o}
DEPS = ${SRCS:%.cpp=%.d}
EXECUTABLE = tetris.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}

C:\SDL2の部分は、SDLをインストールした場所を指定します。