Skip to content

Commit

Permalink
Register fonts in multiple sizes, testing commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aufau committed Jan 14, 2016
1 parent d097c68 commit 591b1cf
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 57 deletions.
53 changes: 42 additions & 11 deletions code/cgame/cg_draw.c
Expand Up @@ -52,28 +52,59 @@ char *showPowersName[] =
};


int MenuFontToHandle(int iMenuFont)
int MenuFontToHandle(int iMenuFont, float *scale)
{
fontHandle_t *face;
char sharpness[5];
int maxSize;
int i;

switch (iMenuFont)
{
case FONT_SMALL: return cgDC.Assets.qhSmallFont;
case FONT_MEDIUM: return cgDC.Assets.qhMediumFont;
case FONT_LARGE: return cgDC.Assets.qhBigFont;
case FONT_SMALL:
face = cgDC.Assets.qhSmallFont;
break;
case FONT_LARGE:
face = cgDC.Assets.qhBigFont;
break;
case FONT_MEDIUM:
default:
face = cgDC.Assets.qhMediumFont;
}

return cgDC.Assets.qhMediumFont;
trap_Cvar_VariableStringBuffer("ui_fontSharpness", sharpness, sizeof(sharpness));
// Shrinking font vertically may produce ugly artifacts.
maxSize = face[0].size * atof(sharpness) * *scale * cgs.screenYScale + 0.99f;

for (i = 1; i < MAX_FONT_VARIANTS; i++) {
if (face[i].index == 0) {
break;
}
if (face[i].size > maxSize) {
break;
}
}

i--;

/* i = atoi(sharpness); */
/* face[i].size = trap_R_Font_HeightPixels(face[i].index, 1.0f); */

*scale *= (float) face[0].size / face[i].size;

return face[i].index;
}

int CG_Text_Width(const char *text, float scale, int iMenuFont)
{
int iFontIndex = MenuFontToHandle(iMenuFont);
int iFontIndex = MenuFontToHandle(iMenuFont, &scale);

return trap_R_Font_StrLenPixels(text, iFontIndex, scale);
}

int CG_Text_Height(const char *text, float scale, int iMenuFont)
{
int iFontIndex = MenuFontToHandle(iMenuFont);
int iFontIndex = MenuFontToHandle(iMenuFont, &scale);

return trap_R_Font_HeightPixels(iFontIndex, scale);
}
Expand All @@ -82,7 +113,7 @@ int CG_Text_Height(const char *text, float scale, int iMenuFont)
void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text, float adjust, int limit, int style, int iMenuFont)
{
int iStyleOR = 0;
int iFontIndex = MenuFontToHandle(iMenuFont);
int iFontIndex = MenuFontToHandle(iMenuFont, &scale);

switch (style)
{
Expand Down Expand Up @@ -3322,19 +3353,19 @@ static void CG_DrawSpectator(void)
}
else
{
CG_Text_Paint ( 320 - CG_Text_Width ( s, 1.0f, 3 ) / 2, 420, 1.0f, colorWhite, s, 0, 0, 0, 3 );
CG_Text_Paint ( 320 - CG_Text_Width ( s, 1.0f, FONT_MEDIUM ) / 2, 420, 1.0f, colorWhite, s, 0, 0, 0, FONT_MEDIUM );
}

if ( cgs.gametype == GT_TOURNAMENT )
{
s = CG_GetStripEdString("INGAMETEXT", "WAITING_TO_PLAY"); // "waiting to play";
CG_Text_Paint ( 320 - CG_Text_Width ( s, 1.0f, 3 ) / 2, 440, 1.0f, colorWhite, s, 0, 0, 0, 3 );
CG_Text_Paint ( 320 - CG_Text_Width ( s, 1.0f, FONT_MEDIUM ) / 2, 440, 1.0f, colorWhite, s, 0, 0, 0, FONT_MEDIUM );
}
else //if ( cgs.gametype >= GT_TEAM )
{
//s = "press ESC and use the JOIN menu to play";
s = CG_GetStripEdString("INGAMETEXT", "SPEC_CHOOSEJOIN");
CG_Text_Paint ( 320 - CG_Text_Width ( s, 1.0f, 3 ) / 2, 440, 1.0f, colorWhite, s, 0, 0, 0, 3 );
CG_Text_Paint ( 320 - CG_Text_Width ( s, 1.0f, FONT_MEDIUM ) / 2, 440, 1.0f, colorWhite, s, 0, 0, 0, FONT_MEDIUM );
}
}

