Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

r_font.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/18 09:31:53 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: r_font.cpp,v 1.3 2002/12/18 09:31:53 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: r_font.cpp,v $
00030   Revision 1.3  2002/12/18 09:31:53  kolrabi
00031   added string rendering flag SF_RIGHT
00032 
00033   Revision 1.2  2002/12/13 12:29:26  kolrabi
00034   fixed some bugs
00035 
00036   Revision 1.1.1.1  2002/12/07 19:02:04  kolrabi
00037   initial release
00038 
00039 
00040 */
00041 
00046 #include            "omicron/internal.h"
00047 #include            "omicron/render.h"
00048 #include            "omicron/image.h"
00049 #include            "omicron/texture.h"
00050  
00051 /**************************************************************************** 
00052  **************************************************************************** 
00053  * VARIABLES **************************************************************** 
00054  **************************************************************************** 
00055  ****************************************************************************/ 
00056  
00057 /**************************************************************************** 
00058  **************************************************************************** 
00059  * FONT ROUTINES ************************************************************ 
00060  **************************************************************************** 
00061  ****************************************************************************/ 
00062 
00063 font_t *renderer_c::font_createfromimage_fixed(image_c *img, uchar width)
00064 {
00065     AssertThisV;
00066     AssertReturnValue2(img, width, NULL);
00067 
00068     font_t *fnt = new font_t;
00069 
00070     ushort w, h;
00071     ushort cw, ch;
00072 
00073     w = img->get_width();
00074     h = img->get_height();
00075 
00076     fnt->width  = cw = (sshort)(w / 16);
00077     fnt->height = ch = (sshort)(h / 16);
00078 
00079     fnt->texture = gv.texman->create(w, h, "*font");
00080     fnt->shader  = shader_create_simple(BT_BLEND, fnt->texture);
00081     fnt->shader->passes[0].magfilter =
00082     fnt->shader->passes[0].minfilter = TF_LINEAR;
00083     gv.texman->set_image(fnt->texture, img);
00084 
00085     for (ulong i=0; i<256; i++)
00086     {
00087         fnt->images[i] = new image_c;
00088         fnt->images[i]->copy(img);
00089         fnt->images[i]->crop((ushort)((i%16)*cw), (ushort)(i/16)*ch, (ushort)cw, (ushort)ch);
00090         fnt->widths[i] = width;
00091     }
00092 
00093     return fnt;
00094 }
00095 
00096 void renderer_c::font_free(font_t *fnt)
00097 {
00098     AssertThis;
00099 
00100     AssertReturn1(fnt);
00101 
00102     for (ulong i=0; i<256; i++)
00103         SafeDelete(fnt->images[i]);
00104 
00105     SafeDelete(fnt);
00106 }
00107  
00108 /**************************************************************************** 
00109  * _reset_charmode                               setting charmode to normal * 
00110  ****************************************************************************/ 
00111 void renderer_c::reset_charmode() 
00112 { 
00113     AssertThis;
00114 
00115     state.font_bold    = 0; 
00116     state.font_italic  = 0; 
00117     state.font_color   = get_color(); 
00118 } 
00119  
00120  
00121  
00122 /**************************************************************************** 
00123  * _r_draw_char                                                draws a char * 
00124  ****************************************************************************/ 
00125 float renderer_c::draw_char 
00126 ( 
00127     float       x,                  // left and 
00128     float       y,                  // bottom coordinates 
00129     float       size,               // height of character 
00130     char        c,                  // character to draw 
00131     const font_t *fnt
00132 ) 
00133 { 
00134     AssertThisV;
00135 
00136     if (c==32)
00137         return size;
00138 
00139     if (c<32) 
00140     { 
00141         if (c<16) 
00142         { 
00143             switch(c) 
00144             { 
00145                 case    1:  state.font_bold    = !state.font_bold; break; 
00146                 case    2:  state.font_italic  = !state.font_italic; break; 
00147             } 
00148         } 
00149         else 
00150         { 
00151             ulong  intensity  = c&7;
00152             char col        = intensity?0xFF:0xC0;
00153 
00154             state.font_color = get_color()&0xFF000000; 
00155 
00156             for (ulong i=0; i<3; i++)
00157                 state.font_color |= ((c&(1<<i))?col:0)<<(8*i);
00158         } 
00159  
00160         return 0; 
00161     } 
00162  
00163     float w   = state.font_bold  ? 3.0f:0.0f; 
00164 //    float it  = state.font_italic?-3.0f:0.0f; 
00165 
00166     set_color(state.font_color);
00167     draw_pic_shader(x, y, x+w+size, y+size, (c%16)/16.0f, (c/16)/16.0f, (c%16+1)/16.0f, (c/16+1)/16.0f, fnt->shader);
00168 
00169     return size; 
00170 } // _r_draw_char 
00171  
00172  
00173 float renderer_c::get_stringwidth(const char *c, const font_t *fnt)
00174 {
00175     AssertThisV;
00176 
00177     AssertReturnValue2(c,fnt,0);
00178 
00179     ushort ww = 0;
00180 
00181     for (ulong i=0; i<strlen(c); i++)
00182         ww += fnt->widths[c[i]];
00183 
00184     return (float)ww/(float)fnt->width;;
00185 }
00186 
00187 ushort renderer_c::get_string_pixelwidth(const char *c, const font_t *fnt)
00188 {
00189     AssertThisV;
00190 
00191     AssertReturnValue2(c,fnt,0);
00192 
00193     ushort ww = 0;
00194 
00195     for (ulong i=0; i<strlen(c); i++)
00196         ww += fnt->widths[c[i]];
00197 
00198     return ww;
00199 }
00200  
00201 /**************************************************************************** 
00202  * _r_draw_string                                            draws a string * 
00203  ****************************************************************************/ 
00204 void renderer_c::draw_string 
00205 ( 
00206     float       x,                  // left and 
00207     float       y,                  // bottom coordinates 
00208     float       size,               // height of character 
00209     const char   *c,                // string 
00210     const font_t *fnt,
00211     ulong       flags               // flags 
00212 )
00213 {
00214     AssertThis;
00215 
00216     AssertReturn2(c,fnt);
00217 
00218     float       cx = x; 
00219  
00220     if (flags & SF_CENTERED) 
00221         cx = x-get_stringwidth(c,fnt)*0.5f*size; 
00222     else if (flags & SF_RIGHT) 
00223         cx = x-get_stringwidth(c,fnt)*size; 
00224  
00225     reset_charmode(); 
00226 
00227     // save currentcolor
00228     color_t col = get_color(); 
00229 
00230     // draw! 
00231 
00232     while (*c)
00233     { 
00234         draw_char(cx, y, size, *c, fnt); 
00235         cx += (float)fnt->widths[*c]/(float)fnt->width*size;
00236         c++; 
00237     }
00238  
00239     // restore current color
00240     set_color(col); 
00241 } // _r_draw_string 
00242  
00243  
00244  

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18