Sign in to follow this  
coolcutter

Beginning texturing

Recommended Posts

every time i try to run my code i get an error in application. I dont have the slightest clue as to whats going wrong. Any help would be greatly appreciated. my code in c#: // Beginning C# Game Programming // (C)2004 Ron Penton // Chapter 7 // Demo 3 - Colors and Alpha using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Direct3D = Microsoft.DirectX.Direct3D; using DirectSound = Microsoft.DirectX.DirectSound; using DirectInput = Microsoft.DirectX.DirectInput; namespace AdvancedFramework { // ------------------------------------------------------------------------ // The main Game class. // ------------------------------------------------------------------------ public class Game : Form { // -------------------------------------------------------------------- // static variables // -------------------------------------------------------------------- static string gametitle = "Demo 7.3 - Colors and Alpha"; static int screenwidth = 640; static int screenheight = 480; static bool windowed = true; static bool graphicslost = false; static Timer gametimer = null; static bool paused = false; // -------------------------------------------------------------------- // Devices // -------------------------------------------------------------------- Direct3D.Device graphics = null; DirectSound.Device sound = null; DirectInput.Device keyboard = null; DirectInput.Device mouse = null; DirectInput.Device gameinput = null; // -------------------------------------------------------------------- // Geometry // -------------------------------------------------------------------- Direct3D.CustomVertex.TransformedTextured[] vertexes = null; //Texture Direct3D.Texture texture = null; // -------------------------------------------------------------------- // Game constructor // -------------------------------------------------------------------- public Game() { ClientSize = new System.Drawing.Size( screenwidth, screenheight ); Text = gametitle; gametimer = new Timer(); } // -------------------------------------------------------------------- // Initialize the Direct3D Graphics subsystem // -------------------------------------------------------------------- public void InitializeGraphics() { // set up the parameters Direct3D.PresentParameters p = new Direct3D.PresentParameters(); p.SwapEffect = Direct3D.SwapEffect.Discard; if( windowed == true ) { p.Windowed = true; } else { // get the current display mode: Direct3D.Format current = Direct3D.Manager.Adapters[0].CurrentDisplayMode.Format; // set up a fullscreen display device p.Windowed = false; // fullscreen p.BackBufferCount = 1; // one back buffer p.BackBufferFormat = current; // use current format p.BackBufferWidth = screenwidth; p.BackBufferHeight = screenheight; } // create a new device: graphics = new Direct3D.Device( 0, Direct3D.DeviceType.Hardware, this, Direct3D.CreateFlags.SoftwareVertexProcessing, p ); // Setup the event handlers for the device graphics.DeviceLost += new EventHandler( this.InvalidateDeviceObjects ); graphics.DeviceReset += new EventHandler( this.RestoreDeviceObjects ); graphics.Disposing += new EventHandler( this.DeleteDeviceObjects ); graphics.DeviceResizing += new CancelEventHandler( this.EnvironmentResizing ); // set up various drawing options graphics.RenderState.CullMode = Direct3D.Cull.None; graphics.RenderState.AlphaBlendEnable = true; graphics.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Subtract; graphics.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha; graphics.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha; } // -------------------------------------------------------------------- // Initialize the DirectSound subsystem // -------------------------------------------------------------------- public void InitializeSound() { // set up a device sound = new DirectSound.Device(); sound.SetCooperativeLevel( this, DirectSound.CooperativeLevel.Normal ); } // -------------------------------------------------------------------- // Initialize the DirectInput subsystem // -------------------------------------------------------------------- public void InitializeInput() { // set up the keyboard keyboard = new DirectInput.Device( DirectInput.SystemGuid.Keyboard ); keyboard.SetCooperativeLevel( this, DirectInput.CooperativeLevelFlags.Background | DirectInput.CooperativeLevelFlags.NonExclusive ); keyboard.Acquire(); // set up the mouse mouse = new DirectInput.Device( DirectInput.SystemGuid.Mouse ); mouse.SetCooperativeLevel( this, DirectInput.CooperativeLevelFlags.Background | DirectInput.CooperativeLevelFlags.NonExclusive ); mouse.Acquire(); } // -------------------------------------------------------------------- // Initialize the Geometry // -------------------------------------------------------------------- public void InitializeGeometry() { vertexes = new Direct3D.CustomVertex.TransformedTextured[4]; // triangle 1: // left vertex: vertexes[1].X = screenwidth / 4.0f; vertexes[1].Y = screenheight / 3.0f; vertexes[1].Z = 0.0f; vertexes[1].Tu = 1.0f; vertexes[1].Tv = 0.0f; // right vertex vertexes[3].X = screenwidth / 1.5f; vertexes[3].Y = screenheight / 3.0f; vertexes[3].Z = 0.0f; vertexes[3].Tu = 1.0f; vertexes[3].Tv = 1.0f; // bottom left vertexes[0].X = screenwidth / 4.0f; vertexes[0].Y = (screenheight / 3.0f) * 2.0f; vertexes[0].Z = 0.0f; vertexes[0].Tu = 0.0f; vertexes[0].Tv = 0.0f; // bottom right vertexes[2].X = screenwidth / 1.5f; vertexes[2].Y = (screenheight / 3.0f) * 2.0f; vertexes[2].Z = 0.0f; vertexes[2].Tu = 1.0f; vertexes[2].Tv = 1.0f; texture = Direct3D.TextureLoader.FromFile( graphics, "image.jpg", 0, 0, 0, 0, Direct3D.Format.Unknown, Direct3D.Pool.Managed, Direct3D.Filter.Linear, Direct3D.Filter.Linear, 0); } // -------------------------------------------------------------------- // Device Event Handlers // -------------------------------------------------------------------- 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 ) { e.Cancel = true; } // -------------------------------------------------------------------- // Process one iteration of the game loop // -------------------------------------------------------------------- protected virtual void ProcessFrame() { // process the game only while it's not paused if( !paused ) { } else System.Threading.Thread.Sleep( 1 ); } // -------------------------------------------------------------------- // Render the current game screen // -------------------------------------------------------------------- protected virtual void Render() { if( graphics != null ) { // check to see if the device has been lost. If so, try to get // it back. if( graphicslost ) { try { graphics.TestCooperativeLevel(); } catch( Direct3D.DeviceLostException ) { // device cannot be reaquired yet, just return return; } catch( Direct3D.DeviceNotResetException ) { // device has not been reset, but it can be reaquired now graphics.Reset( graphics.PresentationParameters ); } graphicslost = false; } try { graphics.Clear( Direct3D.ClearFlags.Target, Color.White, 1.0f, 0 ); graphics.BeginScene(); graphics.SetTexture(0, texture); graphics.VertexFormat = Direct3D.CustomVertex.TransformedTextured.Format; graphics.DrawUserPrimitives(Microsoft.DirectX.Direct3D.PrimitiveType.TriangleStrip, 2, vertexes); graphics.EndScene(); graphics.Present(); } // device has been lost, and it cannot be re-initialized yet catch( Direct3D.DeviceLostException ) { graphicslost = true; } } } // -------------------------------------------------------------------- // Run the game // -------------------------------------------------------------------- public void Run() { // reset the game timer gametimer.Reset(); // loop while form is valid while( this.Created ) { // process one frame of the game ProcessFrame(); // render the current scene Render(); // handle all events Application.DoEvents(); } } // -------------------------------------------------------------------- // Handle windows events // -------------------------------------------------------------------- protected override void OnLostFocus( EventArgs e ) { base.OnLostFocus( e ); Paused = true; } protected override void OnKeyDown( KeyEventArgs e ) { base.OnKeyDown( e ); if( e.KeyCode == System.Windows.Forms.Keys.Escape ) { this.Close(); } if ( e.KeyCode == System.Windows.Forms.Keys.P ) { Paused = !Paused; } } // -------------------------------------------------------------------- // Property to pause/unpause the game, or get its pause state // -------------------------------------------------------------------- public bool Paused { get { return paused; } set { // pause the game if( value == true && paused == false ) { gametimer.Pause(); paused = true; } // unpause the game if( value == false && paused == true ) { gametimer.Unpause(); paused = false; } } } // -------------------------------------------------------------------- // Entry point of the program, creates a new game and runs it // -------------------------------------------------------------------- static void Main() { Game game; try { game = new Game(); game.InitializeGraphics(); game.InitializeSound(); game.InitializeInput(); game.InitializeGeometry(); game.Show(); game.Run(); } catch( Exception e ) { MessageBox.Show( "Error: " + e.Message ); } } } }

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