Skip to main content
GameDev.net gamedev.net
🔒 Locked

A C++ Timer?

Started by Koolchamp May 25, 2006 at 6:42 PM 3 replies 27.1k views
Original Post
Koolchamp
Koolchamp
Right now I'm designing a text-base console game. One thing that I need is a Timer. So say you ask the user to solve and enter the solution to this equation: (2 * 3) + 5. But, the user only has 7 seconds to answer it. If he/she gets it in 5 seconds, he/she wins. If it takes more than 7 seconds, he/she looses. What I want to know, is how would you make that happen. Is there a function in C++ that I could use (haven't found or heard of one at this point)? Do I create my own function to make it happen (if so, how? Where would I start)? Anyway, if anyone could provide any insight or ideas, I would appriciate it. Thanks.
Cheers,Ken(Koolchamp)_____"Choose a job you love, and you will never have to work a day in your life." - Confucius"If you don't have a game industry job and you want to go to E3, do what everyone else does. Launch a game review website and call yourself press." - Mike McShaffry(This is true for me) “…..I'm a geek and jocks are my natural enemy.” – Li C. Kuo
echohead
echohead
on unix, you can use 'time()' if you don't need sub-second precision

on windows, you can use 'timeGetTime()'
taby
taby
The standard clock() function can be used for sub-second precision timing.
Adam Hamilton
Adam Hamilton
If you are using a windows OS then you can use GetTickCount() which increments every millisecond. It is really convienient :)
simon10k
simon10k
I think this will help you.

timer.h
#ifndef _TIMER_#define _TIMER_#include <ctime>class Timer {    clock_t counter;public:    Timer(): counter(0) {};    bool elasped(clock_t ms)    {        clock_t tick = std::clock();        if(tick - counter >= ms)        {             counter = tick;             return true;        }        return false;    }};#endif 


main.cpp
#include <iostream>#include "timer.h"int main(){    Timer timer;    while(true)    {        if(timer.elasped(1000))            std::cout << "ding!\n";    }} 
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.