Main reason I want to find this out is I want to find out something that's equivalent to it that suspends the execution of the current thread until the time-out interval elapses.
I'm pretty sure all functions like that are API-dependent, unless you use a library like Boost. Or are you meaning that the Sleep() function doesn't really guarantee waiting for that exact period of time?
static LONGLONG s_TimerTicksPerSecond = 0;
static LONGLONG s_TimerTicksPerMillisecond = 0;
static LONGLONG s_TimerStart = 0;
static LONGLONG s_TimerStop = 0;
void TimerInitialise();
void TimerStart();
void TimerStop();
int TimerElapsedMS();
void main()
{
TimerInitialise();
//Create Stuff here...
//Populate map / create struct/characters
TimerInitialise();
do
{
TimerStart();
for(int y = 0; y < 21; ++y)
{
for(int x = 0; x < 78; ++x)
{
printf("%c", map[y][x]);
}
printf("\n");
}
printf("\n\tLanders Y: %d, X: %d | Invaders Y: %d, X: %d | Bullets Y: %d, X: %d ", sPlayer.y, sPlayer.x, sAlien.y, sAlien.x, sBullet.y, sBullet.x );
do
{
TimerStop();
} while (TimerElapsedMS() < 1);
Sleep(5 - TimerElapsedMS());
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
}while( gameRunning == true );
}
//Timer Functions
void TimerInitialise()
{
// Returns ticks per second
QueryPerformanceFrequency( (LARGE_INTEGER*) &s_TimerTicksPerSecond);
s_TimerTicksPerMillisecond = s_TimerTicksPerSecond / 1000;
}
void TimerStart()
{
QueryPerformanceCounter( (LARGE_INTEGER*) &s_TimerStart);
}
void TimerStop()
{
QueryPerformanceCounter( (LARGE_INTEGER*) &s_TimerStop);
}
// Returns the time elapsed between TimerStart and TimerStop, measured in Milliseconds
int TimerElapsedMS()
{
LONGLONG t;
t = s_TimerStop - s_TimerStart;
t /= s_TimerTicksPerMillisecond;
return (int) t;
}
Ok, so basically whats happening is when I run this the game will just sit still after one display of the for loops on line 24 - 31 (with the code exactly like this how it is this is not all my code).
Heres the problem:
The game will run if I comment out line 39 (The sleep function) - runs perfect but hogs the cpu.
Now if I leave that line there(line 39) and comment out lines "24 to 31" but leave line 32 So I can read the objects X and Y positions - The game will run fine yet again and I can see the X and Y positions change.
So my problem is how do I get this working without commenting anything out? I've used it on some other code and it works perfect. As in I'm not displaying a 2D array. But displaying something else like a 1D Array constantly looping its elements.
Fixed it changed the 5 to 100 and its all good. Was getting minus values. Haha uhm still if anyone knows anything else other than sleep to use though it would be helpful. Etc something i can make myself that kills the thread too.