[SOLVED]Can't Figure out whats wrong with sleep?

EDIT:

Scroll down to see code

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.
Last edited on
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?
Well this is the code I've got But Ill explain the exact error after I paste this chunk of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.
Topic archived. No new replies allowed.