Jump to content
  • Advertisement
Sign in to follow this  

[SDL] Blitting one surface to another

This topic is 4536 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

My current game is being made in C++ with OpenGL using SDL to create the window, input, etc. I also am using SDL_ttf for rendering text. I am working on making my font render split text up into multiple lines if it goes out of the text box in my GUI. I've already got the part that breaks one long string into each line working. Which leaves me with an std::vector<std::string> with all the strings in it. My problem is that when I render these text to surfaces, and try to blit them onto a 'master' img(and the final one that I will draw to the screen), nothing happens. I get no error from SDL_GetError() and SDL_BlitSurface() returns 0. I have never actually blitted from one surface to another, just always blitted from one surface to the screen, but that should be the same, at least I thought. Here's the relevant code:
	textIMG = SDL_CreateRGBSurface(SDL_SWSURFACE,width,lines.size()*fontSize,32,0xff000000,0x00ff0000,0x0000ff00,0x000000ff);


        //Loops through all the std::strings in my vecotor that contains all the lines
	for(int i = 0; i < lines.size(); i++)
		//Create a temporary img and render the text to it
		SDL_Surface *img = TTF_RenderText_Blended(font, lines.c_str(), color);

		SDL_Rect rect;
		SDL_SaveBMP(img,"img.bmp");//For debuging purposes
		SDL_BlitSurface(img,NULL,textIMG,&rect);//Draw it to the main img



	SDL_SaveBMP(textIMG,"test.bmp");//To help debug
test.bmp is completely, totally black when I check it. It should have all the lines of text blitted onto it. img.bmp has the text correctly drawn to it. So, that leaves the problem to SDL_BlitSurface(). Any suggestions as to what to do? Thanks in advanced!

Share this post

Link to post
Share on other sites
Get rid of the UnLock/LockSurface stuff for textIMG, it should already be unlocked when it comes out of CreateRGBSurface()

Note that ConvertSurface() returns the newly converted surface, so it goes like this:
SDL_Surface* newimg = SDL_ConvertSurface(img,textIMG->format,SDL_SWSURFACE);
;//Oh crap, handle error here
img = newimg

Setting w/h doesn't hurt either
rect.w = img->clip_rect.w;
rect.h = img->clip_rect.h;

Finally, 32bit surface alpha mask may be messed up or have side effects.
Try a 24bit surface to check if this is the case. If this is the case then see SDL_SetAlpha() docs to correct.
surf = SDL_CreateRGBSurface(SDL_SWSURFACE,width,lines.size()*fontSize,24,0xFF0000,0x00FF00,0x0000FF,0x000000);

Good Luck.

Share this post

Link to post
Share on other sites
Many thanks!

I eventually got it with your help, and some geussing. GDNet member pointed me in the way of SDL_FillRect(), which made it work.

Again, thanks.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!