(Canvi d'ordenador)

- [WIP] Treballant en les fonts
This commit is contained in:
2026-03-17 18:25:17 +01:00
parent 560d67ca3d
commit e89d596ea4
3 changed files with 233 additions and 4 deletions

107
mini.cpp
View File

@@ -10,6 +10,7 @@
#include "log.h"
#define MAX_SURFACES 100
#define MAX_FONTS 5
#ifdef MACOS_BUNDLE
#include <libgen.h>
@@ -26,11 +27,14 @@ struct surface_t {
uint32_t size;
};
struct char_t {
uint8_t x,y,w,h,base;
};
struct font_t {
struct special_char_t { uint8_t character; SDL_Rect glyph; SDL_Point displacement; };
int8_t spacing;
SDL_Rect characters[96];
special_char_t specials[64];
char *name {nullptr};
uint8_t surface {0};
char_t chars[256];
};
int fps=0;
@@ -53,6 +57,7 @@ surface_t *dest_surface = screen_surface;
surface_t *source_surface = NULL;
surface_t *map_surface = NULL;
font_t fonts[MAX_FONTS];
FILE *file = NULL;
//uint8_t file_mode = 0;
bool file_ignore_comma=true;
@@ -274,6 +279,20 @@ uint8_t newsurf(int w, int h) {
return i;
}
uint8_t loadsurf(uint8_t* buffer, const char* name) {
// Agafar la pròxima textura lliure
unsigned int i = 0;
while (i<MAX_SURFACES && surfaces[i].p != NULL) ++i;
if (i==MAX_SURFACES) return 255;
surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h);
surfaces[i].size = surfaces[i].w*surfaces[i].h;
surfaces[i].name = (char*)malloc(strlen(name)+1);
strcpy(surfaces[i].name, name);
return i;
}
uint8_t loadsurf(const char* filename, const bool external) {
// Si el gif ja s'ha carregat en una textura, tornem eixa textura
for (unsigned int i=0; i<MAX_SURFACES; ++i)
@@ -658,6 +677,86 @@ int main(int argc,char*argv[]){
return 0;
}
bool loadfont_from_buffer(const char* buffer, font_t& font) {
if (!buffer) return false;
const char* ptr = buffer;
char line[256];
// --- Read first line ---
{
// Extract line
int len = 0;
while (ptr[len] && ptr[len] != '\n' && len < 255) len++;
memcpy(line, ptr, len);
line[len] = '\0';
ptr += (ptr[len] == '\n') ? len + 1 : len; // Advance pointer
if (strncmp(line, "bitmap=", 7) != 0) return false; // Parse first line
font.surface = loadsurf(line + 7);
}
// --- Read character lines ---
while (*ptr) {
// Extract next line
int len = 0;
while (ptr[len] && ptr[len] != '\n' && len < 255) len++;
memcpy(line, ptr, len);
line[len] = '\0';
ptr += (ptr[len] == '\n') ? len + 1 : len; // Advance pointer
// Remove comments
char* hash = strchr(line, '#');
if (hash) *hash = '\0';
// Parse
int code, x, y, w, h, base;
if (sscanf(line, "%d: %d %d %d %d %d", &code, &x, &y, &w, &h, &base) == 6) {
if (code >= 0 && code < 256) {
font.chars[code] = { (uint8_t)x, (uint8_t)y, (uint8_t)w, (uint8_t)h, (uint8_t)base };
}
}
}
return true;
}
static inline uint32_t utf8_decode(const char* s, int& advance) {
const unsigned char* p = (const unsigned char*)s;
uint32_t cp;
if (p[0] < 0x80) {
cp = p[0];
advance = 1;
}
else if ((p[0] & 0xE0) == 0xC0) {
cp = ((p[0] & 0x1F) << 6) |
(p[1] & 0x3F);
advance = 2;
}
else if ((p[0] & 0xF0) == 0xE0) {
cp = ((p[0] & 0x0F) << 12) |
((p[1] & 0x3F) << 6) |
(p[2] & 0x3F);
advance = 3;
}
else if ((p[0] & 0xF8) == 0xF0) {
cp = ((p[0] & 0x07) << 18) |
((p[1] & 0x3F) << 12) |
((p[2] & 0x3F) << 6) |
(p[3] & 0x3F);
advance = 4;
}
else {
cp = 0xFFFD; // carácter inválido
advance = 1;
}
return cp;
}
void simple_pset(int x, int y, uint8_t color) {
x += ds::origin[0]; y += ds::origin[1];
if (x < ds::clip[0] || x > ds::clip[2] || y < ds::clip[1] || y > ds::clip[3]) return;