Public Group

# ok newbie here

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

## Recommended Posts

i'm still writing my game, and now i have a situation, the combat system actually works, but when it comes to updating my health and enemy health and other stuff, instead of writing a new number it owerwrites the old one with a new one(the old one is not deleted). So i thought maybe you could give me a few commands to repair this flaw.
bool update()
{

SDL_FreeSurface(health);
SDL_FreeSurface(power);
SDL_FreeSurface(enemy);

string heal = "", pow = "", eHP = "";
stringstream stream,stream2,stream3;
stream << HP; stream >> heal;
stream2 << power; stream2 >> pow;
stream3 << enemyHP; stream3 >> eHP;

health = TTF_RenderText_Solid(font, heal.c_str(), textColor2);
powersource = TTF_RenderText_Solid(font, pow.c_str(), textColor);
enemy = TTF_RenderText_Solid(font, eHP.c_str(), textColor2);

apply_surface(110,210,health,screen); apply_surface(430,248,energy,screen);
apply_surface(430,210,enemy,screen);
SDL_Flip(screen);
}



##### Share on other sites
Your problem is that you never clear the screen. You must wipe out the background first, otherwise your old text will stay there, and the new text will just get drawn over it.
You can use SDL_FillRect to do this easily.

SDL_FillRect takes a SDL_Surface that you want to draw the rectangle on, a pointer to a SDL_Rect so it knows the location and size of the rectangle to draw, and a Uint32 as the color to make the rect.

Since you want to fill the entire background, you can just pass NULL instead of a SDL_Rect pointer.

SDL_FillRect(screen, NULL, 0);

(The color '0', will be black on Windows, but it could be a different color on different Operating Systems, you can use SDL_MapRGB if you need to be 100% sure of the color)

Just put that line at the beginning of your drawing function, before you drawn anything else, and your previous frame will be cleared. Actually, it'll just be drawn over with a solid color, but that's the same thing. [smile]

One other thing; you are freeing 'health', 'power', and 'enemy' at the beginning of your function call, when you should be freeing them at the end of it. Think of what happens when your program exits: you never enter the update() function, and those surfaces aren't freed.

##### Share on other sites
Your post was great many thx, but ther's still a problem you see i have many more things(surfaces) on that screen if i would use SDL_Rect(screen,NULL,0);
it would black out the whole screen exept those three numbers, i hope you can give me an example where i only black out the three Font messages. Otherwise thx learned a huge lot from you.

##### Share on other sites
Well, you should wipe out the entire screen at the beginning of each frame, and redraw everything that needs to be drawn, every frame. This is how most games work. That is what you should do.
Every frame:Clear screen..Draw stuff in one functionDraw more stuff in your other functionsDraw additional stuff wherever...Call SDL_Flip(screen)
Here's an example:
while(!quit){    //Check for SDL_Events, for things like mouse moving and button presses.    checkEvents();        //Update anything that needs updating.    updateMyStuff();        //Erase the screen.    SDL_FillRect(screen, NULL, 0);    //Draw everything.    drawMyStuff();        //Update the screen.    SDL_Flip(screen);}

However, you *could* just draw a rectangle over your old text, but that's not a good idea. However, if you wanted to do it anyway, you could go like this:
//Make a SDL_Rect be equal to the size and location of the image 'health'.SDL_Rect rect;rect.x = 110; rect.y = 210;rect.w = health->w; rect.h = health->h;//Fill that rect, erasing the old health.SDL_FillRect(screen, rect, 0); //Draw the new health.apply_surface(110,210,health,screen);

Again, it'd be much better to wipe out the entire screen every frame, and redraw everything, every frame.

##### Share on other sites
Yea your right, have many thx. You brought me back to earth, otherwise i would have spent ages trying to make it work. Im gonna have to rewrite about 50 lines of code but in the end its going to make my game more efficient. thx servant of the lord

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011266
• ### Who's Online (See full list)

There are no registered users currently online

×