Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having with this code. Any idea?
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;

namespace RailwaySimulator
{
    public partial class Form1_railwaySimulator : Form
    {
        private Device m_device = null;
        Button btnStart = new Button();
        //Button btnStop = new Button();
        private VertexBuffer m_vb = null;
        private IndexBuffer m_ib = null;
        float MIN_BOX_Z = -10.0f;
        float MAX_BOX_Z = 10.0f;
        float angle, speedIncrement;
        private static readonly short[] indices = 
		{
                0,1,2,1,3,2,  //FRONT
			    7,4,6,7,5,4,  //BACK
                5,7,3,2,5,3,  //LEFT
                0,4,1,1,4,6,  //RIGHT
                7,6,1,7,1,3,  //BOTTOM
                0,4,2,2,4,5  //TOP
        };

        void InitGraphics()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            m_device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        // m_vb =new VertexBuffer(typeof(cusomt
            m_vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 8,m_device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[8];
            m_vb.Created += new EventHandler(this.CreateVertexBuffer);
            CreateVertexBuffer(m_vb, null);
            m_ib = new IndexBuffer(typeof(short), indices.Length, m_device, Usage.WriteOnly, Pool.Default);
            m_ib.Created += new EventHandler(this.OnIndexBufferCreate);
            OnIndexBufferCreate(m_ib, null);

        }

        private void OnIndexBufferCreate(object sender, EventArgs e)
        {
            IndexBuffer buffer = (IndexBuffer)sender;
            buffer.SetData(indices, 0, LockFlags.None);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            m_device.Clear(ClearFlags.Target,
                  Color.PowderBlue, 1.0f, 0);
            SetupCamera();
            m_device.BeginScene();
            m_device.SetStreamSource(0, m_vb, 0);
            m_device.VertexFormat = CustomVertex.PositionColored.Format;
            drawTrain(4, 3, 8,
                       0.0f, 0.0f, 0.0f, // yaw = y-axis, pitch = X-axis
                       2.0f, 1.0f, 2.0f);
            m_device.EndScene();
            m_device.Present();
            // Always re-paint the window.
            this.Invalidate();

        }

        protected void CreateVertexBuffer(object sender, EventArgs e)
        {
            VertexBuffer vb = (VertexBuffer)sender;
            //m_vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 8,m_device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[8];

            // Define 36 triangle corners.
            // Front
            verts[0] = new CustomVertex.PositionColored(
                new Vector3(-1.0f, 1.0f, 1.0f),
                Color.Red.ToArgb());
            verts[1] = new CustomVertex.PositionColored(
                new Vector3(-1.0f, -1.0f, 1.0f),
                Color.Yellow.ToArgb());
            verts[2] = new CustomVertex.PositionColored(
                new Vector3(1.0f, 1.0f, 1.0f),
                Color.Green.ToArgb());
            verts[3] = new CustomVertex.PositionColored(
                new Vector3(1.0f, -1.0f, 1.0f),
                Color.Orange.ToArgb());
            verts[4] = new CustomVertex.PositionColored(
                new Vector3(-1.0f, 1.0f, -1.0f),
                Color.Black.ToArgb());
            verts[5] = new CustomVertex.PositionColored(
                new Vector3(1.0f, 1.0f, -1.0f),
                Color.Blue.ToArgb());
            verts[6] = new CustomVertex.PositionColored(
                new Vector3(-1.0f, -1.0f, -1.0f),
                Color.Indigo.ToArgb());
            verts[7] = new CustomVertex.PositionColored(
                new Vector3(1.0f, -1.0f, -1.0f),
                Color.Aqua.ToArgb());

            m_vb.SetData(verts, 0, LockFlags.None); //y loclflags

        }
        private void SetupCamera()
        {
            m_device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
            m_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), 
                                                      new Vector3(),
                                                      new Vector3(0, 1, 0));
            m_device.RenderState.PointSize = 3.0f;
            m_device.RenderState.Lighting = false;
            m_device.RenderState.Ambient = Color.AntiqueWhite;
         }

        public Form1_railwaySimulator()
        {
            btnStart.Location = new Point(10, 10);
            btnStart.Text = "Start";
            //this.Controls.Add(btnStart);

            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }

        private float F(float x, float z) // Function to set the path on SIN form
        {
            return (float)((Math.Sin(x) + Math.Sin(z)) * 1.5);
        }
        
       private void drawTrain(float scaleX, float scaleY, float scaleZ,
                                float yaw, float pitch, float roll,
                                float x, float y, float z)
        {
           
            angle += 0.1f;
            speedIncrement += 0.01f;

            Vector3 direction = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 vectorA, vectorB, newPosition;
            vectorA = new Vector3(x, 0, z);
            if (z < MIN_BOX_Z || (direction.Z <= 0 && z <= MAX_BOX_Z))
            {
                z -= speedIncrement;//forward

            }
            else
            {
                z += speedIncrement;//backward
            }
            vectorB = new Vector3(0, 0, z);

            direction = vectorA - vectorB;
            Vector3.Normalize(direction);
            newPosition = vectorA + (direction * speedIncrement);
            //device.Transform.World = Matrix.Translation(x * angle * 0.2f, y, z);
             m_device.Transform.World = Matrix.Translation(F(x, z), y, z * angle * 0.1f);
            
   m_device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 
               0,indices.Length/3);
            
        }

        static void Main()
        {
            using (Form1_railwaySimulator frm = new Form1_railwaySimulator())
            {
                frm.Show();
                frm.InitGraphics();
                Application.Run(frm);
            }
        }


    }
}
When I debug, I got the error which is "Error in the application."
What's that mean actually? Any help I appreciate very much.
Posted
Updated 20-Mar-10 21:42pm
v3

1 solution

Where is the error actually occurring?
Is there any more information with the message, or just "Error in the application"?
Is it on any particular line? Or does it just happen as soon as you start to run the app?
Have you tried putting a break point at the start of Main, and single stepping through?
Have you sett it to catch all unhandled exceptions?
Have you tried putting a try/catch block around the whole of Main to see if that helps?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900