Jump to content
  • Advertisement
Sign in to follow this  
vman

SDL -- Incorrect output value

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

Currently im writing a scrolling shooter game. Thanks to your help , im going to finish it soon. This time , a weird problem(maybe?) came up. I use this function to update the status on the screen:
Quote:
int updStatus() { stats_text = TTF_RenderText_Blended(font_name, "Life:"+life, txt_col ); UPDATE_POSITION( 0, 0, stats_text, screen ); }
The output should be : "Life: and the stored value of (int) life". Well , the output is this : alienhunteri7ov.jpg (direct link:http://img133.imageshack.us/img133/143/alienhunteri7ov.jpg) How can i fix it ??

Share this post


Link to post
Share on other sites
Advertisement
I would try concatinating both into a sstream before hand and trying to render that. My guess is that, since TTF_RenderText_Blended() is expecting a char* as its second parameter, it doesn't know how to deal with the concatination your trying to do.

Share this post


Link to post
Share on other sites
Yes, sstream it. You are passing an int to a string or char array, so you first need to convert the int to a string.


#include <sstream>

...

std::stringstream ssLife;
int iLife;
std::string sLife
...

ss << "Hits left: ";
ss << iLife;
sLife = ss.str();

stats_text = TTF_RenderText_Blended(font_name,
sLife, txt_col );
...


Something like that.

Share this post


Link to post
Share on other sites
Thanks :) , it worked.

Quote:

int updStatus() {
std::stringstream ss;
std::string sLife;
ss << "Life: ";
ss << life;
sLife = ss.str();

stats_text = TTF_RenderText_Blended(font_name,
sLife.c_str(), txt_col );

UPDATE_POSITION( 0, 0, stats_text, screen );
}

The only thing needed change was "sLife" -> "sLife.c_str()".

Share this post


Link to post
Share on other sites
hmm..

After i included this function to my main() ,another problem appeared!

When im running the game for about 5-10 minutes the system starts lagging and the window freezes!.
Why this happens?

PS: Without this function the program works great...

Share this post


Link to post
Share on other sites
You have a memory leak! Each time you call TTF_RenderText_Blended it returns a pointer to a new SDL_Surface. You must always free it with SDL_FreeSurface:

int updStatus()
{
std::stringstream ss;
std::string sLife;
ss << "Life: ";
ss << life;
sLife = ss.str();

stats_text = TTF_RenderText_Blended(font_name,
sLife.c_str(), txt_col );

UPDATE_POSITION( 0, 0, stats_text, screen );
SDL_FreeSurface( stats_text );
}


Good luck finishing your game!

Share this post


Link to post
Share on other sites
Quote:
Original post by vman
hmm..

After i included this function to my main() ,another problem appeared!

When im running the game for about 5-10 minutes the system starts lagging and the window freezes!.
Why this happens?

PS: Without this function the program works great...


I find it hard to believe that your program compiled without using .c_str()...

Were there no errors? Warnings?

Your problem is you are leaking surfaces. Use SDL_FreeSurface() on the surface returned from TTF_RenderText_*().

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by vman
hmm..

After i included this function to my main() ,another problem appeared!

When im running the game for about 5-10 minutes the system starts lagging and the window freezes!.
Why this happens?

PS: Without this function the program works great...


I find it hard to believe that your program compiled without using .c_str()...

Were there no errors? Warnings?



As i said , the only thing that i've changed is .c_str() pointer.

[QUOTE=ME]
The only thing needed change was "sLife" -> "sLife.c_str()".
[/quote]
If you still dont believe me , here's the proof :
(WARNING: thats the bugged version)
http://rapidshare.de/files/23041026/Alien_Hunter_bugedVER.zip.html

Share this post


Link to post
Share on other sites
To avoid constantly reallocating and freeing the surface, you may consider keeping a 'static' (in the sense of outliving the function) surface around, and re-creating it only when the 'life' value changes. There are numerous approaches to this, and usually OO techniques would be shown; but in the interest of keeping the example as simple as possible:


int updStatus() {
// The value (if any) that was rendered last time.
static int renderedLife;
// It looks like you already have a global, or otherwise wider-scoped,
// 'stats_text' for the surface. I will use that one...

if (stats_text && (life != renderedLife)) {
// there is an invalid surface to clean up.
SDL_FreeSurface(stats_text);
}
if (!stats_text) {
// either it was just cleaned up, or this is the first run.
// Cache the rendered life value so we know for next time.
renderedLife = life;
// Set up the surface. Note a couple of techniques here ;)
std::stringstream ss("Life: ");
ss << life;

stats_text = TTF_RenderText_Blended(font_name, sLife.str().c_str(), txt_col);
}
// I really hope this isn't a macro :(
UPDATE_POSITION( 0, 0, stats_text, screen );
}


Of course, you normally only optimize when you need to, and making sure you don't leak memory may well be optimization enough - but I thought you should be familiar with this sort of technique in general, because some "resources" (like SDL surfaces) can be very expensive to acquire, depending on your environment.

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!