Ejemplo n.º 1
0
        //Draws an arbitrary space object to the drawing pane.
        private void drawPhysicsObject(PhysicsObject obj, PaintEventArgs e)
        {
            //Console.WriteLine(obj.TextureFilename + ".png");
            //Console.WriteLine(currdir + "\\" + obj.TextureFilename + ".png");
            Image img = Image.FromFile(currdir + "\\" + obj.TextureFilename + ".png");

            //In order to do rotations and scaling in windows forms, we need to determine where
            // the upper right, upper left, and lower left corners map to. We determine these, and
            // tell the graphics to draw the image.

            // unscale before rotating
            //obj.Height = obj.Height / obj.scale;
            //obj.Width = obj.Width / obj.scale;

            // Hack to make the image representing a painted object draw correctly
            if (obj is PaintedObject && !(obj is InstasteelObject) && !(obj is InstasteelCircleObject))
            {
                ((PaintedObject)obj).Height = img.Height;
                ((PaintedObject)obj).Width = img.Width;
            }

            XnaRectangle bb = obj.getBBRelativeToWorld();

            float s = obj.scale;

            //Find the new point for the upper-left corner
            DrawPoint upper_left = Conversion.Vector2ToDrawPoint(new Vector2(bb.X + bb.Width/2f, bb.Y + bb.Height/2f) + RotateVector2(new Vector2(-obj.Width/2f, -obj.Height/2f), obj.Angle));

            DrawPoint upper_right = Conversion.Vector2ToDrawPoint(new Vector2(bb.X + bb.Width/2f, bb.Y + bb.Height/2) + RotateVector2(new Vector2(obj.Width - obj.Width/2f, -obj.Height/2f), obj.Angle));

            DrawPoint lower_left = Conversion.Vector2ToDrawPoint(new Vector2(bb.X + bb.Width/2f, bb.Y + bb.Height/2) + RotateVector2(new Vector2(-obj.Width/2f, obj.Height - obj.Height/2f), obj.Angle));

            // rescale after rotating
            //obj.Height = obj.Height * obj.scale;
            //obj.Width = obj.Width * obj.scale;

            /*
            DrawPoint upper_left = Conversion.Vector2ToDrawPoint(RotateVector2(new Vector2(bb.X, bb.Y), obj.Angle));

            DrawPoint upper_right = Conversion.Vector2ToDrawPoint(RotateVector2(new Vector2(bb.X + bb.Width, bb.Y), obj.Angle));

            DrawPoint lower_left = Conversion.Vector2ToDrawPoint(RotateVector2(new Vector2(bb.X, bb.Y + bb.Height), obj.Angle));
            */

            //Define the point mapping.
            DrawPoint[] destmapping = {upper_left, upper_right, lower_left};
            DrawRect srcrect = new DrawRect(0,0,img.Width,img.Height);

            //Draw the image with the specified position and scaling.
            e.Graphics.DrawImage(img, destmapping, srcrect, GraphicsUnit.Pixel);
        }