Renamed -r to -f flag and added button_down function

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 04:01:54 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 04:01:54 +0200
Commit d2390b0ad887b463c5da4ac0620c026f8ae4c2e7 (patch)
-rw-r--r-- .vimrc 2
-rw-r--r-- main.c 15
-rw-r--r-- tests/graphics.lua 32
3 files changed, 32 insertions, 17 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 tests/graphics.lua'
5
let g:gdb_arguments = '-f 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/main.c b/main.c
...
271
	return 0;
271
	return 0;
272
}
272
}
273
  
273
  
274
static int l_button_pressed(lua_State *L) {
274
static int l_button_down(lua_State *L) {
275
	int button = luaL_checknumber(L, 1);
275
	int button = luaL_checknumber(L, 1);
276
	lua_pushboolean(L, IsKeyDown(button));
276
	lua_pushboolean(L, IsKeyDown(button));
  
277
	return 1;
  
278
}
  
279
  
  
280
static int l_button_pressed(lua_State *L) {
  
281
	int button = luaL_checknumber(L, 1);
  
282
	lua_pushboolean(L, IsKeyPressed(button));
277
	return 1;
283
	return 1;
278
}
284
}
279
  
285
  
...
310
	TraceLogLevel debug_level = LOG_WARNING;
316
	TraceLogLevel debug_level = LOG_WARNING;
311
	const char *run_file = NULL;
317
	const char *run_file = NULL;
312
  
318
  
313
	const char short_options[] = "r:dbhv";
319
	const char short_options[] = "f:dbhv";
314
	const struct option long_options[] = {
320
	const struct option long_options[] = {
315
		{ "run", 1, NULL, 'r' },
321
		{ "file", 1, NULL, 'r' },
316
		{ "debug", 0, NULL, 'd' },
322
		{ "debug", 0, NULL, 'd' },
317
		{ "bundle", 0, NULL, 'b' },
323
		{ "bundle", 0, NULL, 'b' },
318
		{ "help", 0, NULL, 'h' },
324
		{ "help", 0, NULL, 'h' },
...
323
	int opt;
329
	int opt;
324
	while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
330
	while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
325
		switch (opt) {
331
		switch (opt) {
326
			case 'r':
332
			case 'f':
327
				run_file = optarg;
333
				run_file = optarg;
328
				break;
334
				break;
329
			case 'b':
335
			case 'b':
...
377
		lua_register(L, "load_image", l_load_image);
383
		lua_register(L, "load_image", l_load_image);
378
		lua_register(L, "load_audio", l_load_audio);
384
		lua_register(L, "load_audio", l_load_audio);
379
		lua_register(L, "move_camera", l_move_camera);
385
		lua_register(L, "move_camera", l_move_camera);
  
386
		lua_register(L, "button_down", l_button_down);
380
		lua_register(L, "button_pressed", l_button_pressed);
387
		lua_register(L, "button_pressed", l_button_pressed);
381
  
388
  
382
		// Interpreting and running input file Lua script.
389
		// Interpreting and running input file Lua script.
...
diff --git a/tests/graphics.lua b/tests/graphics.lua
...
31
  
31
  
32
function test_buttons()
32
function test_buttons()
33
	-- Testing button presses.
33
	-- Testing button presses.
34
	if button_pressed(button.PAD_UP) then
34
	if button_down(button.PAD_UP) then
35
		draw_text("Pad Up", 10, 10, 20, color.VIOLET)
35
		draw_text("Pad Up", 10, 10, 20, color.VIOLET)
36
	end
36
	end
37
  
37
  
38
	if button_pressed(button.PAD_DOWN) then
38
	if button_down(button.PAD_DOWN) then
39
		draw_text("Pad Down", 10, 40, 20, color.VIOLET)
39
		draw_text("Pad Down", 10, 40, 20, color.VIOLET)
40
	end
40
	end
41
  
41
  
42
	if button_pressed(button.PAD_LEFT) then
42
	if button_down(button.PAD_LEFT) then
43
		draw_text("Pad Left", 10, 70, 20, color.VIOLET)
43
		draw_text("Pad Left", 10, 70, 20, color.VIOLET)
44
	end
44
	end
45
  
45
  
46
	if button_pressed(button.PAD_RIGHT) then
46
	if button_down(button.PAD_RIGHT) then
47
		draw_text("Pad Right", 10, 100, 20, color.VIOLET)
47
		draw_text("Pad Right", 10, 100, 20, color.VIOLET)
48
	end
48
	end
49
	
49
	
50
	if button_pressed(button.A) then
50
	if button_down(button.A) then
51
		draw_text("A", 150, 10, 20, color.VIOLET)
51
		draw_text("A", 150, 10, 20, color.VIOLET)
52
	end
52
	end
53
  
53
  
54
	if button_pressed(button.B) then
54
	if button_down(button.B) then
55
		draw_text("B", 150, 40, 20, color.VIOLET)
55
		draw_text("B", 150, 40, 20, color.VIOLET)
56
	end
56
	end
57
  
57
  
58
	if button_pressed(button.X) then
58
	if button_down(button.X) then
59
		draw_text("X", 150, 70, 20, color.VIOLET)
59
		draw_text("X", 150, 70, 20, color.VIOLET)
60
	end
60
	end
61
  
61
  
62
	if button_pressed(button.Y) then
62
	if button_down(button.Y) then
63
		draw_text("Y", 150, 100, 20, color.VIOLET)
63
		draw_text("Y", 150, 100, 20, color.VIOLET)
64
	end
64
	end
65
  
65
  
66
	-- Moving square left and right.
66
	-- Moving square around.
67
	if button_pressed(button.PAD_LEFT) then
67
	if button_down(button.PAD_UP) then
  
68
		test_button_square.y = test_button_square.y - (test_button_speed * get_dt())
  
69
	end
  
70
  
  
71
	if button_down(button.PAD_DOWN) then
  
72
		test_button_square.y = test_button_square.y + (test_button_speed * get_dt())
  
73
	end
  
74
  
  
75
	if button_down(button.PAD_LEFT) then
68
		test_button_square.x = test_button_square.x - (test_button_speed * get_dt())
76
		test_button_square.x = test_button_square.x - (test_button_speed * get_dt())
69
	end
77
	end
70
  
78
  
71
	if button_pressed(button.PAD_RIGHT) then
79
	if button_down(button.PAD_RIGHT) then
72
		test_button_square.x = test_button_square.x + (test_button_speed * get_dt())
80
		test_button_square.x = test_button_square.x + (test_button_speed * get_dt())
73
	end
81
	end
74
  
82
  
...
84
	clear_window(color.BLACK)
92
	clear_window(color.BLACK)
85
  
93
  
86
	-- test_api()
94
	-- test_api()
87
	-- test_buttons()
95
	test_buttons()
88
  
96
  
89
	draw_info()
97
	draw_info()
90
	end_drawing()
98
	end_drawing()
...