[.net] Sprite Rendering Problem while not debugging

Started by
0 comments, last by web_scripter 18 years, 10 months ago
When i start my project with debugging the sprite renders perfectly. When i start my project without debugging the sprite does not render at all. Here is my source code:

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;

namespace SpriteTest
{
	public class Form1 : Form
	{
		Direct3D.Device device = null;
		VertexBuffer vertexBuffer = null;
		PresentParameters presentParams = new PresentParameters();
		bool pause = false;
		Texture tex;
		Sprite spr;

		public Form1()
		{
			this.ClientSize = new System.Drawing.Size(400,300);
		}
		public bool InitializeGraphics()
		{
			try
			{
				presentParams.Windowed=true; 
				presentParams.SwapEffect = SwapEffect.Discard;
				presentParams.EnableAutoDepthStencil = true;
				presentParams.AutoDepthStencilFormat = DepthFormat.D16;
				device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
				device.DeviceReset += new System.EventHandler(this.OnResetDevice);
				this.OnCreateDevice(device, null);
				this.OnResetDevice(device, null);
				pause = false;
				return true;
			}
			catch (DirectXException)
			{
				return false;
			}
		}
		public void OnCreateDevice(object sender, EventArgs e)
		{
			Device dev = (Device)sender;
			spr = new Sprite(device);
		}
		public void OnResetDevice(object sender, EventArgs e)
		{
			Device dev = (Device)sender;
			dev.RenderState.CullMode = Cull.None;
			dev.RenderState.Lighting = false;
			dev.RenderState.ZBufferEnable = true;
			tex = TextureLoader.FromFile(device, "car1.bmp", 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, Color.FromArgb(255,255,0,0).ToArgb());
		}
		private void SetupMatrices()
		{
			device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f),1,(float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 1000.0f );
			device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );
			device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4.0f, 1.0f, 1.0f, 100.0f );
		}
		private void Render()
		{
			if (pause)
				return;
			
			device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
			device.BeginScene();
			SetupMatrices();
			
			device.RenderState.AlphaBlendEnable = true;
	
			spr.Begin(SpriteFlags.AlphaBlend);
			spr.Draw2D(tex,new Rectangle(0,0,250,210),new Rectangle(0,0,250,210),new Point(45,45),Color.White.ToArgb());
			spr.End();
			
			device.EndScene();
			device.Present();
		}

		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			this.Render();
		}
		protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
		{
			if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
				this.Dispose();
		}
        protected override void OnResize(System.EventArgs e)
        {
            pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
        }
		static void Main() 
		{
            using (Form1 frm = new Form1())
            {
                if (!frm.InitializeGraphics())
                {
                    MessageBox.Show("Could not initialize Direct3D.");
                    return;
                }
                frm.Show();

                while(frm.Created)
                {
                    frm.Render();
                    Application.DoEvents();
                }
            }
		}

	}
}


I have no idea why this is happening. I'm using directX 9.0 (June Update). I appreciate any help possible.
Advertisement
It works when i add:
spr.Draw(tex,new Rectangle(0,0,1,1),new Vector3(0,0,0),new Vector3(0,0,0),Color.White.ToArgb());
before the spr.Draw2D method.

Very strange.
Any ideas why this happens?

This topic is closed to new replies.

Advertisement