Added JSON example via Lua script

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-06 11:47:03 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-06 11:47:03 +0200
Commit a007b4dd156ae1eae302cd43e22186caf854069b (patch)
-rw-r--r-- main.c 14
-rw-r--r-- test/main.lua 38
-rw-r--r-- test/test.json 4
3 files changed, 38 insertions, 18 deletions
diff --git a/main.c b/main.c
...
6
#include "lauxlib.h"
6
#include "lauxlib.h"
7
  
7
  
8
#include "stdlib/color.h"
8
#include "stdlib/color.h"
  
9
#include "stdlib/json.h"
9
  
10
  
10
#define IN_FILE "test/main.lua"
11
#define IN_FILE "test/main.lua"
11
#define DEBUG_LEVEL LOG_DEBUG
12
#define DEBUG_LEVEL LOG_DEBUG
...
86
	lua_State *L = luaL_newstate();
87
	lua_State *L = luaL_newstate();
87
	luaL_openlibs(L);
88
	luaL_openlibs(L);
88
  
89
  
  
90
	// Registring Raylib mappings.
89
	lua_register(L, "open_window", l_open_window);
91
	lua_register(L, "open_window", l_open_window);
90
	lua_register(L, "close_window", l_close_window);
92
	lua_register(L, "close_window", l_close_window);
91
	lua_register(L, "window_running", l_window_running);
93
	lua_register(L, "window_running", l_window_running);
...
95
	lua_register(L, "draw_fps", l_draw_fps);
97
	lua_register(L, "draw_fps", l_draw_fps);
96
	lua_register(L, "clear_window", l_clear_window);
98
	lua_register(L, "clear_window", l_clear_window);
97
  
99
  
  
100
	// Embedding color module.
98
	if (luaL_loadbuffer(L, color, color_len, "color") || lua_pcall(L, 0, 1, 0)) {
101
	if (luaL_loadbuffer(L, color, color_len, "color") || lua_pcall(L, 0, 1, 0)) {
99
		fprintf(stderr, "Error loading color.lua: %s\n", lua_tostring(L, -1));
102
		fprintf(stderr, "Error loading color.lua: %s\n", lua_tostring(L, -1));
100
		lua_close(L);
103
		lua_close(L);
...
102
	}
105
	}
103
	lua_setglobal(L, "color");
106
	lua_setglobal(L, "color");
104
  
107
  
105
	// TODO: This should probably use loadbuffer instead.
108
	// Embedding JSON module.
106
	// https://www.lua.org/manual/5.4/manual.html#luaL_loadbuffer
109
	if (luaL_loadbuffer(L, json, json_len, "json") || lua_pcall(L, 0, 1, 0)) {
  
110
		fprintf(stderr, "Error loading json.lua: %s\n", lua_tostring(L, -1));
  
111
		lua_close(L);
  
112
		return 1;
  
113
	}
  
114
	lua_setglobal(L, "json");
  
115
  
  
116
	// Interpreting and running input file Lua script.
107
	if (luaL_loadfile(L, IN_FILE) || lua_pcall(L, 0, 0, 0)) {
117
	if (luaL_loadfile(L, IN_FILE) || lua_pcall(L, 0, 0, 0)) {
108
		fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
118
		fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
109
		return 1;
119
		return 1;
...
diff --git a/test/main.lua b/test/main.lua
1
for i = 0, 3 do
1
function test_json()
2
	print(string.format("Hi %d times", i))
2
	local file = io.open("test/test.json", "r")
3
end
3
	local content = file:read("*a")
  
4
	file:close()
4
  
5
  
5
local file = io.open("test/test.txt", "r")
6
	local data = json.decode(content)
6
local content = file:read("*a")
  
7
print(content)
  
8
file:close()
  
9
  
7
  
10
open_window(600, 600, "My Game")
8
	print("name: " .. data.name)
11
set_fps(60)
9
	for _, n in pairs(data.numbers) do
  
10
		print(" - number: " .. n)
  
11
	end
  
12
end
12
  
13
  
13
BLACK = { r = 0, g = 0, b = 0 }
14
function test_graphics()
  
15
	open_window(600, 600, "My Game")
  
16
	set_fps(60)
14
  
17
  
15
while window_running() do
18
	while window_running() do
16
	begin_drawing()
19
		begin_drawing()
17
	clear_window(color.VIOLET)
20
		clear_window(color.RAYWHITE)
  
21
		draw_fps()
  
22
		end_drawing()
  
23
	end
18
  
24
  
19
	draw_fps()
25
	close_window()
20
	end_drawing()
  
21
end
26
end
22
  
27
  
23
close_window()
28
test_json()
  
29
test_graphics()
diff --git a/test/test.json b/test/test.json
  
1
{
  
2
	"name": "Bob",
  
3
	"numbers": [1, 3, 5, 2]
  
4
}