Sign in to follow this  
wakebrdkid12

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

Recommended Posts

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();	
		}
	}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this