diff --git a/fonts/dejavusans_mono.ttf b/fonts/dejavusans_mono.ttf deleted file mode 100644 index f5786022f18216b4c59c6fb0c634b52c8b6e7990..0000000000000000000000000000000000000000 Binary files a/fonts/dejavusans_mono.ttf and /dev/null differ diff --git a/main.c b/main.c index a5b227464e45f150d5458ddea1ef5c5a0025fa2d..b237a4686c23e139ef2cb40eae8e1101f6020adb 100644 --- a/main.c +++ b/main.c @@ -12,8 +12,19 @@ #include "stdlib/json.h" #include "stdlib/color.h" #include "stdlib/tilemap.h" +#include "fonts/dejavusans_mono_bold.h" + #define VERSION "x.x" #define DEBUG_LEVEL LOG_DEBUG +#define FONT_IMPORT_SIZE 30 + +typedef struct { + Font font; + int font_size; +} Context; + +// Setting up global context. +Context ctx = {0}; static int lua_getfield_int(lua_State *L, int index, const char *key) { lua_getfield(L, index, key); @@ -45,6 +56,15 @@ const char *title = luaL_checkstring(L, 3); SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI); InitWindow(width, height, title); TraceLog(LOG_DEBUG, "l_open_window"); + + ctx.font_size = FONT_IMPORT_SIZE; + ctx.font = LoadFontFromMemory(".ttf", dejavusans_mono_bold, dejavusans_mono_bold_len, ctx.font_size, NULL, 0); + SetTextureFilter(ctx.font.texture, TEXTURE_FILTER_TRILINEAR); + + if (!IsFontValid(ctx.font)) { + printf("font not valid\n"); + } + return 0; } @@ -94,6 +114,19 @@ DrawFPS(GetScreenWidth() - 100, 20); return 0; } +static int l_draw_info(lua_State *L) { + float delta = GetFrameTime(); + int fps = GetFPS(); + double runtime = GetTime(); + int height = GetScreenHeight(); + + DrawTextEx(ctx.font, TextFormat("dt: %f", delta), (Vector2){ 20, height - 80 }, 20, 0, RAYWHITE); + DrawTextEx(ctx.font, TextFormat("run: %f", runtime), (Vector2){ 20, height - 60 }, 20, 0, RAYWHITE); + DrawTextEx(ctx.font, TextFormat("fps: %d", fps), (Vector2){ 20, height - 40 }, 20, 0, RAYWHITE); + + return 0; +} + static void help(const char *argv0) { printf("Usage: %s [options]\n" "\nAvailable options:\n" @@ -150,6 +183,7 @@ if (run_file) { SetTraceLogLevel(debug_level); + lua_State *L = luaL_newstate(); luaL_openlibs(L); @@ -161,6 +195,7 @@ lua_register(L, "begin_drawing", l_begin_drawing); lua_register(L, "end_drawing", l_end_drawing); lua_register(L, "set_fps", l_set_fps); lua_register(L, "draw_fps_meter", l_draw_fps_meter); + lua_register(L, "draw_info", l_draw_info); lua_register(L, "clear_window", l_clear_window); // Loading embeded modules into Lua state. @@ -174,6 +209,7 @@ fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); return 1; } + UnloadFont(ctx.font); // Unload font from GPU memory (VRAM) lua_close(L); } diff --git a/test/graphics.lua b/test/graphics.lua new file mode 100644 index 0000000000000000000000000000000000000000..2ac5219e7dfdaf59d2914bfd2e6d45203496a5d5 --- /dev/null +++ b/test/graphics.lua @@ -0,0 +1,11 @@ +open_window(800, 800, "My Game") +set_fps(60) + +while window_running() do + begin_drawing() + clear_window(color.BLACK) + draw_info() + end_drawing() +end + +close_window() diff --git a/test/json.lua b/test/json.lua new file mode 100644 index 0000000000000000000000000000000000000000..f656942b34414aa5f45b0b9b5988670104154775 --- /dev/null +++ b/test/json.lua @@ -0,0 +1,10 @@ +local file = io.open("test/test.json", "r") +local content = file:read("*a") +file:close() + +local data = json.decode(content) + +print("name: " .. data.name) +for _, n in pairs(data.numbers) do + print(" - number: " .. n) +end diff --git a/test/main.lua b/test/main.lua deleted file mode 100644 index de44e4211183c7d46923bbe5182c584c6324267a..0000000000000000000000000000000000000000 --- a/test/main.lua +++ /dev/null @@ -1,29 +0,0 @@ -function test_json() - local file = io.open("test/test.json", "r") - local content = file:read("*a") - file:close() - - local data = json.decode(content) - - print("name: " .. data.name) - for _, n in pairs(data.numbers) do - print(" - number: " .. n) - end -end - -function test_graphics() - open_window(600, 600, "My Game") - set_fps(60) - - while window_running() do - begin_drawing() - clear_window(color.BLACK) - draw_fps_meter() - end_drawing() - end - - close_window() -end - -test_json() -test_graphics() diff --git a/test/test.txt b/test/test.txt deleted file mode 100644 index 37ed9046c51eca3279bd6f4c91ef8b171a7a2787..0000000000000000000000000000000000000000 --- a/test/test.txt +++ /dev/null @@ -1 +0,0 @@ -This is the contents of test.txt file.