Commit e94ee17a authored by GoldenTails's avatar GoldenTails
Browse files

Improve how characters are handled in memory

Now it doesn't SIGSEGV when I add mods
parent db4e66a1
......@@ -176,22 +176,25 @@ static void Command_CSay_f(void);
static void Got_Saycmd(UINT8 **p, INT32 playernum);
#endif
void HU_LoadGenericFontGraphics(font_t *font, const char *prefix, UINT8 numbers)
void HU_LoadGenericFontGraphics(font_t *font, const char *lumpprefix)
{
char formatstr[12];
char buffer[9];
UINT8 numbers;
INT32 i, j = font->start;
if (strlen(prefix) > 7 || strlen(prefix) < 1) // Has to have at least 1 letter and at most 7
I_Error("Invalid prefix length");
if (strlen(lumpprefix) > 7 || strlen(lumpprefix) < 1) // Has to have at least 1 letter and at most 7
I_Error("Invalid lumpprefix length");
if (numbers < 1 || numbers > 7) // Has to have at least 1 number and at most 7
I_Error("Invalid number of numbers");
numbers = 8 - strlen(lumpprefix); // Everything after the lumpprefix is a number.
if (strlen(prefix) + numbers > 8) // Has to have at most 8 characters total
I_Error("Length of patch name would be too long!");
if (font->lumpprefix != lumpprefix)
strcpy(font->lumpprefix, lumpprefix);
snprintf(formatstr, 12, "%s%%.%dd", prefix, numbers);
snprintf(formatstr, 12, "%s%%.%dd", lumpprefix, numbers);
if (font->chars)
Z_Free(font->chars);
font->chars = Z_Calloc(font->size * sizeof(patch_t *), PU_STATIC, NULL);
......@@ -211,7 +214,7 @@ void HU_LoadGenericFontGraphics(font_t *font, const char *prefix, UINT8 numbers)
void HU_LoadGraphics(void)
{
char buffer[9];
INT32 i, j;
UINT32 i, j;
if (dedicated)
return;
......@@ -225,30 +228,16 @@ void HU_LoadGraphics(void)
fonts[FONT_HU].charwidth = 8;
fonts[FONT_HU].sixspacewidth = 6;
// cache the heads-up font for entire game execution
HU_LoadGenericFontGraphics(&fonts[FONT_HU], "STCFN");
fonts[FONT_TNY].spacewidth = 2;
fonts[FONT_TNY].monospacewidth = 5;
fonts[FONT_TNY].charwidth = 5;
fonts[FONT_TNY].sixspacewidth = 3;
fonts[FONT_HU].chars = Z_Calloc(fonts[FONT_HU].size * sizeof(patch_t *), PU_STATIC, NULL);
fonts[FONT_TNY].chars = Z_Calloc(fonts[FONT_TNY].size * sizeof(patch_t *), PU_STATIC, NULL);
for (i = 0; i < HU_FONTSIZE; i++, j++)
{
// cache the heads-up font for entire game execution
sprintf(buffer, "STCFN%.3d", j);
if (W_CheckNumForName(buffer) == LUMPERROR)
fonts[FONT_HU].chars[i] = NULL;
else
fonts[FONT_HU].chars[i] = (patch_t *)W_CachePatchName(buffer, PU_HUDGFX);
// tiny version of the heads-up font
sprintf(buffer, "TNYFN%.3d", j);
if (W_CheckNumForName(buffer) == LUMPERROR)
fonts[FONT_TNY].chars[i] = NULL;
else
fonts[FONT_TNY].chars[i] = (patch_t *)W_CachePatchName(buffer, PU_HUDGFX);
}
// tiny version of the heads-up font
HU_LoadGenericFontGraphics(&fonts[FONT_TNY], "TNYFN");
fonts[FONT_LT].start = j = LT_FONTSTART;
fonts[FONT_LT].end = LT_FONTEND;
......@@ -260,7 +249,7 @@ void HU_LoadGraphics(void)
fonts[FONT_LT].sixspacewidth = 16;
// cache the title card font for entire game execution
HU_LoadGenericFontGraphics(&fonts[FONT_LT], "LTFNT", 3);
HU_LoadGenericFontGraphics(&fonts[FONT_LT], "LTFNT");
fonts[FONT_CRED].start = j = CRED_FONTSTART;
fonts[FONT_CRED].end = CRED_FONTEND;
......@@ -271,10 +260,8 @@ void HU_LoadGraphics(void)
fonts[FONT_CRED].charwidth = 0; // If this is 0, it will not process V_OLDSPACING. Cool, right?
fonts[FONT_CRED].sixspacewidth = 16;
fonts[FONT_CRED].chars = Z_Calloc(fonts[FONT_CRED].size * sizeof(patch_t *), PU_STATIC, NULL);
// cache the credits font for entire game execution (why not?)
HU_LoadGenericFontGraphics(&fonts[FONT_CRED], "CRFNT", 3);
HU_LoadGenericFontGraphics(&fonts[FONT_CRED], "CRFNT");
//cache numbers too!
for (i = 0; i < 10; i++)
......@@ -300,27 +287,16 @@ void HU_LoadGraphics(void)
fonts[FONT_NTO].end = fonts[FONT_NTB].end = NT_FONTEND;
fonts[FONT_NTO].size = fonts[FONT_NTB].size = NT_FONTSIZE;
fonts[FONT_NTB].chars = Z_Calloc(fonts[FONT_NTB].size * sizeof(patch_t *), PU_STATIC, NULL);
fonts[FONT_NTO].chars = Z_Calloc(fonts[FONT_NTO].size * sizeof(patch_t *), PU_STATIC, NULL);
for (i = 0; i < NT_FONTSIZE; i++, j++)
{
sprintf(buffer, "NTFNT%.3d", j);
// cache the base name tag font for entire game execution
HU_LoadGenericFontGraphics(&fonts[FONT_NTB], "NTFNT");
// cache the base name tag font for entire game execution
if (W_CheckNumForName(buffer) == LUMPERROR)
fonts[FONT_NTB].chars[i] = NULL;
else
fonts[FONT_NTB].chars[i] = (patch_t *)W_CachePatchName(buffer, PU_HUDGFX);
// cache the outline name tag font for entire game execution
HU_LoadGenericFontGraphics(&fonts[FONT_NTO], "NTFNO");
sprintf(buffer, "NTFNO%.3d", j);
// cache the outline name tag font for entire game execution
if (W_CheckNumForName(buffer) == LUMPERROR)
fonts[FONT_NTO].chars[i] = NULL;
else
fonts[FONT_NTO].chars[i] = (patch_t *)W_CachePatchName(buffer, PU_HUDGFX);
}
// Freeslotted fonts
if (numfonts > FONT_FIRSTFREESLOT)
for (i = FONT_FIRSTFREESLOT; i < numfonts; i++)
HU_LoadGenericFontGraphics(&fonts[i], fonts[i].lumpprefix);
// cache the crosshairs, don't bother to know which one is being used,
// just cache all 3, they're so small anyway.
......
......@@ -33,6 +33,8 @@ typedef struct
INT32 sixspacewidth; // Space width when V_6WIDTHSPACE is enabled
INT32 charwidth; // Character width when V_OLDSPACING is enabled
char lumpprefix[8]; // Prefix (has room for 7 characters)
patch_t **chars;
} font_t;
......@@ -136,7 +138,7 @@ extern boolean hu_showscores;
// init heads up data at game startup.
void HU_Init(void);
void HU_LoadGenericFontGraphics(font_t *font, const char *prefix, UINT8 numbers);
void HU_LoadGenericFontGraphics(font_t *font, const char *lumpprefix);
void HU_LoadGraphics(void);
......
......@@ -88,7 +88,7 @@ enum font {
font_monospacewidth,
font_sixspacewidth,
font_charwidth,
font_loadPatches
font_lumpprefix
};
static const char *const font_opt[] = {
......@@ -99,7 +99,7 @@ static const char *const font_opt[] = {
"monospacewidth",
"sixspacewidth",
"charwidth",
"loadPatches",
"lumpprefix",
NULL};
enum patch {
......@@ -309,8 +309,20 @@ static int lib_setFontList(lua_State *L)
info->sixspacewidth = (INT32)luaL_checkinteger(L, 3);
else if (i == 6 || (str && fastcmp(str,"charwidth")))
info->charwidth = (INT32)luaL_checkinteger(L, 3);
else if (i == 7 || (str && fastcmp(str,"lumpprefix"))) {
const char *lumpprefix = luaL_checkstring(L, 3);
if (strlen(lumpprefix) > 7 || strlen(lumpprefix) < 1) // Has to have at least 1 letter and at most 7
return luaL_error(L, "patch name length %d out of range (1 - 7)", strlen(lumpprefix));
strcpy(info->lumpprefix, lumpprefix);
}
lua_pop(L, 1);
}
if (info->lumpprefix)
HU_LoadGenericFontGraphics(info, info->lumpprefix);
return 0;
}
......@@ -320,25 +332,6 @@ static int lib_fontslen(lua_State *L)
return 1;
}
static int lib_font_loadPatches(lua_State *L)
{
font_t *font = *((font_t **)luaL_checkudata(L, 1, META_FONT));
const char *prefix = luaL_checkstring(L, 2);
INT32 numbers = luaL_checkinteger(L, 3);
if (strlen(prefix) > 7 || strlen(prefix) < 1) // Has to have at least 1 letter and at most 7
return luaL_error(L, "patch name length %d out of range (1 - 7)", strlen(prefix));
if (numbers < 1 || numbers > 7) // Has to have at least 1 number and at most 7
return luaL_error(L, "numeric suffix length %d out of range (1 - 7)", numbers);
if (strlen(prefix) + numbers > 8) // Has to have at most 8 characters total
return luaL_error(L, "possible patch name length %d is longer than 8 characters");
HU_LoadGenericFontGraphics(font, prefix, numbers);
return 0;
}
static int font_get(lua_State *L)
{
INT32 i;
......@@ -386,8 +379,8 @@ static int font_get(lua_State *L)
case font_charwidth:
lua_pushinteger(L, font->charwidth);
break;
case font_loadPatches:
lua_pushcfunction(L, lib_font_loadPatches);
case font_lumpprefix:
lua_pushstring(L, font->lumpprefix);
break;
}
return 1;
......@@ -422,6 +415,17 @@ static int font_set(lua_State *L)
case font_charwidth:
font->charwidth = (INT32)luaL_checkinteger(L, 3);
break;
case font_lumpprefix:
{
const char *lumpprefix = luaL_checkstring(L, 3);
if (strlen(lumpprefix) > 7 || strlen(lumpprefix) < 1) // Has to have at least 1 letter and at most 7
return luaL_error(L, "patch name length %d out of range (1 - 7)", strlen(lumpprefix));
HU_LoadGenericFontGraphics(font, lumpprefix);
break;
}
}
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment