Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,710
6,304
Hi,

I need to animate something in a C++ console application (it's a class assignment.) Right now I just print out all the frames back to back as quickly as possible, but it has two issues:

1 - Several "frames" end up being visible half drawn (I suppose we can call it a "screen tear".)
2 - The "frames" go by much too quickly on some (most) computers.

What would be the best way of pacing in a C++ console application? I want it to be cross platform and not waste system resources (IE, locking the entire computer up during the animation would be undesirable.)
 
This is what I now have:

With my other headers:
Code:
#if defined _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

My actual code:
Code:
	for (unsigned long i = 0, n = moves.size(); i < n; i++)
	{
		// ... code calculating position values
		print(endX, endY, x, y); // function that prints a frame
#if defined _WIN32
        Sleep(100);
#else
        usleep(100000);
#endif

It's not elegant, but it works on OS X and I hope every other system (I haven't tested it on a windows machine yet.)

The output seems pretty nice, and usleep doesn't seem to block me from doing other things between frames. It remains to be seen whether the windows code also works.
 
Hi,

I need to animate something in a C++ console application (it's a class assignment.) Right now I just print out all the frames back to back as quickly as possible, but it has two issues:

1 - Several "frames" end up being visible half drawn (I suppose we can call it a "screen tear".)
2 - The "frames" go by much too quickly on some (most) computers.

What would be the best way of pacing in a C++ console application? I want it to be cross platform and not waste system resources (IE, locking the entire computer up during the animation would be undesirable.)

Regarding sleeping -- you don't really need platform-specific functions anymore, see http://en.cppreference.com/w/cpp/thread/sleep_for

That being said, in general you need to use a timer:
http://gafferongames.com/game-physics/fix-your-timestep/
http://www.gamedev.net/topic/626794-consistent-gaming-loops-and-clocks/

I'd recommend using chrono for this:
http://en.cppreference.com/w/cpp/chrono

For more details, see:
http://www.informit.com/articles/article.aspx?p=1881386&seqNum=2
http://stackoverflow.com/a/5524138/859774
 
All of that comes from C++11 which has only limited support on Windows, I thought.
 
All of that comes from C++11 which has only limited support on Windows, I thought.

No worries, <chrono> is supported:
http://cpprocks.com/c11-compiler-support-shootout-visual-studio-gcc-clang-intel/

There's only an issue with std::chrono::high_resolution_clock in VS11[*] -- you can either:
(a) use the workaround here: http://stackoverflow.com/a/5524138/859774,
(b) use Boost.Chrono -- http://www.boost.org/libs/chrono/ -- a fully compatible drop-in replacement, which also doesn't suffer from any issues (and will work with C++98 compilers).


[*] upvote to make it higher priority: https://connect.microsoft.com/Visua...esolution-clock-does-not-have-high-resolution
 
Last edited:
Is it supported by Dev-C++/MinGW? That's what the TAs in class use to compile our submitted assignments.
 
Is it supported by Dev-C++/MinGW? That's what the TAs in class use to compile our submitted assignments.

I just hope you aren't using the out-of-date and un-maintained Bloodshed Dev-C++ IDE from 2005? It's 2013 now. 8 years makes a world of difference in programming.

See the following for better alternatives:
http://clicktobegin.net/programming/why-you-shouldnt-use-dev-c/

If you're using one of the current and maintained versions (e.g., Orwell Dev-C++, or wxDev-C++), they should ship with a relatively recent GCC (MinGW is just a Windows port of GCC) and you should be just fine :)
 
I just hope you aren't using the out-of-date and un-maintained Bloodshed Dev-C++ IDE from 2005? It's 2013 now. 8 years makes a world of difference in programming.

See the following for better alternatives:
http://clicktobegin.net/programming/why-you-shouldnt-use-dev-c/

If you're using one of the current and maintained versions (e.g., Orwell Dev-C++, or wxDev-C++), they should ship with a relatively recent GCC (MinGW is just a Windows port of GCC) and you should be just fine :)

I believe they use Bloodshed. This isn't me using it - I always use Clang w/ Xcode on Mac or GCC w/ gedit on Linux (and I just never program in C or C++ on Windows), but the TAs who grade the submitted code use MinGW w/ Bloodshed Dev-C++ to test our code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.