VERSIÓ 1.4.6

-[NEW] font.load()
-[NEW] font.current()
-[NEW] font.spacing()
-[NEW] font.DEFAULT
-[NEW] surf.SCREEN
This commit is contained in:
2026-03-18 20:36:14 +01:00
parent bf34c92e09
commit 779ef7acb6
8 changed files with 146 additions and 9 deletions

9
data/font.fnt Normal file
View File

@@ -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

BIN
data/font.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

View File

@@ -16,6 +16,7 @@ function mini.init()
end end
print("========================") print("========================")
f = font.load("font.fnt")
end end
function mini.update() function mini.update()
@@ -42,4 +43,9 @@ function mini.update()
draw.rectf(mx, my, 4, 4, 8) draw.rectf(mx, my, 4, 4, 8)
draw.text(mx .. " " .. my, 1, 8, 8) draw.text(mx .. " " .. my, 1, 8, 8)
--draw.text(other.peiv(),1,100,4) --draw.text(other.peiv(),1,100,4)
font.current(f)
font.spacing(0)
draw.text("0146",100,50,28)
font.current(font.DEFAULT)
end end

60
lua.cpp
View File

@@ -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 // 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_source); lua_setfield(L, -2, "source");
lua_pushcfunction(L,cpp_surf_cls); lua_setfield(L, -2, "cls"); lua_pushcfunction(L,cpp_surf_cls); lua_setfield(L, -2, "cls");
lua_pushcfunction(L,cpp_surf_pixel); lua_setfield(L, -2, "pixel"); 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_setglobal(L, "surf");
lua_newtable(L); lua_newtable(L);
@@ -1170,6 +1220,14 @@ void push_lua_funcs() {
lua_pushcfunction(L,cpp_conf_folder); lua_setfield(L, -2, "folder"); lua_pushcfunction(L,cpp_conf_folder); lua_setfield(L, -2, "folder");
lua_setglobal(L, "config"); 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_newtable(L);
lua_pushcfunction(L,cpp_mouse_pos); lua_setfield(L, -2, "pos"); lua_pushcfunction(L,cpp_mouse_pos); lua_setfield(L, -2, "pos");
lua_pushcfunction(L,cpp_mouse_wheel); lua_setfield(L, -2, "wheel"); lua_pushcfunction(L,cpp_mouse_wheel); lua_setfield(L, -2, "wheel");
@@ -1335,7 +1393,7 @@ void push_lua_funcs() {
lua_setglobal(L, "pad"); lua_setglobal(L, "pad");
lua_getglobal(L, "utf8"); // push utf8 table lua_getglobal(L, "utf8"); // push utf8 table
lua_pushcfunction(L, cpp_utf8_sub); // push C function lua_pushcfunction(L, cpp_utf8_sub); // push C function
lua_setfield(L, -2, "sub"); // utf8.sub = cpp_utf8_sub lua_setfield(L, -2, "sub"); // utf8.sub = cpp_utf8_sub

View File

@@ -37,6 +37,7 @@ struct char_t {
struct font_t { struct font_t {
char *name {nullptr}; char *name {nullptr};
uint8_t surface {0}; uint8_t surface {0};
uint8_t spacing {1};
char_t chars[256]; char_t chars[256];
}; };
@@ -61,6 +62,7 @@ surface_t *source_surface = NULL;
surface_t *map_surface = NULL; surface_t *map_surface = NULL;
font_t fonts[MAX_FONTS]; font_t fonts[MAX_FONTS];
font_t *current_font;
FILE *file = NULL; FILE *file = NULL;
//uint8_t file_mode = 0; //uint8_t file_mode = 0;
bool file_ignore_comma=true; 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_enable() { shader::enable(); }
void shader_disable() { shader::disable(); } 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; if (!buffer) return false;
const char* ptr = buffer; 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 if (strncmp(line, "bitmap=", 7) != 0) return false; // Parse first line
fonts[index].surface = loadsurf(line + 7); fonts[index].surface = loadsurf(line + 7);
fonts[index].name = (char*)malloc(strlen(name)+1);
strcpy(fonts[index].name, name);
} }
// --- Read character lines --- // --- Read character lines ---
@@ -472,7 +477,7 @@ uint8_t loadfont(const char *filename) {
// Agafar la pròxima textura lliure // Agafar la pròxima textura lliure
unsigned int i = 0; unsigned int i = 0;
while (i<MAX_FONTS && surfaces[i].p != NULL) ++i; while (i<MAX_FONTS && fonts[i].name != NULL) ++i;
if (i==MAX_FONTS) return 255; if (i==MAX_FONTS) return 255;
// Carregar l'arxiu de disc // Carregar l'arxiu de disc
@@ -487,13 +492,30 @@ uint8_t loadfont(const char *filename) {
return 255; return 255;
} }
loadfont_from_buffer(buffer, i); loadfont_from_buffer(buffer, i, filename);
free(buffer); free(buffer);
log_msg(LOG_INFO, "Arxiu '%s' carregat en font: %i.\n", filename, i); log_msg(LOG_INFO, "Arxiu '%s' carregat en font: %i.\n", filename, i);
return i; return i;
} }
uint8_t getfont() {
for (unsigned int i=0; i<MAX_FONTS; ++i) if (current_font == &fonts[i]) return i;
return 0;
}
void setfont(uint8_t font) {
current_font = &fonts[font];
}
uint8_t getfontspacing() {
return current_font->spacing;
}
void setfontspacing(uint8_t spacing) {
current_font->spacing = spacing;
}
void createDisplay() { void createDisplay() {
if (screen_zoom <= 0) screen_zoom = 1; if (screen_zoom <= 0) screen_zoom = 1;
while (screen_width*screen_zoom > desktop_width || screen_height*screen_zoom > desktop_height) screen_zoom--; 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(); initaudio();
loadsurf(default_font_gif, "default_font"); 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_init(main_lua_file);
lua_call_init(); 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) { 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); blit(chr.x, chr.y, chr.w, chr.h, x, y-chr.base);
return chr.w; 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 xpos = x;
uint8_t old_source = getsource(); uint8_t old_source = getsource();
setsource(fonts[0].surface); setsource(current_font->surface);
uint8_t old_color = ds::draw_palette[1]; uint8_t old_color = ds::draw_palette[1];
ds::draw_palette[1] = color; ds::draw_palette[1] = color;
uint8_t old_trans = ds::trans;
ds::trans = 0;
while (p[0]!=0) { while (p[0]!=0) {
if (p[0] < 0x80) { if (p[0] < 0x80) {
cp = p[0]; 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); cp = (p[0] << 6) | (p[1] & 0x3F);
p+=2; 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; ds::draw_palette[1] = old_color;
setsource(old_source); setsource(old_source);
} }

6
mini.h
View File

@@ -142,6 +142,12 @@ void shader_init(const char* vshader, const char* fshader);
void shader_enable(); void shader_enable();
void shader_disable(); 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 cls(uint8_t color=0);
void color(uint8_t color=6); void color(uint8_t color=6);
void bcolor(uint8_t color=0); void bcolor(uint8_t color=0);

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define MINI_VERSION "1.4.5" #define MINI_VERSION "1.4.6"

View File

@@ -4,6 +4,7 @@
mini = {} mini = {}
---@class surf ---@class surf
---@field surf.SCREEN number
surf = {} surf = {}
---@param w number ---@param w number
@@ -72,6 +73,9 @@ function surf.pixel(x, y) end
---Set the color for pixel (x,y) on the target surface. ---Set the color for pixel (x,y) on the target surface.
function surf.pixel(x, y, color) end function surf.pixel(x, y, color) end
surf.SCREEN = 0
---@class map ---@class map
map = {} map = {}
@@ -543,6 +547,34 @@ function config.key(key) end
function config.folder() 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 ---@class mouse
---@field mouse.LEFT number ---@field mouse.LEFT number
---@field mouse.MIDDLE number ---@field mouse.MIDDLE number