FPS Display Problem

Hello,

I am having trouble displaying the FPS on my screen correctley. It just shows FPS with numbers going across the screen. I’ll post my code and tell where the problem is occuring. The code runs fine.

Code:

while(GameRunning)
{
iThisTime = SDL_GetTicks();
fDeltaTime = (float)(iThisTime - iLastTime) / 1000;
iLastTime = iThisTime;

    szFPS << "FPS:" << fDeltaTime;
    
    FPS = TTF_RenderText_Solid(Font,szFPS.str().c_str(), textColor);
    FPSRect.x = 1;
    FPSRect.y = 1;
    FPSRect.w = 0;
    FPSRect.h = 0;
    
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
        {
            GameRunning = false;
        }
        
        if(event.type == SDL_KEYDOWN)
        {
            keyPressed = event.key.keysym.sym;
            keysHeld[event.key.keysym.sym] = true;
            
            if(keyPressed == SDLK_ESCAPE)
            {
                GameRunning = false;
            }
        }
        
        if(event.type == SDL_KEYUP)
        {
            keyReleased = event.key.keysym.sym;
            keysHeld[event.key.keysym.sym] = false;
        }
    }
    
    if(SDL_Flip(Screen) == -1)
    {
        exit(1);
    }
    
    SDL_BlitSurface(FPS,NULL,Screen,&FPSRect);
}

}

Where I have the “szFPS” is where it seems to have the random numbers going across the screen. I need to fix this so it shows only the current FPS in the top
left corner of the screen.

Your szFPS is a stringstream, right? You are continually appending to it, so
it keeps getting longer and longer. The simplest solution is to scope the
variable inside the loop. A more complex one might involve trying to empty
the stringstream between iterations of the loop, but this is a little
complex, I think you can use ignore() with
std::numeric_limitsstd::streamsize::max(), and use clear() to remove any
error flags (though there should be none). It might be possible to use
str("") to clear the contents too.

By the way, as “szFPS” isn’t a NULL terminated string, the prefix “sz” is a
poor choice. In fact, hungarian notation in general is usually unnecessary
with modern IDEs. You’ll also want to SDL_FreeSurface(FPS) between
iterations too, or you’ll have a memory leak.On 21 September 2010 01:28, GameCoder <g_andy at live.com> wrote:

Hello,

I am having trouble displaying the FPS on my screen correctley. It just
shows FPS with numbers going across the screen. I’ll post my code and tell
where the problem is occuring. The code runs fine.

Since this is C++ not C, a much simpler solution than messing with the error
flags etc. would be to follow the guideline of defining variables where you
use them (i.e. inside the loop), so you use a new, valid stringstream each
time.On 21 September 2010 09:23, Brian Barrett <brian.ripoff at gmail.com> wrote:

Your szFPS is a stringstream, right? You are continually appending to it,
so it keeps getting longer and longer. The simplest solution is to scope the
variable inside the loop. A more complex one might involve trying to empty
the stringstream between iterations of the loop, but this is a little
complex, I think you can use ignore() with
std::numeric_limitsstd::streamsize::max(), and use clear() to remove any
error flags (though there should be none). It might be possible to use
str("") to clear the contents too.

By the way, as “szFPS” isn’t a NULL terminated string, the prefix “sz” is a
poor choice. In fact, hungarian notation in general is usually unnecessary
with modern IDEs. You’ll also want to SDL_FreeSurface(FPS) between
iterations too, or you’ll have a memory leak.

On 21 September 2010 01:28, GameCoder <g_andy at live.com> wrote:

Hello,

I am having trouble displaying the FPS on my screen correctley. It just
shows FPS with numbers going across the screen. I’ll post my code and tell
where the problem is occuring. The code runs fine.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Never mind. Brian said that. Reading comprehension failOn 21 September 2010 10:23, Nether Hound <@Nether_Hound>wrote:

Since this is C++ not C, a much simpler solution than messing with the
error flags etc. would be to follow the guideline of defining variables
where you use them (i.e. inside the loop), so you use a new, valid
stringstream each time.

On 21 September 2010 09:23, Brian Barrett <brian.ripoff at gmail.com> wrote:

Your szFPS is a stringstream, right? You are continually appending to it,
so it keeps getting longer and longer. The simplest solution is to scope the
variable inside the loop. A more complex one might involve trying to empty
the stringstream between iterations of the loop, but this is a little
complex, I think you can use ignore() with
std::numeric_limitsstd::streamsize::max(), and use clear() to remove any
error flags (though there should be none). It might be possible to use
str("") to clear the contents too.

