Speeding up application

Started by
2 comments, last by Kenny77 16 years, 11 months ago
I have this simple terrain I've been working on, and recently it's taken a long time to load (most the time I just give up). When I would run it before, it would pop up almost automatically, now it takes about 10 minutes to start (and it still shows it running in the taskbar). What could be the problem?
[lang="C#"]
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.IO;
using Microsoft.DirectX.DirectInput;

namespace DirectX_Tutorial
{

    public class WinForm : System.Windows.Forms.Form
    {
        private int WIDTH = 64;
        private int HEIGHT = 64;
        private Microsoft.DirectX.Direct3D.Device device;
        private System.ComponentModel.Container components = null;
        private float angle = 0f;
        private CustomVertex.PositionColored[] vertices;
        private int[,] heightData;
        private int[] indices;
        private IndexBuffer ib;
        private VertexBuffer vb;
        private Microsoft.DirectX.DirectInput.Device keyb;

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

        public void InitializeDevice()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
            device.RenderState.FillMode = FillMode.WireFrame;
            device.RenderState.CullMode = Cull.None;
        }

        public void InitializeKeyboard()
        {
            keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
            keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
            keyb.Acquire();
        }

        private void CameraPositioning()
        {

            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 1000f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(80, 200, 120), new Vector3(-10, 0, 0), new Vector3(0, 0, 1));

            device.RenderState.Lighting = false;
            device.RenderState.CullMode = Cull.None;
        }

        private void VertexDeclaration()
        {
            vb = new VertexBuffer(typeof(CustomVertex.PositionColored), WIDTH * HEIGHT, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            vertices = new CustomVertex.PositionColored[WIDTH * HEIGHT];

            for (int x = 0; x < WIDTH; x++)
            {

                for (int y = 0; y < HEIGHT; y++)
                {
                    vertices[x + y * WIDTH].Position = new Vector3(x, y, heightData[x, y]);
                    vertices[x + y * WIDTH].Color = Color.White.ToArgb();
                }
            }

            vb.SetData(vertices, 0, LockFlags.None);
        }

        private void IndicesDeclaration()
        {
            ib = new IndexBuffer(typeof(int), (WIDTH - 1) * (HEIGHT - 1) * 1600, device, Usage.WriteOnly, Pool.Default);
            indices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];

            for (int x = 0; x < WIDTH - 1; x++)
            {
                for (int y = 0; y < HEIGHT - 1; y++)
                {
                    indices[(x + y * (WIDTH - 1)) * 6] = (x + 1) + (y + 1) * WIDTH;
                    indices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + y * WIDTH;
                    indices[(x + y * (WIDTH - 1)) * 6 + 2] = x + y * WIDTH;

                    indices[(x + y * (WIDTH - 1)) * 6 + 3] = (x + 1) + (y + 1) * WIDTH;
                    indices[(x + y * (WIDTH - 1)) * 6 + 4] = x + y * WIDTH;
                    indices[(x + y * (WIDTH - 1)) * 6 + 5] = x + (y + 1) * WIDTH;
                }
            }
            ib.SetData(indices, 0, LockFlags.None);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.SetStreamSource(0, vb, 0);
            device.Indices = ib;

            device.Transform.World = Matrix.Translation(-HEIGHT / 2, -WIDTH / 2, 0) * Matrix.RotationZ(angle);
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, WIDTH * HEIGHT, 0, indices.Length / 3);
            device.EndScene();

            device.Present();

            this.Invalidate();

            ReadKeyboard();
        }

        private void ReadKeyboard()
        {
            KeyboardState keys = keyb.GetCurrentKeyboardState();
            if (keys[Key.Delete])
            {
                angle += 0.03f;
            }
            if (keys[Key.Next])
            {
                angle -= 0.03f;
            }
        }

        private void LoadHeightData()
        {
            int offset;
            byte dummy;

            FileStream fs = new FileStream("terrain.bmp", FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);

            for (int i = 0; i < 10; i++)
            {
                dummy = r.ReadByte();
            }

            offset = r.ReadByte();
            offset += r.ReadByte() * 256;
            offset += r.ReadByte() * 256 * 256;
            offset += r.ReadByte() * 256 * 256 * 256;

            for (int i = 0; i < 4; i++)
            {
                dummy = r.ReadByte();
            }

            WIDTH = r.ReadByte();
            WIDTH += r.ReadByte() * 256;
            WIDTH += r.ReadByte() * 256 * 256;
            WIDTH += r.ReadByte() * 256 * 256 * 256;

            HEIGHT = r.ReadByte();
            HEIGHT += r.ReadByte() * 256;
            HEIGHT += r.ReadByte() * 256 * 256;
            HEIGHT += r.ReadByte() * 256 * 256 * 256;

            heightData = new int[WIDTH, HEIGHT];
            for (int i = 0; i < (offset - 26); i++)
            {
                dummy = r.ReadByte();
            }

            for (int i = 0; i < HEIGHT; i++)
            {
                for (int y = 0; y < WIDTH; y++)
                {
                    int height = (int)(r.ReadByte());
                    height += (int)(r.ReadByte());
                    height += (int)(r.ReadByte());
                    height /= 8;
                    heightData[WIDTH - 1 - y, HEIGHT - 1 - i] = height;
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.Size = new System.Drawing.Size(500, 500);
            this.Text = "Corey Dawes Duck Pond Simulator";
        }

        static void Main()
        {
            using (WinForm our_directx_form = new WinForm())
            {
                our_directx_form.LoadHeightData();
                our_directx_form.InitializeDevice();
                our_directx_form.CameraPositioning();
                our_directx_form.VertexDeclaration();
                our_directx_form.IndicesDeclaration();
                our_directx_form.InitializeKeyboard();
                our_directx_form.Show();
                Application.Run(our_directx_form);
                our_directx_form.Dispose();
            }
        }
    }
}

Advertisement
After skimming through it, this bit worries me:
            WIDTH = r.ReadByte();            WIDTH += r.ReadByte() * 256;            WIDTH += r.ReadByte() * 256 * 256;            WIDTH += r.ReadByte() * 256 * 256 * 256;            HEIGHT = r.ReadByte();            HEIGHT += r.ReadByte() * 256;            HEIGHT += r.ReadByte() * 256 * 256;            HEIGHT += r.ReadByte() * 256 * 256 * 256;

Place a breakpoint after this section and make sure the values of HEIGHT and WIDTH are what you expect.

If that doesn't help you, try pausing your application while it's loading, and see what lines of code it stops on. That's how you know how far it's gotten.

If you still can't figure it out, just step through your entire application one line at a time, and see how control flows through it, until you find where it's hanging.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
Do you keep previous versions? The best thing would be to go back to the point where it became slower, and see what change you made.

BTW, why do you declare your index buffer to be of size '(WIDTH - 1) * (HEIGHT - 1) * 1600'?

It's hard for me to fix problems right now, because I'm just experimenting with tutorials. I'll probably shift to theory now, to get a better grasp of the basics (I had begun with simple devices thus far, but I'm kind of lost right now, so a better bet may be to just start again).

This topic is closed to new replies.

Advertisement