Drawing speed problems...

Apr 17 2004 2:11 PM
Ok, I'm creating a game. Nothing too fancy, no dreams of commercial success, I'm doing it to teach myself multithreading. So I'm not going to sit here and hype how great my code is, especially since I have a problem. A big problem. I've tried it with both a panel and no panel. I'm trying to draw the "area" of the level that the player is in to the screen. So I have the program create the graphic, but when I tell the image to draw, it is SO slow. Three seconds sometimes. This is a bit of a problem. Ok, the exact problem is that I have an image, a "ship" that is in the center of the screen, that I rotate, etc. I read a file, generate the level image and everything, and then take the relevant "chunk" and drawing it to the screen. I figured this would be faster than either drawing the entire image, or drawing through a for-loop or something. No matter what I do though, it doesn't get fast, and in some cases it stops clearing what was there before, and just draws over top of what was there before! How can I get this moving quickly? I've seen someone set a label image as the level and just MOVE the entire label, which I could do, but that seemed even SLOWER, even though it was really fast for him (am I doing something wrong there?) Here's my CURRENT graphics code (I've been changing it very fast!) private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g=e.Graphics; //label1.Text=string.Format("{0}",playerspeed); //int beginingofdrawwidth = playerdata.Getlocationx()-(this.Width/2); //int endofdrawwidth = playerdata.Getlocationx()+(this.Width/2); //int beginingofdrawheight = playerdata.Getlocationy()-(this.Height/2); //int endofdrawheight = playerdata.Getlocationy()+(this.Height/2); //Obviously don't draw anything if the level doesn't exist yet //This is because at the moment it comes up with a blank level at startup. if (levelimage!=null) { //Grab the relevant chunk, and draw it! Bitmap background=new Bitmap(levelimage,this.Width,this.Height); using(Graphics b = Graphics.FromImage(background)) { Rectangle sourceRectangle = new Rectangle(playerdata.Getlocationx()-this.Width/2, playerdata.Getlocationy()-this.Width, this.Width, this.Height); b.DrawImage(levelimage,0,0,sourceRectangle,System.Drawing.GraphicsUnit.Pixel); } g.DrawImage(background,0,0); } // for (int x=0; x

Answers (4)