summaryrefslogtreecommitdiff
path: root/src/window.c
blob: ffcaee76a6a14b4605ef8ccd2639b21a4e02dc7e (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
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
#include "window.h"

int honey_window_info_ref = LUA_NOREF;
int honey_window_resize_callback_ref = LUA_NOREF;
int honey_window_resize_callback_data_ref = LUA_NOREF;
int honey_window_focus_callback_ref = LUA_NOREF;
int honey_window_focus_callback_data_ref = LUA_NOREF;

static void honey_glfw_window_resize_callback(honey_window window,
                                              int width, int height)
{
    lua_State* L = glfwGetWindowUserPointer(window);

    int callback = honey_window_resize_callback_ref;
    int data = honey_window_resize_callback_data_ref;
    
    if (callback == LUA_NOREF)
        return;
    
    lua_rawgeti(L, LUA_REGISTRYINDEX, callback);

    lua_pushinteger(L, width);
    lua_pushinteger(L, height);
    
    if (data == LUA_NOREF || data == LUA_REFNIL)
        lua_pushnil(L);
    else
        lua_rawgeti(L, LUA_REGISTRYINDEX, data);

    honey_lua_pcall(L, 3, 0);
}

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

static void honey_glfw_window_focus_callback(honey_window window,
                                             int focused)
{
    lua_State* L = glfwGetWindowUserPointer(window);

    int callback = honey_window_focus_callback_ref;
    int data = honey_window_focus_callback_data_ref;
    
    if (callback == LUA_NOREF)
        return;
    
    lua_rawgeti(L, LUA_REGISTRYINDEX, callback);

    lua_pushboolean(L, focused);
    
    if (data == LUA_NOREF || data == LUA_REFNIL)
        lua_pushnil(L);
    else
        lua_rawgeti(L, LUA_REGISTRYINDEX, data);

    honey_lua_pcall(L, 2, 0);
}

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

bool honey_setup_window(lua_State* L)
{
    honey_window_information* info = lua_newuserdata(L, sizeof(honey_window_information));
    honey_window_info_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    info->window = glfwCreateWindow(HONEY_WINDOW_DEFAULT_WIDTH,
                                    HONEY_WINDOW_DEFAULT_HEIGHT,
                                    "honey", NULL, NULL);

    info->width = HONEY_WINDOW_DEFAULT_WIDTH;
    info->height = HONEY_WINDOW_DEFAULT_WIDTH;
    info->fullscreen = false;
    
    if (info->window == NULL) {
        fprintf(stderr, "[honey] ERROR: failed to create window!\n");
        glfwTerminate();
        return false;
    }
    /* store lua state in window, so it's accessible from GLFW callbacks */
    glfwSetWindowUserPointer(info->window, L);
    glfwMakeContextCurrent(info->window);

    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        fprintf(stderr, "[honey] ERROR: failed to initialize GLAD!\n");
        glfwTerminate();
        return false;
    }

    // Enable blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // Enable depth testing
    glEnable(GL_DEPTH_TEST);

    // Enable face culling
    glEnable(GL_CULL_FACE);

    glfwSetWindowSizeCallback(info->window, honey_glfw_window_resize_callback);
    glfwSetWindowFocusCallback(info->window, honey_glfw_window_focus_callback);
    

    honey_lua_create_table
	(L, 8,
	 HONEY_FUNCTION, "set_fullscreen",  honey_window_set_fullscreen,
	 HONEY_FUNCTION, "set_title",       honey_window_set_title,
	 HONEY_FUNCTION, "get_size",        honey_window_get_size,
	 HONEY_FUNCTION, "set_size",        honey_window_set_size,
	 HONEY_FUNCTION, "resize_bind",     honey_window_resize_bind,
	 HONEY_FUNCTION, "resize_unbind",   honey_window_resize_unbind,
	 HONEY_FUNCTION, "focus_bind",      honey_window_focus_bind,
	 HONEY_FUNCTION, "focus_unbind",    honey_window_focus_unbind);

    lua_setfield(L, -2, "window");

    return true;
}

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

