void init_GL_Image(GLimage& img, const char* fname) { SDL_Surface* image; GLfloat texcoord[4]; if (fname == NULL || img.texture != 0) return; /* Load the image using SDL_image library */ image = IMG_Load(fname); if (image == NULL) { printf("Image '%s' could not be loaded\n", fname); return; } img.filename = fname; img.width = image->w; img.height = image->h; /* Convert the image into an OpenGL texture */ img.texture = SDL_GL_LoadTexture(image, texcoord); if (!img.texture) printf("Texture from image '%s' could not be loaded\n", fname); img.texw = texcoord[2]; img.texh = texcoord[3]; /* We don't need the original image anymore */ SDL_FreeSurface(image); }
void LoadBitmap( char* filename, int plane, int val ) { #if SDL_USE_OPENGL if ( s_pYGSTexture[plane].active ) { SDL_GL_FreeTexture(&s_pYGSTexture[plane]); } SDL_Surface *surface = IMG_Load(filename); if ( surface != NULL ) { surface = SDL_DisplayFormat(surface); SDL_GL_LoadTexture(surface, &s_pYGSTexture[plane]); SDL_FreeSurface(surface); } #else if ( s_pYGSTexture[plane] ) { SDL_FreeSurface(s_pYGSTexture[plane]); s_pYGSTexture[plane] = NULL; } s_pYGSTexture[plane] = IMG_Load(filename); if ( s_pYGSTexture[plane] != NULL ) { s_pYGSTexture[plane] = SDL_DisplayFormat(s_pYGSTexture[plane]); } #endif }
void GLimage::init() { SDL_Surface* image; GLfloat texcoord[4]; if (filename.empty() || texture != 0) return; /* Load the image using SDL_image library */ image = IMG_Load(filename.c_str()); if (image == NULL) { printf("Image '%s' could not be loaded\n", filename.c_str()); return; } width = image->w; height = image->h; /* Convert the image into an OpenGL texture */ texture = SDL_GL_LoadTexture(image, texcoord); if (!texture) { printf("Texture from image '%s' could not be loaded\n", filename.c_str()); } texw = texcoord[2]; texh = texcoord[3]; /* We don't need the original image anymore */ SDL_FreeSurface(image); }
GLuint Graphic::LoadTexture( const char* src, GLfloat *texcoord, Uint32* width, Uint32* height) { static std::pair<GLuint, TexCoordArray> dat; tex_iter iter = texMap.find(src); GLuint texture; if (iter != texMap.end()) { texture = iter->second.first; memcpy(texcoord, iter->second.second.data, 4 * sizeof(GLfloat)); *width = iter->second.second.w; *height = iter->second.second.h; } else { SDL_Surface *surface = IMG_Load_RW(Storage::GetInstance()->OpenIFile(src), 1); if (!surface) { char buf[256]; snprintf(buf, sizeof(buf), "Load file failed: %s", src); throw Exception(buf); } texture = SDL_GL_LoadTexture(surface, texcoord); if (texture == 0) { throw Exception("failed create texture."); } dat.first = texture; memcpy(dat.second.data, texcoord, 4*sizeof(GLfloat)); dat.second.w = *width = surface->w; dat.second.h = *height = surface->h; texMap[src] = dat; SDL_FreeSurface(surface); } return texture; }
void DrawLogoCursor(void) { static GLfloat texMinX, texMinY; static GLfloat texMaxX, texMaxY; static int w, h; int x, y; if (!cursor_texture) { SDL_Surface *image; GLfloat texcoord[4]; /* Load the image (could use SDL_image library here) */ image = SDL_LoadBMP(LOGO_FILE); if (image == NULL) { return; } w = image->w; h = image->h; /* Convert the image into an OpenGL texture */ cursor_texture = SDL_GL_LoadTexture(image, texcoord); /* Make texture coordinates easy to understand */ texMinX = texcoord[0]; texMinY = texcoord[1]; texMaxX = texcoord[2]; texMaxY = texcoord[3]; /* We don't need the original image anymore */ SDL_FreeSurface(image); /* Make sure that the texture conversion is okay */ if (!cursor_texture) { return; } } /* Move the image around */ SDL_GetMouseState(&x, &y); x -= w / 2; y -= h / 2; /* Show the image on the screen */ SDL_GL_Enter2DMode(); glBindTexture(GL_TEXTURE_2D, cursor_texture); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(texMinX, texMinY); glVertex2i(x, y); glTexCoord2f(texMaxX, texMinY); glVertex2i(x + w, y); glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y + h); glTexCoord2f(texMaxX, texMaxY); glVertex2i(x + w, y + h); glEnd(); SDL_GL_Leave2DMode(); }
//--------------------------------------------------------------------------- bool uv_image::LoadImageFile(string fname) { static map<string,uv_image*> texturindex; map<string,uv_image*>::iterator ite; ite = texturindex.find(fname); if (ite != texturindex.end()) //Dieses font_set wird bereits verwendet... { //sozusagen this=ite->second.... w = ite->second->get_texture_w(); //Laden h = ite->second->get_texture_w(); //Laden texMinX = ite->second->get_texMinX(); texMinY = ite->second->get_texMinY(); texMaxX = ite->second->get_texMaxX(); texMaxY = ite->second->get_texMaxY(); textur = ite->second->get_texture_index(); return 1; } GLuint global_texture = 0; SDL_Surface *image; GLfloat texcoord[4]; // Load the image (could use SDL_image library here) image = IMG_Load(fname.c_str()); if(image == NULL) { cout << "Error: Can't open " << fname <<endl; return false; } w = image->w; h = image->h; // Convert the image into an OpenGL texture global_texture = SDL_GL_LoadTexture(image, texcoord); // We don't need the original image anymore SDL_FreeSurface(image); if( !global_texture) { cout << "Error: Can't convert "<< fname <<endl; return false; } textur = global_texture; // cout << global_texture; texMinX = texcoord[0]; texMinY = texcoord[1]; texMaxX = texcoord[2]; texMaxY = texcoord[3]; texturindex.insert(pair<string,uv_image*>(fname,this)); return true; };
int BitmapLoad(const char* Filename) { int w, h; SDL_Surface *image; GLfloat texcoord[4]; /* Load the image (could use SDL_image library here) */ image = SDL_LoadBMP(Filename); if ( image == NULL ) { return 0; } w = image->w; h = image->h; /* Convert the image into an OpenGL texture */ return SDL_GL_LoadTexture(image, texcoord); }
/* Loads the texture used as the tile bay */ void load_tilebay( char *filename ) { int i=0; SDL_Rect rect; SDL_Surface *surface; /* Try to load the font image */ if ((surface = IMG_Load(filename))) { tile_count=(surface->w/32)*(surface->h/32); tilebay=malloc(sizeof(texture_t)*tile_count); /* Create individual textures in the character array for each character */ for (i=0;i<((surface->w/32)*(surface->h/32));i++) { /* Calculate the pixel location of the individual character */ rect.x = (i%(surface->w/32))*(32); rect.y = (i/(surface->w/32))*(32); rect.w = 32; rect.h = 32; /* Create the texture from the surface using the above coordinates. */ tilebay[i]=SDL_GL_LoadTexture(surface, &rect, 1); } tile_count=i; printf( "Tile count.. %i\n", tile_count ); /* We don't need the surface anymore. Free it */ SDL_FreeSurface(surface); } else { fprintf(stderr, "Could not load texture: '%s'!\n", filename); quit(1); } /* Load the texture using alpha, as it's needed for the foreground layer. */ /*load_texture( temp_tex, filename, USE_ALPHA ); tilebay=malloc(sizeof(texture_t)*(tilebay->width*tilebay->height));*/ }
void DrawLogoTexture(void) { static GLfloat texMinX, texMinY; static GLfloat texMaxX, texMaxY; static int x = 0; static int y = 0; static int w, h; static int delta_x = 1; static int delta_y = 1; SDL_Surface *screen = SDL_GetVideoSurface(); if (!global_texture) { SDL_Surface *image; GLfloat texcoord[4]; /* Load the image (could use SDL_image library here) */ image = SDL_LoadBMP(LOGO_FILE); if (image == NULL) { return; } w = image->w; h = image->h; /* Convert the image into an OpenGL texture */ global_texture = SDL_GL_LoadTexture(image, texcoord); /* Make texture coordinates easy to understand */ texMinX = texcoord[0]; texMinY = texcoord[1]; texMaxX = texcoord[2]; texMaxY = texcoord[3]; /* We don't need the original image anymore */ SDL_FreeSurface(image); /* Make sure that the texture conversion is okay */ if (!global_texture) { return; } } /* Move the image around */ x += delta_x; if (x < 0) { x = 0; delta_x = -delta_x; } else if ((x + w) > screen->w) { x = screen->w - w; delta_x = -delta_x; } y += delta_y; if (y < 0) { y = 0; delta_y = -delta_y; } else if ((y + h) > screen->h) { y = screen->h - h; delta_y = -delta_y; } /* Show the image on the screen */ SDL_GL_Enter2DMode(); glBindTexture(GL_TEXTURE_2D, global_texture); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(texMinX, texMinY); glVertex2i(x, y); glTexCoord2f(texMaxX, texMinY); glVertex2i(x + w, y); glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y + h); glTexCoord2f(texMaxX, texMaxY); glVertex2i(x + w, y + h); glEnd(); SDL_GL_Leave2DMode(); }
/* * create_font */ int create_font(char *filename, FONT **font, int size, int style, SDL_Color color) { TTF_Font* ttffont; SDL_Surface* character_surface; int ret_code = BUILD_FONT_OK; int rc; Uint16 ii; /* * Allocate the memory required for the font object. */ (*font) = (FONT *) DT_MALLOC(sizeof(FONT)); /* * Set the font attributes. */ memcpy(&((*font)->color), &color, sizeof(SDL_Color)); (*font)->pointsize = size; (*font)->style = style; /* * The font loading may fail if the file does not exist or permissions are * incorrect etc. */ ttffont = TTF_OpenFont(filename, (*font)->pointsize); if (ttffont == NULL) { DT_DEBUG_LOG("Error opening font (%s): %s\n", filename, TTF_GetError()); ret_code = BUILD_FONT_LOAD_FAIL; goto EXIT_LABEL; } /* * Set the style of the font to whatever we passed in. */ TTF_SetFontStyle(ttffont, (*font)->style); /* * Retrieve the font information to store in our font structure. */ (*font)->ascent = TTF_FontAscent(ttffont); (*font)->descent = TTF_FontDescent(ttffont); (*font)->height = TTF_FontHeight(ttffont); (*font)->lineskip = TTF_FontLineSkip(ttffont); /* * For each possible glyph, attempt to load the font character and create a * texture for it. If any of these fail we attempt to close down gracefully * and exit the function. */ for (ii = ' '; ii <= '~'; ii++) { character_surface = TTF_RenderGlyph_Blended(ttffont, ii, (*font)->color); if (NULL == character_surface) { DT_DEBUG_LOG("Error rendering glyph %c whilst creating font (%s): %s\n", (char) ii, filename, TTF_GetError()); ret_code = BUILD_FONT_RENDER_FAIL; goto EXIT_LABEL; } /* * Retrieve the metric info from the font object and store it in our local * structure. */ TTF_GlyphMetrics(ttffont, ii, &((*font)->glyphs[ii].minx), &((*font)->glyphs[ii].maxx), &((*font)->glyphs[ii].miny), &((*font)->glyphs[ii].maxy), &((*font)->glyphs[ii].advance)); /* * This function can fail if opengl cannot allocate any more memory for * textures. */ rc = SDL_GL_LoadTexture(character_surface, &((*font)->glyphs[ii].texid), false); if (LOAD_TEXTURE_OK != rc) { DT_DEBUG_LOG("Failed creating texture for glyph %c from font %s\n", (char) ii, TTF_GetError()); SDL_FreeSurface(character_surface); ret_code = BUILD_FONT_CREATE_TEXTURE_FAIL; goto EXIT_LABEL; } /* * Set the texture coordinates for the glyph (note this is the same for all * glyphs). */ (*font)->glyphs[ii].texcoord[0] = 0.0f; (*font)->glyphs[ii].texcoord[1] = 0.0f; (*font)->glyphs[ii].texcoord[2] = 1.0f; (*font)->glyphs[ii].texcoord[3] = 1.0f; /* * The surface used for that character is no longer required. */ SDL_FreeSurface(character_surface); } EXIT_LABEL: if (NULL != ttffont) { TTF_CloseFont(ttffont); } return ret_code; }
int main(int argc, char *argv[]) { char *argv0 = argv[0]; SDL_Window *window; SDL_GLContext context; TTF_Font *font; SDL_Surface *text; int ptsize; int i, done; SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 }; SDL_Color black = { 0x00, 0x00, 0x00, 0 }; SDL_Color *forecol; SDL_Color *backcol; GLenum gl_error; GLuint texture; int x, y, w, h; GLfloat texcoord[4]; GLfloat texMinX, texMinY; GLfloat texMaxX, texMaxY; float color[8][3]= {{ 1.0, 1.0, 0.0}, { 1.0, 0.0, 0.0}, { 0.0, 0.0, 0.0}, { 0.0, 1.0, 0.0}, { 0.0, 1.0, 1.0}, { 1.0, 1.0, 1.0}, { 1.0, 0.0, 1.0}, { 0.0, 0.0, 1.0}}; float cube[8][3]= {{ 0.5, 0.5, -0.5}, { 0.5, -0.5, -0.5}, {-0.5, -0.5, -0.5}, {-0.5, 0.5, -0.5}, {-0.5, 0.5, 0.5}, { 0.5, 0.5, 0.5}, { 0.5, -0.5, 0.5}, {-0.5, -0.5, 0.5}}; SDL_Event event; int renderstyle; int dump; enum { RENDER_LATIN1, RENDER_UTF8, RENDER_UNICODE } rendertype; char *message; /* Look for special execution mode */ dump = 0; /* Look for special rendering types */ renderstyle = TTF_STYLE_NORMAL; rendertype = RENDER_LATIN1; /* Default is black and white */ forecol = &black; backcol = &white; for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) { if ( strcmp(argv[i], "-utf8") == 0 ) { rendertype = RENDER_UTF8; } else if ( strcmp(argv[i], "-unicode") == 0 ) { rendertype = RENDER_UNICODE; } else if ( strcmp(argv[i], "-b") == 0 ) { renderstyle |= TTF_STYLE_BOLD; } else if ( strcmp(argv[i], "-i") == 0 ) { renderstyle |= TTF_STYLE_ITALIC; } else if ( strcmp(argv[i], "-u") == 0 ) { renderstyle |= TTF_STYLE_UNDERLINE; } else if ( strcmp(argv[i], "-dump") == 0 ) { dump = 1; } else if ( strcmp(argv[i], "-fgcol") == 0 ) { int r, g, b; if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) { fprintf(stderr, Usage, argv0); return(1); } forecol->r = (Uint8)r; forecol->g = (Uint8)g; forecol->b = (Uint8)b; } else if ( strcmp(argv[i], "-bgcol") == 0 ) { int r, g, b; if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) { fprintf(stderr, Usage, argv0); return(1); } backcol->r = (Uint8)r; backcol->g = (Uint8)g; backcol->b = (Uint8)b; } else { fprintf(stderr, Usage, argv0); return(1); } } argv += i; argc -= i; /* Check usage */ if ( ! argv[0] ) { fprintf(stderr, Usage, argv0); return(1); } /* Initialize the TTF library */ if ( TTF_Init() < 0 ) { fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError()); SDL_Quit(); return(2); } /* Open the font file with the requested point size */ ptsize = 0; if ( argc > 1 ) { ptsize = atoi(argv[1]); } if ( ptsize == 0 ) { i = 2; ptsize = DEFAULT_PTSIZE; } else { i = 3; } font = TTF_OpenFont(argv[0], ptsize); if ( font == NULL ) { fprintf(stderr, "Couldn't load %d pt font from %s: %s\n", ptsize, argv[0], SDL_GetError()); cleanup(2); } TTF_SetFontStyle(font, renderstyle); if( dump ) { for( i = 48; i < 123; i++ ) { SDL_Surface* glyph = NULL; glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol ); if( glyph ) { char outname[64]; sprintf( outname, "glyph-%d.bmp", i ); SDL_SaveBMP( glyph, outname ); } } cleanup(0); } /* Set a 640x480 video mode */ window = SDL_CreateWindow("glfont", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL); if ( window == NULL ) { fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError()); cleanup(2); } context = SDL_GL_CreateContext(window); if ( context == NULL ) { fprintf(stderr, "Couldn't create OpenGL context: %s\n", SDL_GetError()); cleanup(2); } /* Render and center the message */ if ( argc > 2 ) { message = argv[2]; } else { message = DEFAULT_TEXT; } switch (rendertype) { case RENDER_LATIN1: text = TTF_RenderText_Blended(font, message, *forecol); break; case RENDER_UTF8: text = TTF_RenderUTF8_Blended(font, message, *forecol); break; case RENDER_UNICODE: { /* This doesn't actually work because you can't pass UNICODE text in via command line, AFAIK, but... */ Uint16 unicode_text[BUFSIZ]; int index; for ( index = 0; (message[0] || message[1]); ++index ) { unicode_text[index] = ((Uint8 *)message)[0]; unicode_text[index] <<= 8; unicode_text[index] |= ((Uint8 *)message)[1]; message += 2; } text = TTF_RenderUNICODE_Blended(font, unicode_text, *forecol); } break; default: text = NULL; /* This shouldn't happen */ break; } if ( text == NULL ) { fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError()); TTF_CloseFont(font); cleanup(2); } x = (WIDTH - text->w)/2; y = (HEIGHT - text->h)/2; w = text->w; h = text->h; printf("Font is generally %d big, and string is %hd big\n", TTF_FontHeight(font), text->h); /* Convert the text into an OpenGL texture */ glGetError(); texture = SDL_GL_LoadTexture(text, texcoord); if ( (gl_error = glGetError()) != GL_NO_ERROR ) { /* If this failed, the text may exceed texture size limits */ printf("Warning: Couldn't create texture: 0x%x\n", gl_error); } /* Make texture coordinates easy to understand */ texMinX = texcoord[0]; texMinY = texcoord[1]; texMaxX = texcoord[2]; texMaxY = texcoord[3]; /* We don't need the original text surface anymore */ SDL_FreeSurface(text); /* Initialize the GL state */ glViewport( 0, 0, WIDTH, HEIGHT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glShadeModel(GL_SMOOTH); /* Wait for a keystroke, and blit text on mouse press */ done = 0; while ( ! done ) { while ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_MOUSEMOTION: x = event.motion.x - w/2; y = event.motion.y - h/2; break; case SDL_KEYDOWN: case SDL_QUIT: done = 1; break; default: break; } } /* Clear the screen */ glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Draw the spinning cube */ glBegin( GL_QUADS ); glColor3fv(color[0]); glVertex3fv(cube[0]); glColor3fv(color[1]); glVertex3fv(cube[1]); glColor3fv(color[2]); glVertex3fv(cube[2]); glColor3fv(color[3]); glVertex3fv(cube[3]); glColor3fv(color[3]); glVertex3fv(cube[3]); glColor3fv(color[4]); glVertex3fv(cube[4]); glColor3fv(color[7]); glVertex3fv(cube[7]); glColor3fv(color[2]); glVertex3fv(cube[2]); glColor3fv(color[0]); glVertex3fv(cube[0]); glColor3fv(color[5]); glVertex3fv(cube[5]); glColor3fv(color[6]); glVertex3fv(cube[6]); glColor3fv(color[1]); glVertex3fv(cube[1]); glColor3fv(color[5]); glVertex3fv(cube[5]); glColor3fv(color[4]); glVertex3fv(cube[4]); glColor3fv(color[7]); glVertex3fv(cube[7]); glColor3fv(color[6]); glVertex3fv(cube[6]); glColor3fv(color[5]); glVertex3fv(cube[5]); glColor3fv(color[0]); glVertex3fv(cube[0]); glColor3fv(color[3]); glVertex3fv(cube[3]); glColor3fv(color[4]); glVertex3fv(cube[4]); glColor3fv(color[6]); glVertex3fv(cube[6]); glColor3fv(color[1]); glVertex3fv(cube[1]); glColor3fv(color[2]); glVertex3fv(cube[2]); glColor3fv(color[7]); glVertex3fv(cube[7]); glEnd( ); /* Rotate the cube */ glMatrixMode(GL_MODELVIEW); glRotatef(5.0, 1.0, 1.0, 1.0); /* Show the text on the screen */ SDL_GL_Enter2DMode(WIDTH, HEIGHT); glBindTexture(GL_TEXTURE_2D, texture); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(texMinX, texMinY); glVertex2i(x, y ); glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y ); glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y+h); glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h); glEnd(); SDL_GL_Leave2DMode(); /* Swap the buffers so everything is visible */ SDL_GL_SwapWindow(window); } SDL_GL_DeleteContext(context); TTF_CloseFont(font); cleanup(0); /* Not reached, but fixes compiler warnings */ return 0; }
bool render_text(font_data *ft_font, const char *text, string_tex_t *string_tex) { SDL_Color white = { 0xFF, 0xFF, 0xFF, 0x00 }; SDL_Color *forecol; SDL_Surface *string_glyph = NULL; SDL_Surface *glyph = NULL; SDL_Rect src, dest; GLenum gl_error; if (!(ft_font)) return false; if (!(ft_font->ttffont)) return false; if (!(string_tex)) return false; #if 0 if (!strlen(text)) return false; /* something is printing an empty string each frame */ #else /* kps - fix for empty author field in cannon dodgers */ if (!strlen(text)) text = " "; #endif forecol = &white; string_tex->font_height = ft_font->h; string_glyph = TTF_RenderText_Blended( ft_font->ttffont, text, *forecol ); string_tex->tex_list = Arraylist_alloc(sizeof(tex_t)); string_tex->width = 0; string_tex->height = string_glyph->h; if (string_glyph) { int i, num = 1 + string_glyph->w / 254; string_tex->text = (char *)malloc(sizeof(char)*(strlen(text)+1)); sprintf(string_tex->text,"%s",text); for( i=0 ; i<num ; ++i ) { tex_t tex; tex.texture = 0; tex.texcoords.MinX = 0.0; tex.texcoords.MaxX = 0.0; tex.texcoords.MinY = 0.0; tex.texcoords.MaxY = 0.0; tex.width = 0; src.x = i*254; dest.x = 0; src.y = dest.y = 0; if (i==num-1) dest.w = src.w = string_glyph->w - i*254; else dest.w = src.w = 254; src.h = dest.h = string_glyph->h; glyph = SDL_CreateRGBSurface(0,dest.w,dest.h,32,0,0,0,0); SDL_SetColorKey(glyph, SDL_SRCCOLORKEY, 0x00000000); SDL_BlitSurface(string_glyph,&src,glyph,&dest); glGetError(); tex.texture = SDL_GL_LoadTexture(glyph,&(tex.texcoords)); if ( (gl_error = glGetError()) != GL_NO_ERROR ) printf("Warning: Couldn't create texture: 0x%x\n", gl_error); tex.width = dest.w; string_tex->width += dest.w; SDL_FreeSurface(glyph); Arraylist_add(string_tex->tex_list,&tex); } SDL_FreeSurface(string_glyph); } else { printf("TTF_RenderText_Blended failed for [%s]\n",text); return false; } return true; }
int FTinit(font_data *font, const char * fontname, int ptsize) { int i; SDL_Color white = { 0xFF, 0xFF, 0xFF, 0x00 }; SDL_Color black = { 0x00, 0x00, 0x00, 0 }; SDL_Color *forecol; SDL_Color *backcol; GLenum gl_error; texcoord_t texcoords; int minx = 0,miny = 0,maxx = 0,maxy = 0; /* We might support changing theese later */ /* Look for special rendering types */ renderstyle = TTF_STYLE_NORMAL; rendertype = RENDER_LATIN1; /* Default is black and white */ forecol = &white; backcol = &black; /* Initialize the TTF library */ /*if ( TTF_Init() < 0 ) { fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError()); return(2); }*/ font->ttffont = TTF_OpenFont(fontname, ptsize); if ( font->ttffont == NULL ) { fprintf(stderr, "Couldn't load %d pt font from %s: %s\n", ptsize, fontname, SDL_GetError()); return(2); } TTF_SetFontStyle(font->ttffont, renderstyle); font->list_base=glGenLists(next_p2(NUMCHARS)); /* Get the recommended spacing between lines of text for this font */ font->linespacing = TTF_FontLineSkip(font->ttffont); font->h = ptsize; for( i = 0; i < NUMCHARS; i++ ) { SDL_Surface *glyph = NULL; GLuint height = 0; /* kps - added default value */ forecol = &white; glyph = TTF_RenderGlyph_Blended( font->ttffont, i, *forecol ); if(glyph) { glGetError(); font->textures[i] = SDL_GL_LoadTexture(glyph, &texcoords); if ( (gl_error = glGetError()) != GL_NO_ERROR ) printf("Warning: Couldn't create texture: 0x%x\n", gl_error); font->W[i] = glyph->w; height = glyph->h; TTF_GlyphMetrics( font->ttffont, i, &minx,&maxx,&miny,&maxy,NULL); } SDL_FreeSurface(glyph); glNewList(font->list_base+i,GL_COMPILE); glBindTexture(GL_TEXTURE_2D, font->textures[i]); glTranslatef(1,0,0); glPushMatrix(); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(texcoords.MinX, texcoords.MaxY); glVertex2i(0 , miny); glTexCoord2f(texcoords.MaxX, texcoords.MaxY); glVertex2i(font->W[i] , miny); glTexCoord2f(texcoords.MinX, texcoords.MinY); glVertex2i(0 ,miny+height ); glTexCoord2f(texcoords.MaxX, texcoords.MinY); glVertex2i(font->W[i] , miny+height); glEnd(); glPopMatrix(); glTranslatef((font->W[i]>3)?font->W[i]:(font->W[i] = 3) + 1,0,0); /*one would think this should be += 2... I guess they overlap or the edge * isn't painted */ font->W[i] += 1; glEndList(); } /*TTF_CloseFont(font->ttffont);*/ /*TTF_Quit();*/ return 0; }