From d2390b0ad887b463c5da4ac0620c026f8ae4c2e7 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 9 Aug 2025 04:01:54 +0200 Subject: Renamed -r to -f flag and added button_down function --- .vimrc | 2 +- main.c | 15 +++++++++++---- tests/graphics.lua | 32 ++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/.vimrc b/.vimrc index 7bd9554..e5a94f6 100644 --- a/.vimrc +++ b/.vimrc @@ -2,7 +2,7 @@ set makeprg=make set errorformat=%f:%l:%c:\ %m let g:gdb_executable = 'bidi' -let g:gdb_arguments = '-r tests/graphics.lua' +let g:gdb_arguments = '-f tests/graphics.lua' nnoremap m :call LocalMake() nnoremap r :execute '!./' . g:gdb_executable . ' ' . g:gdb_arguments diff --git a/main.c b/main.c index 67f3520..019345f 100644 --- a/main.c +++ b/main.c @@ -271,12 +271,18 @@ static int l_draw_triangle(lua_State *L) { return 0; } -static int l_button_pressed(lua_State *L) { +static int l_button_down(lua_State *L) { int button = luaL_checknumber(L, 1); lua_pushboolean(L, IsKeyDown(button)); return 1; } +static int l_button_pressed(lua_State *L) { + int button = luaL_checknumber(L, 1); + lua_pushboolean(L, IsKeyPressed(button)); + return 1; +} + static int l_load_image(lua_State *L) { return 0; } @@ -310,9 +316,9 @@ int main(int argc, char *argv[]) { TraceLogLevel debug_level = LOG_WARNING; const char *run_file = NULL; - const char short_options[] = "r:dbhv"; + const char short_options[] = "f:dbhv"; const struct option long_options[] = { - { "run", 1, NULL, 'r' }, + { "file", 1, NULL, 'r' }, { "debug", 0, NULL, 'd' }, { "bundle", 0, NULL, 'b' }, { "help", 0, NULL, 'h' }, @@ -323,7 +329,7 @@ int main(int argc, char *argv[]) { int opt; while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (opt) { - case 'r': + case 'f': run_file = optarg; break; case 'b': @@ -377,6 +383,7 @@ int main(int argc, char *argv[]) { 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); // Interpreting and running input file Lua script. diff --git a/tests/graphics.lua b/tests/graphics.lua index 0f337df..fc531a9 100644 --- a/tests/graphics.lua +++ b/tests/graphics.lua @@ -31,44 +31,52 @@ end function test_buttons() -- Testing button presses. - if button_pressed(button.PAD_UP) then + if button_down(button.PAD_UP) then draw_text("Pad Up", 10, 10, 20, color.VIOLET) end - if button_pressed(button.PAD_DOWN) then + if button_down(button.PAD_DOWN) then draw_text("Pad Down", 10, 40, 20, color.VIOLET) end - if button_pressed(button.PAD_LEFT) then + if button_down(button.PAD_LEFT) then draw_text("Pad Left", 10, 70, 20, color.VIOLET) end - if button_pressed(button.PAD_RIGHT) then + if button_down(button.PAD_RIGHT) then draw_text("Pad Right", 10, 100, 20, color.VIOLET) end - if button_pressed(button.A) then + if button_down(button.A) then draw_text("A", 150, 10, 20, color.VIOLET) end - if button_pressed(button.B) then + if button_down(button.B) then draw_text("B", 150, 40, 20, color.VIOLET) end - if button_pressed(button.X) then + if button_down(button.X) then draw_text("X", 150, 70, 20, color.VIOLET) end - if button_pressed(button.Y) then + if button_down(button.Y) then draw_text("Y", 150, 100, 20, color.VIOLET) end - -- Moving square left and right. - if button_pressed(button.PAD_LEFT) then + -- Moving square around. + if button_down(button.PAD_UP) then + test_button_square.y = test_button_square.y - (test_button_speed * get_dt()) + end + + if button_down(button.PAD_DOWN) then + test_button_square.y = test_button_square.y + (test_button_speed * get_dt()) + end + + if button_down(button.PAD_LEFT) then test_button_square.x = test_button_square.x - (test_button_speed * get_dt()) end - if button_pressed(button.PAD_RIGHT) then + if button_down(button.PAD_RIGHT) then test_button_square.x = test_button_square.x + (test_button_speed * get_dt()) end @@ -84,7 +92,7 @@ while window_running() do clear_window(color.BLACK) -- test_api() - -- test_buttons() + test_buttons() draw_info() end_drawing() -- cgit v1.2.3