SBSPSS/source/gfx/font.cpp

394 lines
8.2 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;
2000-09-12 01:41:29 +02:00
void FontBank::print( int _x, int _y, 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;
_y+=m_fontData->charHeight; // origin at top left please...
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);
}
Size=printChar(*_text++,_x,fy)+m_fontData->charGapX;
2000-08-29 21:54:22 +02:00
_x+=Size;
Length-=Size;
}
_y+=(m_fontData->charHeight+m_fontData->charGapY);
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()
{
return m_fontData->charHeight+m_fontData->charGapY;
}
/*----------------------------------------------------------------------
Function:
Purpose: return height of a string when wrapped to print area
Params:
Returns:
---------------------------------------------------------------------- */
2000-09-12 01:41:29 +02:00
int FontBank::getStringHeight( 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)
{
length-=getCharWidth(*_text++)+m_fontData->charGapX;
}
height+=(m_fontData->charHeight+m_fontData->charGapY);
if(*_text=='\n') _text++; // kill newline if there is one ( preserve multiple \n )
while (*_text==' ') _text++; // kill trailing spaces
}
return height;
}
/*----------------------------------------------------------------------
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:
---------------------------------------------------------------------- */
int FontBank::getStringWidth( char * text )
{
2000-10-31 17:43:44 +01:00
return getStrWrapLen( text, m_printArea.w );
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:
---------------------------------------------------------------------- */
2000-09-12 01:41:29 +02:00
int FontBank::getStrWrapLen( 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;
length+=getCharWidth(C)+m_fontData->charGapX;
}
if (length>_maxWidth) length=spaceW;
return length;
}
/*===========================================================================
end */