Hi, I have a game that at the moment is a little like mario- a jump and run. I want to add a timer to my main funtion so that after 60 seconds or so the game ends and goes to a highscore table where you can enter your name if your score qualifies. I don't know how to acheive either of these things so any help would be good :)

Thanks

i have been following a tutorial on http://jnrdev.72dpiarmy.com/
I am using SDL in Visual C++.
i want the timer to start at 60 and count down to 0 when it reaches 0 the program then goes to a high score table which allows you to enter your name if your score is high enough.- how is that too general?

You can use sdl's timer or clock, or if your on windows, Queryperformance counter.
For example, here is one that uses clock().

#include <iostream>
#include <ctime>

using namespace std;

class Timer{
public:
	typedef clock_t TimeType;
private:
	TimeType _clockStart; //holds the time of reference
public:
	Timer(): _clockStart(clock()){
	}
	TimeType getTicks()const{ 
		return clock() - _clockStart;  
	}	
	float getSecondsPassed()const{
		return getTicks()* 1.0f/CLOCKS_PER_SEC;
	}
	void startTime(){
		_clockStart = clock();
	}
	void resetTime(){
		_clockStart = clock();
	}

};
int main(){	

	Timer tm;

	tm.startTime();
	int i = 0;
	while(i != 5){
		if(tm.getTicks() > 1000){ //every 1 seconds
			cout << ++i << " seconds" << endl;
			tm.resetTime();
		}
	}

	cout <<"BOOM!!!!!"<<endl;

	return 0;
}

Thats helpful, i have different classes- would i put this code in the main function or make a new .cpp file for it?

thanks, im having some difficulties as i can't figure out what part goes in the header and what part goes in the .cpp.

Header should contain the prototype, if possible. So put the Timer class inside
the header, but strip the definition from it. Then implement the definition, in the
.cpp file

This article has been dead for over six months. Start a new discussion instead.