Game / Lap timer need

Started by
8 comments, last by dazedandconfused 17 years ago
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
It's not until we have fallen, we realize how much it hurts !
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 startint 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)
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 ;-)
It's not until we have fallen, we realize how much it hurts !
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).
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 ?
It's not until we have fallen, we realize how much it hurts !
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
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:
Cchar buffer[10]; strcpy( buffer, "Hello " );strcat( buffer, "World" );  // buffer overflows causing undefined behaviourC++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.
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 !
It's not until we have fallen, we realize how much it hurts !
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.

Did someone say "FREE" !!

I'm totally there dude !!

Thanks again, you've been a GREAT help
It's not until we have fallen, we realize how much it hurts !

This topic is closed to new replies.

Advertisement