aboutsummaryrefslogtreecommitdiff
path: root/graphics.cpp
blob: 14134dd24e3035d71c8e2c108dbdf230958eae2b (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
#include <iostream>
#include <SDL2/SDL.h>
#include "graphics.hpp"

#define WINDOW_TITLE "Title"
#define WINDOW_X 20
#define WINDOW_Y 20

Graphics::Graphics()
{
	window = NULL;
	renderer = NULL;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
		error_quit("init");
	window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_X, WINDOW_Y, 640, 480, 0);
	if (window == NULL)
		error_quit("window init");
	renderer = SDL_CreateRenderer(window, -1, 0);
	if (renderer == NULL)
		error_quit("renderer init");
	running = true;
}

void Graphics::run()
{
	while (running)
	{
		event_handler();
		// update(state);
		SDL_Delay(10);
	}
}

Graphics::~Graphics()
{
	if (renderer != NULL)
		SDL_DestroyRenderer(renderer);
	if (window != NULL)
		SDL_DestroyWindow(window);
	if (SDL_WasInit(SDL_INIT_VIDEO))
		SDL_Quit();
}

void Graphics::event_handler()
{
	SDL_Event e;

	while (SDL_PollEvent(&e))
		switch (e.type)
		{
			case SDL_QUIT:
				running = false;
				break;
		}
}

void Graphics::error_quit(std::string msg)
{
	// ~Graphics();
	std::cerr << "ERROR: " << msg << std::endl;
	exit(1);
}