summaryrefslogtreecommitdiff
path: root/src/honey.c
blob: 73fb2857c38dd0ab4c7332e9b1e6e3dbd947cc70 (plain)
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
#include "honey.h"

void honey_print_help()
{
    printf("usage: honey [OPTIONS] SCRIPT_DIR\n"
           "  -v        Enable verbose logging\n"
           "  -h        Show this help message and exit\n"
           "  -l LOG    Log to LOG instead of stdout\n");
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

bool honey_parse_options(honey_options* options, int argc, char** argv)
{
    honey_options opts;
    opts.verbose = false;
    opts.script_directory = NULL;
    opts.logfile = NULL;

    opterr = 0;
    int c;

    while ((c = getopt(argc, argv, "vhl:")) != -1) {
        switch (c) {
        case 'v':
            opts.verbose = true;
            break;

        case 'h':
            honey_print_help();
            return false;

        case 'l':
            opts.logfile = optarg;
            break;

        case '?':
            fprintf(stderr, "unknown option: '%c'\n", optopt);
            honey_print_help();
            return false;

        default:
            return false;
        }
    }

    if (optind < argc) {
        opts.script_directory = argv[optind];
    }

    *options = opts;

    return true;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
    
bool honey_setup(lua_State** L)
{
    /* set up lua and honey lua bindings */
    *L = luaL_newstate();
    luaL_openlibs(*L);

    lua_createtable(*L, 0, 1);

    if (!honey_setup_window(*L))
        return false;
    lua_setfield(*L, -2, "window");

    honey_setup_input(*L);
    lua_setfield(*L, -2, "input");

    honey_setup_cglm(*L);
    lua_setfield(*L, -2, "cglm");

    lua_pushcfunction(*L, honey_exit);
    lua_setfield(*L, -2, "exit");

    lua_setglobal(*L, "honey");

    return true;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

bool honey_run(lua_State* L, honey_options opts) {
    lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
    honey_window_information* info = lua_touserdata(L, -1);
    honey_window window = info->window;

    char* script;
    honey_result res = honey_format_string(&script,
                                           "%s/main.lua",
                                           opts.script_directory);
    if (res != HONEY_OK) {
        fprintf(stderr, "[honey] FATAL: could not allocate space for script filename!");
        return false;
    }
    
    if (luaL_loadfile(L, script) == 0) {
        if (!honey_lua_pcall(L, 0, 1) == 0) {
            const char* error = lua_tostring(L, -1);
            fprintf(stderr, "[honey] ERROR: %s\n", error);
            return false;
        }
    }
    else {
        fprintf(stderr, "ERROR: failed to open %s!\n", script);
        return false;
    }

    int update_callback = honey_get_callback(L, "update");
    int draw_callback   = honey_get_callback(L, "draw");
    
    float prevTime = 0;
    float currentTime = 0;
    float dt;
    
    while (!glfwWindowShouldClose(window)) {
        currentTime = (float) glfwGetTime();
        dt = currentTime - prevTime;
        prevTime = currentTime;
        glfwPollEvents();

        if (update_callback != LUA_NOREF) {
            lua_rawgeti(L, LUA_REGISTRYINDEX, update_callback);
            lua_pushnumber(L, dt);
            int result = honey_lua_pcall(L, 1, 0);
            if (result != 0) {
                const char* error = lua_tostring(L, -1);
                fprintf(stderr, "[honey] ERROR: %s\n", error);
                glfwSetWindowShouldClose(window, true);
            }
        }

        if (draw_callback != LUA_NOREF) {
            lua_rawgeti(L, LUA_REGISTRYINDEX, draw_callback);
            int result = honey_lua_pcall(L, 0, 0);
            if (result != 0) {
                const char* error = lua_tostring(L, -1);
                fprintf(stderr, "[honey] ERROR: %s\n", error);
                glfwSetWindowShouldClose(window, true);
            }
        }
    }

    lua_close(L);
    
    glfwTerminate();
    return true;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

int honey_get_callback(lua_State* L, char* callback)
{
    lua_getglobal(L, "honey");
    lua_getfield(L, -1, callback);

    int ref = LUA_NOREF;
    
    if (lua_isfunction(L, -1))
        ref = luaL_ref(L, LUA_REGISTRYINDEX);
    else
        lua_pop(L, 1);
    lua_pop(L, 1);

    return ref;
}