blob: ae9409d75a485b8cd290779a9d0ac4882130bdc3 (
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
|
#include "graphics.h"
#define WINDOW_TITLE "Title"
static const char *g_sdl_error_str;
#define SDL_CALL(x) \
SDL_ClearError(); \
x; \
g_sdl_error_str = SDL_GetError(); \
if (*g_sdl_error_str != '\0') { \
SDL_Log("ERROR SDL: %s", g_sdl_error_str); \
exit(EXIT_FAILURE); \
}
static void update(t_state *state);
static void event_handler(t_state *state);
void graphics_init(t_state *state, int width, int height)
{
SDL_CALL(SDL_Init(SDL_INIT_VIDEO));
SDL_CALL(state->window = SDL_CreateWindow(
WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
0
));
SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0));
state->running = true;
}
void graphics_quit(t_state *state)
{
SDL_DestroyRenderer(state->renderer);
SDL_DestroyWindow(state->window);
SDL_Quit();
}
void graphics_run(t_state *state)
{
while (state->running)
{
event_handler(state);
update(state);
SDL_Delay(3);
}
}
static
void update(t_state *state)
{
// do stuff
}
static
void event_handler(t_state *state)
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
switch (e.type)
{
case SDL_QUIT:
state->running = false;
break;
}
}
}
|