Added custom font and refactor some of the code

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-06 18:52:27 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-06 18:52:27 +0200
Commit a29577e869ed6fc25ab4ed3c5014f5811cb6215f (patch)
-rw-r--r-- fonts/dejavusans_mono.ttf bin 332.7 KB -> 0 B
-rw-r--r-- main.c 36
-rw-r--r-- test/graphics.lua 11
-rw-r--r-- test/json.lua 10
-rw-r--r-- test/main.lua 29
-rw-r--r-- test/test.txt 1
6 files changed, 57 insertions, 30 deletions
diff --git a/fonts/dejavusans_mono.ttf b/fonts/dejavusans_mono.ttf
diff --git a/main.c b/main.c
...
12
#include "stdlib/color.h"
12
#include "stdlib/color.h"
13
#include "stdlib/tilemap.h"
13
#include "stdlib/tilemap.h"
14
  
14
  
  
15
#include "fonts/dejavusans_mono_bold.h"
  
16
  
15
#define VERSION "x.x"
17
#define VERSION "x.x"
16
#define DEBUG_LEVEL LOG_DEBUG
18
#define DEBUG_LEVEL LOG_DEBUG
  
19
#define FONT_IMPORT_SIZE 30
  
20
  
  
21
typedef struct {
  
22
	Font font;
  
23
	int font_size;
  
24
} Context;
  
25
  
  
26
// Setting up global context.
  
27
Context ctx = {0};
17
  
28
  
18
static int lua_getfield_int(lua_State *L, int index, const char *key) {
29
static int lua_getfield_int(lua_State *L, int index, const char *key) {
19
	lua_getfield(L, index, key);
30
	lua_getfield(L, index, key);
...
45
	SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
56
	SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
46
	InitWindow(width, height, title);
57
	InitWindow(width, height, title);
47
	TraceLog(LOG_DEBUG, "l_open_window");
58
	TraceLog(LOG_DEBUG, "l_open_window");
  
59
  
  
60
	ctx.font_size = FONT_IMPORT_SIZE;
  
61
	ctx.font = LoadFontFromMemory(".ttf", dejavusans_mono_bold, dejavusans_mono_bold_len, ctx.font_size, NULL, 0);
  
62
	SetTextureFilter(ctx.font.texture, TEXTURE_FILTER_TRILINEAR);
  
63
  
  
64
	if (!IsFontValid(ctx.font)) {
  
65
		printf("font not valid\n");
  
66
	}
  
67
  
48
	return 0;
68
	return 0;
49
}
69
}
50
  
70
  
...
94
	return 0;
114
	return 0;
95
}
115
}
96
  
116
  
  
117
static int l_draw_info(lua_State *L) {
  
118
	float delta = GetFrameTime();
  
119
	int fps = GetFPS();
  
120
	double runtime = GetTime();
  
121
	int height = GetScreenHeight();
  
122
  
  
123
	DrawTextEx(ctx.font, TextFormat("dt: %f", delta), (Vector2){ 20, height - 80 }, 20, 0, RAYWHITE);
  
124
	DrawTextEx(ctx.font, TextFormat("run: %f", runtime), (Vector2){ 20, height - 60 }, 20, 0, RAYWHITE);
  
125
	DrawTextEx(ctx.font, TextFormat("fps: %d", fps), (Vector2){ 20, height - 40 }, 20, 0, RAYWHITE);
  
126
  
  
127
	return 0;
  
128
}
  
129
  
97
static void help(const char *argv0) {
130
static void help(const char *argv0) {
98
	printf("Usage: %s [options]\n"
131
	printf("Usage: %s [options]\n"
99
			"\nAvailable options:\n"
132
			"\nAvailable options:\n"
...
150
	if (run_file) {
183
	if (run_file) {
151
		SetTraceLogLevel(debug_level);
184
		SetTraceLogLevel(debug_level);
152
  
185
  
  
186
  
153
		lua_State *L = luaL_newstate();
187
		lua_State *L = luaL_newstate();
154
		luaL_openlibs(L);
188
		luaL_openlibs(L);
155
  
189
  
...
161
		lua_register(L, "end_drawing", l_end_drawing);
195
		lua_register(L, "end_drawing", l_end_drawing);
162
		lua_register(L, "set_fps", l_set_fps);
196
		lua_register(L, "set_fps", l_set_fps);
163
		lua_register(L, "draw_fps_meter", l_draw_fps_meter);
197
		lua_register(L, "draw_fps_meter", l_draw_fps_meter);
  
198
		lua_register(L, "draw_info", l_draw_info);
164
		lua_register(L, "clear_window", l_clear_window);
199
		lua_register(L, "clear_window", l_clear_window);
165
  
200
  
166
		// Loading embeded modules into Lua state.
201
		// Loading embeded modules into Lua state.
...
174
			return 1;
209
			return 1;
175
		}
210
		}
176
  
211
  
  
212
		UnloadFont(ctx.font);                                                           // Unload font from GPU memory (VRAM)
177
		lua_close(L);
213
		lua_close(L);
178
	}
214
	}
179
  
215
  
...
diff --git a/test/graphics.lua b/test/graphics.lua
  
1
open_window(800, 800, "My Game")
  
2
set_fps(60)
  
3
  
  
4
while window_running() do
  
5
	begin_drawing()
  
6
	clear_window(color.BLACK)
  
7
	draw_info()
  
8
	end_drawing()
  
9
end
  
10
  
  
11
close_window()
diff --git a/test/json.lua b/test/json.lua
  
1
local file = io.open("test/test.json", "r")
  
2
local content = file:read("*a")
  
3
file:close()
  
4
  
  
5
local data = json.decode(content)
  
6
  
  
7
print("name: " .. data.name)
  
8
for _, n in pairs(data.numbers) do
  
9
	print(" - number: " .. n)
  
10
end
diff --git a/test/main.lua b/test/main.lua
1
function test_json()
  
2
	local file = io.open("test/test.json", "r")
  
3
	local content = file:read("*a")
  
4
	file:close()
  
5
  
  
6
	local data = json.decode(content)
  
7
  
  
8
	print("name: " .. data.name)
  
9
	for _, n in pairs(data.numbers) do
  
10
		print(" - number: " .. n)
  
11
	end
  
12
end
  
13
  
  
14
function test_graphics()
  
15
	open_window(600, 600, "My Game")
  
16
	set_fps(60)
  
17
  
  
18
	while window_running() do
  
19
		begin_drawing()
  
20
		clear_window(color.BLACK)
  
21
		draw_fps_meter()
  
22
		end_drawing()
  
23
	end
  
24
  
  
25
	close_window()
  
26
end
  
27
  
  
28
test_json()
  
29
test_graphics()
  
diff --git a/test/test.txt b/test/test.txt
1
This is the contents of test.txt file.