# text to SDL_surface

This topic is 5173 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

int sdl_interface::load_text ( string output_text ) {

TTF_Font* font;

if(TTF_Init())
cout << "error load font: " << TTF_GetError() << endl;

atexit(TTF_Quit);

if(!(font = TTF_OpenFont("data\\fonts\\comic.ttf", 20)))

SDL_Surface *initial;
SDL_Surface *intermediary;
SDL_Surface *intermediary2;
SDL_Rect rect;
SDL_Rect location;
int w;
int h;
GLuint texture;

/* Use SDL_TTF to render our text */
location.x = 0;
location.y = 0;

SDL_Color color;
color.r = 128;
color.g = 255;
color.b = 255;

initial = TTF_RenderText_Blended(font, "TESTING THE TEXT", color);
if(initial == NULL)
cout << "Error crating text" << TTF_GetError() << endl;

intermediary2 = SDL_CreateRGBSurface(SDL_SWSURFACE, 256, 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
intermediary = SDL_DisplayFormatAlpha(initial);

if(intermediary == NULL)
cout << "Error: Creating surface" << endl;

if(intermediary2 == NULL)
cout << "Error: Creating surface2" << endl;

if(SDL_BlitSurface(initial, 0, intermediary, 0) == -1)
cout << "Error: Bliting surface" << endl;

if(SDL_BlitSurface(initial, 0, intermediary2, 0) == -1)
cout << "Error: Bliting surface" << endl;

SDL_UpdateRect( intermediary2, 0, 0, 0, 0 );

SDL_UpdateRect( intermediary, 0, 0, 0, 0 );

SDL_SaveBMP(initial, "blah0.bmp");
SDL_SaveBMP(intermediary, "blah1.bmp");
SDL_SaveBMP(intermediary2, "blah2.bmp");

Outputs: blah0.bmp and blah1.bmp are perfect, they print 'TESTING THE TEXT' On the other hand blah2.bmp prints a black screen From the above code i came to the conclusion it was something to do with the format of the intermediary2 at creatergbsurface(). If anyone can help, being working on this for yonks.

##### Share on other sites
I had this same problem once. I think it should fix it if you do this sometime before saving the surface to a file:

intermediary2 = SDL_DisplayFormat(intermediary2);

I think that should fix it but if that doesn't it might be something to do with the masks you pass to SDL_CreateSurfaceRGB. They depend on if your computer is big endian or little endian.

##### Share on other sites
O M G it worked, i was reading that twenty minutes before posting that and i didnt try it. Thanks so much.

BTW: That line may cause a memory leak
"Remember to use a different variable for the returned surface, otherwise you have a memory leak, since the original surface isn't freed."
Source: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fDisplayFormat

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26