[.net] What's wrong with my DX initialization?

Started by
3 comments, last by wakebrdkid12 19 years, 4 months ago
Hey guys, I'm going to try and create a dual-hand eye coordination test that the Air Force uses to test pilot aptitude. I've made a basic game in C# with DX before but I just read an article about Finite State Machines and I want to try and set up my game to use one so that I can have a menu, the actual game, and an end screen. I'm having trouble already though, setting up my DX window! Could anyone take a look and give me some advice? I think it's pretty obvious what I'm trying to do, but it's not working.

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 Microsoft.DirectX.DirectInput;

namespace FlightTraining1
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;
		private Microsoft.DirectX.Direct3D.Device device = null;
		private FlightTraining1.Menu hMenu = null;
		private enum AppState 
		{
			Menu,
			Running,
			EndScreen,
			Exiting
		};
		private AppState appState;

		public Form1()
		{
			InitializeComponent();
			appState = AppState.Menu;
			try
			{
				PresentParameters presentParams = new PresentParameters();
    
				presentParams.Windowed=true;
				presentParams.SwapEffect = SwapEffect.Discard;
				//presentParams.BackBufferHeight = 768;
				//presentParams.BackBufferWidth = 1024;
				//presentParams.BackBufferFormat= Format.X8R8G8B8;
				//presentParams.FullScreenRefreshRateInHz = 60;
    
				device = new Microsoft.DirectX.Direct3D.Device(0,
					Microsoft.DirectX.Direct3D.DeviceType.Hardware,
					this,
					CreateFlags.SoftwareVertexProcessing,
					presentParams);

				device.RenderState.SourceBlend = Blend.SourceAlpha;
					device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
				device.RenderState.AlphaBlendEnable = true;
			}
			catch (DirectXException)
			{
				appState = AppState.Exiting;
			}
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.Black;
			this.ClientSize = new System.Drawing.Size(1024, 768);
			this.Name = "Form1";
			this.Text = "Flight Training";

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Form1 app = new Form1();

			while(app.Created)
			{
				switch(app.appState)
				{
					case AppState.Menu:
						if(app.hMenu == null)
							app.hMenu = new FlightTraining1.Menu(app.device);
						app.hMenu.Render();
						break;
					case AppState.Running:

						break;
					case AppState.EndScreen:

						break;
					case AppState.Exiting:
						app.Dispose();
						break;
					default:
						app.Dispose();
						break;
				}
			}
		}
	}
}

And this:

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;

namespace FlightTraining1
{
	public class Menu
	{
		private int time;
		private bool startSelected;
		Microsoft.DirectX.Direct3D.Device device;

		public Menu(Microsoft.DirectX.Direct3D.Device deviceRef)
		{
			device = deviceRef;
			time = 3;
			startSelected = false;
		}

		public void Render()
		{
			if (device == null)
				return;
			
			device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);
    
			device.BeginScene();
    
			using(Sprite d3dSprite = new Sprite(device))
			{
				d3dSprite.Begin(SpriteFlags.None);
				
				d3dSprite.End();
			}
    
			device.EndScene();
			device.Present();	
		}
	}
}

Advertisement
Before I try to debug your code for you, what exactly is it doing or not doing? Does it even compile? Are you following some tutorial? If not take a look at drunken hyena's stuff he has some stuff on the differnece between full-screened and windowedmode.
Well the code compiles fine. The problem is that it doesn't seem to create the window. You can see that I have my game loop as a while loop testing the app.Created property. For some reason this is false even after it creates the window. Is there a problem with me initializing the DX stuff when it creates the window? And I also want to get some feedback on my setup of the states that will handle the game. So far I'm just trying to get the first state, the menu state, to work.
Ok, to address your problem of the window not showing up: You need to a) show it and b) handle the window events.

In a regular windows .NET app you do not need a loop and can do something like this:

Application.Run(new MyWhateverForm());

Which will pass control over to the form and hadle messages and closing and other stuff for you.

In DX we typically want to keep control to push the rendering (though you can render in response to windows events, but I would not recomend it) and so you have show your window and handle the messgaing loop yourself:

window.Show();
while(window.Created){
dxengine.Render();
Application.DoEvents();
}

Where window and dxengine are your classes. There are faster ways to handle the DoEvents bit, but they are much more complicated.

Hope this gets you moving.
Thanks so much. I forgot to add the Show command and the DoEvents call. Thanks again!

This topic is closed to new replies.

Advertisement