Sign in to follow this  

countdown timer!

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

hi, i was wondering if anyone can help me, i would like to create a timer to display in a 2d window that counts down, so that when it gets to zero it will do something e.g. the window will exit!

Share this post


Link to post
Share on other sites
Knowing you are using OpenGl helps a lot, but OpenGL is not a language it is an API. C++, Java, LISP, etc are languages. DirectX, SDL, Allegro, OpenGL are APIs. Other helpful information would be "I am using the _____ Operating system with ____ compiler/IDE".

Lastly, it might be beneficial if you said what you understood. Do you know how to draw text? Do you know how to get the time? Anyway, what you may be looking for is something like this(in pseudo C++ which you're probably using(statistically speaking) ):
void action()
{
// event actions here
}

class CountDownTimer
{
void (*event)(); // function pointer(I may have/probably screwed up the syntax)
timeType startTime; // where timeType is appropriate for whatever format the time is in
timeType endTime;
bool running;
public:
CountDownTimer(void (*event)()) : event(event), running(false) { }
void start(timeType timeToWait) {
running = true;
startTime = getTimeFunction(); // where getTimeFunction is whatever timer you choose
endTime = startTime + timeToWait;
}
bool finished() {
if(running && getTimeFunction() > endTime)
{ event(); return true;
}
else { startTime = endTime = 0; }
}
timeType remaining()
{
if(running)
return endTime - getTimeFunction();
else
return 0;
}
};

// later

std::vector<CountDownTimer> timers;
while(gameRunning)
if(youWantToCreateATimer)
{
timers.pushBack( CountDownTimer(action) );
timers.back().start(); // back returns the last valid element of the vector
}

for(iterator i = timers.begin(); i != timers.end(); )
{
if(i->finished())
{
i = timers.remove(i)
}
else
{
++i;
}
}
}

I have no idea how correct that is, but to me it looks pretty good for my first event type class written on the fly. If no one destroys my crappy implementation then I might even use something like it in my own future project(s).

Also, if you want to be really hardcore it would probably be better to have an Event class (maybe even an Event base class to use polymorphism) along with member function pointers (boost::bind?). This way each event can store data associated with the event (maybe you will implement scripting, for instance). Also, you can consider using the standard algorithm std::remove_if on the container. As finished both runs the event AND says whether the event is finished you can perform that for loop on just one line.

Hope that helps (it certainly helped me, I think I have the beginnings of my own time based event system).



edit: oh, forgot about the remaining function. When you want to print the amount of time remaining just call remaining function, convert the result to a string, and then use your favorite text rendering functions to get the result to the screen. (which I don't know how to do in openGL)

Share this post


Link to post
Share on other sites
I should also say that what I wrote may be far more than you need. A far simpler method is just this:
if(youWantATimer)
{
endTime = getTime() + totalTimeToWait;
}
// stuff
if(getTime() > endTime)
{
// perform event
}

string displayTime = ConvertIntToString(endTime - getTime());
print(dispalyTime, x, y);

Share this post


Link to post
Share on other sites

This topic is 3668 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this