第一个direct3d程序

#include <windows.h>
#include <d3d9.h>
#include <time.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"d3d9.lib")

// program settings
const string APPTITLE = "Direct3D_Windowed";
const int WIDTH = 1024;
const int HEIGHT = 768;
// Directx3d objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
bool gameOver = false;
// macro to detect key presses 检测按键的宏
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0)

// Game initialization function
BOOL Game_Init(HWND hwnd)
{
	// initialize Direct3D
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (d3d == NULL)
	{
		MessageBox(hwnd, "Error initializing Directx3D", "Error", MB_OK | MB_ICONERROR);
		return FALSE;
	}
	// set Direct3D presentation parameters
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = WIDTH;
	d3dpp.BackBufferHeight = HEIGHT;
	d3dpp.hDeviceWindow = hwnd;
	// create Direct3D device
	d3d->CreateDevice(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hwnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev);
	if (d3ddev == NULL)
	{
		MessageBox(hwnd, "Error creating Direct#d device", "Error", MB_OK | MB_ICONERROR);
		return FALSE;
	}
	return TRUE;
}

// Game update function
void Game_Run(HWND hwnd)
{
	// make sure the Direct3D ddevice is valid
	if (!d3ddev)
		return;
	// clear the nackbuffer to brigt green
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
	// start rendering
	if (d3ddev->BeginScene())
	{
		// do something?
		// stop rendering
		d3ddev->EndScene();
		// copy back buffer to the frame buffer
		d3ddev->Present(NULL, NULL, NULL, NULL);
	}
	// check for the escape key (to exit program)
	if (KEY_DOWN(VK_ESCAPE))
	{
		PostMessage(hwnd, WM_DESTROY, 0, 0);
	}
}

// Game shutdown function
void Game_End(HWND hwnd)
{
	if (d3ddev)
	{
		d3ddev->Release();
		d3ddev = NULL;
	}
	if (d3d)
	{
		d3d->Release();
		d3d = NULL;
	}
}

// Windows event handling function
LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_DESTROY:
		gameOver = true;
		break;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

// Main Windows entry function
int WINAPI WinMain(
	_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPSTR lpCmdLine,
	_In_ int nShowCmd
)
{
	WNDCLASSEX wc;
	MSG msg;
	ZeroMemory(&wc, sizeof(wc));
	ZeroMemory(&msg, sizeof(msg));
	// set the new window's properties
	// previously found in the MyRegisterClass function
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.lpfnWndProc = (WNDPROC)WinProc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hIcon = NULL;
	wc.hIconSm = NULL;
	wc.lpszMenuName = NULL;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszClassName = "MainWindowClass";
	if (!RegisterClassEx(&wc))
		return FALSE;
	// create a new window
	// previously found in the InitInstance function
	HWND hwnd = CreateWindow("MainWindowClass",
		APPTITLE.c_str(),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		WIDTH, HEIGHT,
		(HWND)NULL,
		(HMENU)NULL,
		hInstance,
		(LPVOID)NULL);
	// was there an error creating the window?
	if (hwnd == NULL)
		return 0;

	// display the window
	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);
	// initialize the game
	if (!Game_Init(hwnd))
		return 0;

	// main message loop
	while (!gameOver)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		Game_Run(hwnd);
	}
	Game_End(hwnd);
	return msg.wParam;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值