Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

int sdl_interface::load_text ( string output_text ) {

	TTF_Font* font;

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


	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.

Share this post

Link to post
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 this post

Link to post
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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!