int honey_window_set_fullscreen(lua_State* L)
{
    bool fullscreen;
    honey_lua_parse_arguments(L, 1, 1, HONEY_BOOLEAN, &fullscreen);

    lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
    honey_window_information* info = lua_touserdata(L, -1);

    if (fullscreen) {
        glfwGetWindowSize(info->window, &(info->width), &(info->height));
        
        GLFWmonitor* monitor = glfwGetPrimaryMonitor();
        const GLFWvidmode* mode = glfwGetVideoMode(monitor);
        glfwSetWindowMonitor(info->window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
        info->fullscreen = true;
    }
    else {
        glfwSetWindowMonitor(info->window, NULL, 20, 20, info->width, info->height, 0);
        info->fullscreen = false;
    }
    return 0;
}

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

int honey_window_set_title(lua_State* L)
{
    char* title;
    honey_lua_parse_arguments(L, 1, 1, HONEY_STRING, &title);

    lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
    honey_window_information* info = lua_touserdata(L, -1);
    
    glfwSetWindowTitle(info->window, title);
    return 0;
}

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

int honey_window_get_size(lua_State* L)
{
    lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
    honey_window_information* info = lua_touserdata(L, -1);
                 
    int width, height;
    glfwGetWindowSize(info->window, &width, &height);
    lua_pushinteger(L, width);
    lua_pushinteger(L, height);
    return 2;
}

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

int honey_window_set_size(lua_State* L)
{
    int width, height;
    honey_lua_parse_arguments
	(L, 1, 2,
	 HONEY_INTEGER, &width,
	 HONEY_INTEGER, &height);

    lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
    honey_window_information* info = lua_touserdata(L, -1);

    glfwSetWindowSize(info->window, width, height);
    return 0;
}

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

int honey_window_resize_bind(lua_State* L)
{
    int choice = honey_lua_parse_arguments
	(L, 2,
	 1, HONEY_FUNCTION,
	 2, HONEY_FUNCTION, HONEY_ANY);

    honey_window_resize_unbind(L);

    lua_pushvalue(L, 1);
    honey_window_resize_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);

    if (choice == 1) {
	lua_pushvalue(L, 2);
	honey_window_resize_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    return 0;
}

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

int honey_window_resize_unbind(lua_State* L)
{
    int callback = honey_window_resize_callback_ref;
    int data = honey_window_resize_callback_data_ref;

    if (callback != LUA_NOREF) {
	luaL_unref(L, LUA_REGISTRYINDEX, callback);
    }

    if (data != LUA_NOREF && data != LUA_REFNIL) {
	luaL_unref(L, LUA_REGISTRYINDEX, data);
    }

    honey_window_resize_callback_ref = LUA_NOREF;
    honey_window_resize_callback_data_ref = LUA_NOREF;
    return 0;
}

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

int honey_window_focus_bind(lua_State* L)
{
    int choice = honey_lua_parse_arguments
	(L, 2,
	 1, HONEY_FUNCTION,
	 2, HONEY_FUNCTION, HONEY_ANY);

    honey_window_focus_unbind(L);

    lua_pushvalue(L, 1);
    honey_window_focus_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);

    if (choice == 1) {
	lua_pushvalue(L, 2);
	honey_window_focus_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    return 0;
}

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

int honey_window_focus_unbind(lua_State* L)
{
    int callback = honey_window_focus_callback_ref;
    int data = honey_window_focus_callback_data_ref;

    if (callback != LUA_NOREF) {
	luaL_unref(L, LUA_REGISTRYINDEX, callback);
    }

    if (data != LUA_NOREF && data != LUA_REFNIL) {
        luaL_unref(L, LUA_REGISTRYINDEX, callback);
    }

    honey_window_focus_callback_ref = LUA_NOREF;
    honey_window_focus_callback_data_ref = LUA_NOREF;
    return 0;
}