psychonaut
psychonaut

Reputation: 101

Making a Timer in c++?

I am developing a simple game in c++, a chase-the-dot style one, where you must click a drawn circle on the display and then it jumps to another random location with every click, but I want to make the game end after 60 seconds or so, write the score to a text file and then upon launching the program read from the text file and store the information into an array and somehow rearrange it to create a high score table. I think I can figure out the high score and mouse clicking in a certain area myself, but I am completely stuck with creating a possible timer. Any help appreciated, cheers!

Upvotes: 10

Views: 21882

Answers (4)

user2808359
user2808359

Reputation: 37

Try this one out:

//Creating Digital Watch in C++
#include<iostream>
#include<Windows.h>
using namespace std;

struct time{

int hr,min,sec;
};
int main()
{
time a;
a.hr = 0;
a.min = 0;
a.sec = 0;

for(int i = 0; i<24; i++)
{
    if(a.hr == 23)
    {
        a.hr = 0;
    }

    for(int j = 0; j<60; j++)
    {
        if(a.min == 59)
        {
            a.min = 0;
        }

        for(int k = 0; k<60; k++)
        {
            if(a.sec == 59)
            {
                a.sec = 0;
            }

            cout<<a.hr<<" : "<<a.min<<" : "<<a.sec<<endl;
            a.sec++;
            Sleep(1000);
            system("Cls");
        }
    a.min++;

}

    a.hr++;
}

}

Upvotes: 0

Howard Hinnant
Howard Hinnant

Reputation: 218700

In C++11 there is easy access to timers. For example:

#include <chrono>
#include <iostream>

int main()
{
    std::cout << "begin\n";
    std::chrono::steady_clock::time_point tend = std::chrono::steady_clock::now()
                                               + std::chrono::minutes(1);
    while (std::chrono::steady_clock::now() < tend)
    {
        // do your game
    }
    std::cout << "end\n";
}

Your platform may or may not support <chrono> yet. There is a boost implementation of <chrono>.

Upvotes: 19

sehe
sehe

Reputation: 392833

Without reference to a particular framework or even the OS this is unanswerable.

In SDL there is SDL_GetTicks() which suits the purpose.

On linux, there is the general purpose clock_gettime or gettimeofday that should work pretty much everywhere (but beware of the details).

Win32 API has several function calls related to this, including Timer callback mechanisms, such as GetTickCount, Timers etc. (article)

Using timers is usually closely related to the meme of 'idle' processing. So you'd want to search for that topic as well (and this is where the message pump comes in, because the message pump decides when (e.g.) WM_IDLE messages get sent; Gtk has a similar concept of Idle hooks and I reckon pretty much every UI framework does)

Upvotes: 1

c-smile
c-smile

Reputation: 27450

Usually GUI program has so called "message pump" loop. Check of that timer should be a part of your loop:

while(running)
{
  if( current_time() > end_time )
  {
    // time is over ...
    break;
  }
  if( next_ui_message(msg) )
    dispatch(msg); 
}

Upvotes: 0

Related Questions