1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
public Form1()
{
this.ClientSize = new System.Drawing.Size(1024, 768);
InitPresentParameters();
//Cube
System.Drawing.Color[] Colors = new System.Drawing.Color[6];
Colors[0] = System.Drawing.Color.Green;
Colors[1] = System.Drawing.Color.Red;
Colors[2] = System.Drawing.Color.Purple;
Colors[3] = System.Drawing.Color.Silver;
Colors[4] = System.Drawing.Color.Snow;
Colors[5] = System.Drawing.Color.Violet;
c1 = new Cube(50, 50, 50, 50, 50, 50, Colors, m_Device);
InitializeComponent();
}
private void InitDevice()
{
}
private void InitPresentParameters()
{
m_PresentParameters = new PresentParameters();
m_PresentParameters.Windowed = true;
m_PresentParameters.SwapEffect = SwapEffect.Discard;
//
// For each adapter, examine all of its display modes to see if any
// of them can give us the hardware support we desire.
//
bool desiredAdapterModeFound = false;
// For each Adapter...
foreach (AdapterInformation adapter in Manager.Adapters)
{
// Examine each display mode available.
foreach (DisplayMode display in adapter.SupportedDisplayModes)
{
// Does this adapter mode support a mode of 640 x 480?
if (display.Width != 640 || display.Height != 480)
continue;
// Does this adapter mode support a 32-bit RGB pixel format?
if (display.Format != Format.X8R8G8B8)
continue;
// Does this adapter mode support a refresh rate of 75 MHz?
if (display.RefreshRate != 75)
continue;
// We found a match!
desiredAdapterModeFound = true;
break;
}
}
if (desiredAdapterModeFound == false)
{
// TODO: Handle lack of support for desired adapter mode...
MessageBox.Show("HELP");
return;
}
//
// Here's the manual way of verifying hardware support.
//
// Can we get a 32-bit back buffer?
if (!Manager.CheckDeviceType(Manager.Adapters.Default.Adapter,
DeviceType.Hardware,
Format.X8R8G8B8,
Format.X8R8G8B8,
false))
{
// TO DO: Handle lack of support for a 32-bit back buffer...
MessageBox.Show("32 Bit Back Buffer Problem");
return;
}
// Does the hardware support a 16-bit z-buffer?
if (!Manager.CheckDeviceFormat(Manager.Adapters.Default.Adapter,
DeviceType.Hardware,
Manager.Adapters.Default.CurrentDisplayMode.Format,
Usage.DepthStencil,
ResourceType.Surface,
DepthFormat.D16))
{
// POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!
MessageBox.Show("16 Bit Z buffer Problem");
return;
}
//
// Do we support hardware vertex processing? if so, use it.
// If not, downgrade to software.
//
Caps caps = Manager.GetDeviceCaps(Manager.Adapters.Default.Adapter,
DeviceType.Hardware);
CreateFlags flags;
if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
flags = CreateFlags.HardwareVertexProcessing;
else
flags = CreateFlags.SoftwareVertexProcessing;
//
// Everything checks out - create a simple, full-screen device.
//
PresentParameters d3dpp = new PresentParameters();
m_PresentParameters.Windowed = true;
m_PresentParameters.EnableAutoDepthStencil = true;
m_PresentParameters.AutoDepthStencilFormat = DepthFormat.D16;
m_PresentParameters.SwapEffect = SwapEffect.Discard;
m_PresentParameters.BackBufferWidth = 640;
m_PresentParameters.BackBufferHeight = 480;
m_PresentParameters.BackBufferFormat = Format.X8R8G8B8;
m_PresentParameters.PresentationInterval = PresentInterval.Immediate;
m_Device = new Device(0, DeviceType.Hardware, this, flags, m_PresentParameters);
// Register an event-handler for DeviceReset and call it to continue
// our setup.
m_Device.DeviceReset += new System.EventHandler(this.OnResetDevice);
OnResetDevice(m_Device, null);
m_mProjection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)ClientSize.Width / (float)ClientSize.Height, 1.0f, 100.0f);
m_Device.SetTransform(TransformType.Projection, m_mProjection);
}
private void OnApplicationIdle(object sender, EventArgs e)
{
double tstart = 0.0f;
double tstop = 0.0f;
double ttime = 0.0f;
int result;
while (AppStillIdle)
{
tstart = Tools.PerformanceCounter.Time;
if(!m_Device.CheckCooperativeLevel(out result))
{
if(result == (int)ResultCode.DeviceLost)
{
// nichts zu machen, warte ein bisschen
//Thread.CurrentThread.Sleep(50);
continue; // nächste schleife
}
else
if(result == (int)ResultCode.DeviceNotReset)
{
m_Device.Reset(m_PresentParameters);
}
}
move(ttime);
try
{
render(ttime);
}
catch (DeviceLostException dle)
{
}
tstop = Tools.PerformanceCounter.Time;
ttime = tstop - tstart;
}
}
private bool AppStillIdle
{
get
{
NativeMethods.Message msg;
return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}
|