I rewrite matrices example from dx sdk with new fast loop. But its speed = speed Doevents loop? Why?
C#:
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;
using System.Runtime.InteropServices;
namespace MatricesTutorial
{
public class Matrices : Form
{
// Our global variables for this project
Device device = null; // Our rendering device
VertexBuffer vertexBuffer = null;
PresentParameters presentParams = new PresentParameters();
bool pause = false;
int count = 0;
double fps = 0f;
double time = 0f;
Microsoft.DirectX.Direct3D.Font dxFont;
System.Drawing.Font sysFont;
public Matrices()
{
// Set the initial size of our form
this.ClientSize = new System.Drawing.Size(400, 300);
// And its caption
this.Text = "Direct3D Tutorial 3 - Matrices";
}
public bool InitializeGraphics()
{
try
{
// Now lets setup our D3D stuff
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Flip;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
this.OnCreateDevice(device, null);
this.OnResetDevice(device, null);
pause = false;
DXTimer.Init();
DXTimer.Start();
time = DXTimer.GetElapsedSeconds();
sysFont = new System.Drawing.Font("ARIAL", 20);
dxFont = new Microsoft.DirectX.Direct3D.Font(device, sysFont);
return true;
}
catch (DirectXException)
{
return false;
}
}
public void OnCreateDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
// Now Create the VB
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, dev, 0, CustomVertex.PositionColored.Format, Pool.Default);
vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);
this.OnCreateVertexBuffer(vertexBuffer, null);
}
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
// Turn off culling, so we see the front and back of the triangle
dev.RenderState.CullMode = Cull.None;
// Turn off D3D lighting, since we are providing our own vertex colors
dev.RenderState.Lighting = false;
}
public void OnCreateVertexBuffer(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vb.Lock(0, 0);
verts[0].X = -1.0f; verts[0].Y = -1.0f; verts[0].Z = 0.0f; verts[0].Color = System.Drawing.Color.DarkGoldenrod.ToArgb();
verts[1].X = 1.0f; verts[1].Y = -1.0f; verts[1].Z = 0.0f; verts[1].Color = System.Drawing.Color.MediumOrchid.ToArgb();
verts[2].X = 0.0f; verts[2].Y = 1.0f; verts[2].Z = 0.0f; verts[2].Color = System.Drawing.Color.Cornsilk.ToArgb();
vb.Unlock();
}
private void Render()
{
//if (device == null)
// return;
//if (pause)
// return;
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, 0x00003F3F, 1.0f, 0);
//Begin the scene
device.BeginScene();
// Setup the world, view, and projection matrices
SetupMatrices();
device.SetStreamSource(0, vertexBuffer, 0);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
//End the scene
device.EndScene();
device.Present();
count++;
}
private void SetupMatrices()
{
int iTime = Environment.TickCount % 1000;
float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f;
device.Transform.World = Matrix.RotationY(fAngle);
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, 1.0f, 1.0f, 100.0f);
}
/*protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close(); // Esc was pressed
}
protected override void OnResize(System.EventArgs e)
{
pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
}*/
private bool AppStillIdle
{
get
{
Message msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}
public enum WindowMessage : uint
{
// Misc messages
Destroy = 0x0002,
Close = 0x0010,
Quit = 0x0012,
Paint = 0x000F,
// autres messages...
}
[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public WindowMessage msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
[System.Security.SuppressUnmanagedCodeSecurity] // We wont use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
private void OnApplicationIdle(object sender, EventArgs e)
{
while (AppStillIdle)
{
// Render a frame during idle time (no messages are waiting)
//UpdateEnvironment();
Render();
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
using (Matrices frm = new Matrices())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return;
}
//frm.Show();
// While the form is still valid, render and process messages
System.Windows.Forms.Application.Idle += new EventHandler(frm.OnApplicationIdle);
System.Windows.Forms.Application.Run(frm);
}
}
}
}
Last edited by a moderator: