-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetro.c
550 lines (494 loc) · 14.3 KB
/
Retro.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "Retro.h"
#define _DEBUG_MODE_
/*
GLOBAL VARIABLES
*/
int __opengl;
int _debug_mode;
int _engine_running;
int _dev_console_active;
double _FPS;
RC_RenderData *_render_data;
RC_PlayerCamera *_pcam;
RC_Map *_world_map;
RC_InputControls _input;
ConsoleInformation *_dev_console;
lua_State *RC_L;
Retro_Clock r_clock;
Retro_MMap r_mmap;
//deprecated
void
debug(const char *msg)
{
if(_debug_mode)
{
if(_dev_console)
CON_Out(_dev_console, msg);
else
puts(msg);
}
}
void
Retro_doRndMap()
{
RC_SaveMap(_world_map);
printf("\npos x %f pos y %f dx %f dy %f ms %f rs %f\n", _pcam->pos_x, _pcam->pos_y, _pcam->dir_x, _pcam->dir_y, _pcam->move_speed, _pcam->rot_speed);
}
int
Retro_init(void)
{
int ret = TRUE;
//GLOBAL INITS
_FPS = 0;
_engine_running = TRUE;
_dev_console_active = FALSE;
_world_map = NULL;
_pcam = NULL;
//__opengl = FALSE; no longer in use
#ifdef _DEBUG_MODE_
_debug_mode = TRUE;
#endif
RC_LuaInit(&RC_L);
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
Uint32 flags;
debug("SDL_Init() successful...");
/* if(__opengl)
flags = SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL;
else*/
flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
if((_render_data = RC_Init(RC_L, flags)) != NULL)
{
//TURN ON LIGHTING FOR NOW
_render_data->render_light = FALSE;
//BILINEAR FILTERING
_render_data->bi_filter = TRUE;
/* Initialize the console, so we can start sending debug statements to it */
SDL_Rect r;
r.x = 0;
r.y = 0;
r.w = 800;
r.h = 400;
_dev_console = RC_ConsoleInit("ConsoleFont.bmp", _render_data->frame_buffer, 100, r);
CON_SetExecuteFunction(_dev_console, Retro_ConsoleEventHandler);
RC_SetDebugConsole(_dev_console);
debug("RC_Init() successful...");
if(TTF_Init() == 0)
debug("TTF_Init() successful...");
else
{
ret = FALSE;
debug("TTF_Init() failed...");
}
}
else
{
ret = FALSE;
debug("RC_Init() failed...");
}
}
else
{
ret = FALSE;
debug("SDL_Init() failed...");
}
if(ret)
{
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
_world_map = NULL;
_pcam = RC_InitCamera(0.0, 0.0, 25.5, 18.0);
_input.kup = SDLK_r;
_input.kdn = SDLK_f;
_input.krt = SDLK_g;
_input.klf = SDLK_d;
_input.kup_state = SDL_KEYUP;
_input.kdn_state = SDL_KEYUP;
_input.krt_state = SDL_KEYUP;
_input.klf_state = SDL_KEYUP;
}
return ret;
}
void
Retro_EventHandler(SDL_Event *event)
{
const int etype = event->type;
RC_ScanKeyboardInput(event, &_input);
RC_ScanMouseInput(event, &_input);
switch(etype)
{
case SDL_QUIT:
_engine_running = FALSE;
break;
case SDL_KEYDOWN:
{
SDL_KeyboardEvent kb = event->key;
SDL_keysym k = kb.keysym;
switch(k.sym)
{
case SDLK_BACKQUOTE:
_dev_console_active = TRUE;
CON_Show(_dev_console);
break;
case SDLK_l:
_render_data->render_light = !_render_data->render_light;
break;
case SDLK_b:
_render_data->bi_filter = !_render_data->bi_filter;
break;
default:
printf("\nKEY : %d", k.sym);
break;
}
}break;
}
}
void
Retro_ConsoleEventHandler(ConsoleInformation *cl, char *cmd)
{
char *delims = " ";
char *tok = strtok(cmd, delims);
/*
* Some basic top level commands, parsing can come later
*/
if(strcmp(tok, "gencave") == 0)
{
RC_ConsoleDebugOut(NULL, "Making a cave");
srand(time(NULL));
if(_world_map)
RC_FreeMap(_world_map);
//these values generate some pretty good maps. its just for testing afterall
_world_map = RC_MapGenCave(rand() % 10000 + 5, 32, 32, 40, 5, 1, 8);
_pcam->pos_x = _world_map->start_x;
_pcam->pos_y = _world_map->start_y;
Retro_MiniMapInit();
RC_ConsoleDebugOut(NULL, "Finished");
}
else if(strcmp(tok, "savemap") == 0)
{
if(_world_map)
{
RC_SaveMap(_world_map);
RC_ConsoleDebugOut(NULL, "Map Saved /maps/%s", _world_map->map_name);
}
}
else if(strcmp(tok, "loadmap") == 0)
{
char *fldr = "maps/";
char *fname;
tok = strtok(NULL, delims);
if(_world_map)
RC_FreeMap(_world_map);
//naive quote trim
fname = malloc(sizeof(char) * strlen(fldr) + strlen(tok));
strcpy(fname, fldr);
strncat(fname, tok+1, strlen(tok)-2);
_world_map = RC_LoadMap(fname);
_pcam->pos_x = _world_map->start_x;
_pcam->pos_y = _world_map->start_y;
Retro_MiniMapInit();
free(fname);
}
else if(strcmp(tok, "stats") == 0)
{
tok = strtok(NULL, delims);
if(strcmp(tok, "display") == 0)
{
RC_ConsoleDebugOut(NULL, "[====Display Stats====]");
RC_ConsoleDebugOut(NULL, "Display Width [%d]",_render_data->scr_width);
RC_ConsoleDebugOut(NULL, "Display Height [%d]",_render_data->scr_height);
RC_ConsoleDebugOut(NULL, "Bit Depth [%d]",_render_data->bit_depth);
RC_ConsoleDebugOut(NULL, "Texture Width [%d]",_render_data->tex_width);
RC_ConsoleDebugOut(NULL, "Texture Height [%d]",_render_data->tex_height);
RC_ConsoleDebugOut(NULL, "[=====================]");
}
}
else if(strcmp(tok, "noclip") == 0)
{
//cheater, its gonna explode
}
else if(strcmp(tok, "-goober") == 0)
{
//for centrinia :)
}
else if(strcmp(tok, "ex_lighting") == 0)
{
//toggle lighting
tok = strtok(NULL, delims);
int do_l = strtol(tok, NULL, 10); //a bit unsafe
if(do_l)
{
_render_data->render_light = TRUE;
}
else
{
_render_data->render_light = FALSE;
}
}
else if(strcmp(tok, "ex_bfilter") == 0)
{
//toggle lighting
tok = strtok(NULL, delims);
int do_l = strtol(tok, NULL, 10); //a bit unsafe
if(do_l)
{
_render_data->bi_filter = TRUE;
}
else
{
_render_data->bi_filter = FALSE;
}
}
else if(strcmp(tok, "timescale") == 0)
{
tok = strtok(NULL, delims);
double t_scale = strtod(tok, NULL); //a bit unsafe
if(t_scale <= 0)
t_scale = 1.0;
r_clock.time_scale = t_scale;
RC_ConsoleDebugOut(NULL, "Time Scale set to: %d", r_clock.time_scale);
}
else if(strcmp(tok, "fullscreen") == 0)
{
tok = strtok(NULL, delims);
int var = strtol(tok, NULL, 10);
if(var != 0)
{
RC_ResetVideoMode(_render_data, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
}
else
{
RC_ResetVideoMode(_render_data, SDL_HWSURFACE | SDL_DOUBLEBUF);
}
}
}
void
Retro_DebugInfoDraw(void)
{
if(TTF_WasInit())
{
SDL_Color color = {0xFF, 0xFF, 0xFF};
SDL_Surface *fps, *xy, *rot;
TTF_Font *font = TTF_OpenFont("Garuda.ttf", 18);
if(!font)
{
RC_ConsoleDebugOut(NULL,"\nTTF_OpenFont failed: %s\n", TTF_GetError());
return;
}
char buf[100];
sprintf(buf, "FPS: %f", _FPS);
if(!(fps=TTF_RenderText_Solid(font, buf, color)))
{
RC_ConsoleDebugOut(NULL,"TTF_RenderText failed: %s", TTF_GetError());
return;
}
sprintf(buf, "X[%3.2f] Y[%3.2f]", _pcam->pos_x, _pcam->pos_y);
if(!(xy=TTF_RenderText_Solid(font, buf, color)))
{
RC_ConsoleDebugOut(NULL,"TTF_RenderText failed: %s", TTF_GetError());
return;
}
sprintf(buf, "Cam X[%3.2f] Y[%3.2f]", _pcam->dir_x, _pcam->dir_y);
if(!(rot=TTF_RenderText_Solid(font, buf, color)))
{
RC_ConsoleDebugOut(NULL,"TTF_RenderText failed: %s", TTF_GetError());
return;
}
SDL_Rect r;
r.x = 0;
r.y = 0;
r.h = fps->h;
r.w = fps->w;
SDL_BlitSurface(fps, NULL, _render_data->frame_buffer, &r);
r.y = r.y + xy->h;
r.h = xy->h;
r.w = xy->w;
SDL_BlitSurface(xy, NULL, _render_data->frame_buffer, &r);
r.y = r.y + rot->h;
r.h = rot->h;
r.w = rot->w;
SDL_BlitSurface(rot, NULL, _render_data->frame_buffer, &r);
SDL_FreeSurface(fps);
SDL_FreeSurface(xy);
SDL_FreeSurface(rot);
TTF_CloseFont(font);
}
}
void
Retro_MiniMapInit(void)
{
if(_world_map)
{
r_mmap.c_size = 4;
r_mmap.m_rect = malloc(sizeof(SDL_Rect));
r_mmap.c_rect = malloc(sizeof(SDL_Rect));
r_mmap.m_rect->w = _world_map->mapsize_x * r_mmap.c_size;
r_mmap.m_rect->h = _world_map->mapsize_y * r_mmap.c_size;
r_mmap.m_rect->y = 0;
r_mmap.m_rect->x = _render_data->scr_width - r_mmap.m_rect->w;
r_mmap.c_rect->w = r_mmap.c_size;
r_mmap.c_rect->h = r_mmap.c_size;
r_mmap.offset_x = _render_data->scr_width - r_mmap.m_rect->w;
r_mmap.offset_y = 0;
r_mmap.s_flr = 0x007F7F7F;
r_mmap.s_wall = 0x00FFFFFF;
r_mmap.u_seen = 0x00000000;
r_mmap.mmap = SDL_CreateRGBSurface(0, r_mmap.m_rect->w, r_mmap.m_rect->h, _render_data->bit_depth, 0, 0, 0, 0);
SDL_SetAlpha(r_mmap.mmap, 0, 180);
int x=0,y=0;
for(x=0; x < _world_map->mapsize_x; x++)
for(y=0; y < _world_map->mapsize_y; y++)
{
r_mmap.c_rect->x = (r_mmap.c_size * x);
r_mmap.c_rect->y = (r_mmap.c_size * y);
SDL_FillRect(r_mmap.mmap,r_mmap.c_rect, r_mmap.u_seen);
}
}
}
void
Retro_MiniMap(void)
{
int x, y;
x = (int)_pcam->pos_x;
y = (int)_pcam->pos_y;
//flag seen cells; TODO: only see cells in front of the camera; Only redraw currently seen cells
int i,j;
Uint32 fl;
for(i = x-1; i < x+1; i++)
for(j = y-1; j < y+1; j++)
{
if(i >= _world_map->mapsize_x) continue;
if(j >= _world_map->mapsize_y) continue;
if(i<0) continue;
if(j<0) continue;
fl = _world_map->map_data[i][j].flags;
if(!(fl&F_SEEN))
{
_world_map->map_data[i][j].flags |= F_SEEN;
}
}
Uint32 xx, yy;
for(xx = 0; xx < _world_map->mapsize_x ; xx++)
for(yy = _world_map->mapsize_y-1; yy >0 ; yy--)
{
// fprintf(stderr, "xx %d yy %d\n", xx, yy);
Uint32 fl = _world_map->map_data[xx][yy].flags;
if(fl & F_SEEN)
{
if(fl & F_WALL)
{
r_mmap.c_rect->x = (r_mmap.c_size * xx);
r_mmap.c_rect->y = (r_mmap.c_size * yy);
SDL_FillRect(r_mmap.mmap,r_mmap.c_rect, r_mmap.s_wall);
}
if(fl & F_FLOOR)
{
r_mmap.c_rect->x = (r_mmap.c_size * xx);
r_mmap.c_rect->y = (r_mmap.c_size * yy);
SDL_FillRect(r_mmap.mmap,r_mmap.c_rect, r_mmap.s_flr);
}
}
/* else
{
r_mmap.c_rect->x = (r_mmap.c_size * xx);
r_mmap.c_rect->y = (r_mmap.c_size * yy);
SDL_FillRect(r_mmap.mmap,r_mmap.c_rect, r_mmap.u_seen);
}*/
}
SDL_BlitSurface(r_mmap.mmap, NULL, _render_data->frame_buffer, r_mmap.m_rect);
}
void
Retro_cleanup(void)
{
SDL_FreeSurface(_render_data->screen);
SDL_FreeSurface(_render_data->frame_buffer);
CON_Destroy(_dev_console);
if(_world_map)
RC_FreeMap(_world_map); //safe to call on NULL, but just to be sure
free(_pcam);
RC_LuaClose(RC_L);
RC_RaycastEngineCleanup(_render_data);
free(_render_data);
TTF_Quit();
SDL_Quit();
}
void
Retro_update(Uint32 dtime)
{
SDL_Event e;
while(SDL_PollEvent(&e))
{
if(_dev_console_active)
if(!CON_Events(&e))
continue;
Retro_EventHandler(&e);
}
RC_UpdateCamera(_pcam, _world_map, &_input, dtime);
}
void
Retro_render(void)
{ //placeholder
RC_Sprite _sprite[1];
SDL_Rect r;
SDL_FillRect(_render_data->frame_buffer, NULL, 0);
if(_world_map != NULL)
{
SDL_LockSurface(_render_data->frame_buffer);
RC_RaycastDraw(_render_data, _world_map, _pcam, _sprite);
SDL_UnlockSurface(_render_data->frame_buffer);
Retro_MiniMap();
}
Retro_DebugInfoDraw();
if(_dev_console_active)
{
RC_ConsoleDraw(_dev_console);
}
//debugDraw()
r.x = 0;
r.y = 0;
SDL_BlitSurface(_render_data->frame_buffer, NULL, _render_data->screen, &r);
SDL_Flip(_render_data->screen);
}
int
Retro_exec(void)
{
int ret = 0, skip=0;
if(Retro_init() == FALSE)
{
ret = -1;
}
else
{
r_clock.time_scale = 1.0;
r_clock.new_time = SDL_GetTicks();
r_clock.old_time = r_clock.new_time;
r_clock.next_tick = r_clock.new_time;
while(_engine_running)
{
r_clock.old_time = r_clock.new_time;
skip = 0;
if(r_clock.delta_time < 1)
r_clock.delta_time = 1;
while(SDL_GetTicks() > r_clock.next_tick && skip < MAX_FRAMESKIP)
{
Retro_update(SKIP_TICKS * r_clock.time_scale);
r_clock.next_tick += SKIP_TICKS;
skip++;
}
r_clock.delta_time *= r_clock.time_scale;
Retro_update(r_clock.delta_time);
Retro_render();
r_clock.new_time = SDL_GetTicks();
r_clock.delta_time = r_clock.new_time - r_clock.old_time;
_FPS = 1.0 / ((r_clock.new_time - r_clock.old_time) / 1000.0);
}
}
Retro_cleanup();
return ret;
}
int
main(int argc, char **argv)
{
if(Retro_exec() == -1)
debug("Engine Error, closing...");
return 0;
}