/
renderer.c
391 lines (298 loc) · 11 KB
/
renderer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <SDL2/SDL.h>
#include <assert.h>
#include <math.h>
#include "renderer.h"
#include "world.h"
#include "projectile.h"
#include "pi.h"
#include "team_id.h"
#include "text_renderer.h"
#include "config.h"
const float RAD2DEGf = 360 / PI2;
const float PROJECTILE_IMPACT_HALF_SIZE = 3.0f;
const int HEALTH_BAR_WIDTH = 32;
const int HEALTH_BAR_HEIGHT = 4;
#define UI_PADDING 128
#define MAP_PADDING 128
typedef union { Uint32 color; struct { Uint8 b; Uint8 g; Uint8 r; }; } RendererColor;
static SDL_Window * create_sdl_window() {
SDL_Window * sdl_window = SDL_CreateWindow("SDL Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
INITIAL_WINDOW_WIDTH, INITIAL_WINDOW_HEIGHT,
SDL_WINDOW_RESIZABLE|SDL_WINDOW_ALLOW_HIGHDPI|SDL_WINDOW_OPENGL);
assert(sdl_window);
return sdl_window;
}
void renderer_initialize(Renderer * renderer) {
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
renderer->window = create_sdl_window();
renderer->gl_context = SDL_GL_CreateContext(renderer->window);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
renderer_notify_viewport_resized(renderer);
renderer->camera = (Vector3) { INITIAL_CAMERA_X, INITIAL_CAMERA_Y, INITIAL_CAMERA_Z };
}
void renderer_clear_color(Renderer * renderer, float r, float g, float b) {
glClearColor(r, g, b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
static float renderer_scale(Renderer * renderer) {
return 1.0f / (1.0f + renderer->camera.z);
}
static void set_gl_team_color_alpha(int team_id, unsigned char alpha) {
RendererColor color = { TEAM_COLOR[team_id] };
glColor4ub(color.r, color.g, color.b, alpha);
}
static void set_gl_team_color(int team_id) {
set_gl_team_color_alpha(team_id, 255);
}
void renderer_render_unit(Unit * unit) {
glPushMatrix();
glTranslatef(unit->position.x, unit->position.y, 0.0f);
set_gl_team_color(unit->team_id);
glRotatef(unit->direction * RAD2DEGf, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-16.0f, -12.0f);
glVertex2f( 16.0f, -12.0f);
glVertex2f( 16.0f, 12.0f);
glVertex2f(-16.0f, 12.0f);
glEnd();
float health = unit_health_percentage(unit);
glBegin(GL_LINES);
glVertex2f(-20.0f, -12.0f);
glVertex2f(-20.0f, -12.0f + 24.0f * health);
glEnd();
glRotatef(unit->head_direction * RAD2DEGf, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_STRIP);
glVertex2f(-4.0f, -8.0f);
glVertex2f( 4.0f, 0.0f);
glVertex2f(-4.0f, 8.0f);
glEnd();
glPopMatrix();
}
void renderer_render_projectile(Projectile * projectile) {
set_gl_team_color_alpha(projectile->team_id,
(unsigned char)(projectile->decay_remaining / PROJECTILE_DECAY * 255.0f));
glPushMatrix();
glTranslatef(projectile->position.x, projectile->position.y, 0.1f);
if(projectile->distance_remaining > 0) {
glRotatef(projectile->direction * RAD2DEGf, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(PROJECTILE_LENGTH, 0.0f);
glEnd();
} else {
glBegin(GL_LINES);
glVertex2f(-PROJECTILE_IMPACT_HALF_SIZE, -PROJECTILE_IMPACT_HALF_SIZE);
glVertex2f( PROJECTILE_IMPACT_HALF_SIZE, PROJECTILE_IMPACT_HALF_SIZE);
glVertex2f( PROJECTILE_IMPACT_HALF_SIZE, -PROJECTILE_IMPACT_HALF_SIZE);
glVertex2f(-PROJECTILE_IMPACT_HALF_SIZE, PROJECTILE_IMPACT_HALF_SIZE);
glEnd();
}
glPopMatrix();
}
void renderer_render_factory(Factory * factory) {
glPushMatrix();
glTranslatef(factory->position.x, factory->position.y, 0.0f);
set_gl_team_color(factory->team_id);
glRotatef(factory->direction * RAD2DEGf, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-28.0f, -24.0f);
glVertex2f( 28.0f, -24.0f);
glVertex2f( 28.0f, -18.0f);
glVertex2f(-22.0f, -18.0f);
glVertex2f(-22.0f, 18.0f);
glVertex2f( 28.0f, 18.0f);
glVertex2f( 28.0f, 24.0f);
glVertex2f(-28.0f, 24.0f);
glEnd();
float health = factory_health_percentage(factory);
glBegin(GL_LINES);
glVertex2f(-32.0f, -24.0f);
glVertex2f(-32.0f, -24.0f + 48.0f * health);
glEnd();
glBegin(GL_LINE_LOOP);
float build_percentage = factory_build_percentage(factory);
glVertex2f(-16.0f * build_percentage, -12.0f * build_percentage);
glVertex2f( 16.0f * build_percentage, -12.0f * build_percentage);
glVertex2f( 16.0f * build_percentage, 12.0f * build_percentage);
glVertex2f(-16.0f * build_percentage, 12.0f * build_percentage);
glEnd();
glPopMatrix();
}
void renderer_notify_viewport_resized(Renderer * renderer) {
SDL_GL_GetDrawableSize(renderer->window, &renderer->viewport_width, &renderer->viewport_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double x = renderer->viewport_width/2;
double y = renderer->viewport_height/2;
glOrtho(-x, x, -y, y, -1.0f, 1.0f);
}
Vector renderer_screen_to_world(Renderer * renderer, Vector point) {
const float scale = renderer_scale(renderer);
return (Vector) {
(point.x - renderer->viewport_width/2) / scale + renderer->camera.x,
(point.y - renderer->viewport_height/2) / -scale + renderer->camera.y
};
}
Vector renderer_world_to_screen(Renderer * renderer, Vector position) {
const float scale = renderer_scale(renderer);
return (Vector) {
(position.x - renderer->camera.x) * scale + renderer->viewport_width/2,
(position.y - renderer->camera.y) * -scale + renderer->viewport_height/2
};
}
static int is_vector_in_viewport(Renderer * renderer, Vector vector, int padding) {
Vector point = renderer_world_to_screen(renderer, vector);
return point.x + padding >= 0.0f && point.y + padding >= 0.0f
&& point.x - padding <= renderer->viewport_width
&& point.y - padding <= renderer->viewport_height;
}
static void render_entity(Entity * entity, void * renderer_ptr) {
Renderer * renderer = (Renderer *)renderer_ptr;
switch(entity->type) {
case ENTITY_UNIT:
if(is_vector_in_viewport(renderer, entity->unit.position, HALF_UNIT_TEXTURE_SIZE))
renderer_render_unit(&entity->unit);
break;
case ENTITY_PROJECTILE:
if(is_vector_in_viewport(renderer, entity->projectile.position, PROJECTILE_LENGTH))
renderer_render_projectile(&entity->projectile);
break;
case ENTITY_FACTORY:
if(is_vector_in_viewport(renderer, entity->factory.position, HALF_FACTORY_TEXTURE_SIZE))
renderer_render_factory(&entity->factory);
break;
case ENTITY_NONE: break;
}
}
void renderer_begin(Renderer * renderer) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
const float scale = renderer_scale(renderer);
glScalef(scale, scale, 1.0f);
glTranslatef(-renderer->camera.x, -renderer->camera.y, 0.0f);
renderer_clear_color(renderer, 0.9f, 0.9f, 0.9f);
}
void renderer_render_map(Renderer * renderer, Map * map) {
glPushMatrix();
glTranslatef(0.0f, 0.0f, -1.0f);
// map padding
glColor3f(0.95f, 0.95f, 0.95f);
glTranslatef(0.0f, 0.0f, 0.1);
glBegin(GL_QUADS);
glVertex2f(-MAP_PADDING, -MAP_PADDING);
glVertex2f(map->size.x + MAP_PADDING, - MAP_PADDING);
glVertex2f(map->size.x + MAP_PADDING, map->size.y + MAP_PADDING);
glVertex2f(-MAP_PADDING, map->size.y + MAP_PADDING);
glEnd();
// map background
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, 0.1);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(map->size.x, 0);
glVertex2f(map->size.x, map->size.y);
glVertex2f(0, map->size.y);
glEnd();
// map inner border
glColor3f(0.8f, 0.8f, 0.8f);
glTranslatef(0.0f, 0.0f, 0.1);
glBegin(GL_LINE_LOOP);
glVertex2f(0, 0);
glVertex2f(map->size.x, 0);
glVertex2f(map->size.x, map->size.y);
glVertex2f(0, map->size.y);
glEnd();
// map outer border
glBegin(GL_LINE_LOOP);
glVertex2f(-MAP_PADDING, -MAP_PADDING);
glVertex2f(map->size.x + MAP_PADDING, - MAP_PADDING);
glVertex2f(map->size.x + MAP_PADDING, map->size.y + MAP_PADDING);
glVertex2f(-MAP_PADDING, map->size.y + MAP_PADDING);
glEnd();
glPopMatrix();
}
void renderer_render_world(Renderer * renderer, World * world) {
glPushMatrix();
// wtf?
glTranslatef(0.0f, 0.0f, 0.0f);
world_iterate_entities(world, renderer, render_entity);
// wtf?
glEnd();
}
void renderer_align_ui(Renderer * renderer, float x, float y, float scale) {
glLoadIdentity();
glTranslatef(
(renderer->viewport_width - UI_PADDING) * x / 2,
(renderer->viewport_height - UI_PADDING) * -y / 2,
1.0f
);
glScalef(scale, scale, scale);
}
static clock_t duration_avg(clock_t * durations, int duration_count) {
clock_t sum = 0;
for (int i = 0; i < duration_count; i++)
sum += durations[i];
return sum / duration_count;
}
typedef struct {
GameModule * mod;
clock_t duration;
} ModulePerformance;
static int compare_module_performances(const void * a, const void * b) {
clock_t duration_a = ((ModulePerformance *)a)->duration;
clock_t duration_b = ((ModulePerformance *)b)->duration;
if (duration_a < duration_b) return 1;
if (duration_a > duration_b) return -1;
return 0;
}
static void render_ui_debug(Renderer * renderer, Game * game) {
renderer_align_ui(renderer, -1, -1, 0.8);
char debug_str[2048];
int length = snprintf(debug_str, sizeof(debug_str),
"%.0f, %.0f %.0f%%; CMDPOS: %.0f, %.0f\n%22s:%7i\n%22s:%7i\n%22s:%7i\n%22s:%7i 0x%08x\n%22s:%7lu fps%6.1f\n",
renderer->camera.x, renderer->camera.y, renderer_scale(renderer) * 100, game->command_position.x, game->command_position.y,
"factories", game->world.factories.entity_count,
"units", game->world.units.entity_count,
"projectiles", game->world.projectiles.entity_count,
"time", game->current_time - game->start_time, game->current_time - game->start_time,
"update realtime ms", (game->cumulative_update_duration * 1000 / CLOCKS_PER_SEC), game->update_count * CLOCKS_PER_SEC * 1.0f / game->cumulative_update_duration
);
const int modcount = game->modules_count;
ModulePerformance mod_performances[modcount];
for (int i = 0; i < modcount; i++) {
ModulePerformance * perf = &mod_performances[i];
perf->mod = &game->modules[i];
perf->duration = duration_avg(perf->mod->duration_update, GAME_MODULE_DURATION_LENGTH);
}
qsort(mod_performances, modcount, sizeof(ModulePerformance), compare_module_performances);
for (int i = 0; i < modcount; i++) {
ModulePerformance * perf = &mod_performances[i];
length += snprintf(debug_str + length, sizeof(debug_str) - length, "%22s:%7luC log2%5.1f\n", perf->mod->name + 4, perf->duration, perf->duration ? log2f(perf->duration) : 0);
}
text_renderer_render_string(debug_str, length);
}
static void render_gui(Renderer * renderer, GUI * gui) {
gui_render(gui);
}
void renderer_render_ui(Renderer * renderer, Game * game) {
glMatrixMode(GL_MODELVIEW);
glColor3f(0.4f, 0.4f, 0.4f);
render_ui_debug(renderer, game);
renderer_align_ui(renderer, -1, -1, 1);
render_gui(renderer, &game->gui);
}
void renderer_present(Renderer * renderer) {
SDL_GL_SwapWindow(renderer->window);
}
void renderer_deinitialize(Renderer * renderer) {
SDL_GL_DeleteContext(renderer->gl_context);
SDL_DestroyWindow(renderer->window);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}