Added button pressed

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 02:12:57 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-09 02:12:57 +0200
Commit 3ba9a8833623da130102ca6b56e25c2cad3fc739 (patch)
-rw-r--r-- README.md 12
-rw-r--r-- examples/graphics.lua 58
-rw-r--r-- examples/icons/icon_1.png bin 0 B -> 343.8 KB
-rw-r--r-- examples/icons/icon_2.png bin 0 B -> 444.7 KB
-rw-r--r-- main.c 57
-rw-r--r-- stdlib/button.lua 13
6 files changed, 125 insertions, 15 deletions
diff --git a/README.md b/README.md
...
15
> download and replace them from official repositories if desired; they have
15
> download and replace them from official repositories if desired; they have
16
> not been tampered with.
16
> not been tampered with.
17
  
17
  
  
18
## Controller mappings
  
19
  
  
20
```
  
21
  Q W E      I O
  
22
  A S D      J k
  
23
```
  
24
  
  
25
## Tools
  
26
  
  
27
- https://hardwaretester.com/gamepad
  
28
  
18
## Libraries
29
## Libraries
19
  
30
  
20
- https://github.com/rxi/microtar
31
- https://github.com/rxi/microtar
...
27
- https://www.love2d.org/wiki/Main_Page
38
- https://www.love2d.org/wiki/Main_Page
28
- https://www.lexaloffle.com/dl/docs/pico-8_manual.html
39
- https://www.lexaloffle.com/dl/docs/pico-8_manual.html
29
- https://home.cs.colorado.edu/~main/bgi/doc/
40
- https://home.cs.colorado.edu/~main/bgi/doc/
  
41
- https://opengameart.org/content/free-rpg-skill-icons-for-crafter-blacksmith-and-gnome
diff --git a/examples/graphics.lua b/examples/graphics.lua
  
1
asset1 = load_image("examples/icons/armor_3.png")
  
2
  
1
open_window(800, 800, "Sample Window")
3
open_window(800, 800, "Sample Window")
2
set_fps(60)
4
set_fps(60)
3
  
5
  
4
while window_running() do
6
function test_api()
5
	begin_drawing()
  
6
	clear_window(color.BLACK)
  
7
  
  
8
	draw_rect(100, 100, 300, 200, color.YELLOW)
7
	draw_rect(100, 100, 300, 200, color.YELLOW)
9
	draw_text("YOOOOO", 10, 10, 20, color.VIOLET)
8
	draw_text("Label text", 10, 10, 20, color.VIOLET)
10
  
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)
11
	draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET)
13
	draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET)
12
	draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET)
14
	draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET)
  
15
end
13
  
16
  
14
	draw_line(400, 10, 500, 100, color.RED)
17
function test_buttons()
15
	draw_circle(500, 500, 100, color.BLUE)
18
	if button_pressed(button.PAD_UP) then
16
	draw_ellipse(200, 500, 100, 50, color.BLUE)
19
		draw_text("Pad Up", 10, 10, 20, color.VIOLET)
  
20
	end
17
  
21
  
18
	draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE)
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()
19
  
57
  
20
	draw_info()
58
	draw_info()
21
	end_drawing()
59
	end_drawing()
...
diff --git a/examples/icons/icon_1.png b/examples/icons/icon_1.png
diff --git a/examples/icons/icon_2.png b/examples/icons/icon_2.png
diff --git a/main.c b/main.c
...
2
#include <stdarg.h>
2
#include <stdarg.h>
3
#include <getopt.h>
3
#include <getopt.h>
4
#include <stdlib.h>
4
#include <stdlib.h>
  
5
#include <time.h>
5
  
6
  
6
#include "raylib.h"
7
#include "raylib.h"
7
#include "lua.h"
8
#include "lua.h"
...
10
  
11
  
11
#include "stdlib/json.h"
12
#include "stdlib/json.h"
12
#include "stdlib/color.h"
13
#include "stdlib/color.h"
  
14
#include "stdlib/button.h"
13
#include "stdlib/tilemap.h"
15
#include "stdlib/tilemap.h"
14
  
16
  
15
#include "fonts/dejavusans_mono_bold.h"
17
#include "fonts/dejavusans_mono_bold.h"
...
17
#define VERSION "x.x"
19
#define VERSION "x.x"
18
#define DEBUG_LEVEL LOG_DEBUG
20
#define DEBUG_LEVEL LOG_DEBUG
19
#define FONT_IMPORT_SIZE 30
21
#define FONT_IMPORT_SIZE 30
  
22
#define UID_LENGTH 36
  
23
  
  
24
typedef struct {
  
25
	char uid[UID_LENGTH + 1];
  
26
	const char* filepath;
  
27
} ExternalImage;
  
28
  
  
29
typedef struct {
  
30
} ExternalAudio;
20
  
31
  
21
typedef struct {
32
typedef struct {
22
	Font font;
33
	Font font;
23
	int font_size;
34
	int font_size;
  
35
	Camera2D camera;
  
36
	ExternalImage *images;
  
37
	ExternalAudio *audio;
24
} Context;
38
} Context;
25
  
