|
-
February 22nd, 2009, 04:20 PM
#1
Reducing CPU Usage while moving images
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.
Last edited by Gordi0075000; February 22nd, 2009 at 11:27 PM.
-
February 22nd, 2009, 05:07 PM
#2
Re: Reducing CPU Usage while moving images
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.
-
February 22nd, 2009, 11:19 PM
#3
Re: Reducing CPU Usage while moving images
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?
-
February 23rd, 2009, 03:14 AM
#4
Re: Reducing CPU Usage while moving images
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?
-
February 23rd, 2009, 01:17 PM
#5
Re: Reducing CPU Usage while moving images
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;
}
}
-
February 23rd, 2009, 02:06 PM
#6
Re: Reducing CPU Usage while moving images
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.
-
February 23rd, 2009, 03:17 PM
#7
Re: Reducing CPU Usage while moving images
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 ??
-
February 23rd, 2009, 04:34 PM
#8
Re: Reducing CPU Usage while moving images
 Originally Posted by Gordi0075000
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?
-
February 23rd, 2009, 11:12 PM
#9
Re: Reducing CPU Usage while moving images
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 ?
-
February 24th, 2009, 12:05 AM
#10
Re: Reducing CPU Usage while moving images
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.
Code:
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);
}
}
}
-
February 25th, 2009, 06:16 PM
#11
Re: Reducing CPU Usage while moving images
using double buffer technique can boost your performance.
add this code to your form load event:
Code:
private void Form1_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
Please rate my post if it was helpful for you.  Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|