Reputation:
Programs and versions i'm running:
Problem: When i update my game in below loop, i open task manager and see that the program is taking up 25 CPU! this is way too much.
The loop:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
if (angleCounter != 359)
angleCounter++;
else
angleCounter = 0;
base.Update(gameTime);
}
The loop makes an angle (degrees) increment so that the image (png) i'm drawing is spinning constantly
The drawing part:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// TODO: Add your drawing code here
Vector2 location = new Vector2(200, 200);
Rectangle sourceRectangle = new Rectangle(0, 0, sprite.Width, sprite.Height);
Vector2 origin = new Vector2(sprite.Width / 2, sprite.Height / 2);
float angle = (float)DegreeToRadianal(angleCounter);
spriteBatch.Draw(sprite, location, sourceRectangle, Color.White, angle, origin, 1f, SpriteEffects.None, 1);
spriteBatch.End();
base.Draw(gameTime);
}
EDIT: I have ran a profiler, this is what it says:
Also: My computer itself is not the problem, it is a very high end PC that should be able to run a program like this with 01 CPU
Upvotes: 0
Views: 2269
Reputation:
Okay, Based on you guys' great help and research on Google, I have found the following:
Monogame intends for the program to run as fast as it can, instead of "saving" the computers' CPU. therefore it "reserves" 1 core (more cores when you get a really really high demand).
The difference between a normal program and a game is huge, normal programs intent to minimize the CPU, therefore the impact on the computer, games intent to make the game itself run smoother by making the computer work harder
Last: I should make clear what my intentions are, for example: I didn't state in this particular question that I wanted to make an as low impact CPU game while still keeping a smooth game (60fps)
Thank you all for helping me come to these conclusions by answering so quickly!
Upvotes: 1
Reputation: 19200
A universal factor in designing for performance is that you need a business-centric definition of "fast enough" to aim for. Does your user care if your game uses 25% of their CPU? No. Your user cares whether or not your game runs smoothly. Does your game run smoothly?
"the animation looks good, nice and smooth"
Looks like your program runs fast enough!
Don't waste time fixing things that aren't broken
That means your program basically says to your computer "Run this loop as fast as you possibly can!"
Given that, how do you arrive at your conclusion that your computer "should be able to run a program like this" with 1% CPU utilisation?
It seems quite self-evident that a rate-unlimited loop will cause 100% CPU utilisation irrespective of clock speed (=25% on a 4-core machine like yours)!
If for some reason it's very very important to you that the game not run as fast as possible, you could try setting:-
IsFixedTimeStep = true;
in your game constructor, but make sure you clearly understand the implications of a fixed vs a variable time step:-
Upvotes: 6