-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtleilax.c
138 lines (113 loc) · 2.89 KB
/
tleilax.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
#include <ctype.h>
#include <errno.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <ncurses.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "lib/Lua/lua_utils.h"
#include "lib/NcursesTools/ncurses_tools.h"
#include "lib/Random/random.h"
#include "lib/TimeOps/timeops.h"
#define MS_PER_UPDATE_GRAPHICS 16
#define MS_PER_UPDATE_LOGIC 1000
/**
* Global variable determining if main loop should run
*/
static volatile sig_atomic_t program_is_running = 1;
/**
* Stops the program from running
*/
static void stop(int sig) { program_is_running = 0; }
/**
* Declaration of Lua <-> C function bindings
*/
int init_lua_bindings() {
Lua.p_randomize_seed_xy_function = Random.randomize_seed_xy;
Lua.p_rnd_int_range_function = Random.rnd_int_range;
Lua.p_rnd_double_range_function = Random.rnd_double_range;
Lua.p_draw_char_function = Ncurses.draw_char;
Lua.p_clear_function = clear;
Lua.p_stop_function = stop;
Lua.p_init_pair_function = init_pair;
return 0;
}
/**
* Initializes the program instance
*/
int init() {
init_lua_bindings();
return 0;
}
/**
* Parse key catched by Ncurses and pass it to Lua script
*/
int handle_user_input(lua_State *L, const float elapsed_ms) {
if (Ncurses.kbhit()) {
char ch = getch();
if (isascii(ch)) {
/* If char pressed is a valid ASCII char convert it to a string and
* pass to Lua */
char key[2];
sprintf(key, "%c", (char)ch);
Lua.key_pressed(L, key, elapsed_ms);
}
}
return 0;
}
/**
* Main program
*
* 1. Initialize state
* 2. Load Lua script
* 2. Run update-render loop
* 3. On exit close Lua handler and free memory
*/
int main(int argc, char **argv) {
init();
Ncurses.init_config();
uint32_t previous_ms = 0, current_ms = 0, elapsed_ms = 0, lag_ms = 0,
count_ms = 0;
previous_ms = get_current_time();
/* Stop program on CTRL+c */
signal(SIGINT, stop);
lua_State *L = Lua.load_script("lua/galaxy.lua");
/*
* Main program loop
*/
while (program_is_running) {
current_ms = get_current_time();
/* Update if enough time elapsed */
if (count_ms > MS_PER_UPDATE_LOGIC) {
count_ms = 0;
}
/* FPS calculations */
elapsed_ms = current_ms - previous_ms;
previous_ms = current_ms;
count_ms += elapsed_ms;
lag_ms += elapsed_ms;
/* Update the background according to lag */
while (lag_ms >= MS_PER_UPDATE_GRAPHICS) {
lag_ms -= MS_PER_UPDATE_GRAPHICS;
}
/* Catch user keypress and pass to Lua handler */
handle_user_input(L, elapsed_ms);
/* Ncurses screen refresh */
refresh();
/* Call Lua render function */
Lua.render_state(L, elapsed_ms);
}
/* Ncurses close window */
endwin();
Lua.close_script(L); // Close running Lua handler
/* Exit */
return 0;
}