#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;
IDirect3DIndexBuffer9* ib;
//
IDirect3DTexture9* pTex;
//
IDirect3DSurface9* pSur;
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
struct VertexBitmap
{
float _x, _y, _z, _rhw;
float _u, _v;
};
struct VertexBase
{
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 D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)
#define D3DFVF_COLORVERTEX (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(VertexBitmap));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->SetIndices(ib);
g_pd3dDevice->SetTexture(0, pTex);
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 6, 0, 2);
g_pd3dDevice->SetTexture(0, nullptr);
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 = GetModuleHandle(NULL);
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, 400, 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()
{
VertexBitmap vertices[] =
{
{100, 100, 0, 1.0, 0.0,0.0},
{700, 100, 0, 1.0, 1.0,0.0},
{700, 500, 0, 1.0, 1.0,1.0},
{100, 500, 0, 1.0, 0.0,1.0},
};
VertexBase vertices1[] =
{
{100, 100, 0, 1.0, 0xffff0000},
{100, 500, 0, 1.0, 0xffff0000},
{700, 100, 0, 1.0, 0xffff0000},
{700, 500, 0, 1.0, 0xffff0000},
};
g_pd3dDevice->CreateVertexBuffer(4 * sizeof(VertexBitmap), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &vb, 0);
//访问缓冲内存
void* pVertices;
vb->Lock(0, sizeof(vertices), (void**)&pVertices, 0);
memcpy(pVertices, vertices, sizeof(vertices));
vb->Unlock();
//索引对象
g_pd3dDevice->CreateIndexBuffer(6 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ib, 0);
WORD* pIndices = 0;
ib->Lock(0, 0, (void**)&pIndices, 0);
WORD verticesIndex[] = { 0,1,2,0,2,3 };
memcpy(pIndices, verticesIndex, sizeof(verticesIndex));
ib->Unlock();
//纹理加载
D3DXCreateTextureFromFile(g_pd3dDevice, L"E:\\sprite\\2.png", &pTex);
pTex->GetSurfaceLevel(0, &pSur);
D3DSURFACE_DESC desc;
pTex->GetLevelDesc(0, &desc);
}
int main()
{
Appation* app = new Appation();
app->initiate();
initData();
app->run();
return 0;
}
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; }
}
d3d9学习二(绘制纹理)
最新推荐文章于 2024-06-28 23:05:31 发布