Changeset 3458 in orxonox.OLD for orxonox/trunk/src/sfont
- Timestamp:
- Mar 5, 2005, 11:49:35 PM (20 years ago)
- Location:
- orxonox/trunk/src/sfont
- Files:
-
- 1 added
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/sfont/fontset.h
r3456 r3458 9 9 loaded by orxonox Texture-class 10 10 */ 11 12 #ifndef _FONTSET_H 13 #define _FONTSET_H 11 14 12 15 class Texture; … … 45 48 46 49 }; 50 51 52 #endif /* _FONTSET_H */ -
orxonox/trunk/src/sfont/glfont.cc
r3457 r3458 44 44 */ 45 45 46 /* $Id: glfont.c,v 1.6 2004/01/04 17:41:55 slouken Exp $ */ 47 48 /* A simple program to test the text rendering feature of the TTF library */ 46 #include "glfont.h" 47 48 49 49 50 50 #include <stdlib.h> … … 56 56 57 57 #include "../glincl.h" 58 #include "../debug.h" 59 58 60 59 61 #define DEFAULT_PTSIZE 18 … … 61 63 #define NUM_COLORS 256 62 64 63 static char *Usage = 64 "Usage: %s [-utf8|-unicode] [-b] [-i] [-u] [-fgcol r,g,b] [-bgcol r,g,b] \ 65 <font>.ttf [ptsize] [text]\n"; 66 65 66 67 GLFont::GLFont() 68 { 69 this->init(""); 70 71 } 72 GLFont::GLFont(const char* fontFile) 73 { 74 this->init(fontFile); 75 } 76 GLFont::~GLFont() 77 { 78 79 } 80 81 bool GLFont::init(const char* fontFile) 82 { 83 // setting default values. 84 renderStyle = TTF_STYLE_NORMAL; 85 86 87 // Initializing lib-TTF if necesary. 88 if (TTF_WasInit() && TTF_Init() < 0 ) 89 { 90 PRINTF(1)("Couldn't initialize TTF: %s\n", SDL_GetError()); 91 return false; 92 } 93 } 94 95 bool GLFont::ttfInitialized = false; 96 97 bool GLFont::setFont(const char* fonFile) 98 { 99 100 } 101 void GLFont::setText(const char* text) 102 { 103 104 } 105 106 void GLFont::setStyle(char* style) 107 { 108 renderStyle = TTF_STYLE_NORMAL; 109 110 for (int i = 0; i < strlen(style); i++) 111 if (strncmp(style+i, "b", 1) == 0) 112 this->renderStyle |= TTF_STYLE_BOLD; 113 else if (strncmp(style+i, "i", 1) == 0) 114 this->renderStyle |= TTF_STYLE_ITALIC; 115 else if (strncmp(style+i, "u", 1) == 0) 116 this->renderStyle |= TTF_STYLE_UNDERLINE; 117 118 // TTF_SetFontStyle(font, renderstyle); 119 120 } 121 void GLFont::setSize(void) 122 { 123 124 } 125 void GLFont::setPosition(int x, int y) 126 { 127 128 } 129 130 void GLFont::renderText(void) 131 { 132 133 } 134 void GLFont::renderText(const char* text, int x, int y) 135 { 136 // enter the 2D-Mode 137 138 SDL_Surface *screen = SDL_GetVideoSurface(); 139 140 /* Note, there may be other things you need to change, 141 depending on how you have your OpenGL state set up. 142 */ 143 glPushAttrib(GL_ENABLE_BIT); 144 glDisable(GL_DEPTH_TEST); 145 glDisable(GL_CULL_FACE); 146 glEnable(GL_TEXTURE_2D); 147 148 /* This allows alpha blending of 2D textures with the scene */ 149 glEnable(GL_BLEND); 150 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 151 152 glViewport(0, 0, screen->w, screen->h); 153 154 glMatrixMode(GL_PROJECTION); 155 glPushMatrix(); 156 glLoadIdentity(); 157 158 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); 159 160 glMatrixMode(GL_MODELVIEW); 161 glPushMatrix(); 162 glLoadIdentity(); 163 164 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 165 166 167 168 169 170 // Leave the 2D-Mode 171 glMatrixMode(GL_MODELVIEW); 172 glPopMatrix(); 173 174 glMatrixMode(GL_PROJECTION); 175 glPopMatrix(); 176 177 glPopAttrib(); 178 } 179 180 181 bool GLFont::loadTexture() 182 { 183 int w, h; 184 SDL_Surface *image; 185 SDL_Rect area; 186 Uint32 saved_flags; 187 Uint8 saved_alpha; 188 189 /* Use the surface width and height expanded to powers of 2 */ 190 w = powerOfTwo(surface->w); 191 h = powerOfTwo(surface->h); 192 texcoord[0] = 0.0f; /* Min X */ 193 texcoord[1] = 0.0f; /* Min Y */ 194 texcoord[2] = (GLfloat)surface->w / w; /* Max X */ 195 texcoord[3] = (GLfloat)surface->h / h; /* Max Y */ 196 197 image = SDL_CreateRGBSurface( 198 SDL_SWSURFACE, 199 w, h, 200 32, 201 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 202 0x000000FF, 203 0x0000FF00, 204 0x00FF0000, 205 0xFF000000 206 #else 207 0xFF000000, 208 0x00FF0000, 209 0x0000FF00, 210 0x000000FF 211 #endif 212 ); 213 if ( image == NULL ) { 214 return 0; 215 } 216 217 /* Save the alpha blending attributes */ 218 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); 219 saved_alpha = surface->format->alpha; 220 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 221 SDL_SetAlpha(surface, 0, 0); 222 } 223 224 /* Copy the surface into the GL texture image */ 225 area.x = 0; 226 area.y = 0; 227 area.w = surface->w; 228 area.h = surface->h; 229 SDL_BlitSurface(surface, &area, image, &area); 230 231 /* Restore the alpha blending attributes */ 232 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 233 SDL_SetAlpha(surface, saved_flags, saved_alpha); 234 } 235 236 /* Create an OpenGL texture for the image */ 237 glGenTextures(1, &texture); 238 glBindTexture(GL_TEXTURE_2D, texture); 239 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 241 glTexImage2D(GL_TEXTURE_2D, 242 0, 243 GL_RGBA, 244 w, h, 245 0, 246 GL_RGBA, 247 GL_UNSIGNED_BYTE, 248 image->pixels); 249 SDL_FreeSurface(image); /* No longer needed */ 250 251 // return texture; 252 } 253 254 /* Quick utility function for texture creation */ 255 int GLFont::powerOfTwo(int input) 256 { 257 int value = 1; 258 259 while ( value < input ) { 260 value <<= 1; 261 } 262 return value; 263 } 264 265 266 /* 267 private: 268 char* fontFile; 269 char* text; 270 int positionX; 271 int positionY; 272 */ 273 274 275 276 277 char Usage[100] = "orxonox test" ; 67 278 void SDL_GL_Enter2DMode() 68 279 { 69 SDL_Surface *screen = SDL_GetVideoSurface();70 71 /* Note, there may be other things you need to change,72 depending on how you have your OpenGL state set up.73 */74 glPushAttrib(GL_ENABLE_BIT);75 glDisable(GL_DEPTH_TEST);76 glDisable(GL_CULL_FACE);77 glEnable(GL_TEXTURE_2D);78 79 /* This allows alpha blending of 2D textures with the scene */80 glEnable(GL_BLEND);81 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);82 83 glViewport(0, 0, screen->w, screen->h);84 85 glMatrixMode(GL_PROJECTION);86 glPushMatrix();87 glLoadIdentity();88 89 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);90 91 glMatrixMode(GL_MODELVIEW);92 glPushMatrix();93 glLoadIdentity();94 95 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);96 280 } 97 281 98 282 void SDL_GL_Leave2DMode() 99 283 { 100 glMatrixMode(GL_MODELVIEW);101 glPopMatrix();102 103 glMatrixMode(GL_PROJECTION);104 glPopMatrix();105 106 glPopAttrib();107 284 } 108 285 … … 110 287 static int power_of_two(int input) 111 288 { 112 int value = 1; 113 114 while ( value < input ) { 115 value <<= 1; 116 } 117 return value; 118 } 289 } 290 291 119 292 120 293 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord) 121 294 { 122 GLuint texture; 123 int w, h; 124 SDL_Surface *image; 125 SDL_Rect area; 126 Uint32 saved_flags; 127 Uint8 saved_alpha; 128 129 /* Use the surface width and height expanded to powers of 2 */ 130 w = power_of_two(surface->w); 131 h = power_of_two(surface->h); 132 texcoord[0] = 0.0f; /* Min X */ 133 texcoord[1] = 0.0f; /* Min Y */ 134 texcoord[2] = (GLfloat)surface->w / w; /* Max X */ 135 texcoord[3] = (GLfloat)surface->h / h; /* Max Y */ 136 137 image = SDL_CreateRGBSurface( 138 SDL_SWSURFACE, 139 w, h, 140 32, 141 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 142 0x000000FF, 143 0x0000FF00, 144 0x00FF0000, 145 0xFF000000 146 #else 147 0xFF000000, 148 0x00FF0000, 149 0x0000FF00, 150 0x000000FF 151 #endif 152 ); 153 if ( image == NULL ) { 154 return 0; 155 } 156 157 /* Save the alpha blending attributes */ 158 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); 159 saved_alpha = surface->format->alpha; 160 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 161 SDL_SetAlpha(surface, 0, 0); 162 } 163 164 /* Copy the surface into the GL texture image */ 165 area.x = 0; 166 area.y = 0; 167 area.w = surface->w; 168 area.h = surface->h; 169 SDL_BlitSurface(surface, &area, image, &area); 170 171 /* Restore the alpha blending attributes */ 172 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 173 SDL_SetAlpha(surface, saved_flags, saved_alpha); 174 } 175 176 /* Create an OpenGL texture for the image */ 177 glGenTextures(1, &texture); 178 glBindTexture(GL_TEXTURE_2D, texture); 179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 181 glTexImage2D(GL_TEXTURE_2D, 182 0, 183 GL_RGBA, 184 w, h, 185 0, 186 GL_RGBA, 187 GL_UNSIGNED_BYTE, 188 image->pixels); 189 SDL_FreeSurface(image); /* No longer needed */ 190 191 return texture; 295 192 296 } 193 297 … … 223 327 dump = 0; 224 328 /* Look for special rendering types */ 225 renderstyle = TTF_STYLE_NORMAL;226 329 rendertype = RENDER_LATIN1; 227 330 /* Default is black and white */ … … 311 414 return(2); 312 415 } 313 TTF_SetFontStyle(font, renderstyle);314 416 315 417 if( dump ) {
Note: See TracChangeset
for help on using the changeset viewer.