Added a bunch of new drawing functions

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-08 03:03:09 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-08 03:03:09 +0200
Commit 976e810d7d3eaa9e6c0a7b0d865ea6cb2e94e02a (patch)
-rw-r--r-- examples/graphics.lua 9
-rw-r--r-- main.c 108
2 files changed, 111 insertions, 6 deletions
diff --git a/examples/graphics.lua b/examples/graphics.lua
...
8
	draw_rect(100, 100, 300, 200, color.YELLOW)
8
	draw_rect(100, 100, 300, 200, color.YELLOW)
9
	draw_text("YOOOOO", 10, 10, 20, color.VIOLET)
9
	draw_text("YOOOOO", 10, 10, 20, color.VIOLET)
10
  
10
  
  
11
	draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET)
  
12
	draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET)
  
13
  
  
14
	draw_line(400, 10, 500, 100, color.RED)
  
15
	draw_circle(500, 500, 100, color.BLUE)
  
16
	draw_ellipse(200, 500, 100, 50, color.BLUE)
  
17
  
  
18
	draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE)
  
19
  
11
	draw_info()
20
	draw_info()
12
	end_drawing()
21
	end_drawing()
13
end
22
end
...
diff --git a/main.c b/main.c
...
81
	return 0;
81
	return 0;
82
}
82
}
83
  
83
  
  
84
static int l_get_dt(lua_State *L) {
  
85
	lua_pushnumber(L, GetFrameTime());
  
86
	return 1;
  
87
}
  
88
  
  
89
static int l_get_fps(lua_State *L) {
  
90
	lua_pushnumber(L, GetFPS());
  
91
	return 1;
  
92
}
  
93
  
84
static int l_close_window(lua_State *L) {
94
static int l_close_window(lua_State *L) {
85
	CloseWindow();
95
	CloseWindow();
86
	TraceLog(LOG_DEBUG, "l_close_window");
96
	TraceLog(LOG_DEBUG, "l_close_window");
...
154
	return 0;
164
	return 0;
155
}
165
}
156
  
166
  
157
static int l_draw_pixel(lua_State *L) {}
167
static int l_draw_pixel(lua_State *L) {
158
static int l_draw_line(lua_State *L) {}
168
	int x = luaL_checknumber(L, 1);
159
static int l_draw_circle(lua_State *L) {}
169
	int y = luaL_checknumber(L, 2);
160
static int l_draw_ellipse(lua_State *L) {}
170
	luaL_checktype(L, 3, LUA_TTABLE);
161
static int l_draw_triangle(lua_State *L) {}
171
	Color color = {
  
172
		.r = (unsigned char)lua_getfield_int(L, 3, "r"),
  
173
		.g = (unsigned char)lua_getfield_int(L, 3, "g"),
  
174
		.b = (unsigned char)lua_getfield_int(L, 3, "b"),
  
175
		.a = (unsigned char)lua_getfield_int_opt(L, 3, "a", 255)
  
176
	};
  
177
	DrawPixel(x, y, color);
  
178
}
  
179
  
  
180
static int l_draw_line(lua_State *L) {
  
181
	int x1 = luaL_checknumber(L, 1);
  
182
	int y1 = luaL_checknumber(L, 2);
  
183
	int x2 = luaL_checknumber(L, 3);
  
184
	int y2 = luaL_checknumber(L, 4);
  
185
	luaL_checktype(L, 5, LUA_TTABLE);
  
186
	Color color = {
  
187
		.r = (unsigned char)lua_getfield_int(L, 5, "r"),
  
188
		.g = (unsigned char)lua_getfield_int(L, 5, "g"),
  
189
		.b = (unsigned char)lua_getfield_int(L, 5, "b"),
  
190
		.a = (unsigned char)lua_getfield_int_opt(L, 5, "a", 255)
  
191
	};
  
192
	DrawLine(x1, y1, x2, y2, color);
  
193
	return 0;
  
194
}
  
195
  
  
196
static int l_draw_circle(lua_State *L) {
  
197
	int center_x = luaL_checknumber(L, 1);
  
198
	int center_y = luaL_checknumber(L, 2);
  
199
	int radius = luaL_checknumber(L, 3);
  
200
	luaL_checktype(L, 4, LUA_TTABLE);
  
201
	Color color = {
  
202
		.r = (unsigned char)lua_getfield_int(L, 4, "r"),
  
203
		.g = (unsigned char)lua_getfield_int(L, 4, "g"),
  
204
		.b = (unsigned char)lua_getfield_int(L, 4, "b"),
  
205
		.a = (unsigned char)lua_getfield_int_opt(L, 4, "a", 255)
  
206
	};
  
207
	DrawCircle(center_x, center_y, radius, color);
  
208
	return 0;
  
209
}
  
