| 1: |
using System; |
| 2: |
using System.Drawing; |
| 3: |
using System.Windows.Forms; |
| 4: |
using Microsoft.DirectX; |
| 5: |
using Microsoft.DirectX.Direct3D; |
| 6: |
|
| 7: |
public class CreateDevice : Form |
| 8: |
{ |
| 9: |
|
| 10: |
Device device = null; |
| 11: |
|
| 12: |
public CreateDevice() |
| 13: |
{ |
| 14: |
|
| 15: |
this.ClientSize = new System.Drawing.Size(400,300); |
| 16: |
|
| 17: |
this.Text = "DirectX Tutorial - CreateDevice"; |
| 18: |
} |
| 19: |
|
| 20: |
public bool InitializeGraphics() |
| 21: |
{ |
| 22: |
try |
| 23: |
{ |
| 24: |
|
| 25: |
PresentParameters presentParams = new PresentParameters(); |
| 26: |
presentParams.Windowed=true; |
| 27: |
presentParams.SwapEffect = SwapEffect.Discard; |
| 28: |
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); |
| 29: |
return true; |
| 30: |
} |
| 31: |
catch (DirectXException) |
| 32: |
{ |
| 33: |
return false; |
| 34: |
} |
| 35: |
} |
| 36: |
private void Render() |
| 37: |
{ |
| 38: |
if (device == null) |
| 39: |
return; |
| 40: |
|
| 41: |
|
| 42: |
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0); |
| 43: |
|
| 44: |
device.BeginScene(); |
| 45: |
|
| 46: |
|
| 47: |
|
| 48: |
|
| 49: |
device.EndScene(); |
| 50: |
device.Present(); |
| 51: |
} |
| 52: |
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) |
| 53: |
{ |
| 54: |
this.Render(); |
| 55: |
} |
| 56: |
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) |
| 57: |
{ |
| 58: |
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) |
| 59: |
this.Close(); |
| 60: |
} |
| 61: |
protected void DisposeGraphics() |
| 62: |
{ |
| 63: |
} |
| 64: |
|
| 65: |
|
| 66: |
static void Main() |
| 67: |
{ |
| 68: |
|
| 69: |
using (CreateDevice frm = new CreateDevice()) |
| 70: |
{ |
| 71: |
if (!frm.InitializeGraphics()) |
| 72: |
{ |
| 73: |
MessageBox.Show("Konnte Hardware nicht initialisieren."); |
| 74: |
return; |
| 75: |
} |
| 76: |
frm.Show(); |
| 77: |
|
| 78: |
|
| 79: |
while(frm.Created) |
| 80: |
{ |
| 81: |
frm.Render(); |
| 82: |
Application.DoEvents(); |
| 83: |
} |
| 84: |
frm.DisposeGraphics(); |
| 85: |
} |
| 86: |
} |
| 87: |
} |