-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaraylib.c
More file actions
147 lines (123 loc) · 3.73 KB
/
luaraylib.c
File metadata and controls
147 lines (123 loc) · 3.73 KB
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
/* LuaRayLib API:
* raylib
* - set_fps(integer)
* - number get_delta()
* - number get_time()
* - window [window control function]
* - init(integer, integer, string)
* - close()
* - boolean should_close()
* - draw [drawing functions]
* - begin()
* - rend()^1
* - clear(integer)
* - rect(integer, integer, integer, integer, integer)
* - circle(integer, integer, number, integer)
* - colors [color constants (is integers)]
* - red ... raywhite
*
* [1] end is keyword, because
* rend mean "Raylib END"
*/
#include <raylib/raylib.h>
#include <lua54/lauxlib.h>
#include <lua54/lua.h>
static inline unsigned lrl_col2int(Color col) {
return (unsigned)col.r << 24 | (unsigned)col.g << 16
| (unsigned)col.b << 8 | (unsigned)col.a;
}
static int lrl_init_window(lua_State* L) {
int width = luaL_checkinteger(L, 1);
int height = luaL_checkinteger(L, 2);
const char* title = luaL_checkstring(L, 3);
luaL_argcheck(L, title != NULL, 3,
"title cannot be empty");
InitWindow(width, height, title);
return 0;
}
static int lrl_close_window(lua_State* L) {
CloseWindow(); (void)L; return 0;
}
static int lrl_window_should_close(lua_State* L) {
lua_pushboolean(L, WindowShouldClose());
return 1;
}
static int lrl_set_target_fps(lua_State* L) {
SetTargetFPS(luaL_checkinteger(L, 1));
return 0;
}
static int lrl_get_frame_time(lua_State* L) {
lua_pushnumber(L, GetFrameTime());
return 1;
}
static int lrl_get_time(lua_State* L) {
lua_pushnumber(L, GetTime());
return 1;
}
static int lrl_begin_drawing(lua_State* L) {
BeginDrawing(); (void)L; return 0;
}
static int lrl_end_drawing(lua_State* L) {
EndDrawing(); (void)L; return 0;
}
static int lrl_clear_background(lua_State* L) {
ClearBackground(GetColor(luaL_checkinteger(L, 1)));
return 0;
}
static int lrl_draw_rectangle(lua_State* L) {
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int width = luaL_checkinteger(L, 3);
int height = luaL_checkinteger(L, 4);
int color = luaL_checkinteger(L, 5);
DrawRectangle(x, y, width, height, GetColor(color));
return 0;
}
static int lrl_draw_circle(lua_State* L) {
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
double radius = luaL_checknumber(L, 3);
int color = luaL_checkinteger(L, 4);
DrawCircle(x, y, radius, GetColor(color));
return 0;
}
static const struct luaL_Reg lrl_window_funcs[] = {
{ "init", lrl_init_window },
{ "close", lrl_close_window },
{ "should_close", lrl_window_should_close },
{ NULL, NULL }
};
static const struct luaL_Reg lrl_draw_funcs[] = {
{ "begin", lrl_begin_drawing },
{ "rend", lrl_end_drawing },
{ "clear", lrl_clear_background },
{ "rect", lrl_draw_rectangle },
{ "circle", lrl_draw_circle },
{ NULL, NULL }
};
static const struct luaL_Reg lrl_glob_funcs[] = {
{ "set_fps", lrl_set_target_fps },
{ "get_delta", lrl_get_frame_time },
{ "get_time", lrl_get_time },
{ NULL, NULL }
};
LUA_API int luaopen_luaraylib(lua_State* L) {
luaL_newlib(L, lrl_glob_funcs);
luaL_newlib(L, lrl_window_funcs);
lua_setfield(L, -2, "window");
luaL_newlib(L, lrl_draw_funcs);
lua_setfield(L, -2, "draw");
lua_createtable(L, 0, 26);
#define DO(name) \
lua_pushinteger(L, lrl_col2int(name)); \
lua_setfield(L, -2, #name);
DO(LIGHTGRAY) DO(GRAY) DO(DARKGRAY) DO(YELLOW)
DO(GOLD) DO(ORANGE) DO(PINK) DO(RED) DO(MAROON)
DO(GREEN) DO(LIME) DO(DARKGREEN) DO(SKYBLUE)
DO(BLUE) DO(DARKBLUE) DO(PURPLE) DO(VIOLET)
DO(DARKPURPLE) DO(BEIGE) DO(BROWN) DO(DARKBROWN)
DO(WHITE) DO(BLACK) DO(BLANK) DO(MAGENTA) DO(RAYWHITE)
#undef DO
lua_setfield(L, -2, "colors");
return 1;
}