#pragma once
#include <windows.h>
#include <d3d9.h>
#define MAX_NAME 36
class Appation
{
public:
Appation();
~Appation();
void run();
bool initiate();
HINSTANCE hInstance;
HWND hWnd;
HICON hIcon;
char lpClassNmae[MAX_NAME];
char lpWndName[MAX_NAME];
};
#include "Appation.h"
#include <tchar.h>
#include <windows.h>
static LRESULT WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
extern Appation* g_theApp = nullptr;
static LPDIRECT3D9 g_pD3D = NULL;
static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
static D3DPRESENT_PARAMETERS g_d3dpp = {};
//创建顶点数据缓冲
IDirect3DVertexBuffer9* vb;
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
struct ColorVertex
{
float _x, _y, _z, rhw;
DWORD _color;
};
Appation::Appation()
{
strcpy_s(lpClassNmae, "Windows Demo");
strcpy_s(lpWndName, "Windows Demo");
hWnd = NULL;
hInstance = GetModuleHandle(NULL);
hIcon = NULL;
g_theApp = this;
}
Appation::~Appation()
{
}
#define D3DFV_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
void Appation::run()
{
MSG msg;
while (true)
{
::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
//开始绘制
g_pd3dDevice->BeginScene();
g_pd3dDevice->SetStreamSource(0, vb, 0, sizeof(ColorVertex));
g_pd3dDevice->SetFVF(D3DFV_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pd3dDevice->EndScene();
//开始显示
g_pd3dDevice->Present(0, 0, 0, 0);
}
}
bool Appation::initiate()
{
//窗口类
WNDCLASSEX wc;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = _T("Windows Demo");
wc.hIcon = hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
RegisterClassEx(&wc);
//窗口创建
hWnd = CreateWindow(wc.lpszClassName, _T("Windows Demo"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
if (!hWnd)
{
return false;
}
if (!CreateDeviceD3D(hWnd))
{
CleanupDeviceD3D();
::UnregisterClass(wc.lpszClassName, wc.hInstance);
return 0;
}
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
return true;
}
void initData()
{
ColorVertex vertices[] =
{
{200.0f, 400.0f, 0.5f, 1.0f, 0xffff0000},
{500.0f, 50.f, 0.5f, 1.0f, 0xff00ff00},
{700.0f, 400.0f, 0.5f, 1.0f, 0xff0000ff},
};
g_pd3dDevice->CreateVertexBuffer(3 * sizeof(ColorVertex), D3DUSAGE_WRITEONLY, D3DFV_CUSTOMVERTEX, D3DPOOL_MANAGED, &vb, 0);
//访问缓冲内存
void* pVertices;
vb->Lock(0, sizeof(vertices), (void**)&pVertices, 0);
memcpy(pVertices, vertices, sizeof(vertices));
vb->Unlock();
}
int main()
{
Appation* app = new Appation();
app->initiate();
initData();
app->run();
}
LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(hwnd, msg, wparam, lparam);
}
bool CreateDeviceD3D(HWND hWnd)
{
//创建d3d对象
if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
return false;
}
//创建d3d设备参数
ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
g_d3dpp.Windowed = TRUE;
g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition.
g_d3dpp.EnableAutoDepthStencil = TRUE;
g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync
//创建d3d设备
if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
return false;
return true;
}
void CleanupDeviceD3D()
{
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
if (g_pD3D) { g_pD3D->Release(); g_pD3D = NULL; }
}