Jump to content
  • Advertisement
Sign in to follow this  
kappa

[.net] MDX, Invalid rectangle exeption?

This topic is 5142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I ran into a problem while following this: http://www.kuhnstall.de/tutorials/dx9sharp1.html tutorial. My program generates an "Invalid rectangle exeption error" at this code (at TargetSurface.DrawFast):
	public void DrawBitmap( Surface TargetSurface )
		{
			if(!this.bVisible )
				return;

			if( this.objectType	== BitmapType.TRANSPARENT )
			
				TargetSurface.DrawFast( rectPosition.X,
										rectPosition.Y,
										bitmapSurface,
										objectSizeRect,
										DrawFastFlags.SourceColorKey | DrawFastFlags.Wait );
			
			else

				TargetSurface.DrawFast( rectPosition.X,
										rectPosition.Y,
										bitmapSurface,
										objectSizeRect,
										DrawFastFlags.NoColorKey | DrawFastFlags.Wait );

		}


I just cannot find where I did my mistake the debugger showed all values for the rectangle where defined. If someone would have time to look at this I would be very greatfull. (almost) full source: Bitmap Object:
using System;
using System.Drawing;
using Microsoft.DirectX.DirectDraw;

namespace MDX
{
	public enum BitmapType
	{
		SOLID		= 0,
		TRANSPARENT = 1
	}

	/// <summary>
	/// Summary description for BitmapObject.
	/// </summary>
	public class BitmapObject
	{
		#region Variables
		protected Bitmap				sourceBitmap;
		protected Surface				bitmapSurface;
		protected BitmapType			objectType;
		protected Device				targetDevice;
		protected SurfaceDescription	surfaceDesc;
		protected Rectangle				objectSizeRect	= Rectangle.Empty;
		protected Rectangle				rectPosition	= Rectangle.Empty;
		protected double				YPosition		= 0;
		protected double				dXPos			= 0.0f;
		protected double				dYPos			= 0.0f;
		protected double				dLastXPos		= 0.0f;
		protected double				dLastYPos		= 0.0f;
		protected bool					bVisible		= true;
		protected int					iWidth			= 0;
		protected int					iHeight			= 0;
		#endregion
		
		#region Properties

		public double XPosition
		{
			get
			{
				return dXPos;
			}
			set
			{
				dLastXPos	= dXPos;

				dXPos		= value;

				rectPosition = new Rectangle( new Point(Convert.ToInt32(dXPos), rectPosition.Y),
					new Size( this.Width, this.Height ) );
			}
		}

		public int Width
		{
			get
			{
				return iWidth;
			}
			set
			{
				iWidth = value;
			}
		}

		public int Height
		{
			get
			{
				return iHeight;
			}
			set
			{
				iHeight = value;
			}
		}

		public double LastYPosition
		{
			get
			{
				return dLastYPos;
			}
		}

		/// <summary>
		/// The position of the bitmap
		/// as rectangle (= integer values).
		/// </summary>
		public Rectangle Position
		{
			get
			{
				return rectPosition;
			}
		}

		/// <summary>
		/// The objects bitmap
		/// </summary>
		public Bitmap BitmapOfObject
		{
			get
			{
				return sourceBitmap;
			}
		}

		/// <summary>
		/// The objects surface
		/// </summary>
		public Surface SurfaceOfBitmap
		{
			get
			{
				return bitmapSurface;
			}
		}

		#endregion
		
		public BitmapObject( string Resource ,
			BitmapType ObjectType,
			Device TargetDevice )
		{
			sourceBitmap = new Bitmap( GetType().Module.Assembly.GetManifestResourceStream( Resource ) );
			objectType	 = ObjectType;
			targetDevice = TargetDevice;

			objectSizeRect	= new Rectangle( 0, 0, sourceBitmap.Width,
				sourceBitmap.Height );

			rectPosition	= new Rectangle( 0, 0, sourceBitmap.Width,
				sourceBitmap.Height );

			iWidth			= sourceBitmap.Width;
			iHeight			= sourceBitmap.Height;
 
			initializeSurfaceDescription();

			initializeSurface();
		}