By the way, as “szFPS” isn’t a NULL terminated string, the prefix “sz” is
a poor choice. In fact, hungarian notation in general is usually unnecessary
with modern IDEs. You’ll also want to SDL_FreeSurface(FPS) between
iterations too, or you’ll have a memory leak.

On 21 September 2010 01:28, GameCoder <g_andy at live.com> wrote:

Hello,

I am having trouble displaying the FPS on my screen correctley. It just
shows FPS with numbers going across the screen. I’ll post my code and tell
where the problem is occuring. The code runs fine.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Since this is C++ not C, a much simpler solution than messing with the
error flags etc. would be to follow the guideline of defining variables
where you use them (i.e. inside the loop), so you use a new, valid
stringstream each time.

You could save yourself the costs of construction and destruction each
frame by defining the variable outside the loop and destructing it after
the fact.

Convert your code to:
szFPS = “FPS:”;
szFPS << fDeltaTime;

And the string is cleared each iteration.On 9/21/2010 3:23 AM, Nether Hound wrote:

On 21 September 2010 09:23, Brian Barrett <brian.ripoff at gmail.com <mailto:brian.ripoff at gmail.com>> wrote:

Your szFPS is a stringstream, right? You are continually appending
to it, so it keeps getting longer and longer. The simplest solution
is to scope the variable inside the loop. A more complex one might
involve trying to empty the stringstream between iterations of the
loop, but this is a little complex, I think you can use ignore()
with std::numeric_limits<std::streamsize>::max(), and use clear() to
remove any error flags (though there should be none). It might be
possible to use str("") to clear the contents too.

By the way, as "szFPS" isn't a NULL terminated string, the prefix
"sz" is a poor choice. In fact, hungarian notation in general is
usually unnecessary with modern IDEs. You'll also want to
SDL_FreeSurface(FPS) between iterations too, or you'll have a memory
leak.


On 21 September 2010 01:28, GameCoder <g_andy at live.com <mailto:g_andy at live.com>> wrote:

    Hello,

    I am having trouble displaying the FPS on my screen correctley.
    It just shows FPS with numbers going across the screen. I'll
    post my code and tell where the problem is occuring. The code
    runs fine.




_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks for the help.

However when I try to convert, I get an error
no match for ‘operator=’ in '((Game*)this)->Game::szFPS = “FPS:”'
note C:\Program Files (x86)\Dev-Cpp\include\c++\3.4.5\iosfwd:84 candidates are: std::basic_stringstream<char, std::char_traits, std::allocator >& std::basic_stringstream<char, std::char_traits, std::allocator >::operator=(const std::basic_stringstream<char, std::char_traits, std::allocator >&)

How do I fix this?

I’m going to defer to the template masters for this one. I use a custom
class for strings so I don’t have to pull my hair out over these things.
I expect the basic problem is the use of stringstream instead of string
but there’s always something.On 9/21/2010 7:03 PM, GameCoder wrote:

Thanks for the help.

However when I try to convert, I get an error
no match for ‘operator=’ in '((Game*)this)->Game::szFPS = “FPS:”'
note C:\Program Files (x86)\Dev-Cpp\include\c++\3.4.5\iosfwd:84
candidates are: std::basic_stringstream, std::allocator >&
std::basic_stringstream, std::allocator >::operator=(const
std::basic_stringstream, std::allocator >&)

How do I fix this?

Perhaps, but I really need to get this fixed, so I can see my framerate.

Stringstreams don’t have an implicit conversion from string to stringstream for the same reason that a fstream does not; even though
it acts so much like a string, it is fundamentally a Stream. Hence, operator= was left undefined for it.

While there is probably a purely C++ approach to this, it seems like this would be a much simpler job for cstrings and the sprintf function.

If you’re unfamiliar, it should look something like this…

Code:
//outside of loop
char csFPS[10]; //10, or however more characters you would expect to need

//inside of loop
sprintf(csFPS, “FPS: %.2f”, fDeltaTime);
FPS = TTF_RenderText_Solid(Font, csFPS, textColor);

sprintf just overwrites the members that were previously in the character array with the new string, so there’s no worry about memory leak(I think)

I hope this helps.

GameCoder wrote:

Thanks for the help.

However when I try to convert, I get an error
no match for ‘operator=’ in '((Game*)this)->Game::szFPS = “FPS:”'
note C:\Program Files (x86)\Dev-Cpp\include\c++\3.4.5\iosfwd:84 candidates are: std::basic_stringstream<char, std::char_traits, std::allocator >& std::basic_stringstream<char, std::char_traits, std::allocator >::operator=(const std::basic_stringstream<char, std::char_traits, std::allocator >&)