39
  
26
// Setting up global context.
40
// Setting up global context.
27
Context ctx = {0};
41
Context ctx = {0};
  
42
  
  
43
static void generate_uid(char *uid) {
  
44
	const char *chars = "0123456789abcdef";
  
45
	for (size_t i = 0; i < UID_LENGTH; i++) {
  
46
		uid[i] = chars[rand() % 16];
  
47
	}
  
48
	uid[UID_LENGTH + 1] = '\0';
  
49
}
28
  
50
  
29
static int lua_getfield_int(lua_State *L, int index, const char *key) {
51
static int lua_getfield_int(lua_State *L, int index, const char *key) {
30
	lua_getfield(L, index, key);
52
	lua_getfield(L, index, key);
...
249
	return 0;
271
	return 0;
250
}
272
}
251
  
273
  
  
274
static int l_button_pressed(lua_State *L) {
  
275
	int button = luaL_checknumber(L, 1);
  
276
	lua_pushboolean(L, IsKeyDown(button));
  
277
	return 1;
  
278
}
  
279
  
  
280
static int l_load_image(lua_State *L) {
  
281
	return 0;
  
282
}
  
283
  
  
284
static int l_load_audio(lua_State *L) {
  
285
	return 0;
  
286
}
  
287
  
  
288
static int l_move_camera(lua_State *L) {
  
289
	return 0;
  
290
}
  
291
  
252
static void help(const char *argv0) {
292
static void help(const char *argv0) {
253
	printf("Usage: %s [options]\n"
293
	printf("Usage: %s [options]\n"
254
			"\nAvailable options:\n"
294
			"\nAvailable options:\n"
...
265
}
305
}
266
  
306
  
267
int main(int argc, char *argv[]) {
307
int main(int argc, char *argv[]) {
  
308
	srand(time(NULL));
  
309
  
268
	TraceLogLevel debug_level = LOG_WARNING;
310
	TraceLogLevel debug_level = LOG_WARNING;
269
	const char *run_file = NULL;
311
	const char *run_file = NULL;
270
  
312
  
...
308
		lua_State *L = luaL_newstate();
350
		lua_State *L = luaL_newstate();
309
		luaL_openlibs(L);
351
		luaL_openlibs(L);
310
  
352
  
  
353
		// Loading embeded modules into Lua state.
  
354
		if (!load_embedded_module(L, json, json_len, "json")) return 1;
  
355
		if (!load_embedded_module(L, color, color_len, "color")) return 1;
  
356
		if (!load_embedded_module(L, button, button_len, "button")) return 1;
  
357
		if (!load_embedded_module(L, tilemap, tilemap_len, "tilemap")) return 1;
  
358
  
311
		// Registring Raylib mappings.
359
		// Registring Raylib mappings.
312
		lua_register(L, "open_window", l_open_window);
360
		lua_register(L, "open_window", l_open_window);
313
		lua_register(L, "close_window", l_close_window);
361
		lua_register(L, "close_window", l_close_window);
...
326
		lua_register(L, "draw_circle", l_draw_circle);
374
		lua_register(L, "draw_circle", l_draw_circle);
327
		lua_register(L, "draw_ellipse", l_draw_ellipse);
375
		lua_register(L, "draw_ellipse", l_draw_ellipse);
328
		lua_register(L, "draw_triangle", l_draw_triangle);
376
		lua_register(L, "draw_triangle", l_draw_triangle);
329
  
377
		lua_register(L, "load_image", l_load_image);
330
		// Loading embeded modules into Lua state.
378
		lua_register(L, "load_audio", l_load_audio);
331
		if (!load_embedded_module(L, json, json_len, "json")) return 1;
379
		lua_register(L, "move_camera", l_move_camera);
332
		if (!load_embedded_module(L, color, color_len, "color")) return 1;
380
		lua_register(L, "button_pressed", l_button_pressed);
333
		if (!load_embedded_module(L, tilemap, tilemap_len, "tilemap")) return 1;
  
334
  
381
  
335
		// Interpreting and running input file Lua script.
382
		// Interpreting and running input file Lua script.
336
		if (luaL_loadfile(L, run_file) || lua_pcall(L, 0, 0, 0)) {
383
		if (luaL_loadfile(L, run_file) || lua_pcall(L, 0, 0, 0)) {
...
diff --git a/stdlib/button.lua b/stdlib/button.lua
  
1
local button = {}
  
2
  
  
3
button.PAD_UP    = 87  -- Key: W
  
4
button.PAD_DOWN  = 83  -- Key: S
  
5
button.PAD_LEFT  = 65  -- Key: A
  
6
button.PAD_RIGHT = 68  -- Key: D
  
7
  
  
8
button.A         = 76  -- Key: L
  
9
button.B         = 80  -- Key: P
  
10
button.X         = 75  -- Key: K
  
11
button.Y         = 79  -- Key: O
  
12
  
  
13
return button