|
|
#include <windows.h>
#include <d3d9.h>
struct Vertex {
float x, y, z;
DWORD diffuse;
};
/*****************************************************************************
GLOBALS
*****************************************************************************/
HWND hWnd = NULL;
PDIRECT3D9 pD3D = NULL;
PDIRECT3DDEVICE9 pd3dDevice = NULL;
PDIRECT3DVERTEXBUFFER9 pd3dVB = NULL;
/*****************************************************************************
PROTOTYPES
*****************************************************************************/
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
int Initialize(HINSTANCE hInstance);
void Shutdown();
void Render();
void CreateWnd(HINSTANCE hInstance, int width, int height);
int CreateDevice();
void CreateVertexBuffer();
void SetupMatrices();
/*****************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, int) {
MSG msg;
ZeroMemory(&msg, sizeof(msg));
if (Initialize(hInstance)) {
while (msg.message != WM_QUIT) {
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
DispatchMessage(&msg);
}
Render();
}
}
Shutdown();
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam) {
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int Initialize(HINSTANCE hInstance) {
WNDCLASSEX wce;
memset(&wce, 0, sizeof(wce));
wce.cbSize = sizeof(wce);
wce.lpfnWndProc = WindowProc;
wce.hInstance = hInstance;
wce.hCursor = LoadCursor(NULL, IDC_ARROW);
wce.lpszClassName = "D3DWND";
RegisterClassEx(&wce);
CreateWnd(hInstance, 800, 600);
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == NULL) {
return 0;
}
if (!CreateDevice()) {
return 0;
}
SetupMatrices();
CreateVertexBuffer();
pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
pd3dDevice->SetStreamSource(0, pd3dVB, 0, sizeof(Vertex));
return 1;
}
void CreateWnd(HINSTANCE hInstance, int width, int height) {
RECT rt;
DWORD style;
rt.top = 0;
rt.bottom = height;
rt.left = 0;
rt.right = width;
style = WS_OVERLAPPEDWINDOW; //WS_POPUP | WS_CAPTION | WS_SYSMENU;
AdjustWindowRectEx(&rt, style, 0, 0);
width = rt.right - rt.left;
height = rt.bottom - rt.top;
hWnd = CreateWindowEx(WS_EX_TOPMOST, "D3DWND", "Direct3D Initialize",
style | WS_VISIBLE, 0, 0, width, height,
NULL, NULL, hInstance, NULL);
}
int CreateDevice(void) {
D3DPRESENT_PARAMETERS d3dpp;
DWORD behavior;
memset(&d3dpp, 0, sizeof(d3dpp));
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
behavior = D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE;
if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
behavior, &d3dpp,
&pd3dDevice))) {
return 0;
}
return 1;
}
void CreateVertexBuffer() {
Vertex vertices[] = {
400, 0, 0, 0xffff0000,
800, 600, 0, 0xff00ff00,
0, 600, 0, 0xff00ffff,
};
pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0,
D3DFVF_XYZ | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT, &pd3dVB, NULL);
void* pV;
pd3dVB->Lock(0, 0, &pV, 0);
memcpy(pV, vertices, sizeof(vertices));
pd3dVB->Unlock();
}
void SetupMatrices() {
D3DMATRIX identity = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
pd3dDevice->SetTransform(D3DTS_WORLD, &identity);
pd3dDevice->SetTransform(D3DTS_VIEW, &identity);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &identity);
}
void Shutdown() {
if (pd3dVB) {
pd3dVB->Release();
pd3dVB = NULL;
}
if (pd3dDevice) {
pd3dDevice->Release();
pd3dDevice = NULL;
}
if (pD3D) {
pD3D->Release();
pD3D = NULL;
}
if (hWnd) {
DestroyWindow(hWnd);
hWnd = NULL;
}
}
void Render() {
pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0, 0, 0);
pd3dDevice->BeginScene();
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
pd3dDevice->EndScene();
pd3dDevice-> resent(NULL, NULL, NULL, NULL);
} |
|