d3d9学习二(绘制纹理)

#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; }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值