Expand Down
3 changes: 3 additions & 0 deletions code/cgame/cg_local.h
Expand Up @@ -1541,7 +1541,10 @@ extern vmCvar_t cg_singlePlayerActive;
extern vmCvar_t cg_recordSPDemo;
extern vmCvar_t cg_recordSPDemoName;

extern vmCvar_t cg_oldFont;

extern vmCvar_t ui_myteam;

/*
Ghoul2 Insert Start
*/
Expand Down
76 changes: 69 additions & 7 deletions code/cgame/cg_main.c
Expand Up @@ -499,6 +499,8 @@ vmCvar_t cg_singlePlayerActive;
vmCvar_t cg_recordSPDemo;
vmCvar_t cg_recordSPDemoName;

vmCvar_t cg_oldFont;

vmCvar_t ui_myteam;

typedef struct {
Expand Down Expand Up @@ -648,6 +650,8 @@ static cvarTable_t cvarTable[] = { // bk001129

{ &ui_myteam, "ui_myteam", "0", CVAR_ROM|CVAR_INTERNAL},

{ &cg_oldFont, "cg_oldFont", "0", CVAR_ARCHIVE },

// { &cg_pmove_fixed, "cg_pmove_fixed", "0", CVAR_USERINFO | CVAR_ARCHIVE }
/*
Ghoul2 Insert Start
Expand Down Expand Up @@ -1599,6 +1603,64 @@ Ghoul2 Insert End
*/
}

/*
=======================
CG_RegisterFont
=======================
*/
void CG_RegisterFont(fontHandle_t face[MAX_FONT_VARIANTS], const char *fontName)
{
fileHandle_t f;
char fileName[MAX_QPATH];
int index;
int i, j;
qboolean sorted;

Com_sprintf(fileName, sizeof(fileName), "fonts/%s.fontdat", fontName);

// RE_RegisterFont is bugged so we need to check ourselves
if (trap_FS_FOpenFile(va("fonts/%s.fontdat", fontName), &f, FS_READ) < 0) {
return;
}
trap_FS_FCloseFile(f);

memset(face, 0, MAX_FONT_VARIANTS * sizeof(fontHandle_t));

index = trap_R_RegisterFont(fontName);

face[0].index = index;
face[0].size = trap_R_Font_HeightPixels(index, 1.0f);

for (i = 1, j = 1; i < MAX_FONT_VARIANTS; i++) {
Com_sprintf(fileName, sizeof(fileName), "%s%d", fontName, i);

if (trap_FS_FOpenFile(va("fonts/%s.fontdat", fileName), &f, FS_READ) < 0) {
continue;
}
trap_FS_FCloseFile(f);

index = trap_R_RegisterFont(fileName);

face[j].index = index;
face[j].size = trap_R_Font_HeightPixels(index, 1.0f);
j++;
}

do {
j--;
sorted = qtrue;

for (i = 1; i < j; i++) {
if (face[i].size > face[i + 1].size) {
fontHandle_t temp = face[i];
face[i] = face[i + 1];
face[i + 1] = temp;
sorted = qfalse;
}
}
} while (!sorted);
}

const char *CG_GetStripEdString(char *refSection, char *refName)
{
Expand Down Expand Up @@ -1749,7 +1811,7 @@ qboolean CG_Asset_Parse(int handle) {
}

// cgDC.registerFont(tempStr, pointSize, &cgDC.Assets.textFont);
cgDC.Assets.qhMediumFont = cgDC.RegisterFont(tempStr);
CG_RegisterFont(cgDC.Assets.qhMediumFont, tempStr);
continue;
}

Expand All @@ -1760,7 +1822,7 @@ qboolean CG_Asset_Parse(int handle) {
return qfalse;
}
// cgDC.registerFont(tempStr, pointSize, &cgDC.Assets.smallFont);
cgDC.Assets.qhSmallFont = cgDC.RegisterFont(tempStr);
CG_RegisterFont(cgDC.Assets.qhSmallFont, tempStr);
continue;
}

