Thread: SDL_ttf & OpenGL - text color issue

  1. #1
    Registered User
    Join Date
    Mar 2010

    SDL_ttf & OpenGL - text color issue

    i have a problem trying to get white text to an opengl texture.

    no matter what i try the best i can get is either yellow, cyan or purple/pink text depending on the combination of formats ive try, just always seems to be missing a mask.

    make texture
    ZGLTexture ConvertTTFSurfaceToGLTexture(SDL_Surface *source){
    		ZGLTexture ztex;
    		ztex.size.w = GetPowerOfTwo(source->w);
    		ztex.size.h = GetPowerOfTwo(source->h);
    		uint32 Rmask, Gmask, Bmask, Amask;
    			Rmask = 0xff000000;
    			Gmask = 0x00ff0000;
    			Bmask = 0x0000ff00;
    			Amask = 0x000000ff;
    			Rmask = 0x000000ff;
    			Gmask = 0x0000ff00;
    			Bmask = 0x00ff0000;
    			Amask = 0xff000000;
                    // did have format based on endianess since how some tutorials i been 
                    //reading had it but sperated it to test since didnt display proper
    		GLenum tex_format;
    		//tex_format = GL_RGBA;
    		//tex_format = GL_BGRA;
    		tex_format = GL_ABGR_EXT;
    		SDL_Surface *temp_surf = NULL;
                    // have tried different masks combos with different tex_format but none is right
    		temp_surf = SDL_CreateRGBSurface(0, ztex.size.w, ztex.size.h, 32,
    				Rmask, Gmask, Bmask, Amask);
    	    SDL_BlitSurface(source, 0, temp_surf, 0);
    	    glGenTextures( 1, &ztex.texture );
    	    glBindTexture( GL_TEXTURE_2D, ztex.texture );
    	    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    	    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    	    glTexImage2D(GL_TEXTURE_2D, 0, 4, temp_surf->w, temp_surf->h, 0,
    	    		tex_format, GL_UNSIGNED_BYTE, temp_surf->pixels );
    	    return ztex;
    rendering for now... planned to refine it if i can get it to work right
    void Render(){
    		if (font == NULL)
    		if (text != last_text){
    			SDL_Surface *text_surf = NULL;
    			SDL_Color col;
    			col.r = 255;
    			col.g = 255;
    			col.b = 255;
    			text_surf = TTF_RenderText_Blended(font, text.c_str(), col);
    			glDeleteTextures(1, &ztexture.texture);
    			ztexture = ZImageLoader::loader.TextSurfaceToTexture(text_surf);
    			last_text = text;
                    // draws textured quad which works perfect for pngs
    		ZRender::render.Image(ZRect(pos.x, pos.y, ztexture.size.w, ztexture.size.h), 
                                   ZColor(255, 255, 255, 255), ztexture.texture, false, 0, 1.0, 1.0, false, true, 1);
    how i render images normally and works fine... have tried disabling depth testing but text still messed up

    	void Image(const ZRect &rect,
                       const ZColor &color,
                       const GLuint &texture,
                       bool center_origin,
                        float rotate_angle,
                        float scale_x,
                        float scale_y,
                        bool horizontal_flip,
                        bool vertical_flip,
                        const uint8 &z_index) {
    		if (center_origin)
    			glTranslatef((GLfloat)rect.pos.x, (GLfloat)rect.pos.y, 0.0f);
    		if (horizontal_flip && vertical_flip)
    			glScalef(-scale_x, -scale_y, 1.0f);
    		else if (horizontal_flip && !vertical_flip)
    			glScalef(-scale_x, scale_y, 1.0f);
    		else if (!horizontal_flip && vertical_flip)
    			glScalef(scale_x, -scale_y, 1.0f);
    			glScalef(scale_x, scale_y, 1.0f);
    		glRotatef(rotate_angle, 0.0f, 0.0f, 1.0f);
    		glBindTexture(GL_TEXTURE_2D, texture);
    		GLfloat depth;
    		if (z_index > 10)
    			depth = 1.0f;
    			depth = (GLfloat)z_index/10;
    		GLfloat x, y, w, h;
    		if (center_origin){
    			x = ((GLfloat)rect.size.w/2.0f)*(-1);
    			y = ((GLfloat)rect.size.h/2.0f)*(-1);
    		else {
    			x = 0;
    			y = 0;
    		w = rect.size.w;
    		h = rect.size.h;
    			glTexCoord2i(0, 0);
    			glVertex3f(x, y, depth);
    			glTexCoord2i(1, 0);
    			glVertex3f(x+w, y, depth);
    			glTexCoord2i(1, 1);
    			glVertex3f(x+w, y+h, depth);
    			glTexCoord2i(0, 1);
    			glVertex3f(x, y+h, depth);
    didnt have this problem while building and learning on a more modern graphics adapter so may be thinking its a problem with old hardware
    just curious what/if im doing wrong and any possible way to solve it

    TTF_RenderText_Solid works perfect not sure how im messing up masks or the format if thats the issue

    ok so its not hardware issue since i tested on a decent graphics card...
    would appreciate if anyone had any idea at all why color of text surface would be off or missing the rmask in the above code...
    Last edited by illizit; 11-01-2010 at 04:13 PM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    solved by adding

    SDL_SetAlpha(source, 0, 0);
    to the source surface before blitting it on to the new properly formatted surface

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-02-2010, 01:26 PM
  2. OpenGL - text output
    By gavra in forum C Programming
    Replies: 3
    Last Post: 08-06-2008, 01:22 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. OpenGL and text
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 09-06-2001, 08:01 PM