How do I fix this?

Code:

cannot convert char**' tochar*’ for argument 1' toint sprintf(char*, const char*, …)‘
cannot convert char**' toconst char*’ for argument 2' toSDL_Surface* TTF_RenderText_Solid(TTF_Font*, const char*, SDL_Color)’

Now these errors come up after I tried to use the sprintf thing.

Did you make sure to declare your cstring as char name[size] and not char* name[size]?

The first is an array of Characters, the second is an array of Character pointers, or char**.

GameCoder wrote:> cannot convert char**' tochar*’ for argument 1' toint sprintf(char*, const char*, …)’

cannot convert char**' toconst char*’ for argument 2' toSDL_Surface* TTF_RenderText_Solid(TTF_Font*, const char*, SDL_Color)’

Now these errors come up after I tried to use the sprintf thing.

Looks like you need some C and C++ lessons before continuing any
further. Using sprintf or std::stringstream is far from advanced.Em 21-09-2010 20:36, GameCoder escreveu:

cannot convert char**' tochar*’ for argument 1' toint
sprintf(char*, const char*, …)‘
cannot convert char**' toconst char*’ for argument 2' toSDL_Surface* TTF_RenderText_Solid(TTF_Font*, const char*, SDL_Color)’

Now these errors come up after I tried to use the sprintf thing.


Daniel K. O.
“The only way to succeed is to build success yourself.”

Yes I know, I’m a bad programmer, but I’m learning. Also, it worked.
Thanks for all the help.

I’m just saying that trying to learn both the language and a multimedia
library both at the same time is twice as hard and will make you
confused. Case in point: you are asking for help with the language in
the SDL mailing list, when you should be doing it on a C or
C++beginners’ group. Put SDL (and whatever other library you might be
using) down, and stick with the basic language until you are confident
on it. Game Development is one of the most complex endeavors in
programming, requiring you to be fluent in a number of fields and
techniques if you want to do anything more than yet another tetris clone.Em 21-09-2010 21:55, GameCoder escreveu:

Yes I know, I’m a bad programmer, but I’m learning. Also, it worked.
Thanks for all the help.


Daniel K. O.
“The only way to succeed is to build success yourself.”

Stringstreams don’t have an implicit conversion from string to stringstream for
the same reason that a fstream does not; even though

it acts so much like a string, it is fundamentally a Stream. Hence, operator=
was left undefined for it.

While there is probably a purely C++ approach to this, it seems like this would
be a much simpler job for cstrings and the sprintf function.

Not only simpler, it is also worth mentioning is that in many cases, using the C
library functions are orders of magnitude faster.

When designing my first game using C++, I was using streams and things were
slow, especially debugging output to a console. I too was using stringstream for
numeric conversions all over.

I picked up a copy of Effective C++ that was gathering dust on my shelves and
one item was exactly what I was looking for, the proper use of streams vs. C
library functions. I switched to sprintf for some cases and never looked back.
Especially where GameCoder is using it, in a block that will be called
frequently, using stringstream may hurt more than he realizes.

GameCoder, if you’re starting out, when you’re comfortable with the language I
suggest you checking Scott Meyer’s books Effective C++ books. Though not
perfect, they’ve taught me a lot in the way of optimizing my coding. Not all of
it is performance related, it will help you in maintenance too.

AnthonyFrom: kyle_park0@hotmail.com (Park)
To: sdl at lists.libsdl.org
Sent: Tue, September 21, 2010 10:19:14 PM
Subject: Re: [SDL] FPS Display Problem

I’m going to have to give the opposite advice here. Game programming
is a wonderful way to learn to be a good programmer, assuming you
have the basic aptitudes for computer programming in the first place,
precisely because it forces you to confront and learn to deal with a
wide spectrum of problems.

GameCoder, your problem is that you’re trying to do string processing in
C++, which C and C++ are just plain really bad at. You’d be a lot better
off working in a more user-friendly language, especially if you’re new to
programming. There is no good reason to start any new project of any
kind in C++ in this day and age.>----- Original Message ----

From: Daniel K. O. <danielko.listas at gmail.com>
Subject: Re: [SDL] FPS Display Problem

Em 21-09-2010 21:55, GameCoder escreveu:

Yes I know, I’m a bad programmer, but I’m learning. Also, it worked.
Thanks for all the help.