Expand All @@ -1771,7 +1833,7 @@ qboolean CG_Asset_Parse(int handle) {
return qfalse;
}
// cgDC.registerFont(tempStr, pointSize, &cgDC.Assets.bigFont);
cgDC.Assets.qhBigFont = cgDC.RegisterFont(tempStr);
CG_RegisterFont(cgDC.Assets.qhBigFont, tempStr);
continue;
}

Expand Down Expand Up @@ -2212,7 +2274,7 @@ void CG_LoadHudMenu()
cgDC.clearScene = &trap_R_ClearScene;
cgDC.addRefEntityToScene = &trap_R_AddRefEntityToScene;
cgDC.renderScene = &trap_R_RenderScene;
cgDC.RegisterFont = &trap_R_RegisterFont;
cgDC.RegisterFont = &CG_RegisterFont;
cgDC.Font_StrLenPixels = &trap_R_Font_StrLenPixels;
cgDC.Font_StrLenChars = &trap_R_Font_StrLenChars;
cgDC.Font_HeightPixels = &trap_R_Font_HeightPixels;
Expand Down Expand Up @@ -2389,9 +2451,9 @@ Ghoul2 Insert End
// if desired during parse. Dunno how legal it is to store in these cgDC things, but it causes no harm
// and even if/when they get overwritten they'll be legalised by the menu asset parser :-)
// CG_LoadFonts();
cgDC.Assets.qhSmallFont = trap_R_RegisterFont("ocr_a");
cgDC.Assets.qhMediumFont = trap_R_RegisterFont("ergoec");
cgDC.Assets.qhBigFont = cgDC.Assets.qhMediumFont;
CG_RegisterFont(cgDC.Assets.qhSmallFont, "ocr_a");
CG_RegisterFont(cgDC.Assets.qhMediumFont, "ergoec");
memcpy(cgDC.Assets.qhBigFont, cgDC.Assets.qhMediumFont, sizeof(cgDC.Assets.qhBigFont));

memset( &cgs, 0, sizeof( cgs ) );
memset( cg_weapons, 0, sizeof(cg_weapons) );
Expand Down
10 changes: 3 additions & 7 deletions code/cgame/cg_newDraw.c
Expand Up @@ -373,20 +373,16 @@ const char *CG_GameTypeString(void) {
return "";
}

extern int MenuFontToHandle(int iMenuFont);


// maxX param is initially an X limit, but is also used as feedback. 0 = text was clipped to fit within, else maxX = next pos
//
static void CG_Text_Paint_Limit(float *maxX, float x, float y, float scale, vec4_t color, const char* text, float adjust, int limit, int iMenuFont)
{
qboolean bIsTrailingPunctuation;

// this is kinda dirty, but...
//
int iFontIndex = MenuFontToHandle(iMenuFont);

//float fMax = *maxX;
int iPixelLen = trap_R_Font_StrLenPixels(text, iFontIndex, scale);
int iPixelLen = CG_Text_Width(text, scale, iMenuFont);
if (x + iPixelLen > *maxX)
{
// whole text won't fit, so we need to print just the amount that does...
Expand All @@ -398,7 +394,7 @@ static void CG_Text_Paint_Limit(float *maxX, float x, float y, float scale, vec4
char *psOutLastGood = psOut;
unsigned int uiLetter;

while (*psText && (x + trap_R_Font_StrLenPixels(sTemp, iFontIndex, scale)<=*maxX)
while (*psText && (x + CG_Text_Width(sTemp, scale, iMenuFont)<=*maxX)
&& psOut < &sTemp[sizeof(sTemp)-1] // sanity
)
{
Expand Down
9 changes: 6 additions & 3 deletions code/game/q_shared.h
Expand Up @@ -2071,10 +2071,13 @@ typedef enum


enum {
FONT_NONE,
FONT_SMALL=1,
FONT_NONE = 0,
FONT_SMALL,
FONT_MEDIUM,
FONT_LARGE
FONT_LARGE,
FONT_SMALL_HIRES,
FONT_MEDIUM_HIRES,
FONT_LARGE_HIRES,
};


Expand Down
1 change: 1 addition & 0 deletions code/ui/ui_local.h
Expand Up @@ -112,6 +112,7 @@ extern vmCvar_t ui_smallFont;
extern vmCvar_t ui_bigFont;
extern vmCvar_t ui_serverStatusTimeOut;

extern vmCvar_t ui_fontSharpness;


//
Expand Down

0 comments on commit 591b1cf

Please sign in to comment.