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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// Test.cpp : Definiert den Einstiegspunkt für die Anwendung.
//
#include "stdafx.h"
#include "Test.h"
#include "d3d9.h"
#define MAX_LOADSTRING 100
// Globale Variablen:
HINSTANCE hInst; // Aktuelle Instanz
TCHAR szTitle[MAX_LOADSTRING]; // Titelleistentext
TCHAR szWindowClass[MAX_LOADSTRING]; // Klassenname des Hauptfensters
// The Direct3D and Device object
IDirect3D9 *g_pD3D = NULL;
IDirect3DDevice9 *g_pD3DDevice = NULL;
// The 2-D vertex format and descriptor
typedef struct {
FLOAT x, y, z; // 2-D coordinates
FLOAT rhw; // rhw
D3DCOLOR Diffuse; // Diffuse color component
} sVertex;
#define VERTEXFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
// Vertex buffer
IDirect3DVertexBuffer9 *g_pVB = NULL;
// Vorwärtsdeklarationen der in diesem Codemodul enthaltenen Funktionen:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Hier Code einfügen.
MSG msg;
HACCEL hAccelTable;
// Globale Zeichenfolgen initialisieren
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Anwendungsinitialisierung ausführen:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST));
// Initialize application
if( !DoInit() )
return -1;
// Hauptmeldungsschleife:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if( !Render() ) break;
if( !Move() ) break;
}
// Run shutdown function
DoShutdown();
return (int) msg.wParam;
}
//
// FUNKTION: MyRegisterClass()
//
// ZWECK: Registriert die Fensterklasse.
//
// KOMMENTARE:
//
// Sie müssen die Funktion verwenden, wenn Sie möchten, dass der Code
// mit Win32-Systemen kompatibel ist, bevor die RegisterClassEx-Funktion
// zu Windows 95 hinzugefügt wurde. Der Aufruf der Funktion ist wichtig,
// damit die kleinen Symbole, die mit der Anwendung verknüpft sind,
// richtig formatiert werden.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNKTION: InitInstance(HINSTANCE, int)
//
// ZWECK: Speichert das Instanzenhandle und erstellt das Hauptfenster.
//
// KOMMENTARE:
//
// In dieser Funktion wird das Instanzenhandle in einer globalen Variablen gespeichert, und das
// Hauptprogrammfenster wird erstellt und angezeigt.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;
HWND hWnd;
hInst = hInstance; // Instanzenhandle in der globalen Variablen speichern
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Do a windowed mode initialization of Direct3D
if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return FALSE;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
return FALSE;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = FALSE;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pD3DDevice)))
return FALSE;
return TRUE;
}
//
// FUNKTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// ZWECK: Verarbeitet Meldungen vom Hauptfenster.
//
// WM_COMMAND - Verarbeiten des Anwendungsmenüs
// WM_PAINT - Zeichnen des Hauptfensters
// WM_DESTROY - Beenden-Meldung anzeigen und zurückgeben
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Menüauswahl bearbeiten:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Hier den Zeichnungscode hinzufügen.
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Meldungshandler für Infofeld.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
//
// FUNCTION: DoInit()
//
// PURPOSE: Initialize the sVertex struct and d3d
//
//
bool DoInit()
{
BYTE *Ptr;
sVertex Verts[8] = {
{ 100.0f, 100.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(0,64,128,255) },
{ 300.0f, 100.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(0,64,128,255) },
{ 100.0f, 300.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(0,64,128,255) },
{ 300.0f, 300.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(0,64,128,255) },
{ 50.0f, 150.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(128,0,0,128) },
{ 350.0f, 150.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(128,0,0,128) },
{ 50.0f, 350.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(128,0,0,128) },
{ 350.0f, 350.0f, 1.0f, 1.0f, D3DCOLOR_RGBA(128,0,0,128) }
};
// Create the vertex buffer and set data
g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)*8, 0, \
VERTEXFVF, D3DPOOL_DEFAULT, \
&g_pVB, NULL);
g_pVB->Lock(0,0, (void**)&Ptr, 0);
memcpy(Ptr, Verts, sizeof(Verts));
g_pVB->Unlock();
return TRUE;
}
//
// FUNCTION: DoShutdown()
//
// PURPOSE: Close d3d and d3d buffer
//
//
bool DoShutdown()
{
// Release vertex buffer
if(g_pVB != NULL)
g_pVB->Release();
// Release device and 3D objects
if(g_pD3DDevice != NULL)
g_pD3DDevice->Release();
if(g_pD3D != NULL)
g_pD3D->Release();
return TRUE;
} // endfunction: DoShutdown
//
// FUNCTION: Render()
//
// PURPOSE: Renders d3d objects
//
//
bool Render()
{
// Clear device backbuffer
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, \
D3DCOLOR_RGBA(0,0,0,255), 1.0f, 0);
// Begin scene
if(SUCCEEDED(g_pD3DDevice->BeginScene()))
{
// Set the vertex stream and shader
g_pD3DDevice->SetStreamSource(0, g_pVB, 0, sizeof(sVertex));
g_pD3DDevice->SetFVF(VERTEXFVF);
// Disable alpha blending and draw 2 polygons
g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
// Enable alpha blending and draw 2 polygons
g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
// End the scene
g_pD3DDevice->EndScene();
}
// Display the scene
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
return true;
} // endfunction: Render
//
// FUNCTION: Move()
//
// PURPOSE: Moves d3d objects
//
//
bool Move()
{
return true;
} // endfunction: Move
|