From 8fed3ca3744c489e9e1d33fed7cf25a3dca26a21 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 9 Aug 2025 04:39:39 +0200 Subject: Added camera controls --- main.c | 65 ++++++++++++++++++++++++++++++++++++++++++------------ tests/graphics.lua | 37 ++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index 019345f..3ede22b 100644 --- a/main.c +++ b/main.c @@ -77,16 +77,20 @@ static int l_open_window(lua_State *L) { const char *title = luaL_checkstring(L, 3); SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI); InitWindow(width, height, title); - TraceLog(LOG_DEBUG, "l_open_window"); ctx.font_size = FONT_IMPORT_SIZE; ctx.font = LoadFontFromMemory(".ttf", dejavusans_mono_bold, dejavusans_mono_bold_len, ctx.font_size, NULL, 0); SetTextureFilter(ctx.font.texture, TEXTURE_FILTER_TRILINEAR); if (!IsFontValid(ctx.font)) { - printf("font not valid\n"); + TraceLog(LOG_DEBUG, "Font not valid."); } + ctx.camera.target = (Vector2){ 0.0f, 0.0f }; + ctx.camera.offset = (Vector2){ GetScreenHeight()/2.0f, GetScreenHeight()/2.0f }; + ctx.camera.rotation = 0.0f; + ctx.camera.zoom = 1.0f; + return 0; } @@ -99,7 +103,6 @@ static int l_window_running(lua_State *L) { static int l_set_fps(lua_State *L) { int fps = luaL_checknumber(L, 1); SetTargetFPS(fps); - TraceLog(LOG_DEBUG, "l_set_fps"); return 0; } @@ -113,22 +116,50 @@ static int l_get_fps(lua_State *L) { return 1; } +static int l_get_width(lua_State *L) { + lua_pushnumber(L, GetScreenWidth()); + return 1; +} + +static int l_get_height(lua_State *L) { + lua_pushnumber(L, GetScreenHeight()); + return 1; +} + static int l_close_window(lua_State *L) { CloseWindow(); - TraceLog(LOG_DEBUG, "l_close_window"); return 0; } -static int l_begin_drawing(lua_State *L) { +static int l_start_drawing(lua_State *L) { BeginDrawing(); return 0; } -static int l_end_drawing(lua_State *L) { +static int l_stop_drawing(lua_State *L) { EndDrawing(); return 0; } +static int l_start_camera(lua_State *L) { + BeginMode2D(ctx.camera); + return 0; +} + +static int l_stop_camera(lua_State *L) { + EndMode2D(); + return 0; +} + +static int l_move_camera(lua_State *L) { + int x = luaL_checknumber(L, 1); + int y = luaL_checknumber(L, 2); + + ctx.camera.target.x = x; + ctx.camera.target.y = y; + return 0; +} + static int l_clear_window(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); Color color = { @@ -291,10 +322,6 @@ static int l_load_audio(lua_State *L) { return 0; } -static int l_move_camera(lua_State *L) { - return 0; -} - static void help(const char *argv0) { printf("Usage: %s [options]\n" "\nAvailable options:\n" @@ -366,12 +393,21 @@ int main(int argc, char *argv[]) { lua_register(L, "open_window", l_open_window); lua_register(L, "close_window", l_close_window); lua_register(L, "window_running", l_window_running); - lua_register(L, "begin_drawing", l_begin_drawing); - lua_register(L, "end_drawing", l_end_drawing); + + lua_register(L, "start_drawing", l_start_drawing); + lua_register(L, "stop_drawing", l_stop_drawing); + lua_register(L, "clear_window", l_clear_window); + + lua_register(L, "start_camera", l_start_camera); + lua_register(L, "stop_camera", l_stop_camera); + lua_register(L, "move_camera", l_move_camera); + lua_register(L, "set_fps", l_set_fps); lua_register(L, "get_fps", l_get_fps); lua_register(L, "get_dt", l_get_dt); - lua_register(L, "clear_window", l_clear_window); + lua_register(L, "get_width", l_get_width); + lua_register(L, "get_height", l_get_height); + lua_register(L, "draw_info", l_draw_info); lua_register(L, "draw_rect", l_draw_rect); lua_register(L, "draw_text", l_draw_text); @@ -380,9 +416,10 @@ int main(int argc, char *argv[]) { lua_register(L, "draw_circle", l_draw_circle); lua_register(L, "draw_ellipse", l_draw_ellipse); lua_register(L, "draw_triangle", l_draw_triangle); + lua_register(L, "load_image", l_load_image); lua_register(L, "load_audio", l_load_audio); - lua_register(L, "move_camera", l_move_camera); + lua_register(L, "button_down", l_button_down); lua_register(L, "button_pressed", l_button_pressed); diff --git a/tests/graphics.lua b/tests/graphics.lua index fc531a9..1322101 100644 --- a/tests/graphics.lua +++ b/tests/graphics.lua @@ -4,6 +4,9 @@ test_button_square = { x = 400, y = 400 } test_button_color = color.RED test_button_speed = 200 +test_camera_position = { x = 0, y = 0 } +test_camera_speed = 200 + test_images_asset1 = load_image("tests/icons/icon_1.png") open_window(800, 800, "Sample Window") @@ -87,15 +90,43 @@ function test_buttons() draw_rect(test_button_square.x, test_button_square.y, 50, 50, test_button_color) end +function test_camera() + if button_down(button.PAD_UP) then + test_camera_position.y = test_camera_position.y - (test_camera_speed * get_dt()) + end + + if button_down(button.PAD_DOWN) then + test_camera_position.y = test_camera_position.y + (test_camera_speed * get_dt()) + end + + if button_down(button.PAD_LEFT) then + test_camera_position.x = test_camera_position.x - (test_camera_speed * get_dt()) + end + + if button_down(button.PAD_RIGHT) then + test_camera_position.x = test_camera_position.x + (test_camera_speed * get_dt()) + end + + -- Using camera + move_camera(test_camera_position.x, test_camera_position.y) + + start_camera() + draw_rect(0, 0, 300, 200, color.YELLOW) + stop_camera() + + draw_text("This text doesn't move!", 10, 10, 20, color.VIOLET) +end + while window_running() do - begin_drawing() + start_drawing() clear_window(color.BLACK) -- test_api() - test_buttons() + -- test_buttons() + -- test_camera() draw_info() - end_drawing() + stop_drawing() end close_window() -- cgit v1.2.3