Sign in to follow this  
thallish

[.net] [MDX]performance question

Recommended Posts

hi I have just created a MDX application and got a FPS counter up and running, but what really surprises me is that i only get around 60 FPS, and there is nothing but a clean window, and the text I write to the screen, so I guess I am doing something wrong? here are my main class:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Framework
{
    class Program : Form
    {
        /// private member variables ////////////////////////////////////////////////////
        private Device d3dDevice = null;
        private PresentParameters PresentParm = new PresentParameters();
        private NativeMethods.PeekMsg msg;

        private PerformanceStats perfStat;
        //private bool IsFullscreen;

        /// public member variables /////////////////////////////////////////////////////
        public Device D3DDevice
        {
            get
            {
                return d3dDevice; // TODO: this is until I find out how to check probably
            }
        }

        /// constructors ///////////////////////////////////////////////////////////////// 
        public Program(int width, int height, bool fuulscreen)
        {
            // setting form properties
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Width = width;
            this.Height = height;
            this.Text = "The framework of doom";
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
        }

        /// private member methods
        /// <summary>
        /// This is where the present parameters is setup
        /// </summary>
        private void SetupPresentParameters()
        {
            PresentParm.Windowed = true;
            PresentParm.SwapEffect = SwapEffect.Discard;
        }

        /// <summary>
        /// This is where the DirectX device gets created 
        /// </summary>
        private void CreateDevice()
        {
            // creating the device
            d3dDevice = new Device(0, // default adapter
                                DeviceType.Hardware,
                                this, // this form is the window used
                                CreateFlags.SoftwareVertexProcessing, //
                                PresentParm //present parameters
                                );

            if (d3dDevice == null)
                MessageBox.Show("Device null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// This is where the updating of the the environment takes place 
        /// </summary>
        private void UpdateEnv()
        {
            // update game state
            perfStat.CalculateFPS();
           
        }

        /// <summary>
        /// This is where the geometry and like gets rendered to screen 
        /// </summary>
        private void RenderEnv()
        {
            // checking if the device is valid
            if (D3DDevice == null)
                return;

            d3dDevice.Clear(ClearFlags.Target, System.Drawing.Color.AliceBlue, 1.0f, 0);
            d3dDevice.BeginScene();

            // draw stuff here
            perfStat.Display();

            d3dDevice.EndScene();
            d3dDevice.Present();
        }

        /// <summary>
        /// This method initialize the graphics
        /// </summary>
        private void InitializeGraphics()
        {
            SetupPresentParameters();
            CreateDevice();

            perfStat = new PerformanceStats(d3dDevice);
        }

        // protected member methods
        protected void Application_Idle(object sender, EventArgs e)
        {
            // this is the message loop
            while (!NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0))
            {
                // Do game specifics when no messages are waiting
                UpdateEnv();
                RenderEnv();
            }
        }

        // public member methods
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Program myForm = new Program(800, 600, false); // create window/form
            myForm.InitializeGraphics(); // init d3d
            Application.Idle += new EventHandler(myForm.Application_Idle); // setup render loop 
            Application.Run(myForm);
        }

        #region NativeMethods Class
        internal class NativeMethods
        {
            private NativeMethods() { }

            [StructLayout(LayoutKind.Sequential)]
            public struct PeekMsg
            {
                public IntPtr hWnd;
                public Message msg;
                public IntPtr wParam;
                public IntPtr lParam;
                public uint time;
                public System.Drawing.Point p;
            }

            [System.Security.SuppressUnmanagedCodeSecurity] // This prevents run-time security checks, so DON'T LEAVE THIS SOMEWHERE INSECURE
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern bool PeekMessage(out PeekMsg msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
        }
        #endregion
    }
}

and my counter class:
using System.Runtime.InteropServices;
using Microsoft.DirectX.Direct3D;

namespace Framework
{
    class PerformanceStats
    {
        /// private member variables
        private float fps;
        private float numFrames;
        private float timeElapsed;
        private float milliSecsPerFrame;
       
        private long currentTime;
        private long previousTime;
        private long countPerSecs;
        
        private Font statFont;

        /// constructor
        public PerformanceStats(Device _device)
        {
            NativeMethods.QueryPerformanceFrequency(ref countPerSecs);
            statFont = new Font(_device, 15, 7, FontWeight.Normal,0, false, CharacterSet.Arabic, 0, FontQuality.Default, PitchAndFamily.DefaultPitch, "Verdana");
        }
        
        public void CalculateFPS()
        {
            // how long time has passed since last time the method was invoked
            NativeMethods.QueryPerformanceCounter(ref currentTime);

            float deltaTime = (currentTime - previousTime) * (1.0f/(float)countPerSecs);
            
            // increment frame count
            numFrames += 1.0f;

            // increasing how much time has elapsed since last fps update
            timeElapsed += deltaTime;

            System.Console.Write("TimeElapsed: " + timeElapsed + "\n");

            if (timeElapsed >= 1.0f)
            {
                // setting fps
                fps = numFrames;
                
                // how long did it take to render average frame
                milliSecsPerFrame = 1000.0f / fps;

                // resetting
                numFrames = 0.0f;
                timeElapsed = (long)0.0;
            }

            previousTime = currentTime;
        }

        public void Display()
        {
            // display the values here
            System.String display = "Frames per second: " + fps +
                                    "\nMilliSeconds per frame: " + milliSecsPerFrame; 

            statFont.DrawText(null, display, 5,5,System.Drawing.Color.Black);
        }

        #region NativeMethods
        internal class NativeMethods
        {
            [System.Security.SuppressUnmanagedCodeSecurity]
            [DllImport("kernel32.dll")]
            public static extern bool QueryPerformanceFrequency(ref long frequency);

            [System.Security.SuppressUnmanagedCodeSecurity]
            [DllImport("kernel32.dll")]
            public static extern bool QueryPerformanceCounter(ref long count);
        }
        #endregion
    }
}

Can anyone spot what I might do wrong? regards

Share this post


Link to post
Share on other sites
Try setting PresentParams.PresentationInterval to Direct3D.PresentInterval.Immediate and PresentParams.SwapEffect to Direct3D.SwapEffect.Discard. Chances are your monitor is set to 60Hz and you have VSync on.

Share this post


Link to post
Share on other sites
hehe i totally forgot about that one. You were right;-)
now the FPS is around 530-550. Now the question is if that is acceptable? I have no idea since it is the first time I have done this.

regards

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