Fixed button definitions and refactor

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 02:56:49 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 02:56:49 +0200
Commit b6197cc1c5ca7632a604f354f8a2d7274b25f308 (patch)
-rw-r--r-- .vimrc 2
-rw-r--r-- examples/graphics.lua 62
-rw-r--r-- tests/icons/icon_1.png bin 343.8 KB -> 343.8 KB
-rw-r--r-- tests/icons/icon_2.png bin 444.7 KB -> 444.7 KB
-rw-r--r-- tests/json.lua 0
-rw-r--r-- tests/test.json 0
-rw-r--r-- tests/graphics.lua 93
7 files changed, 95 insertions, 64 deletions
diff --git a/.vimrc b/.vimrc
...
2
set errorformat=%f:%l:%c:\ %m
2
set errorformat=%f:%l:%c:\ %m
3
  
3
  
4
let g:gdb_executable = 'bidi'
4
let g:gdb_executable = 'bidi'
5
let g:gdb_arguments = '-r examples/graphics.lua'
5
let g:gdb_arguments = '-r tests/graphics.lua'
6
  
6
  
7
nnoremap <leader>m :call LocalMake()<CR>
7
nnoremap <leader>m :call LocalMake()<CR>
8
nnoremap <leader>r :execute '!./' . g:gdb_executable . ' ' . g:gdb_arguments<CR>
8
nnoremap <leader>r :execute '!./' . g:gdb_executable . ' ' . g:gdb_arguments<CR>
...
diff --git a/examples/graphics.lua b/examples/graphics.lua
1
asset1 = load_image("examples/icons/armor_3.png")
  
2
  
  
3
open_window(800, 800, "Sample Window")
  
4
set_fps(60)
  
5
  
  
6
function test_api()
  
7
	draw_rect(100, 100, 300, 200, color.YELLOW)
  
8
	draw_text("Label text", 10, 10, 20, color.VIOLET)
  
9
	draw_line(400, 10, 500, 100, color.RED)
  
10
	draw_circle(500, 500, 100, color.BLUE)
  
11
	draw_ellipse(200, 500, 100, 50, color.BLUE)
  
12
	draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE)
  
13
	draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET)
  
14
	draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET)
  
15
end
  
16
  
  
17
function test_buttons()
  
18
	if button_pressed(button.PAD_UP) then
  
19
		draw_text("Pad Up", 10, 10, 20, color.VIOLET)
  
20
	end
  
21
  
  
22
	if button_pressed(button.PAD_DOWN) then
  
23
		draw_text("Pad Down", 10, 40, 20, color.VIOLET)
  
24
	end
  
25
  
  
26
	if button_pressed(button.PAD_LEFT) then
  
27
		draw_text("Pad Left", 10, 70, 20, color.VIOLET)
  
28
	end
  
29
  
  
30
	if button_pressed(button.PAD_RIGHT) then
  
31
		draw_text("Pad Right", 10, 100, 20, color.VIOLET)
  
32
	end
  
33
	
  
34
	if button_pressed(button.A) then
  
35
		draw_text("A", 150, 10, 20, color.VIOLET)
  
36
	end
  
37
  
  
38
	if button_pressed(button.B) then
  
39
		draw_text("B", 150, 40, 20, color.VIOLET)
  
40
	end
  
41
  
  
42
	if button_pressed(button.X) then
  
43
		draw_text("X", 150, 70, 20, color.VIOLET)
  
44
	end
  
45
  
  
46
	if button_pressed(button.Y) then
  
47
		draw_text("Y", 150, 100, 20, color.VIOLET)
  
48
	end
  
49
end
  
50
  
  
51
while window_running() do
  
52
	begin_drawing()
  
53
	clear_window(color.BLACK)
  
54
  
  
55
	-- test_api()
  
56
	test_buttons()
  
57
  
  
58
	draw_info()
  
59
	end_drawing()
  
60
end
  
61
  
  
62
close_window()
  
