Problem With Text On A Surface


bbodin

Still Fresh
Joined
Jun 2, 2006
Messages
55
I want to load an image to the screen. I then want to write some text in a certain section of that screen. Since I will be refreshing etc within that text area, I thought I should just place another surface on top of the bitmap and then within that surface display my other text surfaces.

Anyway, here's the gist of what I'm doing to accomplish this (leaving out the other stuff that seems to be working fine, like fonts, etc.). Where am I going wrong?

SDL_Surface *screen, *textBlock;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
screen = SDL_SetVideoMode( 320, 240, 32, SDL_SWSURFACE);
SDL_Surface* bitmap = SDL_LoadBMP("test.bmp");

//draw the bitmap (works fine)
SDL_BlitSurface(bitmap, NULL, screen, NULL)

int done = 0;
while (!done)
{
SDL_Rect targetareaText;
targetareaText.x = 20;
targetareaText.y = 60;
targetareaText.w = 145; //ignored
targetareaText.h = 140; //ignored

textBlock = SDL_CreateRGBSurface(SDL_SWSURFACE, 145, 140, 32, rmask, gmask, bmask, amask);
SDL_BlitSurface(textBlock, NULL, bitmap, &targetareaText);

SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Surface* textSurface = TTF_RenderText_Blended(font,"Test", foregroundColor);
SDL_Rect textLocation = { 20, 60, 0, 0 };
SDL_BlitSurface(textSurface, NULL, textBlock, &textLocation);
SDL_FreeSurface(textSurface);

SDL_Flip(screen);

}


So I blit the "bitmap" to the "screen", blit the "textBlock" to the "bitmap", then blit the "textSurface" to the "textBlock".

The text doesn't appear if I write it to the "textBlock" surface. If I write it to the "screen" or "bitmap" surfaces, it works fine...so I'm not sure the "textBlock" surface is being set correctly. To test this I also tried to do something like


SDL_FillRect(textBlock, NULL, SDL_MapRGB(bitmap->format, 0,0,0));


but it doesn't fill the surface as black (but again, if I do the same to "screen" or "bitmap" surfaces they display black. )

So am I misunderstanding something? Shouldn't I be able to write to this "textBlock" surface that is sitting on top of my "bitmap" surface?
 
Am I right in saying that this piece of code doesn't work? The reason being is that the textblock/textsurface is never blitted to the screen surface so therefore the text doesn't show up.

The screen surface is what is displayed, anything that you blit to the screen surface is displayed, however in this code, you are not blitting to the screen surface every frame therefore the text never appears. You need to blit the text block to the screen every frame.

Side note: You have a memory leak every frame.
 
Am I right in saying that this piece of code doesn't work? The reason being is that the textblock/textsurface is never blitted to the screen surface so therefore the text doesn't show up.

The screen surface is what is displayed, anything that you blit to the screen surface is displayed, however in this code, you are not blitting to the screen surface every frame therefore the text never appears. You need to blit the text block to the screen every frame.

Side note: You have a memory leak every frame.

Thanks for the help. By memory leak every frame you mean I wasn't doing a SDL_FreeSurface(textBlock ), correct? Or is there something else I'm missing?
 
Last edited by a moderator:
Back
Top