Added camera controls

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 04:39:39 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 04:39:39 +0200
Commit 8fed3ca3744c489e9e1d33fed7cf25a3dca26a21 (patch)
-rw-r--r-- main.c 65
-rw-r--r-- tests/graphics.lua 37
2 files changed, 85 insertions, 17 deletions
diff --git a/main.c b/main.c
...
77
	const char *title = luaL_checkstring(L, 3);
77
	const char *title = luaL_checkstring(L, 3);
78
	SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
78
	SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
79
	InitWindow(width, height, title);
79
	InitWindow(width, height, title);
80
	TraceLog(LOG_DEBUG, "l_open_window");
  
81
  
80
  
82
	ctx.font_size = FONT_IMPORT_SIZE;
81
	ctx.font_size = FONT_IMPORT_SIZE;
83
	ctx.font = LoadFontFromMemory(".ttf", dejavusans_mono_bold, dejavusans_mono_bold_len, ctx.font_size, NULL, 0);
82
	ctx.font = LoadFontFromMemory(".ttf", dejavusans_mono_bold, dejavusans_mono_bold_len, ctx.font_size, NULL, 0);
84
	SetTextureFilter(ctx.font.texture, TEXTURE_FILTER_TRILINEAR);
83
	SetTextureFilter(ctx.font.texture, TEXTURE_FILTER_TRILINEAR);
85
  
84
  
86
	if (!IsFontValid(ctx.font)) {
85
	if (!IsFontValid(ctx.font)) {
87
		printf("font not valid\n");
86
		TraceLog(LOG_DEBUG, "Font not valid.");
88
	}
87
	}
  
88
  
  
89
	ctx.camera.target = (Vector2){ 0.0f, 0.0f };
  
90
	ctx.camera.offset = (Vector2){ GetScreenHeight()/2.0f, GetScreenHeight()/2.0f };
  
91
	ctx.camera.rotation = 0.0f;
  
92
	ctx.camera.zoom = 1.0f;
89
  
93
  
90
	return 0;
94
	return 0;
91
}
95
}
...
99
static int l_set_fps(lua_State *L) {
103
static int l_set_fps(lua_State *L) {
100
	int fps = luaL_checknumber(L, 1);
104
	int fps = luaL_checknumber(L, 1);
101
	SetTargetFPS(fps);
105
	SetTargetFPS(fps);
102
	TraceLog(LOG_DEBUG, "l_set_fps");
  
103
	return 0;
106
	return 0;
104
}
107
}
105
  
108
  
...
113
	return 1;
116
	return 1;
114
}
117
}
115
  
118
  
  
119
static int l_get_width(lua_State *L) {
  
120
	lua_pushnumber(L, GetScreenWidth());
  
121
	return 1;
  
122
}
  
123
  
  
124
static int l_get_height(lua_State *L) {
  
125
	lua_pushnumber(L, GetScreenHeight());
  
126
	return 1;
  
127
}
  
128
  
116
static int l_close_window(lua_State *L) {
129
static int l_close_window(lua_State *L) {
117
	CloseWindow();
130
	CloseWindow();
118
	TraceLog(LOG_DEBUG, "l_close_window");
  
119
	return 0;
131
	return 0;
120
}
132
}
121
  
133
  
122
static int l_begin_drawing(lua_State *L) {
134
static int l_start_drawing(lua_State *L) {
123
	BeginDrawing();
135
	BeginDrawing();
124
	return 0;
136
	return 0;
125
}
137
}
126
  
138
  
127
static int l_end_drawing(lua_State *L) {
139
static int l_stop_drawing(lua_State *L) {
128
	EndDrawing();
140
	EndDrawing();
129
	return 0;
141
	return 0;
130
}
142
}
131
  
143
  
  
144
static int l_start_camera(lua_State *L) {
  
145
	BeginMode2D(ctx.camera);
  
146
	return 0;
  
147
}
  
148
  
  
149
static int l_stop_camera(lua_State *L) {
  
150
	EndMode2D();
  
151
	return 0;
  
152
}
  
153
  
  
154
static int l_move_camera(lua_State *L) {
  
155
	int x = luaL_checknumber(L, 1);
  
156
	int y = luaL_checknumber(L, 2);
  
157
  
  
158
	ctx.camera.target.x = x;
  
159
	ctx.camera.target.y = y;
  
160
	return 0;
  
161
}
  
162
  
