From b6197cc1c5ca7632a604f354f8a2d7274b25f308 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 9 Aug 2025 02:56:49 +0200 Subject: Fixed button definitions and refactor --- .vimrc | 2 +- examples/graphics.lua | 62 ------------------------------- examples/icons/icon_1.png | Bin 352069 -> 0 bytes examples/icons/icon_2.png | Bin 455412 -> 0 bytes examples/json.lua | 10 ----- examples/test.json | 4 -- tests/graphics.lua | 93 ++++++++++++++++++++++++++++++++++++++++++++++ tests/icons/icon_1.png | Bin 0 -> 352069 bytes tests/icons/icon_2.png | Bin 0 -> 455412 bytes tests/json.lua | 10 +++++ tests/test.json | 4 ++ 11 files changed, 108 insertions(+), 77 deletions(-) delete mode 100644 examples/graphics.lua delete mode 100644 examples/icons/icon_1.png delete mode 100644 examples/icons/icon_2.png delete mode 100644 examples/json.lua delete mode 100644 examples/test.json create mode 100644 tests/graphics.lua create mode 100644 tests/icons/icon_1.png create mode 100644 tests/icons/icon_2.png create mode 100644 tests/json.lua create mode 100644 tests/test.json diff --git a/.vimrc b/.vimrc index 46cc038..7bd9554 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 examples/graphics.lua' +let g:gdb_arguments = '-r tests/graphics.lua' nnoremap m :call LocalMake() nnoremap r :execute '!./' . g:gdb_executable . ' ' . g:gdb_arguments diff --git a/examples/graphics.lua b/examples/graphics.lua deleted file mode 100644 index 6c1f8e4..0000000 --- a/examples/graphics.lua +++ /dev/null @@ -1,62 +0,0 @@ -asset1 = load_image("examples/icons/armor_3.png") - -open_window(800, 800, "Sample Window") -set_fps(60) - -function test_api() - draw_rect(100, 100, 300, 200, color.YELLOW) - draw_text("Label text", 10, 10, 20, color.VIOLET) - draw_line(400, 10, 500, 100, color.RED) - draw_circle(500, 500, 100, color.BLUE) - draw_ellipse(200, 500, 100, 50, color.BLUE) - draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE) - draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET) - draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET) -end - -function test_buttons() - if button_pressed(button.PAD_UP) then - draw_text("Pad Up", 10, 10, 20, color.VIOLET) - end - - if button_pressed(button.PAD_DOWN) then - draw_text("Pad Down", 10, 40, 20, color.VIOLET) - end - - if button_pressed(button.PAD_LEFT) then - draw_text("Pad Left", 10, 70, 20, color.VIOLET) - end - - if button_pressed(button.PAD_RIGHT) then - draw_text("Pad Right", 10, 100, 20, color.VIOLET) - end - - if button_pressed(button.A) then - draw_text("A", 150, 10, 20, color.VIOLET) - end - - if button_pressed(button.B) then - draw_text("B", 150, 40, 20, color.VIOLET) - end - - if button_pressed(button.X) then - draw_text("X", 150, 70, 20, color.VIOLET) - end - - if button_pressed(button.Y) then - draw_text("Y", 150, 100, 20, color.VIOLET) - end -end - -while window_running() do - begin_drawing() - clear_window(color.BLACK) - - -- test_api() - test_buttons() - - draw_info() - end_drawing() -end - -close_window() diff --git a/examples/icons/icon_1.png b/examples/icons/icon_1.png deleted file mode 100644 index 4785224..0000000 Binary files a/examples/icons/icon_1.png and /dev/null differ diff --git a/examples/icons/icon_2.png b/examples/icons/icon_2.png deleted file mode 100644 index c19d642..0000000 Binary files a/examples/icons/icon_2.png and /dev/null differ diff --git a/examples/json.lua b/examples/json.lua deleted file mode 100644 index 17a1930..0000000 --- a/examples/json.lua +++ /dev/null @@ -1,10 +0,0 @@ -local file = io.open("examples/test.json", "r") -local content = file:read("*a") -file:close() - -local data = json.decode(content) - -print("name: " .. data.name) -for _, n in pairs(data.numbers) do - print(" - number: " .. n) -end diff --git a/examples/test.json b/examples/test.json deleted file mode 100644 index 4bd3e38..0000000 --- a/examples/test.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "Bob", - "numbers": [1, 3, 5, 2] -} diff --git a/tests/graphics.lua b/tests/graphics.lua new file mode 100644 index 0000000..0f337df --- /dev/null +++ b/tests/graphics.lua @@ -0,0 +1,93 @@ +math.randomseed(os.time()) + +test_button_square = { x = 400, y = 400 } +test_button_color = color.RED +test_button_speed = 200 + +test_images_asset1 = load_image("tests/icons/icon_1.png") + +open_window(800, 800, "Sample Window") +set_fps(60) + +function test_api() + draw_rect(100, 100, 300, 200, color.YELLOW) + draw_text("Label text", 10, 10, 20, color.VIOLET) + draw_line(400, 10, 500, 100, color.RED) + draw_circle(500, 500, 100, color.BLUE) + draw_ellipse(200, 500, 100, 50, color.BLUE) + draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE) + draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET) + draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET) +end + +function get_random_color() + local keys = {} + for k in pairs(color) do + table.insert(keys, k) -- Collect all keys + end + local randomKey = keys[math.random(1, #keys)] -- Select a random key + return color[randomKey] -- Return the corresponding color +end + +function test_buttons() + -- Testing button presses. + if button_pressed(button.PAD_UP) then + draw_text("Pad Up", 10, 10, 20, color.VIOLET) + end + + if button_pressed(button.PAD_DOWN) then + draw_text("Pad Down", 10, 40, 20, color.VIOLET) + end + + if button_pressed(button.PAD_LEFT) then + draw_text("Pad Left", 10, 70, 20, color.VIOLET) + end + + if button_pressed(button.PAD_RIGHT) then + draw_text("Pad Right", 10, 100, 20, color.VIOLET) + end + + if button_pressed(button.A) then + draw_text("A", 150, 10, 20, color.VIOLET) + end + + if button_pressed(button.B) then + draw_text("B", 150, 40, 20, color.VIOLET) + end + + if button_pressed(button.X) then + draw_text("X", 150, 70, 20, color.VIOLET) + end + + if button_pressed(button.Y) then + draw_text("Y", 150, 100, 20, color.VIOLET) + end + + -- Moving square left and right. + if button_pressed(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 + test_button_square.x = test_button_square.x + (test_button_speed * get_dt()) + end + + if button_pressed(button.A) then + test_button_color = get_random_color() + end + + draw_rect(test_button_square.x, test_button_square.y, 50, 50, test_button_color) +end + +while window_running() do + begin_drawing() + clear_window(color.BLACK) + + -- test_api() + -- test_buttons() + + draw_info() + end_drawing() +end + +close_window() diff --git a/tests/icons/icon_1.png b/tests/icons/icon_1.png new file mode 100644 index 0000000..4785224 Binary files /dev/null and b/tests/icons/icon_1.png differ diff --git a/tests/icons/icon_2.png b/tests/icons/icon_2.png new file mode 100644 index 0000000..c19d642 Binary files /dev/null and b/tests/icons/icon_2.png differ diff --git a/tests/json.lua b/tests/json.lua new file mode 100644 index 0000000..a659889 --- /dev/null +++ b/tests/json.lua @@ -0,0 +1,10 @@ +local file = io.open("tests/test.json", "r") +local content = file:read("*a") +file:close() + +local data = json.decode(content) + +print("name: " .. data.name) +for _, n in pairs(data.numbers) do + print(" - number: " .. n) +end diff --git a/tests/test.json b/tests/test.json new file mode 100644 index 0000000..4bd3e38 --- /dev/null +++ b/tests/test.json @@ -0,0 +1,4 @@ +{ + "name": "Bob", + "numbers": [1, 3, 5, 2] +} -- cgit v1.2.3