Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 16, 2013, 10:26 PM   #1
ArtOfWarfare
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Pacing (as in timing or delaying) in C++ console?

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.)
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   0 Reply With Quote
Old Mar 17, 2013, 09:35 AM   #2
ArtOfWarfare
Thread Starter
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   0 Reply With Quote
Old Mar 17, 2013, 10:23 PM   #3
N-M
macrumors newbie
 
Join Date: Jan 2013
Quote:
Originally Posted by ArtOfWarfare View Post
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-physic...your-timestep/
http://www.gamedev.net/topic/626794-...ps-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/art...81386&seqNum=2
http://stackoverflow.com/a/5524138/859774
N-M is offline   0 Reply With Quote
Old Mar 17, 2013, 10:56 PM   #4
ArtOfWarfare
Thread Starter
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
All of that comes from C++11 which has only limited support on Windows, I thought.
ArtOfWarfare is offline   0 Reply With Quote
Old Mar 18, 2013, 07:59 AM   #5
N-M
macrumors newbie
 
Join Date: Jan 2013
Quote:
Originally Posted by ArtOfWarfare View Post
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-sup...c-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/Visual...igh-resolution

Last edited by N-M; Mar 18, 2013 at 08:06 AM.
N-M is offline   0 Reply With Quote
Old Mar 18, 2013, 09:07 AM   #6
ArtOfWarfare
Thread Starter
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Is it supported by Dev-C++/MinGW? That's what the TAs in class use to compile our submitted assignments.
ArtOfWarfare is offline   0 Reply With Quote
Old Mar 18, 2013, 12:51 PM   #7
N-M
macrumors newbie
 
Join Date: Jan 2013
Quote:
Originally Posted by ArtOfWarfare View Post
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/...dnt-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 :-)
N-M is offline   0 Reply With Quote
Old Mar 18, 2013, 03:18 PM   #8
ArtOfWarfare
Thread Starter
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by N-M View Post
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/...dnt-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.
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:06 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC