-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.c
More file actions
434 lines (326 loc) · 11.2 KB
/
renderer.c
File metadata and controls
434 lines (326 loc) · 11.2 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "renderer.h"
#include <ncurses.h>
#include <math.h> //rounding
#include "bitmap.c"
// Where to find graphical resources.
// Not used if images are turned off.
#define SHIP_FILEPATH "./resources/ship.bmp"
#define ASTEROID_FILEPATH "./resources/asteroid.bmp"
// This saves us some pain,
// but lowers the beauty.
//
/* #define ASTEROIDS_ROTATE */
// ncurses resources
WINDOW* main_win;
WINDOW* main_border;
WINDOW* score_win;
WINDOW* pause_win;
// Transformation variables, re-used during rendering.
float sw, sh; // scale factors
float tx, ty; // translations
// Bitmaps for ships, asteroids.
bitmap_t* ship_bmp;
bitmap_t* asteroid_bmp;
// Caches.
bitmap_t* ship_cache;
#ifdef ASTEROIDS_ROTATE
bitmap_t* asteroid_cache;
#endif
/* ------------------------------------------------------------------------- */
/* Initialise the rendering system */
bool rinit(){
// Start curses mode
initscr();
// Refresh for some reason
refresh();
// Construct windows
main_win = newwin( LINES - 5, COLS-2, 4, 1);
main_border = newwin( LINES - 3, COLS, 3, 0 );
score_win = newwin( 3, COLS, 0, 0 );
// Set backgrounds
wbkgdset(main_win, ' ');
wbkgdset(score_win, ' ');
// Load bitmaps (TODO: make this gooderer.)
ship_bmp = load_resource(SHIP_FILEPATH);
asteroid_bmp = load_resource(ASTEROID_FILEPATH);
// TODO: some kind of function to do this for me
// FIXME: re-use a cache, THIS WILL LEAK LIKE FUCK.
ship_cache = new_bmp(ship_bmp->width, ship_bmp->height);
#ifdef ASTEROIDS_ROTATE
asteroid_cache = new_bmp(asteroid_bmp->width, asteroid_bmp->height);
#endif
// Successful init
return true;
}
/* Tear down the rendering system */
void rteardown(){
endwin(); /* End curses mode */
// Free bitmap resources
free_bmp(ship_bmp);
free_bmp(ship_cache);
free_bmp(asteroid_bmp);
#ifdef ASTEROIDS_ROTATE
free_bmp(asteroid_cache);
#endif
}
/* ------------------------------------------------------------------------- */
// Transforms and coordinate conversion
void compute_tranforms(bounds_t* bounds){
// Find window bounds of curses window
int wcmax, wrmax;
getmaxyx(main_win, wrmax, wcmax);
// Compute centre
float wcc = wcmax/2.0;
float wrc = wrmax/2.0;
// Compute bound centre
float bxc = (bounds->xmax + bounds->xmin) / 2.0;
float byc = (bounds->ymax + bounds->ymin) / 2.0;
// Compute scale
sw = wcmax / (float)bounds->width;
sh = wrmax / (float)bounds->height;
// Compute translation
// TODO/FIXME: currently clamped to 0, 0
tx = wcc;
ty = wrc;
}
// Convert from curses space to game space
float inv_trans_horz(float c){
return (c - tx) / sw;
}
float inv_trans_vert(float r){
return (r - ty) / sh;
}
// Full transform from game->curses
float trans_horz(float x){
return x * sw + tx;
}
float trans_vert(float y){
return y * sh + ty;
}
// Scale only, no translation from game->curses
float scale_horz(float x){
return x * sw;
}
float scale_vert(float y){
return y * sh;
}
// Plot at r, c on the curses plane
void plot_with_wrap(int c, int r, char ch){
int wcmax, wrmax;
getmaxyx(main_win, wrmax, wcmax);
// and t'other way
while(c >= wcmax) c -= wcmax;
while(r >= wrmax) r -= wrmax;
// Wrap
while(c < 0) c += wcmax;
while(r < 0) r += wrmax;
// Then plot
mvwprintw(main_win, r, c, "%c", ch);
}
/* ------------------------------------------------------------------------- */
// Ordinary rendering
// Return the ship's character for a given rotation
#define PIPI (2*M_PI)
char get_ship_char( positional_entity_t* e, float x, float y){
orientation_t eo = e->orientation;
char c = 's';
if(eo < (1 * PIPI/8) || eo > (7 * PIPI/8) ) c = '^';
if(eo > (1 * PIPI/8) && eo < (3 * PIPI/8) ) c = '>';
if(eo > (3 * PIPI/8) && eo < (5 * PIPI/8) ) c = 'v';
if(eo > (5 * PIPI/8) && eo < (7 * PIPI/8) ) c = '<';
/* mvwprintw( main_win, round(y), round(x), "%c", c); */
return c;
}
/* ------------------------------------------------------------------------- */
// Bitmap rendering support
//
//
// Rotate around centre
// Thanks to http://www.programmersheaven.com/download/15441/0/ZipView.aspx
void rotate(bitmap_t* bmp, bitmap_t* newbmp, orientation_t orientation){
// todo: Check newbmp is the same size...?
// Precompute these
float cosT = cos(orientation);
float sinT = sin(orientation);
// Loop over each pixel, with a half offset to move the origin
int srcx, srcy;
for(int i=-bmp->width/2; i<bmp->width/2; i++){
for(int j=-bmp->height/2; j<bmp->height/2; j++){
// Do magic stuff
srcx = i*cosT + j*sinT + bmp->width/2.0;
srcy = j*cosT - i*sinT + bmp->height/2.0;
// Check it's in the image bounds
if(srcx >= 0 && srcx < bmp->width &&
srcy >= 0 && srcy < bmp->height){
// Get from src, put into i+offsetx, j+offsety
bmp_put( newbmp,
i + newbmp->width/2,
j + newbmp->height/2,
bmp_get(bmp, srcx, srcy)
);
}
}
}
}
// Render a bitmap using sampling at a given place
void subpixel( bitmap_t* bmp, positional_entity_t* e, char ch ){
// Compute location in curses-space
float x = trans_horz(e->x);
float y = trans_vert(e->y);
// Compute size in curses-space
float w = scale_horz(e->size);
float h = scale_vert(e->size);
// Iterate char-by-char and see if each point is in the
// bitmap. If it is, plot it.
for(int i=floor(x - (w/2)); i<=ceil(x + (w/2)); i++){
for(int j=floor(y - (h/2)); j<=ceil(y + (h/2)); j++){
// Re-normalise for 0,0 addressing
//
// TODO: this is very carefully limited to avoid
// oversampling the bitmap,
// but it is a big mash of float error....
float bmpx = ((i - x) + (w/2));
float bmpy = ((j - y) + (h/2));
if(bmpx < 0) bmpx = 0;
if(bmpy < 0) bmpy = 0;
// Then move from curses to bitmap address space
bmpx /= w;
bmpy /= h;
bmpx *= bmp->width;
bmpy *= bmp->height;
if(bmpx > bmp->width -1) bmpx = bmp->width - 1;
if(bmpy > bmp->height -1) bmpy = bmp->height - 1;
/* unsigned int bmpx = ((((i - x) + (w/2)) / w) * (float)bmp->width) - 1; */
/* unsigned int bmpy = ((((j - y) + (h/2)) / h) * (float)bmp->height) - 1; */
/* debug: mvwprintw(main_win, j, i, "[%.1f,%.1f]", bmpx, bmpy); */
// If this pixel is on, plot it on the curses plane
if(bmp_get( bmp, bmpx, bmpy ))
plot_with_wrap(i, j, ch);
}
}
}
// Renders a given entity using whatever method is appropriate
void render_entity( positional_entity_t* e ){
// Compute actual values
float ex = trans_horz(e->x);
float ey = trans_vert(e->y);
// Non-bitmap rendering
if(e->type == bullet){
mvwprintw( main_win, round(ey), round(ex), "*");
return;
}
// Bitmap rendering....
switch(e->type){
case ship:
rotate( ship_bmp, ship_cache, e->orientation );
char ch = get_ship_char(e, ex, ey);
subpixel(ship_cache, e, ch);
mvwprintw( main_win, round(ey), round(ex), "%c", ch);
break;
case asteroid:
#ifdef ASTEROIDS_ROTATE
rotate( asteroid_bmp, asteroid_cache, e->orientation );
subpixel(asteroid_cache, e, 'a');
#else
subpixel(asteroid_bmp, e, 'a');
#endif
mvwprintw( main_win, round(ey), round(ex), "a");
break;
}
}
/* Render the game */
void rrender(game_state_t* current){
// Blank the windowas
werase(main_win);
werase(score_win);
compute_tranforms(¤t->bounds);
// TODO: check if terminal has been resized( is_term_resized, man resizeterm(3x) and wresize).
// Write score from game state
mvwprintw(score_win, 1, 1, "Lives: %d \t Score: %d \t Wave: %d \t Warps: %d \t Ship: %.0f%% (%.1f, %.0f°) %s",
current->lives, current->score, current->wave,
current->warps,
current->damage,
sqrt(pow(current->player->dx, 2) + pow(-1 * current->player->dy, 2)),
current->player->orientation * 180 / M_PI,
((current->temporary_invulnerability > 0) ? "(inv.)" : "")
);
// Loop over entities in game and render them if not null
for(int i=0; i<=current->scene->max; i++){
/* mvwprintw(main_win, 0, 0, "[i%d/e%d]", i, current->scene->count); */
if(current->scene->entities[i].type != null){
render_entity( ¤t->scene->entities[i] );
}
}
// Draw curses cruft.
box(main_border, 0, 0);
box(score_win, 0, 0);
wrefresh(main_border);
wrefresh(score_win);
wrefresh(main_win);
refresh(); /* Print it on to the real screen */
}
/* ------------------------------------------------------------------------- */
// Show the pause dialog
void rpause_dialog( game_state_t* current ){
if( pause_win ) return;
// Cnstruct windows
WINDOW* sum_win = newwin( 10, 30, 6, 6 );
wbkgdset(main_win, ' ');
werase(sum_win);
box(sum_win, 0, 0);
// Write summary info
mvwprintw(sum_win, 1, 9, "Game Paused");
mvwprintw(sum_win, 2, 1, "----------------------------");
mvwprintw(sum_win, 4, 4, "Score: %d", current->score);
mvwprintw(sum_win, 5, 4, "Waves: %d", current->wave);
mvwprintw(sum_win, 7, 3, "Press a key to continue.");
// Refresh
wrefresh(sum_win);
refresh();
}
// Clear the pause window if one exists.
void rclear_pause_dialog(){
if(!pause_win) return;
// Erase
werase(pause_win);
wborder(pause_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
wrefresh(pause_win);
// Destroy
delwin(pause_win);
// Set pointer to null
pause_win = 0;
}
void rsummarise_game( game_state_t* current ){
// Cnstruct windows
WINDOW* sum_win = newwin( 13, 30, 6, 6 );
wbkgdset(main_win, ' ');
werase(sum_win);
box(sum_win, 0, 0);
// Write summary info
mvwprintw(sum_win, 1, 11, "Game Over");
mvwprintw(sum_win, 2, 1, "----------------------------");
mvwprintw(sum_win, 4, 4, "Score: %d", current->score);
mvwprintw(sum_win, 5, 4, "Waves: %d", current->wave);
/* mvwprintw(sum_win, 1, 9, ""); */
mvwprintw(sum_win, 7, 2, "Brought to you by Steve W");
mvwprintw(sum_win, 8, 4, "<stephenwattam.com>");
mvwprintw(sum_win, 10, 5, "Press a key to quit.");
// Refresh
wrefresh(sum_win);
refresh();
}
/* ------------------------------------------------------------------------- */
float rget_aspect(){
int wcmax, wrmax;
getmaxyx(main_win, wrmax, wcmax);
return (float)wcmax/(float)wrmax;
}
// In REAL units, as an expression of resolution
//
// For terminals, I presume characters are, diagonally,
// 1/10th of an inch
float rget_diag_size(){
int wcmax, wrmax;
getmaxyx(main_win, wrmax, wcmax);
return (float)sqrt( pow(wcmax, 2) + pow(wrmax, 2) ) / 10.0;
}