aboutsummaryrefslogtreecommitdiff
path: root/src/graphics.cpp
blob: c434bcc6e55ee2c394cdc2275fb3c05682cbe9d6 (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
63
64
65
66
67
68
69
70
71
72
73
74
#include "graphics.hpp"

#define UPDATE_DELAY 3

Graphics::Graphics(Game &game, std::string title, int width, int height):
    m_running(true), m_game(game), m_title(title), m_width(width), m_height(height)
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        error();
    if ((m_window = SDL_CreateWindow(m_title.c_str(), 0, 0, m_width, m_height, 0)) == NULL)
        error();
    if ((m_renderer = SDL_CreateRenderer(m_window, -1, 0)) == NULL)
        error();
}

Graphics::~Graphics()
{
    SDL_DestroyRenderer(m_renderer);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

bool Graphics::isRunning() const
{
    return m_running;
}

void Graphics::update()
{
    SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(m_renderer);
    handleEvent();
    drawGame();
    SDL_RenderPresent(m_renderer);
    SDL_Delay(UPDATE_DELAY);
}

void Graphics::drawGame()
{

}

void Graphics::handleEvent()
{
    SDL_Event e;

    while (SDL_PollEvent(&e))
    {
        switch (e.type)
        {
            case SDL_QUIT:
                m_running = false;
                break;
            case SDL_KEYDOWN:
                switch (e.key.keysym.sym)
                {
                    case SDLK_LEFT:
                        break;
                    case SDLK_RIGHT:
                        break;
                    case SDLK_DOWN:
                        break;
                    case SDLK_UP:
                        break;
                }
        }
    }
}

void Graphics::error() const
{
    std::cerr << SDL_GetError() << std::endl;
    exit(1);
}