countdown timer!

Started by
4 comments, last by nobodynews 16 years, 4 months ago
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!
Advertisement
you could try being more specific about things, like, i dunno, what language you are using perhaps?
i am using opengl
Moved to For Beginners.
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;  }};// laterstd::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)

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

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;}// stuffif(getTime() > endTime){  // perform event}string displayTime = ConvertIntToString(endTime - getTime());print(dispalyTime, x, y);

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement