Game loop using 33% CPU (100% one core) out of nowhere

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 7405 Views - Last Post: 20 June 2011 - 08:28 AM Rate Topic: -----

#1 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 10:12 AM

Hi,

Yesterday my program was running fine. But when I launched it today (without changing anything) my fps was 35.4 instead of a constant 60. So when I looked at cpu usage one core was at 100%, why does my game loop suddenly starts hogging my cpu? (NOTE: I also tried another project with EXACTLY the same game loop and it does not happen)
Here is my game loop:
	while(Running)
	{
		PeekMessage(&msg,NULL,0,0,PM_REMOVE);
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		switch(msg.message)
		{
		case WM_QUIT:
				Running = false;
			break;
		case WM_INPUT:
                        //Working on this one
			break;
		}

		Render(WIDTH,HEIGHT);
	}



Its very weird my loop suddenly started doing this. The exact same loop does not do this in another project like I said.

Is This A Good Question/Topic? 0
  • +

Replies To: Game loop using 33% CPU (100% one core) out of nowhere

#2 Camerosaurus Rex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 06-June 11

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 10:31 AM

I know it may seem simple, but did you reboot and try it again?
Was This Post Helpful? 0
  • +
  • -

#3 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 10:39 AM

Hehe good one :)
Not yet, going to try it now. I will post the results.

EDIT: Nope, didn't work, I didn't expect it to work because like I said, the loop works fine in another project. Also I had my computer on for about 2-3 hours so I don't think it would be that internal counter of the cpu.


EDIT2:Okay, now it gets weird.

I copied the ENTIRE project to another one and now it runs fine? How is that possible? I did not change any settings or stuff...

EDIT3:
Okay... As soon as I loaded in the model it began using 100% cpu again. Does anyone know why? Here is the source:http://www.mediafire...ui2dkk980n5an3j

There is too much code to post here so if anyone would like to check it out you would need to download that rar file.

This post has been edited by JL29: 17 June 2011 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 11:13 AM

I can't edit so sorry for the double post... but yes I know there is almost no error checking :)
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 12:14 PM

I'm not going to download your project, but the most likely reason is that you have no timing in your code. For instance your game loop should be ticking over at around 60fps or faster depending what what is going on in your game.
Was This Post Helpful? 1
  • +
  • -

#6 Camerosaurus Rex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 06-June 11

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 12:17 PM

View PostButchDean, on 17 June 2011 - 12:14 PM, said:

I'm not going to download your project, but the most likely reason is that you have no timing in your code. For instance your game loop should be ticking over at around 60fps or faster depending what what is going on in your game.


Good eye.
I don't know why but I totally overlooked that.
Sometimes compilers will default a 60 fps, for whatever reason it stopped doing so. Either way it's a good idea to put some code in there for compatibility purposes.
Was This Post Helpful? 1
  • +
  • -

#7 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 12:20 PM

View PostCamerosaurus Rex, on 17 June 2011 - 12:17 PM, said:

View PostButchDean, on 17 June 2011 - 12:14 PM, said:

I'm not going to download your project, but the most likely reason is that you have no timing in your code. For instance your game loop should be ticking over at around 60fps or faster depending what what is going on in your game.


Good eye.
I don't know why but I totally overlooked that.
Sometimes compilers will default a 60 fps, for whatever reason it stopped doing so. Either way it's a good idea to put some code in there for compatibility purposes.


Ah okay,

Time to put that in then :)
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 12:24 PM

View PostCamerosaurus Rex, on 17 June 2011 - 08:17 PM, said:

Good eye.
I don't know why but I totally overlooked that.
...

Nothing more than experience. :)

View PostJL29, on 17 June 2011 - 08:20 PM, said:

View PostCamerosaurus Rex, on 17 June 2011 - 12:17 PM, said:

View PostButchDean, on 17 June 2011 - 12:14 PM, said:

I'm not going to download your project, but the most likely reason is that you have no timing in your code. For instance your game loop should be ticking over at around 60fps or faster depending what what is going on in your game.


Good eye.
I don't know why but I totally overlooked that.
Sometimes compilers will default a 60 fps, for whatever reason it stopped doing so. Either way it's a good idea to put some code in there for compatibility purposes.


Ah okay,

Time to put that in then :)

Good, then after that you might need to look into multithreading to make things run even smoother. :)
Was This Post Helpful? 0
  • +
  • -

#9 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 12:53 PM

View PostButchDean, on 17 June 2011 - 12:24 PM, said:

View PostCamerosaurus Rex, on 17 June 2011 - 08:17 PM, said:

