aboutsummaryrefslogtreecommitdiff
path: root/src/screen.h
blob: 9aa242d98272b0d0c1fbc814606b8c3559757740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <SDL2/SDL.h>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include "constants.h"

// Game Screen Class -> Uses SDL2
class GameScreen {
    SDL_Window *window{};
    SDL_Renderer *renderer{};
    SDL_bool done;
    std::vector<SDL_FPoint> points;
    std::vector<SDL_Color> colors;
    SDL_Event event{};

public:
    GameScreen() {
        SDL_Init(SDL_INIT_VIDEO);
        SDL_CreateWindowAndRenderer(SCREEN_WIDTH * SCALE_FACTOR,
                                    SCREEN_HEIGHT * SCALE_FACTOR,
                                    0, &window, &renderer);
        SDL_SetWindowTitle(window, "Game of Life");
        SDL_RenderSetScale(renderer, SCALE_FACTOR, SCALE_FACTOR);
        done = SDL_FALSE;
    }

    void drawPixel(double xm, double ym, uint8_t r = 255, uint8_t g = 255, uint8_t b = 255, uint8_t a = 255) {
        points.push_back({static_cast<float>(xm), static_cast<float>(ym)});
        colors.push_back({r, g, b, a});
    }

    void clearPixels() {
        points.clear();
    }

    void update() {
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        for (long unsigned int i = 0; i < points.size(); ++i) {
            SDL_SetRenderDrawColor(renderer, colors[i].r, colors[i].g, colors[i].b, colors[i].a);
            SDL_RenderDrawPointF(renderer, points[i].x, points[i].y);
        }

        SDL_RenderPresent(renderer);
    }

    void input() {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_QUIT:
                    done = SDL_TRUE;
                    SDL_Quit();
                    exit(0);
                    break;
                default:
                    break;
            }
        }
    }
};