diff --git a/tests/icons/icon_1.png b/tests/icons/icon_1.png
diff --git a/tests/icons/icon_2.png b/tests/icons/icon_2.png
diff --git a/tests/json.lua b/tests/json.lua
1
local file = io.open("examples/test.json", "r")
1
local file = io.open("tests/test.json", "r")
2
local content = file:read("*a")
2
local content = file:read("*a")
3
file:close()
3
file:close()
4
  
4
  
...
diff --git a/tests/test.json b/tests/test.json
...
diff --git a/tests/graphics.lua b/tests/graphics.lua
  
1
math.randomseed(os.time())
  
2
  
  
3
test_button_square = { x = 400, y = 400 }
  
4
test_button_color = color.RED
  
5
test_button_speed = 200
  
6
  
  
7
test_images_asset1 = load_image("tests/icons/icon_1.png")
  
8
  
  
9
open_window(800, 800, "Sample Window")
  
10
set_fps(60)
  
11
  
  
12
function test_api()
  
13
	draw_rect(100, 100, 300, 200, color.YELLOW)
  
14
	draw_text("Label text", 10, 10, 20, color.VIOLET)
  
15
	draw_line(400, 10, 500, 100, color.RED)
  
16
	draw_circle(500, 500, 100, color.BLUE)
  
17
	draw_ellipse(200, 500, 100, 50, color.BLUE)
  
18
	draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE)
  
19
	draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET)
  
20
	draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET)
  
21
end
  
22
  
  
23
function get_random_color()
  
24
    local keys = {}
  
25
    for k in pairs(color) do
  
26
        table.insert(keys, k)  -- Collect all keys
  
27
    end
  
28
    local randomKey = keys[math.random(1, #keys)]  -- Select a random key
  
29
    return color[randomKey]  -- Return the corresponding color
  
30
end
  
31
  
  
32
function test_buttons()
  
33
	-- Testing button presses.
  
34
	if button_pressed(button.PAD_UP) then
  
35
		draw_text("Pad Up", 10, 10, 20, color.VIOLET)
  
36
	end
  
37
  
  
38
	if button_pressed(button.PAD_DOWN) then
  
39
		draw_text("Pad Down", 10, 40, 20, color.VIOLET)
  
40
	end
  
41
  
  
42
	if button_pressed(button.PAD_LEFT) then
  
43
		draw_text("Pad Left", 10, 70, 20, color.VIOLET)
  
44
	end
  
45
  
  
46
	if button_pressed(button.PAD_RIGHT) then
  
47
		draw_text("Pad Right", 10, 100, 20, color.VIOLET)
  
48
	end
  
49
	
  
50
	if button_pressed(button.A) then
  
51
		draw_text("A", 150, 10, 20, color.VIOLET)
  
52
	end
  
53
  
  
54
	if button_pressed(button.B) then
  
55
		draw_text("B", 150, 40, 20, color.VIOLET)
  
56
	end
  
57
  
  
58
	if button_pressed(button.X) then
  
59
		draw_text("X", 150, 70, 20, color.VIOLET)
  
60
	end
  
61
  
  
62
	if button_pressed(button.Y) then
  
63
		draw_text("Y", 150, 100, 20, color.VIOLET)
  
64
	end
  
65
  
  
66
	-- Moving square left and right.
  
67
	if button_pressed(button.PAD_LEFT) then
  
68
		test_button_square.x = test_button_square.x - (test_button_speed * get_dt())
  
69
	end
  
70
  
  
71
	if button_pressed(button.PAD_RIGHT) then
  
72
		test_button_square.x = test_button_square.x + (test_button_speed * get_dt())
  
73
	end
  
74
  
  
75
	if button_pressed(button.A) then
  
76
		test_button_color = get_random_color()
  
77
	end
  
78
  
  
79
	draw_rect(test_button_square.x, test_button_square.y, 50, 50, test_button_color)
  
80
end
  
81
  
  
82
while window_running() do
  
83
	begin_drawing()
  
84
	clear_window(color.BLACK)
  
85
  
  
86
	-- test_api()
  
87
	-- test_buttons()
  
88
  
  
89
	draw_info()
  
90
	end_drawing()
  
91
end
  
92
  
  
93
close_window()