using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace D3D_basics
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Devise DX_Device = null; // Drawing device
public dx()
{
this.ClientSize = new Size(256,256); // Specify the client size
this.Text = "My First DirectX Program"; // Specify the title
}
public bool InitializeDirect3D()
{
try
{
PresentParameters pps = new PresentParameters();
pps.Windowed = true; // Specify that it will be in a window
pps.SwapEffect = SwapEffect.Discard; // After the current screen is drawn, it will be automatically // deleted from memory
DX_Device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pps); // Put everything into the device
return true;
}
catch(DirectXException e)
{
MessageBox.Show(e.ToString(), "Error"); // Handle all the exceptions
return false;
}
}
static void Main()
{
dx form = new dx(); // Create the form
if (form.InitializeDirect3D() == false) // Check if D3D could be initialized
{
MessageBox.Show("Could not initialize Direct3D.", "Error");
return;
}
form.Show(); // When everything is initialized, show the form
while(form.Created) // This is our message loop
{
form.Render(); // Keep rendering until the program terminates
Application.DoEvents(); // Process the events, like keyboard and mouse input
}
}
private void Render()
{
if(DX_Device == null) // If the device is empty don't bother rendering
{
return;
}
DX_Device.Clear(ClearFlags.Target, Color.White, 1.0f, 0); // Clear the window to white
DX_Device.BeginScene();
// Rendering is done here
DX_Device.EndScene();
DX_Device.Present();
}
}
}