Skip to content

Commit

Permalink
added TTF font support (currently works wrong)
Browse files Browse the repository at this point in the history
  • Loading branch information
entdark committed Aug 7, 2014
1 parent b1994cc commit 87631b3
Show file tree
Hide file tree
Showing 14 changed files with 701 additions and 20 deletions.
34 changes: 17 additions & 17 deletions CODE-mp/cgame/cg_draw.c
Expand Up @@ -2489,9 +2489,9 @@ static void CG_DrawCenterString( void ) {
char *start;//, s[1024];
int l;
int x, y, w;
int h;
int h;
float *color;
const float scale = 1.0; //0.5
float scale = 1.0; //0.5
qboolean broke = qfalse;

if ( !cg.centerPrintTime ) {
Expand Down Expand Up @@ -2524,7 +2524,10 @@ static void CG_DrawCenterString( void ) {
while (*start && *start == ' ') start++;

y = cg.centerPrintY - cg.centerPrintLines * BIGCHAR_HEIGHT / 2;

if ( cgs.textFontValid ) {
scale *= 0.5;
y += scale * BIGCHAR_HEIGHT;
}
while ( 1 ) {
char linebuffer[1024];//, linebufferFix[1024];

Expand All @@ -2542,21 +2545,18 @@ static void CG_DrawCenterString( void ) {
}
linebuffer[l] = 0;

/* for ( l = 0; l < 50; l++ ) {
if ( !s[l] || s[l] == '\n' ) {
break;
}
linebufferFix[l] = s[l];
if ( cgs.textFontValid ) {
w = CG_Text_Width2( linebuffer, scale, 0 );
x = ( SCREEN_WIDTH - w ) / 2;
CG_Text_Paint2( x, y, scale, color, linebuffer, qtrue );
y += 1.3 * CG_Text_Height2( linebuffer, scale, 0 );;
} else {
w = CG_Text_Width(linebuffer, scale, FONT_MEDIUM);
h = CG_Text_Height(linebuffer, scale, FONT_MEDIUM);
x = (SCREEN_WIDTH - w) / 2;
CG_Text_Paint(x, y + h, scale, color, linebuffer, 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE, FONT_MEDIUM);
y += h + 6;
}
linebufferFix[l] = 0;*/

w = CG_Text_Width(linebuffer, scale, FONT_MEDIUM);
h = CG_Text_Height(linebuffer, scale, FONT_MEDIUM);
// w = CG_Text_Width(linebufferFix, scale, FONT_MEDIUM);
// h = CG_Text_Height(linebufferFix, scale, FONT_MEDIUM);
x = (SCREEN_WIDTH - w) / 2;
CG_Text_Paint(x, y + h, scale, color, linebuffer, 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE, FONT_MEDIUM);
y += h + 6;

while ( *start && ( *start != '\n' ) ) {
start++;
Expand Down
134 changes: 134 additions & 0 deletions CODE-mp/cgame/cg_drawtools.c
Expand Up @@ -198,6 +198,140 @@ void CG_DrawChar( int x, int y, int width, int height, int ch ) {

}


/* True Type functions */

int CG_Text_Width2(const char *text, float scale, int limit) {
int count,len;
float out;
const mmeGlyphInfo_t *glyph;
float useScale;
const char *s = text;
const mmeFontInfo_t *font = &cgs.textFont;
useScale = scale * font->glyphScale;
out = 0;
if (text) {
len = strlen(text);
if (limit > 0 && len > limit) {
len = limit;
}
count = 0;
while (s && *s && count < len) {
if ( ( demo15detected && cg.ntModDetected && Q_IsColorStringNT( s ) ) || Q_IsColorString( s ) ) {
s += 2;
continue;
} else {
glyph = &font->glyphs[(int)*s];
out += glyph->xSkip;
s++;
count++;
}
}
}
return out * useScale;
}

int CG_Text_Height2(const char *text, float scale, int limit) {
int len, count;
float max;
mmeGlyphInfo_t *glyph;
float useScale;
const char *s = text;
mmeFontInfo_t *font = &cgs.textFont;
useScale = scale * font->glyphScale;
max = 0;
if (text) {
len = strlen(text);
if (limit > 0 && len > limit) {
len = limit;
}
count = 0;
while (s && *s && count < len) {
if ( ( demo15detected && cg.ntModDetected && Q_IsColorStringNT( s ) ) || Q_IsColorString( s ) ) {
s += 2;
continue;
} else {
glyph = &font->glyphs[(int)*s];

if (max < glyph->height) {
max = glyph->height;
}
s++;
count++;
}
}
}
return max * useScale;
}

static void CG_Text_PaintChar(float x, float y, float width, float height, float scale, float s, float t, float s2, float t2, qhandle_t hShader) {
float w, h;
w = width * scale;
h = height * scale;
trap_R_DrawStretchPic( x, y, w, h, s, t, s2, t2, hShader );
}

void CG_Text_Paint2(float x, float y, float scale, vec4_t color, const char *text, qboolean shadowed ) {
int len, count;
vec4_t newColor;
mmeGlyphInfo_t *glyph;
float useScale;
mmeFontInfo_t *font = &cgs.textFont;
useScale = scale * font->glyphScale;
if (text) {
const char *s = text;
trap_R_SetColor( color );
memcpy(&newColor[0], &color[0], sizeof(vec4_t));
len = strlen(text);
count = 0;
while (s && *s && count < len) {
int colorLen;
glyph = &font->glyphs[(int)*s]; // TTimo: FIXME: getting nasty warnings without the cast, hopefully this doesn't break the VM build
//int yadj = Assets.textFont.glyphs[text[i]].bottom + Assets.textFont.glyphs[text[i]].top;
//float yadj = scale * (Assets.textFont.glyphs[text[i]].imageHeight - Assets.textFont.glyphs[text[i]].height);
if ( demo15detected && cg.ntModDetected && Q_IsColorStringNT( s ) ) {
memcpy( newColor, g_color_table_nt[ColorIndexNT(*(s+1))], sizeof( newColor ) );
newColor[3] = color[3];
trap_R_SetColor( newColor );
s += 2;
continue;
} else if ( Q_IsColorString( s ) ) {
memcpy( newColor, g_color_table[ColorIndex(*(s+1))], sizeof( newColor ) );
newColor[3] = color[3];
trap_R_SetColor( newColor );
s += 2;
continue;
} else {
float yadj = useScale * glyph->top;
if ( shadowed ) {
int ofs = 2;
colorBlack[3] = newColor[3];
trap_R_SetColor( colorBlack );
CG_Text_PaintChar(x + ofs, y - yadj + ofs,
glyph->imageWidth,
glyph->imageHeight,
useScale,
glyph->s,
glyph->t,
glyph->s2,
glyph->t2,
glyph->glyph);
colorBlack[3] = 1.0;
trap_R_SetColor( newColor );
}
CG_Text_PaintChar(x, y - yadj,
glyph->imageWidth, glyph->imageHeight,
useScale, glyph->s, glyph->t, glyph->s2, glyph->t2, glyph->glyph);
x += (glyph->xSkip * useScale);
s++;
count++;
}
}
trap_R_SetColor( NULL );
}
}


/*
==================
CG_DrawStringExt
Expand Down
8 changes: 7 additions & 1 deletion CODE-mp/cgame/cg_local.h
Expand Up @@ -1552,6 +1552,9 @@ Ghoul2 Insert End

// effects
cgEffects_t effects;

mmeFontInfo_t textFont;
qboolean textFontValid;

float widthRatioCoef; //to make 2Ds be not stretched
} cgs_t;
Expand Down Expand Up @@ -1834,6 +1837,10 @@ void CG_DrawRotatePic2( float x, float y, float width, float height,float angle,
void CG_DrawString( float x, float y, const char *string,
float charWidth, float charHeight, const float *modulate );

int CG_Text_Width2(const char *text, float scale, int limit);
int CG_Text_Height2(const char *text, float scale, int limit);
void CG_Text_Paint2(float x, float y, float scale, vec4_t color, const char *text, qboolean shadowed );

void CG_DrawNumField (float x, float y, int width, float value,float charWidth,float charHeight,int style,qboolean zeroFill);

void CG_DrawStringExt( int x, int y, const char *string, const float *setColor,
Expand Down Expand Up @@ -1891,7 +1898,6 @@ void CG_GetTeamColor(vec4_t *color);
const char *CG_GetGameStatusText(void);
const char *CG_GetKillerText(void);
void CG_Draw3DModel( float x, float y, float w, float h, qhandle_t model, qhandle_t skin, vec3_t origin, vec3_t angles );
void CG_Text_PaintChar(float x, float y, float width, float height, float scale, float s, float t, float s2, float t2, qhandle_t hShader);
void CG_CheckOrderPending(void);
const char *CG_GameTypeString(void);
qboolean CG_YourTeamHasFlag(void);
Expand Down
7 changes: 7 additions & 0 deletions CODE-mp/cgame/cg_main.c
Expand Up @@ -530,6 +530,8 @@ vmCvar_t mov_seekInterval;
vmCvar_t mov_musicFile;
vmCvar_t mov_musicStart;
vmCvar_t mov_chaseRange;
vmCvar_t mov_fontName;
vmCvar_t mov_fontSize;

vmCvar_t mov_deltaYaw;
vmCvar_t mov_deltaPitch;
Expand Down Expand Up @@ -2542,6 +2544,7 @@ Called after every level change or subsystem restart
Will perform callbacks to make the loading info screen update.
=================
*/
extern void trap_MME_RegisterFont(const char *fontName, int pointSize, mmeFontInfo_t *font);
extern qboolean trap_MME_Demo15Detection( void );
void CG_Init( int serverMessageNum, int serverCommandSequence, int clientNum ) {
const char *s;
Expand Down Expand Up @@ -2704,6 +2707,10 @@ Ghoul2 Insert End


CG_RegisterCvars();
//Load the fonts
cgs.textFont.glyphScale = 0;
trap_MME_RegisterFont( mov_fontName.string, mov_fontSize.integer, &cgs.textFont );
cgs.textFontValid = cgs.textFont.glyphScale > 0;

CG_InitConsoleCommands();

Expand Down
1 change: 1 addition & 0 deletions CODE-mp/cgame/cg_public.h
Expand Up @@ -245,6 +245,7 @@ Ghoul2 Insert End
CG_MME_SEEKTIME,
CG_MME_DEMOINFO,
CG_MME_MUSIC,
CG_MME_FONT,
CG_MME_FONTRATIOFIX,
CG_MME_DEMO15DETECTION,
CG_MME_NTDETECTION,
Expand Down
3 changes: 3 additions & 0 deletions CODE-mp/cgame/cg_syscalls.c
Expand Up @@ -902,6 +902,9 @@ int trap_MME_SeekTime( int seekTime ) {
void trap_MME_Music( const char *musicName, float time, float length ) {
syscall( CG_MME_MUSIC, musicName, PASSFLOAT(time), PASSFLOAT(length) );
}
void trap_MME_RegisterFont(const char *fontName, int pointSize, mmeFontInfo_t *font) {
syscall( CG_MME_FONT, fontName, pointSize, font );
}
void trap_MME_FontRatioFix( float ratio ) {
syscall( CG_MME_FONTRATIOFIX, PASSFLOAT(ratio) );
}
Expand Down
3 changes: 3 additions & 0 deletions CODE-mp/client/cl_cgame.cpp
Expand Up @@ -1178,6 +1178,9 @@ Ghoul2 Insert End
case CG_MME_MUSIC:
S_MMEMusic( (const char *)VMA(1), VMF(2), VMF(3) );
return 0;
case CG_MME_FONT:
re.MMERegisterFont( (const char *)VMA(1), args[2], (mmeFontInfo_t *)VMA(3));
return 0;
case CG_MME_FONTRATIOFIX:
re.FontRatioFix(VMF(1));
return 0;
Expand Down
26 changes: 26 additions & 0 deletions CODE-mp/game/q_shared.h
Expand Up @@ -2050,6 +2050,32 @@ typedef enum {
CA_CINEMATIC // playing a cinematic or a static pic, not connected to a server
} connstate_t;

// font support

#define GLYPH_START 0
#define GLYPH_END 255
#define GLYPHS_PER_FONT GLYPH_END - GLYPH_START + 1
typedef struct {
int height; // number of scan lines
int top; // top of glyph in buffer
int bottom; // bottom of glyph in buffer
int pitch; // width for copying
int xSkip; // x adjustment
int imageWidth; // width of actual image
int imageHeight; // height of actual image
float s; // x offset in image where glyph starts
float t; // y offset in image where glyph starts
float s2;
float t2;
qhandle_t glyph; // handle to the shader with the glyph
char shaderName[32];
} mmeGlyphInfo_t;

typedef struct {
mmeGlyphInfo_t glyphs [GLYPHS_PER_FONT];
float glyphScale;
char name[MAX_QPATH];
} mmeFontInfo_t;

#define Square(x) ((x)*(x))

Expand Down
1 change: 1 addition & 0 deletions CODE-mp/jk2mp.vcxproj
Expand Up @@ -270,6 +270,7 @@
<ClCompile Include="renderer\tr_mme.cpp" />
<ClCompile Include="renderer\tr_mme_avi.cpp" />
<ClCompile Include="renderer\tr_mme_common.cpp" />
<ClCompile Include="renderer\tr_mme_font.cpp" />
<ClCompile Include="renderer\tr_mme_sse2.cpp" />
<ClCompile Include="renderer\tr_mme_stereo.cpp" />
<ClCompile Include="renderer\tr_model.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions CODE-mp/jk2mp.vcxproj.filters
Expand Up @@ -665,6 +665,9 @@
<ClCompile Include="client\snd_main.cpp">
<Filter>Sound</Filter>
</ClCompile>
<ClCompile Include="renderer\tr_mme_font.cpp">
<Filter>Renderer</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="client\client.h">
Expand Down
3 changes: 1 addition & 2 deletions CODE-mp/qcommon/tags.h
Expand Up @@ -33,8 +33,7 @@
TAGDEF(MODEL_GLA), // "
TAGDEF(ICARUS), // Memory used internally by the Icarus scripting system
TAGDEF(SHADERTEXT),
TAGDEF(SND_MP3STREAMHDR), // specific MP3 struct for decoding (about 18..22K each?), not the actual MP3 binary
TAGDEF(SND_DYNAMICMUSIC), // in-mem MP3 files
TAGDEF(FONT),
TAGDEF(SND_RAWDATA), // raw sound data, either MP3 or WAV
TAGDEF(TEMP_WORKSPACE), // anything like file loading or image workspace that's only temporary
TAGDEF(TEXTPOOL), // for some special text-pool class thingy
Expand Down
3 changes: 3 additions & 0 deletions CODE-mp/renderer/tr_init.cpp
Expand Up @@ -1132,6 +1132,7 @@ void R_Init( void ) {
R_InitShaders();
R_InitSkins();
R_InitFonts();
R_InitFreeType();
#endif
R_ModelInit();
#ifndef DEDICATED
Expand Down Expand Up @@ -1236,6 +1237,8 @@ void RE_Shutdown( qboolean destroyWindow ) {

R_MME_Shutdown();
R_MME_ShutdownStereo();

R_DoneFreeType();

// shut down platform specific OpenGL stuff
if ( destroyWindow ) {
Expand Down
4 changes: 4 additions & 0 deletions CODE-mp/renderer/tr_local.h
Expand Up @@ -1963,6 +1963,10 @@ void RB_DrawSurfaceSprites( shaderStage_t *stage, shaderCommands_t *input);
int SaveJPG( int quality, int image_width, int image_height, mmeShotType_t image_type, byte *image_buffer, byte *out_buffer, int out_size );
int SaveTGA( int image_compressed, int image_width, int image_height, mmeShotType_t image_type, byte *image_buffer, byte *out_buffer, int out_size );
int SavePNG( int compresslevel, int image_width, int image_height, mmeShotType_t image_type, byte *image_buffer, byte *out_buffer, int out_size );
// font stuff
void R_InitFreeType( void );
void R_DoneFreeType( void );
void R_MME_RegisterFont(const char *fontName, int pointSize, mmeFontInfo_t *font);

void R_MME_Init(void);
void R_MME_InitStereo(void);
Expand Down

0 comments on commit 87631b3

Please sign in to comment.