I’m just saying that trying to learn both the language and a multimedia
library both at the same time is twice as hard and will make you
confused. Case in point: you are asking for help with the language in
the SDL mailing list, when you should be doing it on a C or
C++beginners’ group. Put SDL (and whatever other library you might be
using) down, and stick with the basic language until you are confident
on it. Game Development is one of the most complex endeavors in
programming, requiring you to be fluent in a number of fields and
techniques if you want to do anything more than yet another tetris clone.

Especially where GameCoder is using it, in a block that will be called
frequently, using stringstream may hurt more than he realizes.

What, once per frame? I hardly call that “frequently”. You get more bang for
your buck by optimising actual bottlenecks (the pareto principle, etc). In
most games the bottleneck isn’t string processing in the main loop.


From: brian.ripoff@gmail.com (Brian Barrett)
To: SDL Development List
Sent: Wed, September 22, 2010 1:52:25 PM
Subject: Re: [SDL] FPS Display Problem

What, once per frame? I hardly call that “frequently”. You get more bang for
your buck by optimising actual

bottlenecks (the pareto principle, etc). In most games the bottleneck isn’t
string processing in the main loop.

You’re right. I made the assumption if he’s using it in the case, he’s probably
using it all over for number to string conversions in his program, which can add
up. Even so, I didn’t call it a bottleneck. Like I said, I was using it for
everything (not just conversions, stringstream has other uses), and it hurt.
Just sharing that experience. I guess the moral of the story is know your tools
and its alternatives.

And cool, thanks, I didn’t know the 80-20 rule had a name, learn something new
every day.

Oh, Mason. You’re totally right, but I haven’t found a modern compiled,
statically-typed language which I like more than C++.

Jonny DOn Wed, Sep 22, 2010 at 1:45 PM, Mason Wheeler wrote:

I’m going to have to give the opposite advice here. Game programming
is a wonderful way to learn to be a good programmer, assuming you
have the basic aptitudes for computer programming in the first place,
precisely because it forces you to confront and learn to deal with a
wide spectrum of problems.

GameCoder, your problem is that you’re trying to do string processing in
C++, which C and C++ are just plain really bad at. You’d be a lot better
off working in a more user-friendly language, especially if you’re new to
programming. There is no good reason to start any new project of any
kind in C++ in this day and age.

----- Original Message ----
From: Daniel K. O. <danielko.listas at gmail.com>
Subject: Re: [SDL] FPS Display Problem

Em 21-09-2010 21:55, GameCoder escreveu:

Yes I know, I’m a bad programmer, but I’m learning. Also, it worked.
Thanks for all the help.

I’m just saying that trying to learn both the language and a multimedia
library both at the same time is twice as hard and will make you
confused. Case in point: you are asking for help with the language in
the SDL mailing list, when you should be doing it on a C or
C++beginners’ group. Put SDL (and whatever other library you might be
using) down, and stick with the basic language until you are confident
on it. Game Development is one of the most complex endeavors in
programming, requiring you to be fluent in a number of fields and
techniques if you want to do anything more than yet another tetris clone.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hang on now. I don’t think what’s going on here is string processing, it
appears to be game development with a string tossed in just to keep an
eye on performance. Pretty basic stuff really.

It’s also worth mentioning that C and C++ are very good at string
processing, but as in all cases programming, you need to build something
that meets your own specifications.

Personally I find the basic string templates difficult to use. Half
because they weren’t intuitive the first time I tried them. Or the
second time. In C++ you have the opportunity to roll your own string
class, which is what I’ve done.

Buffer myString;

myString = “FPS:”;
myString += floatVariable;

Handles growth all by itself up to all the memory the OS will give it.
Streams to an from disk.

Has overloaded += operators for all basic types and any custom types I’d
like to add.

The only complaint I have with it is that it doesn’t do wide or
multibyte chars seamlessly, but that’s an exercise I can deal with if it
ever becomes important to me.

Every language sucks at everything because you have to write code to get
it to do what YOU want. And that’s why every language is so great,
because you can write code to get it to do what YOU want.On 9/22/2010 11:45 AM, Mason Wheeler wrote:

I’m going to have to give the opposite advice here. Game programming
is a wonderful way to learn to be a good programmer, assuming you
have the basic aptitudes for computer programming in the first place,
precisely because it forces you to confront and learn to deal with a
wide spectrum of problems.

GameCoder, your problem is that you’re trying to do string processing in
C++, which C and C++ are just plain really bad at. You’d be a lot better
off working in a more user-friendly language, especially if you’re new to
programming. There is no good reason to start any new project of any
kind in C++ in this day and age.