Skip to content

Commit

Permalink
maybe if i use a wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
KrahJohlito committed Nov 16, 2024
1 parent 9ec7510 commit a025ec8
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions src/fntsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ void fntInit()
for (; i < FNT_MAX_COUNT; ++i)
fntInitSlot(&fonts[i]);

fntLoadDefault(NULL);
fntLoadDefaultInternal(NULL, FNTSYS_DEFAULT_SIZE, FNT_DEFAULT);
fntLoadDefaultInternal(NULL, 12, 1);
}

int fntLoadFile(char *path, int fontSize)
Expand All @@ -304,44 +305,36 @@ int fntLoadFile(char *path, int fontSize)
return FNT_ERROR;
}

int fntLoadDefault(char *path)
static int fntLoadDefaultInternal(char *path, int fontSize, int slot)
{
font_t newFont, oldFont, newFont2, oldFont2;

WaitSema(gFontSemaId);

if (fntLoadSlot(&newFont, path, FNTSYS_DEFAULT_SIZE) != FNT_ERROR) {
// copy over the new font definition
// we have to lock this phase, as the old font may still be used
// Note: No check for concurrency is done here, which is kinda funky!
memcpy(&oldFont, &fonts[FNT_DEFAULT], sizeof(font_t));
memcpy(&fonts[FNT_DEFAULT], &newFont, sizeof(font_t));
font_t newFont, oldFont;

// delete the old font
fntDeleteSlot(&oldFont);
LOG("Default font loaded with size: %d\n", fonts[FNT_DEFAULT].fontSize);
if (slot < 0 || slot >= FNT_MAX_COUNT) {
LOG("FNTSYS Invalid font slot: %d\n", slot);
return -1;
}

//memset(&newFont, 0, sizeof(font_t));
//memset(&oldFont, 0, sizeof(font_t));

// Load the font into slot1 as well to have a font size of 12
if (fntLoadSlot(&newFont2, path, FNTSYS_DEFAULT_SIZE - 5) != FNT_ERROR) {
memcpy(&oldFont2, &fonts[1], sizeof(font_t));
memcpy(&fonts[1], &newFont2, sizeof(font_t));
if (fntLoadSlot(&newFont, path, fontSize) != FNT_ERROR) {
// Lock access and replace the font in the specified slot
WaitSema(gFontSemaId);
memcpy(&oldFont, &fonts[slot], sizeof(font_t));
memcpy(&fonts[slot], &newFont, sizeof(font_t));

fntDeleteSlot(&oldFont2);
LOG("Second font loaded with size: %d\n", fonts[1].fontSize);
// Delete the old font
fntDeleteSlot(&oldFont);
SignalSema(gFontSemaId);

return 0;
}

SignalSema(gFontSemaId);

LOG("Failed to load second font.\n");
return -1;
}

int fntLoadDefault(char *path)
{
return fntLoadDefaultInternal(path, FNTSYS_DEFAULT_SIZE, FNT_DEFAULT);
}

void fntEnd()
{
LOG("FNTSYS End\n");
Expand Down

0 comments on commit a025ec8

Please sign in to comment.