SBSPSS/source/gfx/font.cpp

492 lines
10 KiB
C++
Raw Normal View History

2000-08-29 21:54:22 +02:00
/*=========================================================================
font.cpp
Author: PKG
Created:
Project:
Purpose:
Copyright (c) 2000
===========================================================================*/
/*----------------------------------------------------------------------
Includes
-------- */
#ifndef __GFX_FONT_H__
#include "font.h"
#endif
#ifndef __VID_HEADER_
#include "system\vid.h"
#endif
#ifndef __PRIM_HEADER__
#include "gfx\prim.h"
#endif
#ifndef __MATHTABLE_HEADER__
#include "utils\mathtab.h"
#endif
#ifndef __LOCALE_TEXTDBASE_H__
#include "locale\textdbase.h"
#endif
/* Std Lib
------- */
/* Local
----- */
/*----------------------------------------------------------------------
Tyepdefs && Defines
------------------- */
/*----------------------------------------------------------------------
Structure defintions
-------------------- */
/*----------------------------------------------------------------------
Function Prototypes
------------------- */
/*----------------------------------------------------------------------
Vars
---- */
2000-11-09 17:58:22 +01:00
int FontBank::s_wobbleValue=0;
2000-08-29 21:54:22 +02:00
/*----------------------------------------------------------------------
Data
---- */
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::initialise( FontData *_fontData )
{
ASSERT( !m_initialised );
m_fontData=_fontData;
m_spriteBank.load( m_fontData->fontFileId );
setOt( DEFAULT_OT );
setColour( DEFAULT_R, DEFAULT_G, DEFAULT_B );
setJustification( (Justification)DEFAULT_JUSTIFICATION );
setPrintArea( 0,0, VidGetScrW(),VidGetScrH() );
setTrans( 0 );
setSMode( 0 );
m_initialised=true;
2000-11-09 17:58:22 +01:00
m_wobble=false;
2000-08-29 21:54:22 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::dump()
{
m_spriteBank.dump();
m_initialised=false;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::print( int _x, int _y, s32 _textId )
{
2000-09-12 01:41:29 +02:00
print(_x,_y,(char*)TranslationDatabase::getString(_textId));
2000-08-29 21:54:22 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-11-09 17:58:22 +01:00
int wstep=50;
int wspeed=100;
int wscale=1000;
2001-05-31 18:27:21 +02:00
void FontBank::print( int _x, int _y, const char *_text )
2000-08-29 21:54:22 +02:00
{
ASSERT( m_initialised );
int Size;
int StartX;
int Length=0;
int RectWidth;
switch(m_justification)
{
case JUST_CENTRE:
RectWidth=m_printArea.w;
break;
default:
RectWidth=m_printArea.w-_x;
break;
}
_x+=m_printArea.x;
_y+=m_printArea.y;
2001-05-31 18:27:21 +02:00
_y+=getCharHeight(); // origin at top left please...
2000-08-29 21:54:22 +02:00
StartX=_x;
while (*_text)
{
Length=getStrWrapLen(_text,RectWidth);
switch (m_justification)
{
case JUST_LEFT:
_x=StartX;
break;
case JUST_RIGHT:
_x=StartX/*+RectWidth*/-Length;
break;
case JUST_CENTRE:
_x=StartX-(Length/2);
break;
}
2000-11-09 17:58:22 +01:00
int fy=_y;
2000-08-29 21:54:22 +02:00
while(*_text && Length>0)
{
2000-11-09 17:58:22 +01:00
if(m_wobble)
{
fy=_y+(msin((s_wobbleValue+(_x*wstep))&4095)/wscale);
}
2001-05-31 18:27:21 +02:00
Size=printChar(*_text++,_x,fy)+getCharGapX();
2000-08-29 21:54:22 +02:00
_x+=Size;
Length-=Size;
}
2001-05-31 18:27:21 +02:00
_y+=(getCharHeight());
2000-08-29 21:54:22 +02:00
if(*_text=='\n') _text++; // kill newline if there is one ( preserve multiple \n )
while (*_text==' ') _text++; // kill trailing spaces
}
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::setColour( u8 _r, u8 _g, u8 _b )
{
m_r = _r;
m_g = _g;
m_b = _b;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::setJustification( Justification _justification )
{
m_justification = _justification;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::setOt( u16 _ot )
{
m_ot = _ot;
2001-05-02 00:40:25 +02:00
m_ot=0; // Sorry!!
2000-08-29 21:54:22 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::setPrintArea( int _x, int _y, int _w, int _h )
{
m_printArea.x=_x;
m_printArea.y=_y;
m_printArea.w=_w;
m_printArea.h=_h;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::setTrans( int _trans )
{
m_trans=_trans;
}
void FontBank::setSMode( int _sMode )
{
m_sMode=_sMode;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-09-12 01:41:29 +02:00
int FontBank::getCharWidth( char _char )
2000-08-29 21:54:22 +02:00
{
int size;
if( _char!=' ' )
{
if( m_fontData->fontTab[_char]==-1 ) _char='X';
size=m_spriteBank.getFrameWidth( m_fontData->fontTab[_char] );
}
else
{
size=m_fontData->spaceWidth;
}
return size;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int FontBank::getCharHeight()
{
2001-05-31 18:27:21 +02:00
return m_fontData->charHeight+getCharGapY();
2000-08-29 21:54:22 +02:00
}
/*----------------------------------------------------------------------
Function:
Purpose: return height of a string when wrapped to print area
Params:
Returns:
---------------------------------------------------------------------- */
2001-05-31 18:27:21 +02:00
int FontBank::getStringHeight( const char *_text )
2000-08-29 21:54:22 +02:00
{
int length=0;
int height=0;
while (*_text)
{
length=getStrWrapLen(_text,m_printArea.w);
while(*_text && length>0)
{
2001-05-31 18:27:21 +02:00
length-=getCharWidth(*_text++)+getCharGapX();
2000-08-29 21:54:22 +02:00
}
2001-05-31 18:27:21 +02:00
height+=(getCharHeight());
2000-08-29 21:54:22 +02:00
if(*_text=='\n') _text++; // kill newline if there is one ( preserve multiple \n )
while (*_text==' ') _text++; // kill trailing spaces
}
return height;
}
2001-05-31 18:27:21 +02:00
int FontBank::getStringHeight( s32 _textId )
{
return getStringHeight( TranslationDatabase::getString(_textId) );
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int FontBank::getCharGapX()
{
return m_fontData->charGapX;
}
int FontBank::getCharGapY()
{
return m_fontData->charGapY;
}
2000-08-29 21:54:22 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-09-12 01:41:29 +02:00
int FontBank::printChar( char _char,int _x,int _y )
2000-08-29 21:54:22 +02:00
{
if (_char!=' ')
{
if( m_fontData->fontTab[_char]==-1 ) _char='X';
POLY_FT4 *Ft4=m_spriteBank.printFT4(m_fontData->fontTab[_char],_x,_y,0,0,m_ot);
setRGB0(Ft4,m_r,m_g,m_b);
setShadeTex(Ft4,0);
Ft4->tpage |= ( m_sMode << 5 );
setSemiTrans( Ft4,m_trans );
setShadeTex( Ft4,0 );
}
return getCharWidth(_char);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2001-05-31 18:27:21 +02:00
int FontBank::getStringWidth( const char * text )
2000-08-29 21:54:22 +02:00
{
2000-10-31 17:43:44 +01:00
return getStrWrapLen( text, m_printArea.w );
2000-08-29 21:54:22 +02:00
}
2001-05-31 18:27:21 +02:00
int FontBank::getStringWidth( s32 _textId )
{
return getStringWidth( TranslationDatabase::getString(_textId) );
}
2000-08-29 21:54:22 +02:00
2000-11-09 17:58:22 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void FontBank::think(int _frames)
{
s_wobbleValue+=(_frames*wspeed)&4095;
}
2000-08-29 21:54:22 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2001-05-31 18:27:21 +02:00
int FontBank::getStrWrapLen( const char *_text,int _maxWidth )
2000-08-29 21:54:22 +02:00
{
2000-09-12 01:41:29 +02:00
int length=0,spaceW;
char C;
2000-08-29 21:54:22 +02:00
spaceW=length+1; // +1 to prevent infinite loop
while (*_text && length<=_maxWidth)
{
C=*_text++;
if (C=='\n') break;
if (C==' ') spaceW=length;
2001-05-31 18:27:21 +02:00
length+=getCharWidth(C)+getCharGapX();
2000-08-29 21:54:22 +02:00
}
if (length>_maxWidth) length=spaceW;
return length;
}
2001-05-31 18:27:21 +02:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
void ScalableFontBank::initialise( FontData *_fontData )
{
FontBank::initialise(_fontData);
setScale(256);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int ScalableFontBank::printChar( char _char,int _x,int _y )
{
if (_char!=' ')
{
if( m_fontData->fontTab[_char]==-1 ) _char='X';
POLY_FT4 *Ft4=m_spriteBank.printFT4Scaled(m_fontData->fontTab[_char],_x,_y,0,0,m_ot,m_fontScale);
setRGB0(Ft4,m_r,m_g,m_b);
setShadeTex(Ft4,0);
Ft4->tpage|=(m_sMode<<5);
setSemiTrans(Ft4,m_trans);
setShadeTex(Ft4,0);
}
return getCharWidth(_char);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int ScalableFontBank::getCharWidth( char _char )
{
return (FontBank::getCharWidth(_char)*m_fontScale)>>8;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int ScalableFontBank::getCharHeight()
{
return (FontBank::getCharHeight()*m_fontScale)>>8;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int ScalableFontBank::getCharGapX()
{
return (m_fontData->charGapX*m_fontScale)>>8;
}
int ScalableFontBank::getCharGapY()
{
return (m_fontData->charGapY*m_fontScale)>>8;
}
2000-08-29 21:54:22 +02:00
/*===========================================================================
end */