-1

I draw some text to a surface (using SDL_ttf) and then I want to change the text on the surface. If I just redraw the surface the text does not go away. I have looked at several forum posts on how to fix the problem but I just cannot seem to figure it out. In particular I cannot understand why this solution does not work: (code is long so this just gives the essentials)

In Class file declared:

       SDL_Surface* box; // These two are initialised to the 
       SDL_Surface* boxCopy; // same image

At the start of my render function:

       *box = *boxCopy; \\Reset box surface

My understanding of pointers and C++ (which is admittedly limited) suggests that this should make the surface pointed at by box equal to the surface pointed at by boxCopy. Instead the boxCopy surface becomes a copy of box. I have no idea how boxCopy can be changed by this line of code but it seems like that is what is happening.

Martin G
  • 17,357
  • 9
  • 82
  • 98
Beetroot
  • 701
  • 6
  • 12
  • Of course it doesn't go away, if you draw on a surface it is changed for good. If you need a copy use SDL_CreateRGBSurface() and manually copy the pixel data. – this Apr 10 '14 at 19:04

1 Answers1

0

I'm not sure i completely understand your problem but hopefully this can help.. It's easier to update the text whenever the surface it's drawn on is to be updated rather than updating it whenever the actual text is updated. It might not be as optimized performance wise but i would say it's easier in most cases.

A typical program loop would include a re-rendering of a surface representing the screen followed by an SDL_Flip of this surface. You can of course optimize your re-rendering so you only render what has actually been updated since last frame. Is that what you're working on perhaps? If so, and if you use the method below you should be aware that the new text only covers the size of the new text and not the entire old text. I usually solve this by first drawing a filled rectangle and then the new text.

Here is a TTF example showing how text can be drawn on a surface (here called m_Screen, which is the surface flipped to screen every frame) in the simple case where i have one background color only:

void drawText(const char* string, int x, int y, 
              int fR, int fG, int fB, int bR, int bG, int bB)
{
    SDL_Color foregroundColor = { fR, fG, fB }; 
    SDL_Color backgroundColor = { bR, bG, bB }; 
    SDL_Surface* textSurface = TTF_RenderText_Shaded(m_Font, string, 
                                                     foregroundColor, 
                                                     backgroundColor);  
    SDL_Rect textLocation = { x, y, 0, 0 }; 
    SDL_BlitSurface(textSurface, NULL, m_Screen, &textLocation);    
    SDL_FreeSurface(textSurface);   
}

Notice that this has been done before calling drawText (with some suitable font size):

m_Font = TTF_OpenFont("arial.ttf", size);

And this is done at cleanup:

TTF_CloseFont(m_Font);
Martin G
  • 17,357
  • 9
  • 82
  • 98