-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.c
403 lines (315 loc) · 13.3 KB
/
window.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "window.h"
#include "text_input.h"
extern struct RClass* mrb_gosu_window;
extern struct RClass* mrb_gosu_text_input;
typedef struct mrb_gosu_window_callback
{
mrb_state* mrb;
mrb_value self;
} mrb_gosu_window_callback;
typedef struct mrb_gosu_window_data_t
{
Gosu_Window* window;
mrb_value text_input;
} mrb_gosu_window_data_t;
static void mrb_gosu_window_data_free(mrb_state* mrb, void* ptr)
{
mrb_gosu_window_data_t* data = (mrb_gosu_window_data_t*) ptr;
if (data != NULL) {
if (data->window != NULL) {
Gosu_Window_destroy(data->window);
}
mrb_free(mrb, data);
}
}
static struct mrb_data_type const mrb_gosu_window_data_type = {
"Window",
mrb_gosu_window_data_free,
};
Gosu_Window* mrb_gosu_window_get_ptr(mrb_state* mrb, mrb_value self)
{
mrb_gosu_window_data_t* data = (mrb_gosu_window_data_t*) DATA_PTR(self);
return data->window;
}
static void mrb_gosu_window_update_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "update", 0);
}
static void mrb_gosu_window_draw_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "draw", 0);
}
static void mrb_gosu_window_button_down_callback(void* data, unsigned int btn_id)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "button_down", 1, mrb_fixnum_value(btn_id));
}
static void mrb_gosu_window_button_up_callback(void* data, unsigned int btn_id)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "button_up", 1, mrb_fixnum_value(btn_id));
}
static void mrb_gosu_window_drop_callback(void* data, const char* filename)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "drop", 1, mrb_str_new_cstr(mrb, filename));
}
static bool mrb_gosu_window_needs_redraw_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
return mrb_bool(mrb_funcall(mrb, self, "needs_redraw?", 0));
}
static bool mrb_gosu_window_needs_cursor_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
return mrb_bool(mrb_funcall(mrb, self, "needs_cursor?", 0));
}
static void mrb_gosu_window_close_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "close", 0);
}
static void mrb_gosu_window_gamepad_connected_callback(void* data, int index)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "gamepad_connected", 1, mrb_fixnum_value(index));
}
static void mrb_gosu_window_gamepad_disconnected_callback(void* data, int index)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "gamepad_disconnected", 1, mrb_fixnum_value(index));
}
static void mrb_gosu_window_gain_focus_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "gain_focus", 0);
}
static void mrb_gosu_window_lose_focus_callback(void* data)
{
mrb_gosu_window_callback* callback = (mrb_gosu_window_callback*) data;
mrb_state* mrb = callback->mrb;
mrb_value self = callback->self;
mrb_funcall(mrb, self, "lose_focus", 0);
}
static mrb_value mrb_gosu_window_show(mrb_state* mrb, mrb_value self)
{
Gosu_Window_show(mrb_gosu_window_get_ptr(mrb, self));
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_tick(mrb_state* mrb, mrb_value self)
{
Gosu_Window_tick(mrb_gosu_window_get_ptr(mrb, self));
return self;
}
static mrb_value mrb_gosu_window_caption(mrb_state* mrb, mrb_value self)
{
const char* _caption = Gosu_Window_caption(mrb_gosu_window_get_ptr(mrb, self));
return mrb_str_new(mrb, _caption, strlen(_caption));
}
static mrb_value mrb_gosu_window_set_caption(mrb_state* mrb, mrb_value self)
{
mrb_value caption;
const int argc = mrb_get_args(mrb, "S", &caption);
if (argc != 1) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments.");
}
Gosu_Window_set_caption(mrb_gosu_window_get_ptr(mrb, self), mrb_string_value_cstr(mrb, &caption));
return self;
}
static mrb_value mrb_gosu_window_width(mrb_state* mrb, mrb_value self)
{
return mrb_fixnum_value(Gosu_Window_width(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_set_width(mrb_state* mrb, mrb_value self)
{
mrb_int width;
mrb_get_args(mrb, "i", &width);
Gosu_Window_set_width(mrb_gosu_window_get_ptr(mrb, self), width);
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_height(mrb_state* mrb, mrb_value self)
{
return mrb_fixnum_value(Gosu_Window_height(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_set_height(mrb_state* mrb, mrb_value self)
{
mrb_int height;
mrb_get_args(mrb, "i", &height);
Gosu_Window_set_height(mrb_gosu_window_get_ptr(mrb, self), height);
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_update_interval(mrb_state* mrb, mrb_value self)
{
return mrb_fixnum_value(Gosu_Window_update_interval(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_set_update_interval(mrb_state* mrb, mrb_value self)
{
mrb_float interval;
mrb_get_args(mrb, "f", &interval);
Gosu_Window_set_update_interval(mrb_gosu_window_get_ptr(mrb, self), interval);
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_mouse_x(mrb_state* mrb, mrb_value self)
{
return mrb_fixnum_value(Gosu_Window_mouse_x(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_set_mouse_x(mrb_state* mrb, mrb_value self)
{
mrb_int point;
mrb_get_args(mrb, "i", &point);
Gosu_Window_set_mouse_x(mrb_gosu_window_get_ptr(mrb, self), point);
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_mouse_y(mrb_state* mrb, mrb_value self)
{
return mrb_fixnum_value(Gosu_Window_mouse_y(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_set_mouse_y(mrb_state* mrb, mrb_value self)
{
mrb_int point;
mrb_get_args(mrb, "i", &point);
Gosu_Window_set_mouse_y(mrb_gosu_window_get_ptr(mrb, self), point);
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_is_resizable(mrb_state* mrb, mrb_value self)
{
return mrb_bool_value(Gosu_Window_is_resizable(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_is_fullscreen(mrb_state* mrb, mrb_value self)
{
return mrb_bool_value(Gosu_Window_is_fullscreen(mrb_gosu_window_get_ptr(mrb, self)));
}
static mrb_value mrb_gosu_window_text_input(mrb_state* mrb, mrb_value self)
{
Gosu_TextInput* text_input;
text_input = Gosu_Window_text_input(mrb_gosu_window_get_ptr(mrb, self));
if (text_input != NULL) {
mrb_gosu_window_data_t* data = (mrb_gosu_window_data_t*) DATA_PTR(self);
return data->text_input;
}
else {
return mrb_nil_value();
}
}
static mrb_value mrb_gosu_window_set_text_input(mrb_state* mrb, mrb_value self)
{
mrb_value text_input;
mrb_get_args(mrb, "o", &text_input);
if (!mrb_nil_p(text_input)) {
Gosu_Window_set_text_input(mrb_gosu_window_get_ptr(mrb, self), mrb_gosu_text_input_get_ptr(mrb, text_input));
}
else {
Gosu_Window_set_text_input(mrb_gosu_window_get_ptr(mrb, self), NULL);
}
mrb_value old_text_input;
mrb_gosu_window_data_t* data = (mrb_gosu_window_data_t*) DATA_PTR(self);
old_text_input = data->text_input;
if (mrb_obj_is_kind_of(mrb, old_text_input, mrb_gosu_text_input)) {
mrb_gc_unregister(mrb, old_text_input);
}
if (mrb_obj_is_kind_of(mrb, text_input, mrb_gosu_text_input)) {
mrb_gc_register(mrb, text_input);
}
data->text_input = text_input;
return mrb_nil_value();
}
static mrb_value mrb_gosu_window_close_immediately(mrb_state* mrb, mrb_value self)
{
Gosu_Window_close_immediately(mrb_gosu_window_get_ptr(mrb, self));
return self;
}
static mrb_value mrb_gosu_window_new(mrb_state* mrb, mrb_value self)
{
mrb_int width, height;
mrb_float update_interval;
mrb_bool fullscreen, resizable;
mrb_gosu_window_data_t* data;
unsigned window_flags = 0;
int const argc = mrb_get_args(mrb, "iibfb", &width, &height, &fullscreen, &update_interval, &resizable);
if (argc != 5) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments.");
}
data = (mrb_gosu_window_data_t*) DATA_PTR(self);
if (data) {
mrb_free(mrb, data);
}
DATA_TYPE(self) = &mrb_gosu_window_data_type;
DATA_PTR(self) = NULL;
data = (mrb_gosu_window_data_t*) mrb_malloc(mrb, sizeof(mrb_gosu_window_data_t));
if (data == NULL) {
mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory.");
}
if (fullscreen) window_flags |= 1;
if (fullscreen) window_flags |= 2;
// window_flags |= 4;
data->window = Gosu_Window_create(width, height, window_flags, update_interval);
DATA_PTR(self) = data;
// TODO: This should probably be refactored in future
static mrb_gosu_window_callback callback;
callback.mrb = mrb;
callback.self = self;
Gosu_Window_set_update(data->window, mrb_gosu_window_update_callback, &callback);
Gosu_Window_set_draw(data->window, mrb_gosu_window_draw_callback, &callback);
Gosu_Window_set_button_down(data->window, mrb_gosu_window_button_down_callback, &callback);
Gosu_Window_set_button_up(data->window, mrb_gosu_window_button_up_callback, &callback);
Gosu_Window_set_drop(data->window, mrb_gosu_window_drop_callback, &callback);
Gosu_Window_set_needs_redraw(data->window, mrb_gosu_window_needs_redraw_callback, &callback);
Gosu_Window_set_needs_cursor(data->window, mrb_gosu_window_needs_cursor_callback, &callback);
Gosu_Window_set_gamepad_connected(data->window, mrb_gosu_window_gamepad_connected_callback, &callback);
Gosu_Window_set_gamepad_disconnected(data->window, mrb_gosu_window_gamepad_disconnected_callback, &callback);
Gosu_Window_set_gain_focus(data->window, mrb_gosu_window_gain_focus_callback, &callback);
Gosu_Window_set_lose_focus(data->window, mrb_gosu_window_lose_focus_callback, &callback);
Gosu_Window_set_close(data->window, mrb_gosu_window_close_callback, &callback);
return self;
}
void mrb_gosu_window_init(mrb_state* mrb, struct RClass* mrb_gosu)
{
mrb_gosu_window = mrb_define_class_under(mrb, mrb_gosu, "Window", mrb->object_class);
mrb_define_method(mrb, mrb_gosu_window, "_new", mrb_gosu_window_new, MRB_ARGS_REQ(5));
mrb_define_method(mrb, mrb_gosu_window, "caption", mrb_gosu_window_caption, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "caption=", mrb_gosu_window_set_caption, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "width", mrb_gosu_window_width, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "width=", mrb_gosu_window_set_width, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "height", mrb_gosu_window_height, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "height=", mrb_gosu_window_set_height, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "update_interval", mrb_gosu_window_update_interval, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "update_interval=", mrb_gosu_window_set_update_interval, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "mouse_x", mrb_gosu_window_mouse_x, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "mouse_x=", mrb_gosu_window_set_mouse_x, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "mouse_y", mrb_gosu_window_mouse_y, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "mouse_y=", mrb_gosu_window_set_mouse_y, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "fullscreen?", mrb_gosu_window_is_fullscreen, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "resizable?", mrb_gosu_window_is_resizable, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "text_input", mrb_gosu_window_text_input, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "text_input=", mrb_gosu_window_set_text_input, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb_gosu_window, "show", mrb_gosu_window_show, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "tick", mrb_gosu_window_tick, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb_gosu_window, "close!", mrb_gosu_window_close_immediately, MRB_ARGS_NONE());
}
void mrb_gosu_window_free(mrb_state* mrb, mrb_value self)
{
}