00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Teddy/TeddyConfig.h"
00026 #include <cmath>
00027 #include "Teddy/Graphics/Font.h"
00028 #include "Teddy/Graphics/Texture.h"
00029 #include "Teddy/Graphics/View.h"
00030 #include "Teddy/SysSupport/EndianIn.h"
00031 #include "Teddy/SysSupport/Messages.h"
00032 using namespace Teddy::Graphics;
00033 using namespace Teddy::Maths;
00034 using namespace Teddy::PhysicalComponents;
00035 using namespace Teddy::SysSupport;
00036
00037
00038 namespace Teddy {
00039 namespace Graphics {
00040
00041
00042 Font Font::dummy_font;
00043 Font *Font::default_font = &Font::dummy_font;
00044 const int Font::MODE_NORMAL = 0;
00045 const int Font::MODE_NO_BLEND = 1;
00046
00047
00048
00049
00050
00051 static float DIV_256 = 1.0f/256.0f;
00052
00053
00055 Font::Font(){
00056 int i;
00057 for( i=0; i<256; i++ ){
00058 tx[i] = Vector2( 255-8, 0 );
00059 }
00060 for( i=0; i<4; i++ ){
00061 tsize[4] = Vector2( 0.0f, 0.0f );
00062 psize[4] = Vector2( 0.0f, 0.0f );
00063 }
00064 pos = Vector2( 0.0f, 0.0f );
00065 texture = NULL;
00066 cw = 0;
00067 ch = 0;
00068 tw = 0.0f;
00069 th = 0.0f;
00070 }
00071
00076 Font::Font( const char *fname, int mode ):mode(mode){
00077 texture = new Texture( "font texture" );
00078
00079 unsigned char array[10][21] = {
00080
00081
00082 "!\"#$%&'()*+,-./01234",
00083 "56789:;<=>?@ABCDEFGH",
00084 "IJKLMNOPQRSTUVWXYZ[\\",
00085 "]^_`abcdefghijklmnop",
00086 "qrstuvwxyz{|}~ ¡¢£¤¥",
00087 "¦§¨©ª«¬®¯°±²³´µ¶·¸¹",
00088 "º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍ",
00089 "ÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàá",
00090 "âãäåæçèéêëìíîïðñòóôõ",
00091 "ö÷øùúûüýþÿ??????????"
00092 };
00093 int row;
00094 int col;
00095 int i;
00096
00097 for( i=0; i<256; i++ ){
00098 tx[i] = Vector2( 255-8, 0 );
00099 }
00100
00101 IntVector2 size(256,256);
00102 EndianIn *s = NULL;
00103 unsigned char *filler = NULL;
00104 unsigned char *data = NULL;
00105 texture->setEnv ( Texture::ENV_MODULATE );
00106 texture->setWrap ( Texture::WRAP_REPEAT, Texture::WRAP_REPEAT );
00107 texture->setFilter( Texture::FILTER_NEAREST );
00108
00109 # if defined( USE_TINY_GL )
00110 mode = MODE_NO_BLEND;
00111 # endif
00112
00113
00114 if( mode==MODE_NORMAL ){
00115
00116 try{
00117 s = new EndianIn( fname );
00118 s->set_bigendian();
00119 filler = data = new unsigned char[256*256];
00120 for(int i=0; i<256*256;i++){
00121 *filler++ = s->read_byte();
00122 }
00123 }catch( ... ){
00124 msg( M_INIT, "Could not find font file %s", fname );
00125 }
00126 texture->putData( data, size, Texture::FORMAT_ALPHA, Texture::TX_BLIT_NO_SCALE );
00127 }else{
00128 try{
00129 s = new EndianIn( fname );
00130 s->set_bigendian();
00131 filler = data = new unsigned char[256*256*3];
00132 for(int i=0; i<256*256;i++){
00133 unsigned char in = s->read_byte();
00134 *filler++ = in;
00135 *filler++ = in;
00136 *filler++ = in;
00137
00138 }
00139 }catch( ... ){
00140 msg( M_INIT, "Could not find font file %s", fname );
00141 }
00142 texture->putData( data, size, Texture::FORMAT_RGB, Texture::TX_BLIT_NO_SCALE );
00143 delete[] data;
00144 }
00145
00146 s->close();
00147 delete s;
00148
00149 texture->doBind();
00150
00151 cw = 8;
00152 ch = 8;
00153 tw = (cw) *DIV_256;
00154 th = (ch) *DIV_256;
00155
00156 for( row=0; row<10; row++ ){
00157 for( col=0; col<20; col++ ){
00158 tx[ array[row][col] ] = Vector2( (1+col*cw)*DIV_256, (row*ch)*DIV_256 );
00159 }
00160 }
00161
00162 # if defined( USE_TINY_GL )
00163 tsize[0] = Vector2( 0.0f, 0.0f );
00164 tsize[1] = Vector2( tw, 0.0f );
00165 tsize[2] = Vector2( tw, th-DIV_256 );
00166 tsize[3] = Vector2( 0.0f, th-DIV_256 );
00167 psize[0] = Vector2( 0, 1 );
00168 psize[1] = Vector2( cw, 1 );
00169 psize[2] = Vector2( cw, ch );
00170 psize[3] = Vector2( 0, ch );
00171 # else
00172 tsize[0] = Vector2( 0.0f, 0.0f );
00173 tsize[1] = Vector2( tw, 0.0f );
00174 tsize[2] = Vector2( tw, th );
00175 tsize[3] = Vector2( 0.0f, th );
00176 psize[0] = Vector2( 0, 0 );
00177 psize[1] = Vector2( cw, 0 );
00178 psize[2] = Vector2( cw, ch );
00179 psize[3] = Vector2( 0, ch );
00180 # endif
00181 }
00182
00183
00191 void Font::drawString( const Vector2 &pos, const char *str, View *v ){
00192 const char *ptr = str;
00193 v->setTexture ( texture );
00194 v->enable ( View::TEXTURE_2D );
00195 v->setPolygonMode ( GL_FILL );
00196 if( mode==MODE_NORMAL ){
00197 v->setBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00198 v->enable ( View::BLEND );
00199 }else{
00200 v->disable ( View::BLEND );
00201 v->color ( Color::WHITE );
00202 }
00203 v->beginQuads();
00204 Vector2 cursor = pos;
00205 while( *ptr != '\0' ){
00206 v->vertex( cursor + psize[0], tx[*ptr] + tsize[0] );
00207 v->vertex( cursor + psize[1], tx[*ptr] + tsize[1] );
00208 v->vertex( cursor + psize[2], tx[*ptr] + tsize[2] );
00209 v->vertex( cursor + psize[3], tx[*ptr] + tsize[3] );
00210 cursor[0] += psize[1][0];
00211 ptr++;
00212 }
00213 v->end();
00214 }
00215
00216
00221 int Font::getWidth(){
00222 return cw;
00223 }
00224
00225
00230 int Font::getHeight(){
00231 return ch;
00232 }
00233
00234
00235 };
00236 };
00237