210
  
  
211
static int l_draw_ellipse(lua_State *L) {
  
212
	int center_x = luaL_checknumber(L, 1);
  
213
	int center_y = luaL_checknumber(L, 2);
  
214
	int radius_h = luaL_checknumber(L, 3);
  
215
	int radius_v = luaL_checknumber(L, 4);
  
216
	luaL_checktype(L, 5, LUA_TTABLE);
  
217
	Color color = {
  
218
		.r = (unsigned char)lua_getfield_int(L, 5, "r"),
  
219
		.g = (unsigned char)lua_getfield_int(L, 5, "g"),
  
220
		.b = (unsigned char)lua_getfield_int(L, 5, "b"),
  
221
		.a = (unsigned char)lua_getfield_int_opt(L, 5, "a", 255)
  
222
	};
  
223
	DrawEllipse(center_x, center_y, radius_h, radius_v, color);
  
224
	return 0;
  
225
}
  
226
  
  
227
static int l_draw_triangle(lua_State *L) {
  
228
	int x1 = luaL_checknumber(L, 1);
  
229
	int y1 = luaL_checknumber(L, 2);
  
230
	int x2 = luaL_checknumber(L, 3);
  
231
	int y2 = luaL_checknumber(L, 4);
  
232
	int x3 = luaL_checknumber(L, 5);
  
233
	int y3 = luaL_checknumber(L, 6);
  
234
	luaL_checktype(L, 7, LUA_TTABLE);
  
235
	Color color = {
  
236
		.r = (unsigned char)lua_getfield_int(L, 7, "r"),
  
237
		.g = (unsigned char)lua_getfield_int(L, 7, "g"),
  
238
		.b = (unsigned char)lua_getfield_int(L, 7, "b"),
  
239
		.a = (unsigned char)lua_getfield_int_opt(L, 7, "a", 255)
  
240
	};
  
241
  
  
242
	// NOTE: Raylib orders vertices in counter-clockwise order. We order them
  
243
	// in clockwise order instead to make this a bit easier to use.
  
244
	Vector2 p1 = { x1, y1 };
  
245
	Vector2 p2 = { x3, y3 };
  
246
	Vector2 p3 = { x2, y2 };
  
247
  
  
248
	DrawTriangle(p1, p2, p3, color);
  
249
	return 0;
  
250
}
162
  
251
  
163
static void help(const char *argv0) {
252
static void help(const char *argv0) {
164
	printf("Usage: %s [options]\n"
253
	printf("Usage: %s [options]\n"
...
226
		lua_register(L, "begin_drawing", l_begin_drawing);
315
		lua_register(L, "begin_drawing", l_begin_drawing);
227
		lua_register(L, "end_drawing", l_end_drawing);
316
		lua_register(L, "end_drawing", l_end_drawing);
228
		lua_register(L, "set_fps", l_set_fps);
317
		lua_register(L, "set_fps", l_set_fps);
  
318
		lua_register(L, "get_fps", l_get_fps);
  
319
		lua_register(L, "get_dt", l_get_dt);
  
320
		lua_register(L, "clear_window", l_clear_window);
229
		lua_register(L, "draw_info", l_draw_info);
321
		lua_register(L, "draw_info", l_draw_info);
230
		lua_register(L, "clear_window", l_clear_window);
  
231
		lua_register(L, "draw_rect", l_draw_rect);
322
		lua_register(L, "draw_rect", l_draw_rect);
232
		lua_register(L, "draw_text", l_draw_text);
323
		lua_register(L, "draw_text", l_draw_text);
  
324
		lua_register(L, "draw_pixel", l_draw_pixel);
  
325
		lua_register(L, "draw_line", l_draw_line);
  
326
		lua_register(L, "draw_circle", l_draw_circle);
  
327
		lua_register(L, "draw_ellipse", l_draw_ellipse);
  
328
		lua_register(L, "draw_triangle", l_draw_triangle);
233
  
329
  
234
		// Loading embeded modules into Lua state.
330
		// Loading embeded modules into Lua state.
235
		if (!load_embedded_module(L, json, json_len, "json")) return 1;
331
		if (!load_embedded_module(L, json, json_len, "json")) return 1;
...