From 779ef7acb63e82f649207b8487dc89f1e48a7908 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 18 Mar 2026 20:36:14 +0100 Subject: [PATCH] =?UTF-8?q?VERSI=C3=93=201.4.6=20-[NEW]=20font.load()=20-[?= =?UTF-8?q?NEW]=20font.current()=20-[NEW]=20font.spacing()=20-[NEW]=20font?= =?UTF-8?q?.DEFAULT=20-[NEW]=20surf.SCREEN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/font.fnt | 9 +++++++ data/font.gif | Bin 0 -> 179 bytes data/main.lua | 6 +++++ lua.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++- mini.cpp | 40 ++++++++++++++++++++++++------ mini.h | 6 +++++ version.h | 2 +- vscode/library.lua | 32 ++++++++++++++++++++++++ 8 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 data/font.fnt create mode 100644 data/font.gif diff --git a/data/font.fnt b/data/font.fnt new file mode 100644 index 0000000..46e7299 --- /dev/null +++ b/data/font.fnt @@ -0,0 +1,9 @@ +bitmap=font.gif +48: 0 0 7 7 0 # 0 +49: 8 0 4 7 0 # 1 +50: 16 0 7 7 0 # 2 +51: 24 0 7 7 0 # 3 +52: 32 0 7 7 0 # 4 +53: 40 0 7 7 0 # 5 +54: 48 0 7 7 0 # 6 +55: 56 0 7 7 0 # 7 diff --git a/data/font.gif b/data/font.gif new file mode 100644 index 0000000000000000000000000000000000000000..71725e472cf310a802197a7e165d44917412c282 GIT binary patch literal 179 zcmZ?wbh9u|bYL)JXkY+=|Ns9h{$$}`1Tu9%e2@$SQ-j)+qB1T`P5K(m9>v0_W9p%Gw_G9K2g(czs=lM4gBF%+=csjjZio=KWmaHrIV# zpEq~+@h6*p_V}&7{$|^6-u~0izwG+kcmDPFKl}dkOK4blL}XNS%viDG#Elm}I3zVK eJtH$KJ7=!kdGhAVpPW)!R$ftARky+z7_0%5J5hW9 literal 0 HcmV?d00001 diff --git a/data/main.lua b/data/main.lua index cdb8c3d..8bc70c2 100644 --- a/data/main.lua +++ b/data/main.lua @@ -16,6 +16,7 @@ function mini.init() end print("========================") + f = font.load("font.fnt") end function mini.update() @@ -42,4 +43,9 @@ function mini.update() draw.rectf(mx, my, 4, 4, 8) draw.text(mx .. " " .. my, 1, 8, 8) --draw.text(other.peiv(),1,100,4) + + font.current(f) + font.spacing(0) + draw.text("0146",100,50,28) + font.current(font.DEFAULT) end diff --git a/lua.cpp b/lua.cpp index cae9c5c..3fcc13e 100644 --- a/lua.cpp +++ b/lua.cpp @@ -888,6 +888,54 @@ extern "C" { } + // font + // =============================================== + static int cpp_font_load(lua_State *L) { + const char* str = luaL_checkstring(L, 1); + uint8_t s = loadfont(str); + if (s==255) { + luaL_error(L, "Error while loading font: Max fonts reached"); + return 0; + } + lua_pushinteger(L, s); + return 1; + } + + static int cpp_font_current(lua_State *L) { + const int numargs = lua_gettop(L); + switch (numargs) { + case 0: { + lua_pushinteger(L, getfont()); + return 1; + } + case 1: { + uint8_t font = luaL_checkinteger(L, 1); + setfont(font); + return 0; + } + default: + return luaL_error(L, "Function 'font.current' Unexpected number of parameters."); + }; + } + + static int cpp_font_spacing(lua_State *L) { + const int numargs = lua_gettop(L); + switch (numargs) { + case 0: { + lua_pushinteger(L, getfontspacing()); + return 1; + } + case 1: { + uint8_t spacing = luaL_checkinteger(L, 1); + setfontspacing(spacing); + return 0; + } + default: + return luaL_error(L, "Function 'font.spacing' Unexpected number of parameters."); + }; + } + + // mouse // =============================================== @@ -1061,6 +1109,8 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_surf_source); lua_setfield(L, -2, "source"); lua_pushcfunction(L,cpp_surf_cls); lua_setfield(L, -2, "cls"); lua_pushcfunction(L,cpp_surf_pixel); lua_setfield(L, -2, "pixel"); + + lua_pushinteger(L, 0); lua_setfield(L, -2, "SCREEN"); lua_setglobal(L, "surf"); lua_newtable(L); @@ -1170,6 +1220,14 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_conf_folder); lua_setfield(L, -2, "folder"); lua_setglobal(L, "config"); + lua_newtable(L); + lua_pushcfunction(L,cpp_font_load); lua_setfield(L, -2, "load"); + lua_pushcfunction(L,cpp_font_current); lua_setfield(L, -2, "current"); + lua_pushcfunction(L,cpp_font_spacing); lua_setfield(L, -2, "spacing"); + + lua_pushinteger(L, 0); lua_setfield(L, -2, "DEFAULT"); + lua_setglobal(L, "font"); + lua_newtable(L); lua_pushcfunction(L,cpp_mouse_pos); lua_setfield(L, -2, "pos"); lua_pushcfunction(L,cpp_mouse_wheel); lua_setfield(L, -2, "wheel"); @@ -1335,7 +1393,7 @@ void push_lua_funcs() { lua_setglobal(L, "pad"); - + lua_getglobal(L, "utf8"); // push utf8 table lua_pushcfunction(L, cpp_utf8_sub); // push C function lua_setfield(L, -2, "sub"); // utf8.sub = cpp_utf8_sub diff --git a/mini.cpp b/mini.cpp index 8421cb3..b4bcfdc 100644 --- a/mini.cpp +++ b/mini.cpp @@ -37,6 +37,7 @@ struct char_t { struct font_t { char *name {nullptr}; uint8_t surface {0}; + uint8_t spacing {1}; char_t chars[256]; }; @@ -61,6 +62,7 @@ surface_t *source_surface = NULL; surface_t *map_surface = NULL; font_t fonts[MAX_FONTS]; +font_t *current_font; FILE *file = NULL; //uint8_t file_mode = 0; bool file_ignore_comma=true; @@ -416,7 +418,7 @@ void shader_init(const char* vshader, const char* fshader) void shader_enable() { shader::enable(); } void shader_disable() { shader::disable(); } -uint8_t loadfont_from_buffer(const char* buffer, uint8_t index) { +uint8_t loadfont_from_buffer(const char* buffer, uint8_t index, const char* name) { if (!buffer) return false; const char* ptr = buffer; @@ -435,6 +437,9 @@ uint8_t loadfont_from_buffer(const char* buffer, uint8_t index) { if (strncmp(line, "bitmap=", 7) != 0) return false; // Parse first line fonts[index].surface = loadsurf(line + 7); + + fonts[index].name = (char*)malloc(strlen(name)+1); + strcpy(fonts[index].name, name); } // --- Read character lines --- @@ -472,7 +477,7 @@ uint8_t loadfont(const char *filename) { // Agafar la pròxima textura lliure unsigned int i = 0; - while (ispacing; +} + +void setfontspacing(uint8_t spacing) { + current_font->spacing = spacing; +} + void createDisplay() { if (screen_zoom <= 0) screen_zoom = 1; while (screen_width*screen_zoom > desktop_width || screen_height*screen_zoom > desktop_height) screen_zoom--; @@ -641,7 +663,8 @@ int main(int argc,char*argv[]){ initaudio(); loadsurf(default_font_gif, "default_font"); - loadfont_from_buffer((const char*)default_font_fnt, 0); + loadfont_from_buffer((const char*)default_font_fnt, 0, "default_font"); + current_font = &fonts[0]; lua_init(main_lua_file); lua_call_init(); @@ -1041,7 +1064,7 @@ void old_print(const char *str, int x, int y, uint8_t color) { } const uint8_t printchar(uint8_t c, int x, int y) { - char_t &chr = fonts[0].chars[c]; + char_t &chr = current_font->chars[c]; blit(chr.x, chr.y, chr.w, chr.h, x, y-chr.base); return chr.w; } @@ -1053,9 +1076,11 @@ void print(const char* str, int x, int y, uint8_t color) { uint8_t xpos = x; uint8_t old_source = getsource(); - setsource(fonts[0].surface); + setsource(current_font->surface); uint8_t old_color = ds::draw_palette[1]; ds::draw_palette[1] = color; + uint8_t old_trans = ds::trans; + ds::trans = 0; while (p[0]!=0) { if (p[0] < 0x80) { cp = p[0]; @@ -1064,8 +1089,9 @@ void print(const char* str, int x, int y, uint8_t color) { cp = (p[0] << 6) | (p[1] & 0x3F); p+=2; } - xpos += printchar(cp, xpos, y) + 1; + xpos += printchar(cp, xpos, y) + current_font->spacing; } + ds::trans = old_trans; ds::draw_palette[1] = old_color; setsource(old_source); } diff --git a/mini.h b/mini.h index 9f323db..5a4ec20 100644 --- a/mini.h +++ b/mini.h @@ -142,6 +142,12 @@ void shader_init(const char* vshader, const char* fshader); void shader_enable(); void shader_disable(); +uint8_t loadfont(const char *filename); +uint8_t getfont(); +void setfont(uint8_t font); +uint8_t getfontspacing(); +void setfontspacing(uint8_t spacing); + void cls(uint8_t color=0); void color(uint8_t color=6); void bcolor(uint8_t color=0); diff --git a/version.h b/version.h index 2c93adf..21e0f01 100644 --- a/version.h +++ b/version.h @@ -1,3 +1,3 @@ #pragma once -#define MINI_VERSION "1.4.5" +#define MINI_VERSION "1.4.6" diff --git a/vscode/library.lua b/vscode/library.lua index 2e8883d..5997d44 100644 --- a/vscode/library.lua +++ b/vscode/library.lua @@ -4,6 +4,7 @@ mini = {} ---@class surf +---@field surf.SCREEN number surf = {} ---@param w number @@ -72,6 +73,9 @@ function surf.pixel(x, y) end ---Set the color for pixel (x,y) on the target surface. function surf.pixel(x, y, color) end +surf.SCREEN = 0 + + ---@class map map = {} @@ -543,6 +547,34 @@ function config.key(key) end function config.folder() end +---@class font +---@field font.DEFAULT number +font = {} + +---@param filename string +---@return number fnt +---Loads a font from a file +function font.load(filename) end + +---@return number fnt +---Gets the currently selected font +function font.current() end + +---@param fnt number +---Sets the specified font as selected +function font.current(fnt) end + +---@return number value +---Gets the currently selected font's spacing +function font.spacing() end + +---@param value number +---Sets the currently selected font's spacing +function font.spacing(value) end + +font.DEFAULT = 0 + + ---@class mouse ---@field mouse.LEFT number ---@field mouse.MIDDLE number