Good eye.
I don't know why but I totally overlooked that.
...

Nothing more than experience. :)

View PostJL29, on 17 June 2011 - 08:20 PM, said:

View PostCamerosaurus Rex, on 17 June 2011 - 12:17 PM, said:

View PostButchDean, on 17 June 2011 - 12:14 PM, said:

I'm not going to download your project, but the most likely reason is that you have no timing in your code. For instance your game loop should be ticking over at around 60fps or faster depending what what is going on in your game.


Good eye.
I don't know why but I totally overlooked that.
Sometimes compilers will default a 60 fps, for whatever reason it stopped doing so. Either way it's a good idea to put some code in there for compatibility purposes.


Ah okay,

Time to put that in then :)

Good, then after that you might need to look into multithreading to make things run even smoother. :)


Do you happen to know what is the best way to do the timing?
I'm reading something about delta time etc. And sleeping the time that the loop is not really needed. Or am I thinking wrong now?
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 12:59 PM

What are you reading about delta time?
Was This Post Helpful? 0
  • +
  • -

#11 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 01:00 PM

it's a windows only solution but this will get your delta time


//before loop
__int64 countsPerSec = 0;
QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);

float secsPerCount = 1.0f / (float)countsPerSec;

__int64 prevTimestamp = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&prevTimestamp);

// inside loop
__int64 currentTimestamp =  0;
QueryPerformanceCounter((LARGE_INTEGER*)&currentTimestamp);
float dt = (currentTimestamp  - prevTimestamp)* secsPerCount;

// do your loop passing in dt as needed

prevTimestamp = currentTimestamp;



You might want to wrap this up a bit as well, because it's a bit ugly :)
Was This Post Helpful? 2
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 01:03 PM

View Poststayscrisp, on 17 June 2011 - 09:00 PM, said:

it's a windows only solution but this will get your delta time


//before loop
__int64 countsPerSec = 0;
QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);

float secsPerCount = 1.0f / (float)countsPerSec;

__int64 prevTimestamp = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&prevTimestamp);

// inside loop
__int64 currentTimestamp =  0;
QueryPerformanceCounter((LARGE_INTEGER*)&currentTimestamp);
float dt = (currentTimestamp  - prevTimestamp)* secsPerCount;

// do your loop passing in dt as needed

prevTimestamp = currentTimestamp;



You might want to wrap this up a bit as well, because it's a bit ugly :)

Wow! there is a much cleaner solution to this, but let's see what JL29 has been researching. :)
Was This Post Helpful? 0
  • +
  • -

#13 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 01:10 PM

Well, That delta time I read about is to compensate the A.I. code etc. when it could get run to fast or to slow from my understanding. For instance when you move a sprite and because of a coding problem it takes to long to calculate a frame, that frame is or "lost", or you could use delta time to compensate it.

Also, I see that checking if a frame is finished, and 1sec. has not yet passed , then sleeping for the rest of the time, is a way to time the loop.(Awkward sentence, I know) At least, that's what it looks like.

EDIT: The problem I want to solve first is that the cpu lowers my fps by more then 50%!!! when I import a model. I'm going to take a look at the examples of the directx sdk too, to see if I can find out how they did it :)

This post has been edited by JL29: 17 June 2011 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 01:19 PM

View PostJL29, on 17 June 2011 - 09:10 PM, said:

Well, That delta time I read about is to compensate the A.I. code etc. when it could get run to fast or to slow from my understanding. For instance when you move a sprite and because of a coding problem it takes to long to calculate a frame, that frame is or "lost", or you could use delta time to compensate it.

Also, I see that checking if a frame is finished, and 1sec. has not yet passed , then sleeping for the rest of the time, is a way to time the loop.(Awkward sentence, I know) At least, that's what it looks like.

Yes, this is why I mentioned back in this post about multithreading. Right now, though, for the games that you are likely to be writing you shouldn't need it. What you need is to get the game loop to tick over roughly once every 1/60th of a second, so on game launch you will initialize the timer and then check to see if 1/60th of a second has past then reinitialize the time if it has and do whatever game actions are supposed to happen. If not you re-evaluate the game loop to check if it has quit then check the elapsed time again.

Check out GetDisplayFrequency(), GetTickCount(), and timeGetTime(). :)
Was This Post Helpful? 1
  • +
  • -

#15 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Game loop using 33% CPU (100% one core) out of nowhere

Posted 17 June 2011 - 01:21 PM

Thank you very much for the help :) I'm going to look into those functions! But first it is time to sleep here :) G'night.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3