-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsdlwnd.c
225 lines (196 loc) · 4.84 KB
/
sdlwnd.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
#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>
#include <stdint.h>
#include <string.h>
#include "SDL.h"
#include "SDL_syswm.h"
#define FRAMESEC 1000
struct context {
SDL_Window *window;
uint64_t tick;
int frame;
int fps;
int w;
int h;
};
static struct context *
getCtx(lua_State *L) {
struct context * ctx = (struct context *)lua_touserdata(L, lua_upvalueindex(1));
return ctx;
}
static int
get_int(lua_State *L, int idx, const char * name) {
if (lua_getfield(L, idx, name) != LUA_TNUMBER) {
luaL_error(L, "Can't get %s as number", name);
}
int isnum;
int r = lua_tointegerx(L, -1, &isnum);
if (!isnum)
luaL_error(L, "Can't get %s as integer", name);
lua_pop(L, 1);
return r;
}
static uint32_t
is_enable(lua_State *L, int idx, const char * name) {
lua_getfield(L, idx, name);
int enable = lua_toboolean(L, -1);
lua_pop(L, 1);
return enable ? 0xffffffff : 0;
}
static int
linit(lua_State *L) {
struct context * ctx = getCtx(L);
if (ctx->window != NULL)
return luaL_error(L, "Already init");
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return luaL_error(L, "Couldn't initialize SDL: %s\n", SDL_GetError());
luaL_checktype(L, 1, LUA_TTABLE);
ctx->w = get_int(L, 1, "width");
ctx->h = get_int(L, 1, "height");
uint32_t flags = 0;
flags |= is_enable(L, 1, "borderless") & SDL_WINDOW_BORDERLESS;
flags |= is_enable(L, 1, "resizeable") & SDL_WINDOW_RESIZABLE;
flags |= is_enable(L, 1, "fullscreen") & SDL_WINDOW_FULLSCREEN;
const char * title = "";
if (lua_getfield(L, 1, "title") == LUA_TSTRING) {
title = lua_tostring(L, -1);
lua_pop(L, 1);
}
SDL_Window *wnd = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ctx->w, ctx->h, flags);
if (wnd == NULL) {
return luaL_error(L, "Couldn't create window : %s", SDL_GetError());
}
ctx->window = wnd;
ctx->tick = SDL_GetTicks64();
ctx->frame = 0;
ctx->fps = get_int(L, 1, "fps");
return 0;
}
static int
lframe(lua_State *L) {
struct context * ctx = getCtx(L);
uint64_t c = SDL_GetTicks64();
int lastframe = ctx->frame;
int frame = lastframe + 1;
if (frame > ctx->fps) {
lastframe = 0;
frame = 1;
}
int delta = FRAMESEC * frame / ctx->fps - FRAMESEC * lastframe / ctx->fps;
ctx->frame = frame;
ctx->tick += delta;
if (c < ctx->tick)
SDL_Delay(ctx->tick - c);
if (c - ctx->tick > FRAMESEC) {
// reset frame count if the error is too large
ctx->tick = c;
ctx->frame = 0;
}
return 0;
}
static int
winevent(lua_State *L, SDL_Event *ev) {
switch (ev->window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED :
lua_pushstring(L, "RESIZE");
lua_pushinteger(L, ev->window.data1);
lua_pushinteger(L, ev->window.data2);
return 3;
}
return 0;
}
static int
keyevent(lua_State *L, SDL_Event *ev) {
// if (ev->key.repeat)
// return 0;
lua_pushstring(L, "KEY");
lua_pushstring(L, SDL_GetKeyName(ev->key.keysym.sym));
lua_pushboolean(L, ev->key.type == SDL_KEYDOWN);
return 3;
}
static int
motionevent(lua_State *L, SDL_Event *ev) {
int x = ev->motion.x;
int y = ev->motion.y;
lua_pushstring(L, "MOTION");
lua_pushinteger(L, x);
lua_pushinteger(L, y);
return 3;
}
static int
buttonevent(lua_State *L, SDL_Event *ev) {
int x = ev->motion.x;
int y = ev->motion.y;
lua_pushstring(L, "BUTTON");
lua_pushinteger(L, x);
lua_pushinteger(L, y);
lua_pushinteger(L, ev->button.button);
lua_pushboolean(L, ev->button.state == SDL_PRESSED);
lua_pushinteger(L, ev->button.clicks);
return 6;
}
static int
levent(lua_State *L) {
SDL_Event event;
int r;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
lua_pushstring(L, "QUIT");
return 1;
case SDL_WINDOWEVENT:
if ((r = winevent(L, &event)) > 0)
return r;
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
if ((r = keyevent(L, &event)) > 0)
return r;
break;
case SDL_TEXTEDITING:
SDL_StopTextInput();
break;
case SDL_MOUSEMOTION:
if ((r = motionevent(L, &event)) > 0)
return r;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
return buttonevent(L, &event);
default:
break;
}
}
return 0;
}
static int
lhandle(lua_State *L) {
// todo : support other platforms, it's windows only now
struct context * ctx = getCtx(L);
if (ctx->window == NULL)
return luaL_error(L, "Init first");
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
if (!SDL_GetWindowWMInfo(ctx->window, &wmInfo)) {
return luaL_error(L, "SDL_GetWindowWMInfo error : %s", SDL_GetError());
}
lua_pushlightuserdata(L, (void *)wmInfo.info.win.window);
return 1;
}
LUAMOD_API int
luaopen_sdlwnd(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "init", linit },
{ "frame", lframe },
{ "event", levent },
{ "handle", lhandle },
{ NULL, NULL },
};
luaL_newlibtable(L, l);
struct context *ctx = (struct context *)lua_newuserdatauv(L, sizeof(struct context), 1);
memset(ctx, 0, sizeof(*ctx));
luaL_setfuncs(L,l,1);
return 1;
}