text to SDL_surface

Started by
1 comment, last by crim 18 years, 8 months ago

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)))
		cout << "Error loading font: " << TTF_GetError() << endl;

	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.
Advertisement
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.
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

This topic is closed to new replies.

Advertisement