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
|
#include <stdio.h>
#include <honeysuckle.h>
#include "logging/logging.h"
#include "options/honey_options.h"
#include "gl/honey_gl.h"
static int get_func(lua_State *L, char *name);
static void loop(lua_State *L, int update, int draw);
int main(int argc, char **argv)
{
/* process command-line options */
struct honey_options options;
if (!parse_options(&options, argc, argv))
return 1;
if (options.display_help)
/* help displayed, exit */
return 0;
honey_info("options:\n"
" main script: %s\n"
" log level: %s\n",
options.main_script,
honey_log_level_str(options.log_level));
/* setup lua environment */
honey_debug("initialize lua_State\n");
lua_State *L = luaL_newstate();
luaL_openlibs(L);
honey_debug("load honey bindings\n");
lua_createtable(L, 0, 1);
int index = lua_gettop(L);
setup_window(L, index);
lua_setglobal(L, "honey");
/* load main script */
honey_debug("loading '%s'\n", options.main_script);
if (luaL_loadfile(L, options.main_script)) {
const char *error = lua_tostring(L, -1);
honey_fatal("%s\n", error);
return 1;
}
if (!hs_call(L, 0, 0)) {
const char *error = lua_tostring(L, -1);
honey_fatal("unhandled error: %s", error);
goto close; /* sorry */
}
/* ensure window exists */
if (!window.created) {
lua_getglobal(L, "honey");
lua_getfield(L, -1, "window");
lua_getfield(L, -1, "createWindow");
lua_pushinteger(L, 640);
lua_pushinteger(L, 480);
hs_call(L, 2, 0);
lua_pop(L, 2);
}
/* load main callback functions */
int update = get_func(L, "update");
if (update) honey_info("honey.update: %s\n", lua_tostring(L, update));
else honey_info("honey.update: (nil)\n");
int draw = get_func(L, "draw");
if (draw) honey_info("honey.draw: %s\n", lua_tostring(L, draw));
else honey_info("honey.draw: (nil)\n");
/* main loop */
while(!glfwWindowShouldClose(window.window))
loop(L, update, draw);
close:
lua_close(L);
glfwTerminate();
return 0;
}
static int get_func(lua_State *L, char *name)
{
lua_getglobal(L, "honey");
int index = lua_gettop(L);
lua_getfield(L, -1, name);
if (lua_isfunction(L, -1)) {
lua_remove(L, index);
return lua_gettop(L);
}
else {
lua_pop(L, 2);
return 0;
}
}
static void loop(lua_State *L, int update, int draw)
{
static float prev_time = 0;
static float draw_time = 0;
float current_time = (float) glfwGetTime();
float dt = current_time - prev_time;
prev_time = current_time;
draw_time += dt;
glfwPollEvents();
if (update) {
lua_pushvalue(L, update);
lua_pushnumber(L, dt);
if (!hs_call(L, 1, 0)) {
/* error! */
const char *error = lua_tostring(L, -1);
honey_fatal("unhandled error: %s\n", error);
glfwSetWindowShouldClose(window.window, 1);
}
}
/* cap framerate at 60fps */
if (draw && draw_time > 0.016) {
draw_time -= 0.016;
lua_pushvalue(L, draw);
if(!hs_call(L, 0, 0)) {
/* error! */
const char *error = lua_tostring(L, -1);
honey_fatal("unhandled error: %s\n", error);
glfwSetWindowShouldClose(window.window, 1);
}
}
}
|