# Logic-defying problem??

## Recommended Posts

DWORD start = GetTickCount(); DWORD sleeptime = SLEEP_TIME - (GetTickCount() - start); if(sleeptime > 0) { if(sleeptime != 50) GAMELOG("Sleeping %i\n",(int)sleeptime); // _sleep(sleeptime); } the macro GAMELOG leads to this: void cErrorLog::append(const char*pText,...) { va_list l; va_start(l,pText); //fprintf(mLog,pText,l); ::vfprintf(mLog,pText,l); fflush(mLog); va_end(l); } This code yields a log containing entries like this: Sleeping -903 and Sleeping -872 These entries are all surrounded by LOTS of "Sleeping 34". SLEEP_TIME is defined as ( 50 ) How can this be?? Thanks

##### Share on other sites
You can easily be overflowing the DWORD and your printing it with the %i specifier . which is for signed input so teh trace is misleading.

http://www.cplusplus.com/ref/cstdio/printf.html

Cheers
Chris

##### Share on other sites
if(GetTickCount()-start >= SLEEP_TIME)
{

}

##### Share on other sites
I do not see the point of this:

DWORD start = GetTickCount();
DWORD sleeptime = SLEEP_TIME - (GetTickCount() - start);

because GetTickCount is not going to change values over the course of these 2 lines of code being executed.

hence you are writing

DWORD start = GetTickCount();
DWORD sleeptime = SLEEP_TIME - (2 * start);

##### Share on other sites
Quote:
 Original post by moussen15DWORD start = GetTickCount();DWORD sleeptime = SLEEP_TIME - (GetTickCount() - start);if(sleeptime > 0){ if(sleeptime != 50) GAMELOG("Sleeping %i\n",(int)sleeptime); //_sleep(sleeptime);}

Hi,

Take care: you are handling DWORDs and therefore sleeptime is allways >= 0. Moreover, all negative values are >= 0x80000000 and therefore are not equal to 50. When it is printed, it uses the %i format (signed integer) and is transformed to a signed integer - thus the -903 and so on.

Plus: you should store sleeptime in a 64 bit integer. GetTickCount() return the uptime in milliseconds. If the machine runs for more than 39,7 days you'll may have start == 0xFFFFFFFFe and GetTickCount() == 0x00000001 (for example).

And you'll sleep for another 39,7 days.... :)

I believe more correct code should be:

// init__int64 start = (__int64)GetTickCount();// ....// then, later: __int64 current = (__int64)GetTickCount();if (current < start) {  current += __int64(1) << 32; // adds 0x0000 0001 0000 0000}__int64 sleeptime = __int64(SLEEP_TIME) - (current - start);if (sleeptime > 0) {  // do da bigstuff    _sleep(sleeptime);}

HTH,

##### Share on other sites
This code was quite simplified. Of course I had put stuff inbetween those two lines :D I thank you all for your replies. They've been quite helpful. Though I suppose I could have found them out myself if I only hadn't been so stubborn.

EDIT:
I didn't see your reply emmanuel. This is a supposed to be a game (well actually an editor for a game), so I'd like to have it run reasonably fast. I'm not very familiar with how much overhead an int64 needs to get converted to an int32 but would you still recommend this now that I've said its for a game-like app?? And seeing as it is a game, I don't think it is very probable that one would want to run it for 40 days in a row, heh

##### Share on other sites
Quote:
 Original post by moussen15EDIT:I didn't see your reply emmanuel. This is a supposed to be a game (well actually an editor for a game), so I'd like to have it run reasonably fast. I'm not very familiar with how much overhead a int64 needs to get converted to a int32 but would you still recommend this now that I've said its for a game-like app?? And seeing as it is a game, I don't think it is very probable that one would want to run it for 40 days in a row.

Well, have a look to the code: there is 5 operation which involves 64 bit integers. Do you think you will blew up your code ? :) (hint: this may possibly add a 0.0001 ms overhead for each frame).

For the 40 day thingy: I never stop my computer. He may has run for more than 39 day before I start playing to your game. You can't assume that I will stop my computer each time I want to play to your game :)

Regards,

##### Share on other sites
hahah ok, thank you I think I will go with your solution. Thing is I'm not into how stuff gets computed at all (I'm only 15 so I haven't come to that level yet I suppose) so I am glad you were kind enough to explain it to me.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627653
• Total Posts
2978440

• 10
• 12
• 22
• 13
• 33