Click to See Complete Forum and Search --> : Reducing CPU Usage while moving images
Gordi0075000
February 22nd, 2009, 03:20 PM
Hi everyone, i'm pretty new to C#, and i'm using .NET 2.0.
I'm building a small game involving a moving background. The background is constantly moving by Timer, and consists of 2 pictureboxes - an upper bound and a lower bound.
I just move the picture boxes using the row
picUpperBound.Left -= 3
Or something of the sorts.
When i move one picture, the form's drawing mechanism can handle it. but when i add another image to move, the cpu usage of my program bumps to 50%. I was wondering why exactly does this happen, and how can i fix this?
(I'm guessing it has something to do with the form constantly redrawing the images)
Thanks in advance,
Sagie
P.S. Both images need to move at the same pace, so maybe there is some way of making the form Invalidate after both have moved, instead of redrawing itself with each Picturebox's movement.
BigEd781
February 22nd, 2009, 04:07 PM
What is the interval on your timer? I did something just last week that woul be more processor intensive than this. I had multiple images coming in from a server which I had to false color, sum, and then paint on a timer. I managed to keep that to about 30% CPU use, so we can figure something out. MY guess is the same as yours; too many redraws.
Gordi0075000
February 22nd, 2009, 10:19 PM
i'm running the timer at an iterval of 20.
Increasing the interval does reduce the amount of redraws and thus reduces CPU usage, but at a certain point the animation becomes jagged and influient.
What baffles me, is that while moving just one picturebox, the cpu usage was 1% and then i added one more and it leaped to 50!! any explaination anyone?
BigEd781
February 23rd, 2009, 02:14 AM
20 ms is a very fast rate at which to scroll the image i this if for a game. If the CPU use is disproportional when you have both moving, it may be a bug in the code. Can we see how you are doing this?
Gordi0075000
February 23rd, 2009, 12:17 PM
sure, no problem. when moving just one of the images, the cpu usage of the process is at 0%. when moving the second as well, it bumps to 18%. (on an interval of 40)
this is the code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Left -= 3;
pictureBox2.Left -= 3;
}
}
sotoasty
February 23rd, 2009, 01:06 PM
Not exactly sure, but this could be an explanation.
Since the 20 is 20 microseconds, think about it this way. You CPU useage is probably updating every second. Now you code is executing every 20 microseconds. Let's say your code in the timer1_Tick takes 11 microseconds with 1 picturebox. So your code executes, then waits. Now, you add a new picturebox. You tick code now takes 21 microseconds. So your code executes, and the tick event gets fired BEFORE you tick event has finished. You have essentially tied up the processor. Since most computers now have multiple processers, 1 is now pegged at 100% and the total is 50%, and the other allows you to do other work.
That is the best guess I can come up with. Maybe try adding a timespan to check exactly how long the function takes. Then expand your timer interval to work around that time frame.
Gordi0075000
February 23rd, 2009, 02:17 PM
I see. That does sound sensible. So basically the only way i have to improve performance is increasing the interval?
Isn't there a way to cause the form to invalidate both pictureboxes silmutaneuosly, instead of invalidating seperately for each picturebox ??
BigEd781
February 23rd, 2009, 03:34 PM
I see. That does sound sensible. So basically the only way i have to improve performance is increasing the interval?
Isn't there a way to cause the form to invalidate both pictureboxes silmutaneuosly, instead of invalidating seperately for each picturebox ??
Nothing on a CPU happens simultaneously. It all happens in serial (on a single CPU). That is a very fast interval as it is now, so fast that I don't see how it could be desirable. Have you tried actually drawing the images o\nto the Graphics objects yourself instead of moving the actual control?
Gordi0075000
February 23rd, 2009, 10:12 PM
I tried that. I tried drawing it using an ImageAnimator, but it made the whole thing worse... my cpu usage was at 50% with just 1 image.... But that's probably 'cause i'm doing it wrong. I don't have any experience working with draphics in c#.
Do you think that drawing the image myself with the Graphics object would be more efficient than moving the control ?
BigEd781
February 23rd, 2009, 11:05 PM
I guess that I just don't see moving the picturebox as a great way to make a game because it will get kind of clunky to maintain. If you have an image as your background, you can load the whole thing into memory on start up and keep it around. You can then paint portions of that image that fit the screen depending on where the character is. I am not sure what type of game that you are trying to make, so I could be off on the specifics, but I can show you how to draw an image.
namespace MyGame
{
public class myPicBox : PictureBox
{
private Bitmap m_bgImage;
public myPicBox()
{
InitializeComponent();
m_bgImage = new Bitmap("C:\BG_Image.png")
}
override OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// figure out the source rectangle from which to copy to the
// Graphics object. Your background image could be large,
// but you are only painting a portion of it into the control.
srcRect = this.ClientRectangle;
e.Graphics.DrawImage(m_bgImage, destRect, srcRect, GraphicsUnit.Pixel);
}
}
}
toraj58
February 25th, 2009, 05:16 PM
using double buffer technique can boost your performance.
add this code to your form load event:
private void Form1_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.