-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.c
177 lines (133 loc) · 4.2 KB
/
gui.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "gui.h"
#include <SDL2/SDL_image.h>
#include "config.h"
#include "entity.h"
static inline Uint8 html_red(int html);
static inline Uint8 html_green(int html);
static inline Uint8 html_blue(int html);
result init_display(Display *disp, Scene *scene)
{
SDL_Window *win = NULL;
SDL_Renderer *render = NULL;
TTF_Font *font = NULL;
if (SDL_Init(SDL_INIT_VIDEO) != 0) goto_fail(SDL_GetError());
if (TTF_Init() != 0) goto_fail(TTF_GetError());
win = SDL_CreateWindow(
TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
scene->size.w / PIXEL_SCALE,
scene->size.h / PIXEL_SCALE,
WINDOW_FLAGS);
if (win == NULL) goto_fail(SDL_GetError());
render = SDL_CreateRenderer(win, -1, RENDERER_FLAGS);
if (render == NULL) goto_fail(SDL_GetError());
font = TTF_OpenFont(FONT, 16);
if (font == NULL) goto_fail(TTF_GetError());
TTF_SetFontStyle(font, TTF_STYLE_BOLD);
disp->win = win;
disp->render = render;
disp->font = font;
disp->font_colour.r = 0xff;
disp->font_colour.b = 0x00;
disp->font_colour.g = 0x00;
disp->font_colour.a = 0x80;
return SUCCESS;
fail:
if (font != NULL) TTF_CloseFont(font);
if (render != NULL) SDL_DestroyRenderer(render);
if (win != NULL) SDL_DestroyWindow(win);
TTF_Quit();
SDL_Quit();
return FAILURE;
}
void quit_display(Display *disp)
{
if (disp->font != NULL) TTF_CloseFont(disp->font);
if (disp->render != NULL) SDL_DestroyRenderer(disp->render);
if (disp->win != NULL) SDL_DestroyWindow(disp->win);
TTF_Quit();
SDL_Quit();
}
result draw_scene(Display *disp, Scene *scene)
{
SDL_Rect dest;
int sdl_return = 0;
EntityLink *current = scene->entities;
Entity *entity = NULL;
sdl_return = SDL_SetRenderDrawColor(
disp->render,
html_red(scene->bg_colour),
html_green(scene->bg_colour),
html_blue(scene->bg_colour),
0xff);
if (sdl_return < 0) goto_fail(SDL_GetError());
sdl_return = SDL_RenderClear(disp->render);
if (sdl_return < 0) goto_fail(SDL_GetError());
sdl_return = SDL_SetRenderDrawColor(disp->render, 0, 0, 0, 0xff);
if (sdl_return < 0) goto_fail(SDL_GetError());
while (current != NULL)
{
entity = current->this;
dest.x = entity->pos.x / PIXEL_SCALE;
dest.y = entity->pos.y / PIXEL_SCALE;
dest.w = entity->size.w / PIXEL_SCALE;
dest.h = entity->size.h / PIXEL_SCALE;
sdl_return = SDL_RenderCopy(disp->render, entity->img, NULL, &dest);
if (sdl_return < 0) goto_fail(SDL_GetError());
current = current->next;
}
return SUCCESS;
fail:
return FAILURE;
}
SDL_Texture *load_image(Display *disp, const char *filename)
{
SDL_Surface *img_surface;
SDL_Texture *img_texture;
img_surface = IMG_Load(filename);
if (img_surface == NULL) goto_fail(IMG_GetError());
img_texture = SDL_CreateTextureFromSurface(disp->render, img_surface);
SDL_FreeSurface(img_surface);
if (img_texture == NULL) goto_fail(SDL_GetError());
return img_texture;
fail:
return NULL;
}
result draw_text(Display *disp, const char *text, int x, int y)
{
SDL_Surface *surface = NULL;
SDL_Texture *texture = NULL;
SDL_Rect dest;
surface = TTF_RenderText_Solid(disp->font, text, disp->font_colour);
if (surface == NULL) goto_fail(TTF_GetError());
texture = SDL_CreateTextureFromSurface(disp->render, surface);
if (texture == NULL) goto_fail(SDL_GetError());
dest.x = x;
dest.y = y;
dest.w = surface->w;
dest.h = surface->h;
if (SDL_RenderCopy(disp->render, texture, NULL, &dest) != 0)
{
goto_fail(SDL_GetError());
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
return SUCCESS;
fail:
if (texture != NULL) SDL_DestroyTexture(texture);
if (surface != NULL) SDL_FreeSurface(surface);
return FAILURE;
}
static inline Uint8 html_red(int html)
{
return (Uint8)((0xff0000 & html) >> 16);
}
static inline Uint8 html_green(int html)
{
return (Uint8)((0x00ff00 & html) >> 8);
}
static inline Uint8 html_blue(int html)
{
return (Uint8)(0x0000ff & html);
}