[SDL] Blitting one surface to another

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

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);

SDL_UnlockSurface(textIMG);

//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_ConvertSurface(img,textIMG->format,SDL_SWSURFACE);

SDL_Rect rect;
rect.x=0;
rect.y=fontSize*i;
SDL_SaveBMP(img,"img.bmp");//For debuging purposes
SDL_BlitSurface(img,NULL,textIMG,&rect);//Draw it to the main img

SDL_FreeSurface(img);
}

SDL_LockSurface(textIMG);

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 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);
if(newimg)
SDL_FreeSurface(img);
else
;//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 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.

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• Forum Statistics

• Total Topics
632171
• Total Posts
3004555

×