DirectX9学习(二)

2017.08.23

了解了如何创建一个D3D变量,如何利用这个变量创建设备,配置这个变量并画一个蓝色背景


#include
#include
#include
#include
using namespace std;
#pragma comment(lib,"d3d9.lib")

const string APPTITLE = "Direct3D_Windowed";
const int SCREEN_W = 1024;
const int SCREEN_H = 768;


//D3D设置
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
bool GameOver = false;


#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)


//GameInit
bool Game_Init(HWND hwnd)
{
	//初始化D3D
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (d3d == NULL)
	{
		MessageBox(hwnd, "Error Initialize D3D9", "ERROR", MB_OK);
		return false;
	}
	//设置呈现参数
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = SCREEN_W;
	d3dpp.BackBufferHeight = SCREEN_H;
	d3dpp.hDeviceWindow = hwnd;
	
	//创建D3D的设备
	d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hwnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev
		);
	
	if (d3ddev == NULL)
	{
		MessageBox(hwnd, "Error Create D3DDEV", "ERROR", MB_OK);
		return false;
	}


	//初始化完成
	return true;
}


void Game_Run(HWND hwnd)
{
	if (!d3ddev)
		return;
	//Clean backbuffer to bright screen
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);


	//开始渲染
	if (d3ddev->BeginScene())
	{
		//do sth
		
		//停止渲染
		d3ddev->EndScene();
		//copy back buffer to the frame buffer
		d3ddev->Present(NULL, NULL, NULL, NULL);


	}


	if (KEY_DOWN(VK_ESCAPE))
	{
		PostMessage(hwnd, WM_DESTROY, 0, 0);
	}


}


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


//Win消息处理
LRESULT CALLBACK WinProc(HWND hWnd, UINT message,
	WPARAM wParam, LPARAM lParam)
{
	//HWND:窗口句柄,使用窗口句柄创建一个新的设备环境句柄HDC,只要引用一个窗口或空间就必须得用到窗口句柄


	switch (message)
	{		
	case WM_DESTROY:
		GameOver = true;
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, LPSTR lpCmdLine, int nCmdShow)
{
	//set the new window properties
	WNDCLASSEX wc;
	MSG msg;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;  //在移动或尺寸更新完|高度调整后重新绘制
	wc.lpfnWndProc = (WNDPROC)WinProc;  //返回一个指向回调函数的指针,如果不设定这个值,消息就无法传递给HWND
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "MainWindowClass";
	wc.hIconSm = NULL;
	if (!RegisterClassEx(&wc))
		return FALSE;
	
	//Create a Window
	HWND hwnd = CreateWindow("MainWindowClass",
		APPTITLE.c_str(),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		SCREEN_W, SCREEN_H,
		(HWND)NULL,
		(HMENU)NULL,
		hInstance,
		(LPVOID)NULL
	);


	if (hwnd == 0) //创建失败
		return 0;
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);


	//初始化
	if (!Game_Init(hwnd))
		return 0;


	while (!GameOver)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);


		}
		Game_Run(hwnd);
	}
	
	Game_End(hwnd);
	return msg.wParam;
}

D3D9应该是可以直接用的(VS2015) 直接在这边加上

#pragma comment(lib,"d3d9.lib") 

#include

就能用了


2017.08.24

全屏的操作:在创建窗口句柄的时候,将CreateWindow做修改

HWND hwnd = CreateWindow("MainWindowClass",
		APPTITLE.c_str(),
		WS_EX_TOPMOST | WS_POPUP,
		0,
		0,
		SCREEN_W, SCREEN_H,
		(HWND)NULL,
		(HMENU)NULL,
		hInstance,
		(LPVOID)NULL
	);
然后需要修改呈现参数,也就是d3dpp的参数,不过首先需要获得显示设备的资料,然后再修改点呈现参数,需要添加和修改的代码:

//添加
//为了在任意机器上全屏,需要获得显示器的资料,防止显示器模式转换
	D3DDISPLAYMODE dm;
	d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);
//修改:
	d3dpp.Windowed = FALSE;
	d3dpp.BackBufferFormat = dm.Format; //颜色深度为显示器格式的颜色深度
	d3dpp.BackBufferWidth = dm.Width;
	d3dpp.BackBufferHeight = dm.Height;





你可能感兴趣的:(DirectX学习)