132
static int l_clear_window(lua_State *L) {
163
static int l_clear_window(lua_State *L) {
133
	luaL_checktype(L, 1, LUA_TTABLE);
164
	luaL_checktype(L, 1, LUA_TTABLE);
134
	Color color = {
165
	Color color = {
...
291
	return 0;
322
	return 0;
292
}
323
}
293
  
324
  
294
static int l_move_camera(lua_State *L) {
  
295
	return 0;
  
296
}
  
297
  
  
298
static void help(const char *argv0) {
325
static void help(const char *argv0) {
299
	printf("Usage: %s [options]\n"
326
	printf("Usage: %s [options]\n"
300
			"\nAvailable options:\n"
327
			"\nAvailable options:\n"
...
366
		lua_register(L, "open_window", l_open_window);
393
		lua_register(L, "open_window", l_open_window);
367
		lua_register(L, "close_window", l_close_window);
394
		lua_register(L, "close_window", l_close_window);
368
		lua_register(L, "window_running", l_window_running);
395
		lua_register(L, "window_running", l_window_running);
369
		lua_register(L, "begin_drawing", l_begin_drawing);
396
  
370
		lua_register(L, "end_drawing", l_end_drawing);
397
		lua_register(L, "start_drawing", l_start_drawing);
  
398
		lua_register(L, "stop_drawing", l_stop_drawing);
  
399
		lua_register(L, "clear_window", l_clear_window);
  
400
		
  
401
		lua_register(L, "start_camera", l_start_camera);
  
402
		lua_register(L, "stop_camera", l_stop_camera);
  
403
		lua_register(L, "move_camera", l_move_camera);
  
404
		
371
		lua_register(L, "set_fps", l_set_fps);
405
		lua_register(L, "set_fps", l_set_fps);
372
		lua_register(L, "get_fps", l_get_fps);
406
		lua_register(L, "get_fps", l_get_fps);
373
		lua_register(L, "get_dt", l_get_dt);
407
		lua_register(L, "get_dt", l_get_dt);
374
		lua_register(L, "clear_window", l_clear_window);
408
		lua_register(L, "get_width", l_get_width);
  
409
		lua_register(L, "get_height", l_get_height);
  
410
		
375
		lua_register(L, "draw_info", l_draw_info);
411
		lua_register(L, "draw_info", l_draw_info);
376
		lua_register(L, "draw_rect", l_draw_rect);
412
		lua_register(L, "draw_rect", l_draw_rect);
377
		lua_register(L, "draw_text", l_draw_text);
413
		lua_register(L, "draw_text", l_draw_text);
...
380
		lua_register(L, "draw_circle", l_draw_circle);
416
		lua_register(L, "draw_circle", l_draw_circle);
381
		lua_register(L, "draw_ellipse", l_draw_ellipse);
417
		lua_register(L, "draw_ellipse", l_draw_ellipse);
382
		lua_register(L, "draw_triangle", l_draw_triangle);
418
		lua_register(L, "draw_triangle", l_draw_triangle);
  
419
		
383
		lua_register(L, "load_image", l_load_image);
420
		lua_register(L, "load_image", l_load_image);
384
		lua_register(L, "load_audio", l_load_audio);
421
		lua_register(L, "load_audio", l_load_audio);
385
		lua_register(L, "move_camera", l_move_camera);
422
		
386
		lua_register(L, "button_down", l_button_down);
423
		lua_register(L, "button_down", l_button_down);
387
		lua_register(L, "button_pressed", l_button_pressed);
424
		lua_register(L, "button_pressed", l_button_pressed);
388
  
425
  
...
diff --git a/tests/graphics.lua b/tests/graphics.lua
...
4
test_button_color = color.RED
4
test_button_color = color.RED
5
test_button_speed = 200
5
test_button_speed = 200
6
  
6
  
  
7
test_camera_position = { x = 0, y = 0 }
  
8
test_camera_speed = 200
  
9
  
7
test_images_asset1 = load_image("tests/icons/icon_1.png")
10
test_images_asset1 = load_image("tests/icons/icon_1.png")
8
  
11
  
9
open_window(800, 800, "Sample Window")
12
open_window(800, 800, "Sample Window")
...
87
	draw_rect(test_button_square.x, test_button_square.y, 50, 50, test_button_color)
90
	draw_rect(test_button_square.x, test_button_square.y, 50, 50, test_button_color)
88
end
91
end
89
  
92
  
  
93
function test_camera()
  
94
	if button_down(button.PAD_UP) then
  
95
		test_camera_position.y = test_camera_position.y - (test_camera_speed * get_dt())
  
96
	end
  
97
  
  
98
	if button_down(button.PAD_DOWN) then
  
99
		test_camera_position.y = test_camera_position.y + (test_camera_speed * get_dt())
  
100
	end
  
101
  
  
102
	if button_down(button.PAD_LEFT) then
  
103
		test_camera_position.x = test_camera_position.x - (test_camera_speed * get_dt())
  
104
	end
  
105
  
  
106
	if button_down(button.PAD_RIGHT) then
  
107
		test_camera_position.x = test_camera_position.x + (test_camera_speed * get_dt())
  
108
	end
  
109
  
  
110
	-- Using camera
  
111
	move_camera(test_camera_position.x, test_camera_position.y)
  
112
  
  
113
	start_camera()
  
114
	draw_rect(0, 0, 300, 200, color.YELLOW)
  
115
	stop_camera()
  
116
  
  
117
	draw_text("This text doesn't move!", 10, 10, 20, color.VIOLET)
  
118
end
  
119
  
90
while window_running() do
120
while window_running() do
91
	begin_drawing()
121
	start_drawing()
92
	clear_window(color.BLACK)
122
	clear_window(color.BLACK)
93
  
123
  
94
	-- test_api()
124
	-- test_api()
95
	test_buttons()
125
	-- test_buttons()
  
126
	-- test_camera()
96
  
127
  
97
	draw_info()
128
	draw_info()
98
	end_drawing()
129
	stop_drawing()
99
end
130
end
100
  
131
  
101
close_window()
132
close_window()