Jump to content
  • Advertisement
Sign in to follow this  
dazedandconfused

Game / Lap timer need

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

Any one help with a game / lap timer ? I can get the time / elapsed time etc etc, for diplaying Frame rate This is C++/OpenGL by the way
void getTimes()	
{
	const int buffer_size = 256;
	static char strTime[buffer_size] = {0};

	timeCurrentTime = (GetTickCount() * 0.001f);

	timeElapsedTime = timeCurrentTime - timeDemoStartTime;

	sprintf_s(strTime, buffer_size, "Time: %0.3f", timeElapsedTime);

	timeElapsedTxt = strTime;

}

Pretty basic, but work for FPS and a basic 'elapsed time' diplay. But obviously this just continues to run from 00.000 seconds to 60.000 .. 70.000 etc Where after 60 seconds, I should really display minutes !! Anyone got a simple example of getting a timer/clock in hours,minutes,seconds,milliseconds. (HH:MM:SS:nnn) Sorry C++ noobie, never worked with timers yet

Share this post


Link to post
Share on other sites
Advertisement
That isn't C++, it's C. Also it would probably be easier to do this with integers then floats. I see no reason why you would store milliseconds/seconds/minutes/etc.. as floats.

Anyway if you can get the number of milliseconds the rest should look like this:


int Milliseconds = <insert fucntion here>; // note this is milliseconds from program start
int Seconds = Milliseconds / 1000;
int Minutes = Seconds / 60;
int Hours = Minutes / 60;
etc..

Milliseconds %= 1000;
Seconds %= 60;
Minutes %= 60;
Hours %= 24;
(there's probably a much better way to do this)

Share this post


Link to post
Share on other sites
OH !!

Can you explain what the difference in the code is here then.

I am just going by many examples and tutorials, on here, and NeHe's examples for OpenGL.

Could you show some better c++ source for this ;-)

Share this post


Link to post
Share on other sites
Well your C function in C++ might look like this:


std::string getTimes()
{
timeCurrentTime = ( GetTickCount() * 0.001f );
timeElapsedTime = timeCurrentTime - timeDemoStartTime;
std::stringstream ss;
// I forget how to set it to 3 decimal places, but it is possible
ss << "Time: " << timeElapsedTime;
return stringstream.str();
}




NeHe's tutorials are a good OpenGL reference, but I would suggest not learning C++ programming off them. His early ones are entirely in C and the later ones are a horrible mix of C and C++ code. Sometimes he puts in C++ code and never even uses it(usually strings).

Share this post


Link to post
Share on other sites
Thanks Scet,

that worked once I'd figured out return was actually
ss.scr()

and not return
stringstream.str();

Thanks, so now my question is, should I use string for everything, dumb question but what's the difference between string and char.

when should I use each one?

for example following some tutorials, on things like we just covered.. ie displaying time, FPS, score, health or whatever .. should I use strings.

what about when I use a loadTexture(GLuint texture, char filename) to load textures for my openGL, or CreateGLWindow(char* title, int width, int height)
again shoud I be using strings or chars ?

Share this post


Link to post
Share on other sites
Quote:
for everything, dumb question but what's the difference between string and char.


A char variable represents a single character, such as a single letter or symbol. A string are multiple characters. With a char array you can also use multiple characters. Generally a string is represented as an array of characters. And then there is a char pointer (char * ptr_p)

www.nextdawn.nl

Share this post


Link to post
Share on other sites
Quote:
Original post by dazedandconfused
Thanks, so now my question is, should I use string for everything, dumb question but what's the difference between string and char.

when should I use each one?


std::string is safer then char pointers because it's an actual object, it can manage it's length itself so there are no buffer overrun problems. std::string also has operator overloading, you can add on to it like it was a normal variable.

Example:

C
char buffer[10];
strcpy( buffer, "Hello " );
strcat( buffer, "World" ); // buffer overflows causing undefined behaviour

C++
std::string buffer = "Hello ";
buffer += "World"; // no problems

So if you want to use C++, use std::string for actually strings and use char*s when you need a char pointer/array(probably not very often). Plus using the standard library adds a lot more functionallity then just strings.

Quote:

what about when I use a loadTexture(GLuint texture, char filename) to load textures for my openGL, or CreateGLWindow(char* title, int width, int height)
again shoud I be using strings or chars ?


If you made those functions yourself, yes you should change them to strings. If however you're calling functions from a library that only accepts char*s, use the .c_str() function.

std::string Title = "Hello World";
SomeOldFunction( Title.c_str() );

Note that this returns a const char*, because the strings internal data shouldn't be messed with.

dazedandconfused, I have a feeling your code right now is pretty much in C. If you want/wanted to learn C++, buy a book or take a look at C++ Workshop Forum.

Share this post


Link to post
Share on other sites
Scet,

Thanks, that's AWESOME.

You explained that well !

And yeah, I noticed that most of what I have learned seems to be 'C'

About time I got a new book hey !

I have programmed in Pascal for some time, and taking the leap to C/C++.

Thanks for the advice though [wink]

I will get either Accelerated C++, or C++ Primer, which would you suggest, as they seems the best 2 books, and also listed in the GameDev Book Section.

Wow a noobie, that actually searched, or looked at the FAQ .. it's a miracle

Yes and a noob, who's just hacking away at C/C++ lol.

I have Dave Astle's 'Beginning OpenGL' and to be honest the word 'string' is never mentioned !
And it is aimed at C++ .. go figure !

Share this post


Link to post
Share on other sites
Quote:
Original post by dazedandconfused
I will get either Accelerated C++, or C++ Primer, which would you suggest, as they seems the best 2 books, and also listed in the GameDev Book Section.


I wouldn't know, I don't actually use C++. There's a free book called Thinking in C++ though.

Quote:
Original post by dazedandconfused
I have Dave Astle's 'Beginning OpenGL' and to be honest the word 'string' is never mentioned !
And it is aimed at C++ .. go figure !


OpenGL and Win32(which the book also uses) are both C libraries, so they probably made it to work for C programmers as well. C++ is backwards compatible with C, you can still use C fucntions in C++. However sometimes there is a better, "more C++" alternative like with strings and other standard library functions.

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!