Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
wait a bit Posted by profchuck on 20 May 2007 at 10:15 AM
I am trying to create a very simple console app game. When I run the game the console screen goes all wacko because the game operates to fast. I tried using this function:
#include <time.h>

. . .
#define DELAY 0.1//I think 0.1 is about right

. . .
void pause(void)
{
  time_t then;

  time(&then);
  while(difftime(time(NULL),then) < DELAY);
}

but pause() waited for 0.5 seconds in stead of 0.1 witch is to slow. Is there any way to wait less than 0.5 seconds

I am working in windows XP and using minGW-3.1.0-1 as a compiler.
Report
Re: wait a bit Posted by MT2002 on 20 May 2007 at 11:27 AM
: I am trying to create a very simple console app game. When I run the
: game the console screen goes all wacko because the game operates to
: fast. I tried using this function:
:
: #include <time.h>
:
: . . .
: #define DELAY 0.1//I think 0.1 is about right
:
: . . .
: void pause(void)
: {
: time_t then;
:
: time(&then);
: while(difftime(time(NULL),then) < DELAY);
: }[/code]:

: but pause() waited for 0.5 seconds in stead of
: 0.1 witch is to slow. Is there any way to wait less than 0.5 seconds
:
: I am working in windows XP and using minGW-3.1.0-1 as a compiler.
:
The code looks fine. I would just modify DELAY until the speed is to your
liking. (ie, mabey try 0.05?)

You can also try another timing routine (sleep(), timeGettime(), etc..)
Report
Re: wait a bit Posted by profchuck on 20 May 2007 at 2:12 PM
: : I am trying to create a very simple console app game. When I run the
: : game the console screen goes all wacko because the game operates to
: : fast. I tried using this function:
: :
: : #include <time.h>
: :
: : . . .
: : #define DELAY 0.1//I think 0.1 is about right
: :
: : . . .
: : void pause(void)
: : {
: : time_t then;
: :
: : time(&then);
: : while(difftime(time(NULL),then) < DELAY);
: : }[/code]: :

: : but pause() waited for 0.5 seconds in stead of
: : 0.1 witch is to slow. Is there any way to wait less than 0.5 seconds
: :
: : I am working in windows XP and using minGW-3.1.0-1 as a compiler.
: :
: The code looks fine. I would just modify DELAY until the speed is to
: your
: liking. (ie, mabey try 0.05?)
:
: You can also try another timing routine (sleep(), timeGettime(),
: etc..)
:
I can't find sleep() or timeGettime() in minGW's include file and something in my code is rounding DELAY to the nearest half second. I need to find a way to count in smaller bits of seconds
Report
Re: wait a bit Posted by MT2002 on 20 May 2007 at 8:55 PM
: I can't find sleep() or
: timeGettime()
in minGW's include file and something in my
: code is rounding DELAY to the nearest half second. I need to find a
: way to count in smaller bits of seconds
:
My mistake--Sleep() and timeGettime() are both in windows.h.

Nontheless, this should work:

void pause (void) {

         clock_t start_time, cur_time;
         start_time = clock();
         while((clock() - start_time) < DELAY * CLOCKS_PER_SEC);
}


By changing DELAY you can achieve resolution greater then 1 second
using this code.
Report
Re: wait a bit Posted by profchuck on 21 May 2007 at 7:53 PM
: : I can't find sleep() or
: : timeGettime()
in minGW's include file and something in my
: : code is rounding DELAY to the nearest half second. I need to find a
: : way to count in smaller bits of seconds
: :
: My mistake--Sleep() and timeGettime() are both in windows.h.
:
: Nontheless, this should work:
:
:
: 
: void pause (void) {
: 
:          clock_t start_time, cur_time;
:          start_time = clock();
:          while((clock() - start_time) < DELAY * CLOCKS_PER_SEC);
: }
: 
:
:
: By changing DELAY you can achieve resolution greater then 1 second
: using this code.
:
tank you vary much, your version of pause() worked perfectly and my game is on the verge of being fun!(for a console app game)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.