Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

alnite

Responding to lost states in MD3D (C#)

This topic is 5476 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

This has been bugging me for the past days. I tried to do it the same way as in the sample code, yet I still can't get it right. Source code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

using Direct3D = Microsoft.DirectX.Direct3D;

namespace WindowsApplication2
{
	// Summary description for Form1.

	public class frmMain : System.Windows.Forms.Form
	{
		// Required designer variable.

		private System.ComponentModel.Container		components = null;

		private Direct3D.Device		d3dDevice = null;
		private bool deviceIsLost;
		private PresentParameters	presentParams;

		public frmMain()
		{
			InitializeComponent( );
//			if ( !InitializeGraphics() )

//			{

//				MessageBox.Show( "Could not initialize Direct3D. Application will now exit." );

//				return;

//			}

//		}

		
		private bool InitializeGraphics( )
		{
			try
			{
				presentParams = new PresentParameters( );

				// fills in PresentParameters

				presentParams.Windowed = false;
				presentParams.AutoDepthStencilFormat = DepthFormat.D16;
				presentParams.EnableAutoDepthStencil = false;

				presentParams.BackBufferCount = 1;
				presentParams.BackBufferFormat = Format.X8R8G8B8;
				presentParams.BackBufferWidth = 640;
				presentParams.BackBufferHeight = 480;
				presentParams.FullScreenRefreshRateInHz = 75;

				presentParams.PresentationInterval = PresentInterval.Default;
				presentParams.PresentFlag = PresentFlag.None;
				presentParams.SwapEffect = SwapEffect.Discard;

				presentParams.DeviceWindow = this;

				// Create Direct3D Device

				d3dDevice = new Direct3D.Device( 0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams );

				// Set event handlers

//				d3dDevice.DeviceLost += new System.EventHandler( this.InvalidateObjects );

//				d3dDevice.DeviceReset += new System.EventHandler( this.RestoreObjects );

//				d3dDevice.Disposing += new System.EventHandler( this.DeleteObjects );


				deviceIsLost = false;

				return true;
			}
			catch
			{
				return false;
			}
		}

		private void Render( )
		{
			if ( d3dDevice == null )
				return;

			if ( deviceIsLost )
			{
				try
				{
					d3dDevice.TestCooperativeLevel();
				}
				catch( DeviceLostException )
				{
					return;
				}
				catch( DeviceNotResetException )
				{
					d3dDevice.Reset( presentParams );
					deviceIsLost = false;
				}
			}

			d3dDevice.Clear( ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0 );

			d3dDevice.BeginScene( );

			d3dDevice.EndScene( );

			try
			{
				d3dDevice.Present( );
			}
			catch( DeviceLostException )
			{
				deviceIsLost = true;
			}
		}


		protected void InvalidateObjects( System.Object sender, System.EventArgs e )
		{
		}

		protected void RestoreObjects( System.Object sender, System.EventArgs e )
		{
		}

		protected void DeleteObjects( System.Object sender, System.EventArgs e )
		{
		}


		// The main entry point for the application.

		static void Main() 
		{
			using ( frmMain mainForm = new frmMain() )
			{
				if ( !mainForm.InitializeGraphics() )
				{
					MessageBox.Show( "Could not initialize Direct3D. Application will now exit." );
					return;
				}

				mainForm.Show( );
				
				while( mainForm.Created )
				{
					mainForm.Render( );
					System.Windows.Forms.Application.DoEvents();
				}
			}
		}
	}
}
The problem arises when I tried to switch back to my game. A DeviceLostException is thrown, and the code stops at while( mainForm.Created ). When I tried to remove the mainForm.Render() line, same thing still happened! Even though the Direct3D code is no longer inside the loop, DeviceLostException is still thrown!? There is no way System.Windows.Forms.Form would throw DeviceLostException! What did I do wrong?? edit: removed Windows Form generated code [edited by - alnite on September 25, 2003 4:37:38 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try adding a try catch block around the whole Render function.

If I had to guess you are getting the DeviceLostException when you are calling either Clear, BeginScene or Endscene which has no try catch block around it.

Share this post


Link to post
Share on other sites
Nope, I put the try block around the Clear(), BeginScene(), and EndScene(), and exception is still thrown.

Even when I remove the whole Render() function, it is still there!

Like this:
try
{
   Clear();
   BeginScene();
   EndScene();
{


This doesn't work either:
try
{
   mainForm.Render();
}

DeviceLostException is still thrown.

This works:
try
{
   mainForm.Render();
   DoEvents()
}

But then, I got an InvalidCallException in:
if ( deviceIsLost )
{
   try
   {
      d3dDevice.TestCooperativeLevel();
   }
   catch( DeviceLostException )
   {
      return;
   }
   catch( DeviceNotResetException )
   {
      d3dDevice.Reset( presentParams );
      deviceIsLost = false; // <--------------- here!!?
   }
}


My assumption is it's actually thrown by Reset(), but I got the presentParams correct. And I tried to use d3dDevice.PresentParameters (like in the samples), I still got the same thing.

[edited by - alnite on September 25, 2003 4:52:56 PM]

Share this post


Link to post
Share on other sites

  • 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!