2 Replies - 988 Views - Last Post: 05 June 2014 - 04:57 PM Rate Topic: -----

#1 Resdin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-June 14

Beginning C# Game Programming CH6 Issue

Posted 05 June 2014 - 02:42 PM

Hello everyone, and thank you for taking the time to read this.

Beginning C# Game Programming by Ron Penton CH6, Page 132 (If you happen to have the book). I am having trouble getting the framework to correctly load into Visual Studio 2008. The first five chapters had near zero coding involved: Finally I get to chapter six when we're just getting ready to dive into some simple game programming, and the book breaks down.

I tried to find a forum relating to this book in particular however I can't seem to find anything.

(I am using Visual Studio 2008 Professional)
The directions state: 1-"Create an empty project" 2-"Copy over the two. cs files from Demo 6.1 into the Demo 6.2 directory, and add them into the project manually". 3-"Add the references: System.dll, System.Drawing.dll, System.Windows.Forms.dll, Microsoft.DirectX.dll, and Microsoft.DirectX.Direct3D.dll.

Here are the two files it tells me to drag into my project:
MainClass.cs
// Beginning C# Game Programming
// (C)2004 Ron Penton
// Chapter 6
// Demo 2 - Visual C# Framework

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MyDirect3DProject
{
	/// <summary>
	/// This is the main class of my Direct3D application
	/// </summary>
	public class MainClass : Form
	{
		/// <summary>
		/// The rendering device
		/// </summary>
		Device device = null;
		
		public MainClass()
		{
			this.ClientSize = new System.Drawing.Size(640, 480);
			this.Text = "Direct3D Project";
		}
		
		public bool InitializeGraphics()
		{
			try {
				// Now let's setup the Direct3D stuff
				PresentParameters presentParams = new PresentParameters();
				presentParams.Windowed   = true;
				presentParams.SwapEffect = SwapEffect.Discard;
				
				// Create the device
				device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
				
				// Setup the event handlers for the device
				device.DeviceLost     += new EventHandler(this.InvalidateDeviceObjects);
				device.DeviceReset    += new EventHandler(this.RestoreDeviceObjects);
				device.Disposing      += new EventHandler(this.DeleteDeviceObjects);
				device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
				
				return true;
			} catch (DirectXException) {
				return false;
			}
		}
		
		protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
		{
		}
		
		protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
		{
		}
		
		protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
		{
		}
		
		protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
		{
		}
		
		/// <summary>
		/// This method moves the scene
		/// </summary>
		protected virtual void FrameMove()
		{
			// TODO : Frame movement
		}
		
		/// <summary>
		/// This method renders the scene
		/// </summary>
		protected virtual void Render()
		{
			if (device != null) {
				device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
				device.BeginScene();
				
				// TODO : Scene rendering
				
				device.EndScene();
				device.Present();
			}
		}
		
		/// <summary>
		/// Our mainloop
		/// </summary>
		public void Run()
		{
			// While the form is still valid, render and process messages
			while (Created) {
				FrameMove();	
				Render();
				Application.DoEvents();
			}
		}
		
		protected override void OnPaint(PaintEventArgs e)
		{
			this.Render();
		}
		
		protected override void onkeypress(KeyPressEventArgs e)
		{
			base.onkeypress(e);
			if ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) {
				this.Close(); 
			}
		}
		
		/// <summary>
		/// The main entry point for the application
		/// </summary>
		static void Main()
		{
			using (MainClass mainClass = new MainClass()) {
				if (!mainClass.InitializeGraphics()) {
					MessageBox.Show("Error while initializing Direct3D");
					return;
				}
				mainClass.Show();
				mainClass.Run();
			}
		}

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MainClass
            // 
            this.ClientSize = new System.Drawing.Size(475, 554);
            this.Name = "MainClass";
            this.Load += new System.EventHandler(this.MainClass_Load);
            this.ResumeLayout(false);

        }

        private void MainClass_Load(object sender, EventArgs e)
        {

        }
	}
}


AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;

// Information about this assembly is defined by the following
// attributes.
//
// change them to the information which is associated with the assembly
// you compile.

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all values by your own or you can build default build and revision
// numbers with the '*' character (the default):

[assembly: AssemblyVersion("1.0.*")]

// The following attributes specify the key for the sign of your assembly. See the
// .NET Framework documentation for more information about signing.
// This is not required, if you don't want signing let these attributes like they're.
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]



I have added the references that were stated in the directions. The book says this should compile right out of the box, and it does not.

Error recieved is: System.BadImageFormatException: is not a valid win32 application

I was really looking forward to actually getting to code some C#, but so far I am thinking due to the age of the book it is not going to work, and I have wasted my money on this book.

I have Visual Studio 08,2010, and 2012, as well as SharpDevelop. (The directions for use of SharpDevelop are out dated as well and do not appear to work either.) If anyone could help me get this to a working state I would be very greatful... so I can at least finish the book. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Beginning C# Game Programming CH6 Issue

#2 doby48   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 02-June 14

Re: Beginning C# Game Programming CH6 Issue

Posted 05 June 2014 - 04:43 PM

I'm not familiar with the book but if you're using a 64-bit OS you may want to set your project to use x86 platform. Its possible the dlls you're referencing are the 32-bit version of them.

In VisualStudio 2008, if you have the Standard Toolbar open you'll see next to the Debug/Release dropdown there is another for the CPU which by default is set to Any CPU, change it to x86. DirectX and SQL are two in particular I have seen where this is necessary (and also something you likely will want to do moving forward anyway if you are coding any apps that may also need to run on x86).

If x86 is not listed in the dropdown, go to configuration manager and the Active Solution platform dropdow, choose New and in the window that opens you type x86 in the first dropdown (or select if it already exists) and in the second dropdown choose x86 and save it. Now you'll be able to switch from Any CPU/x86 easily in any project using the dropdown in the Standard Toolbar.
Was This Post Helpful? 3
  • +
  • -

#3 Resdin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-June 14

Re: Beginning C# Game Programming CH6 Issue

Posted 05 June 2014 - 04:57 PM

I am a little embarrassed it was that simple. I assumed it would default to x86. This did seem to correct the issue I was having, funny I hadn't had an issue prior to this. I was working with VS2012 a few months back for VB and never had an issue.

Anyhow, I really appreciate your time, and I am glad this book isn't a waste. Hopefully things will go smooth from here on out since I have my IDE configured correctly. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1