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.
Jan 11, 2017 Since dev-cpp keeps seperate compiter Mingw while Tubo-c was using Borland. However there are several ways to implement/use delay function in cpp code. First way: code#include <time.h> void delay(int delay) int now=time(NULL); int later=now+. Mar 24, 2018 In this video, delay function is used to make animations in C graphics delay function is defined in dos.h header file All the details are given in the video.
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,002 Views
Let'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:
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.
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:
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.
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.
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:
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?
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. /auto-tune-efx-cheap.html.
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.
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.