-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
88 lines (70 loc) · 1.73 KB
/
main.c
File metadata and controls
88 lines (70 loc) · 1.73 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
/* [[ INCLUDES ]] */
#define TB_IMPL
#include "termbox2.h"
#include "config.h"
#include "utils.h"
#include "camera.h"
#include "raycaster.h"
/* [[ MAIN ]] */
int main() {
// Termbox2 setup.
if (tb_init() != 0) {
return 1;
}
// Screen dimensions.
int width = tb_width(); // cols.
int height = tb_height(); // rows.
// Camera.
Camera cm = {
{5.5, 6}, // Initial position.
{1, 0}, // " direction.
{0, PLANE_HWIDTH} // " plane vector.
};
// Gamestate: ON / OFF.
GameState gs = ON;
// Event struct.
struct tb_event ev;
// Variables to calculate delta time.
double last_time, current_time, delta_time;
last_time = get_time_in_seconds();
// Gameloop.
while (gs) {
// Delta time calculation.
current_time = get_time_in_seconds();
delta_time = current_time - last_time;
last_time = current_time;
// Deal with events.
if ((tb_peek_event(&ev, 0) == 0) && ev.type == TB_EVENT_KEY) {
switch (ev.key) {
case TB_KEY_ARROW_UP:
move(&cm, 1, delta_time);
break;
case TB_KEY_ARROW_DOWN:
move(&cm, -1, delta_time);
break;
case TB_KEY_ARROW_LEFT:
rotate(&cm, -1, delta_time);
break;
case TB_KEY_ARROW_RIGHT:
rotate(&cm, 1, delta_time);
break;
case TB_KEY_ESC:
gs = OFF;
break;
}
} else if (ev.type == TB_EVENT_RESIZE) {
width = ev.w;
height = ev.h;
}
// Clear terminal buffer.
tb_clear();
// Raycasting.
ray_cast(&cm, width, height);
// Flush / Render terminal buffer.
tb_present();
}
// Termbox2 cleanup.
tb_shutdown();
return 0;
}
/* [[ END ]] */