		protected void initializeSurfaceDescription()
		{
			surfaceDesc = new SurfaceDescription();

			surfaceDesc.SurfaceCaps.OffScreenPlain = true;

			surfaceDesc.Width	= objectSizeRect.Width;
			surfaceDesc.Height	= objectSizeRect.Height;
		}

		protected void initializeSurface()
		{
			this.bitmapSurface = new Surface(	sourceBitmap,
				surfaceDesc,
				targetDevice );

			switch( this.objectType )
			{
				case BitmapType.TRANSPARENT:

					ColorKey tempKey = new ColorKey();

					tempKey.ColorSpaceHighValue = 0;
					tempKey.ColorSpaceLowValue	= 0;

					bitmapSurface.SetColorKey( ColorKeyFlags.SourceDraw, tempKey );

					break;
			}
		}

		public void DrawBitmap( Surface TargetSurface,
			double  XPos,
			double  YPos )
		{
			this.XPosition	= XPos;
			this.YPosition	= YPos;
			
			DrawBitmap( TargetSurface );
		}

		public void DrawBitmap( Surface TargetSurface )
		{
			if(!this.bVisible )
				return;

			if( this.objectType	== BitmapType.TRANSPARENT )
			
				TargetSurface.DrawFast( rectPosition.X,
										rectPosition.Y,
										bitmapSurface,
										objectSizeRect,
										DrawFastFlags.SourceColorKey | DrawFastFlags.Wait );
			
			else

				TargetSurface.DrawFast( rectPosition.X,
										rectPosition.Y,
										bitmapSurface,
										objectSizeRect,
										DrawFastFlags.NoColorKey | DrawFastFlags.Wait );

		}

		public void RestoreSurface()
		{
			this.initializeSurfaceDescription();
			this.initializeSurface();
		}
	}
}


Game loop
using System;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;

namespace MDX
{
	public enum GameStates
	{
		Initialized = 0,
		Exit		= 1
	}
	
	/// <summary>
	/// Summary description for Game.
	/// </summary>
	public class Game
	{
		protected BitmapObject		boTitleScreen;

		protected Control			target;
		protected GraphicHandeler	graphics;
		protected GameStates		GameState;
		protected InputHandeler		input;
		protected double			dLoopDuration;

		public Game( Control RenderTarget)
		{
			this.target = RenderTarget;

			this.target.GotFocus += new System.EventHandler( this.restore );

			this.graphics  = new GraphicHandeler( this.target ); 

			this.input	= new InputHandeler( this.target );

			try
			{
				DXTimer.Init();
			}
			catch( Exception ex )
			{
				MessageBox.Show( "Error while initializing" + ex.Message );
				return;
			}

			this.gameLoop();
		}

		public void processInput()
		{
			KeyboardState state;

			state = this.input.GetKeyboardState();

			if( state != null )
			{
				if( state[Key.Escape] )
				{
					GameState = GameStates.Exit;
				}
			}
		}

		protected void restore( object Sender, System.EventArgs e )
		{
			this.graphics = new GraphicHandeler( this.target );

			this.boTitleScreen.RestoreSurface();
		}

		protected void gameLoop()
		{
			dLoopDuration = 0.0;

			DXTimer.Start();

			this.boTitleScreen = new BitmapObject( "MDX.Bitmaps.Title.title.jpg",
				BitmapType.SOLID,
				this.graphics.DDDevice );

			while( target.Created )
			{
				if( !target.Focused )
				{
					Application.DoEvents();
					
					DXTimer.GetElapsedMilliseconds();
					continue;
				}

				#if DEBUG
				dLoopDuration += DXTimer.GetElapsedMilliseconds();

				if( dLoopDuration > 10 )
				{
				#else
					dLoopDuration += DXTimer.GetElapsedMilliseconds();	
				#endif

					if( GameState == GameStates.Exit )
					{
						return;
					}

					
					if( GameState == GameStates.Initialized )
					{
						this.boTitleScreen.DrawBitmap( this.graphics.RenderSurface );
					}

					graphics.Flip();

					this.processInput();
					
				#if DEBUG
					dLoopDuration = 0.0;
				}
				#endif

				Application.DoEvents();
			}
		}
	}
}


Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!