This is a timer class that I wrote a while ago (cleaned up a bit for the snippet) for adding delays in some of the games I have written over the years.
Note: I normally have the class functions in a .cpp file but since it looks like I can only post in one 'file' I merged them together in what would be a .h file.
- Nov 17, 2011 Using sleep in dev cpp Sometime we require a screen to stay for a while and moreover we don't want to use key pressing to move, we just want to use the concept of sleep for a while then its quite easy in dev we just need to write the code.
- The command to do so from Dev-C is Alt-P. Choose the Parameters tab from the pop-up window and type the following into the Linker area: -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 You can now compile and run programs that use the WinBGIm graphics library, such as this one that opens a.
Oct 07, 2008 The problem is that i don't use Visual C, I use Borland C 5.0. Surely you can access the Windows standard library with any C or C compiler? Sleep is a basic windows function accessible from both compilers. The point is the 'sleep' is not an available function in standard C. If you want it available, go to a newsgroup that deals with. Delay in C: delay function is used to suspend execution of a program for a particular time. Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the 'dos.h' header file which is not a part of standard C library.
The timer basically counts up in miliseconds and can be paused/resumed while maintaining the amount of miliseconds elapsed while the timer was active.
Here is a basic example of counting from 1 to 10 (FIRE!) with a 1 second delay between each increment.
9,127 ViewsLet's start with the design of the class.
This is kind of an equivalent design issue, but in many cases one would want the timer to start immediately without having to explicitly call Start(). Perhaps a constructor parameter that tells it to start or not?
I have a beef with your choice of the name GetTicks() because it exposes the implementation and also goes against your claim that this timer works in intervals of milliseconds. If you're guaranteeing millisecond resolution, then call it Milliseconds() or simply Elapsed().
Another unnecessary exposure of the implementation is the return type of clock_t.
You provide a Start(), Stop(), Pause(), Resume(), and Reset(), but Elapsed() is only effective when the timer is running. The interface itself implies different behavior than is actually offered. Elapsed() in this case should return the result of the previous or current bout of timing (depending on whether the timer is running or not), in my opinion.
I think the interface is too complicated, actually. Still using the clock_t implementation (which I don't agree with in principle), something simpler and more dummy-proof would be better for a what essentially constitutes a stopwatch. In fact, stopwatch is a better name for the class. ;)
Consider the following:
Dev Delay Icd 10
This addresses my issues with the interface, but there's still the underlying problem of assuming that clock_t always has a resolution of milliseconds. That's not a safe assumption, and on systems where a 'clock tick' isn't anything close to a millisecond, your timer class will behave unexpectedly.
The correct solution is to use a proper system-specific method of acquiring milliseconds. The down side to that is your code will be non-portable, but there's really not a way of getting subsecond granularity with the standard C++ library (prior to '>C++11's <chrono> library, of course).
But let's assume that we're stuck with <ctime> for the implementation. One option of fitting clock_t into the range of milliseconds is to derive the resolution of clock_t from CLOCKS_PER_SEC:
There are a lot of non-portable assumptions in that implementation, but I think those assumptions are more likely to be true than CLOCKS_PER_SEC being equal to 1000 for all plausible systems on which the timer class is going to be used.
How To Use Delay Function In Dev C++
You may notice the added step of multiplying by 1000 to produce milliseconds. Another option for the interface is to work primarily with seconds, but offer that value as floating-point where the precision is where subsecond resolution can be found. For example:
How To Use Delay In Dev C++
This is a glomping of the previous Elapsed() but excluding the final milliseconds step and just returning the seconds as a double. You can then compare for at least half a second with something like sw.Elapsed() >= .5
. That's just another option for the interface, as I've seen both an integer type and floating-point type for the result of the elapsed function in the wild.
Rashakil Fol978
Hello Sean Fuoco. You are now prohibited from using your timer class. By posting your code, you have granted DaniWeb an exclusive copyright license to your code according to DaniWeb's terms of service. You may no longer use it and have no rights to you code. Please delete your code from your computer. As the Terms of Service say:
Any and all information posted on DaniWeb may not be copied or used elsewhere, in any way, shape, or form, either on the Internet or in print, without prior written permission from Dani Horowitz.
Further transmission of your source code material, such as in a personal project or in handing in an assignment, may be prosecutable as criminal copyright infringement.
With my moderator hat on, I say:
Ignore Rash's post, he doesn't have the slightest clue what he's talking about, almost everything he said is wrong and misguided. Of course, you are free to use your code for personal projects or assignment submissions. And you still own copyrights to your code.
Sorry for the inconvenience (or scare), Rash is just throwing a tantrum against the terms of services. Why he would inflict this on innocent posters is beyond my comprehension.
sfuo111
Either way I modified it before posting it here so it doesn't matter.
Also, deceptikon showed that lots of parts of the code could be improved so I have to make many changes to it.
check this one:
- See the details at: http://www.programmingtunes.com/creating-timer-c/#sthash.j9WLtng2.dpuf
aniqbear0
What is the intended purpose of these nestled loops, along with the if-statements? I do not see any comments. Could you bother to explain your code?
dexblack_114
Grokked that last code post in <20 seconds.
Obvious to anyone, no comments required.
Pointless code though.
Runs in a DOS command prompt and prints a 24h clock timer.
I want a count down timer in the output of a program running seprately and my other program running seprately for example program asks me to enter a,b,c or d and at the same time a countdown timer should run and as soon as the time approches 0 the program should exit wether i have give the answer or not.
dexblack_114
Then you will need to write a second thread for the input loop.
The main thread can print the time to the screen via direct console writes rather than plain old std::cout or printf.
When the time runs out stop the input loop thread, join and terminate.
Comments are closed.