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
|
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
LPDIRECT3D9 d3d; //the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; //the pointer to the device class
LPDIRECT3DTEXTURE9 Point; //Pointer to Texture
LPD3DXSPRITE d3dspt; //pointer to Sprite
//function prototypen
void initD3D(HWND hWnd); //Initializies Direct3D
void render_frame(); //renders a single frame
void cleanD3D(); //release memory
//Set up window Callback funktion
LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wparam,LPARAM lparam);
//MAIN
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our First Direct3D Program",
WS_OVERLAPPEDWINDOW,
300, 300,
800, 600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
// enter the main loop:
MSG msg;
while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break;
render_frame();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp; //create a struct to hold various device informations
ZeroMemory(&d3dpp,sizeof(d3dpp)); //struct frei machen
d3dpp.Windowed = TRUE; //Windowed
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; //Discard old frame
d3dpp.hDeviceWindow = hWnd; //set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
//Create a device class using this informations an dinformation from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
D3DXCreateSprite(d3ddev,&d3dspt);
D3DXCreateTextureFromFile(d3ddev, L"image.png",&Point);
if(Point == 0)
{
SendMessage(hWnd,WM_QUIT,0,0);
return;
}
return;
}
void render_frame()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0);
d3ddev->BeginScene();
d3dspt->Begin(NULL);
D3DXVECTOR3 center(0.0f,0.0f,0.0f); //center at the upper left corner
D3DXVECTOR3 postition(10.f,10.f,10.f); //position on screen
d3dspt->Draw(Point,NULL,¢er,&postition,D3DCOLOR_XRGB(255,0,0));//draw
d3dspt->End();
d3ddev->EndScene(); //ends the 3D scene
d3ddev->Present(NULL,NULL,NULL,NULL); //Display all not only parts of it!
}
void cleanD3D()
{
Point->Release();
d3ddev->Release();
d3d->Release();
}
|