CLASS PROJECT FORM TYPE "DemoEngine":
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DemoEngine
{
public partial class DEngine : Form
{
private Device myDevice;
private PresentParameters presentParams;
private bool isWindowed;
public DEngine(int width, int height, bool windowed, string name)
{
InitializeComponent(); // This is already here for window creation
isWindowed = windowed;
this.Name = name;
this.Text = name;
this.Size = new Size(width, height);
this.ClientSize = this.Size;
if (!windowed) this.FormBorderStyle = FormBorderStyle.None;
// If fullscreen, remove window borders
// This will save us grief later //
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
this.Paint += new PaintEventHandler(Render);
}
private bool InitializeGraphics() // We specify bool so we can catch failures
{
try
{
// This first group of settings applies to both windowed and fullscreen modes
presentParams = new PresentParameters();
presentParams.Windowed = isWindowed;
presentParams.SwapEffect = SwapEffect.Discard;
if (!isWindowed)
{
presentParams.BackBufferFormat = Manager.Adapters[0].CurrentDisplayMode.Format;
presentParams.BackBufferCount = 1;
presentParams.BackBufferWidth = Manager.Adapters[0].CurrentDisplayMode.Width;
presentParams.BackBufferHeight = Manager.Adapters[0].CurrentDisplayMode.Height;
presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
presentParams.PresentationInterval = PresentInterval.Immediate;
}
myDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}
catch (DirectXException)
{
MessageBox.Show("Unable to initialize Direct3D"); return false;
}
return true;
}
// These parameters are required to use this as an addition to the Paint delegate of the Form
private void Render(object sender, PaintEventArgs e)
{
myDevice.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);
myDevice.Present();
this.Invalidate(); // This will tell the window to redraw immediately when it finishes each frame
}
public DEngine()
{
InitializeComponent();
}
}
}
WINDOWS APPLICATION PROJECT CLASS TYPE "DEMO"
static DEngine demo1 = new DEngine(800, 600, true, "Hazy Mind 3D Engine Demo Application");
[STAThread]
public static void Main()
{
demo1.Show();
Application.Run(demo1);
}