Logic-defying problem??
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
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
http://www.cplusplus.com/ref/cstdio/printf.html
Cheers
Chris
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);
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);
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,
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.
Anyway thanks for your time
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
Anyway thanks for your time
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
Quote:Original post by moussen15
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 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,
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement