#include <stdio.h> // 要调用sprintf函数
#include "DirectRender.h"
#include "MazeAutoCreate.h"
// Direct3D的对象和设备
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
// 顶点缓存,用来装载几何图形
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
// DirectX字体全局变量
LPD3DXFONT g_DemoFont = NULL;
RECT g_DemoFontPosition = {0, 0, 0, 0};
bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;
// 创建D3D对象
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;
// 获取计算机显示的模式
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&displayMode))) return false;
// 为创建D3D设备做好准备
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
//全屏选项
if(fullscreen)
{
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = WIDTH;
d3dpp.BackBufferHeight = HEIGHT;
}
else
d3dpp.Windowed = TRUE;
//设置后台缓存
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = displayMode.Format;
// 创建D3D设备
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_D3DDevice))) return false;
// 创建需要进行显示的对象
if(!InitializeMaze()) return false;
return true;
}
// 将迷宫数据m[M][N]转换成顶点缓存的函数
void TranslateToVertex( Maze m[][N], stD3DMaze* pObj )
{
// 规定每一块的宽度和高度
float perWidth = WIDTH*0.78125f/N;
float perHeight = HEIGHT*0.625f/M;
// 规定起始方块的位置
float startX = WIDTH*0.109f;
float startY = HEIGHT*0.1875f;
unsigned long color;
int i, j;
for ( i = 0; i < M; i++ )
{
for ( j = 0; j < N; j++)
{
switch ( m[i][j].state )
{ // 判断颜色
case 0:color = D3DCOLOR_XRGB(255, 255, 255); break;
case 1:color = D3DCOLOR_XRGB(80, 80, 80); break;
case 2:color = D3DCOLOR_XRGB(180, 0, 0); break;
} // 开始转换为顶点格式
pObj->Assign( startX, startY, 0, 1, color ); pObj++;
pObj->Assign( startX + perWidth, startY, 0, 1, color ); pObj++;
pObj->Assign( startX, startY + perHeight, 0, 1, color ); pObj++;
pObj->Assign( startX, startY + perHeight, 0, 1, color ); pObj++;
pObj->Assign( startX + perWidth, startY, 0, 1, color ); pObj++;
pObj->Assign( startX + perWidth, startY + perHeight, 0, 1, color ); pObj++;
startX += perWidth;
}
startX = WIDTH*0.109f;
startY += perHeight;
}
}
// 初始化对象的函数
bool InitializeMaze( void )
{
// 创建字体
if( FAILED( D3DXCreateFont( g_D3DDevice, 26, 0, 0, 0, 0,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, "黑体", &g_DemoFont) ) ) return false;
// 设置字体的位置
g_DemoFontPosition.top = 0;
g_DemoFontPosition.left = 0;
g_DemoFontPosition.right = WIDTH;
g_DemoFontPosition.bottom = HEIGHT;
// 这里定义了maze类的对象
stD3DMaze objData[M*N*6] = { 0 };
Maze m[M][N] = { 0 };
// 自动创建迷宫,把迷宫的值放在m[M][N]数组上
MazeAutoCreate( m );
// 将迷宫数据m[M][N]转换成顶点缓存
TranslateToVertex( m, objData);
// 创建顶点缓存
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
NULL))) return false;
// 将顶点缓存注入内存中
void *ptr;
if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(void**)&ptr, 0))) return false;
// 数据的复制
memcpy(ptr, objData, sizeof(objData));
g_VertexBuffer->Unlock();
return true;
}
// 渲染场景的函数
void RenderScene( void )
{
char str[50];
sprintf( str, "迷宫大小:%d×%d", N, M );
// 将后台缓存置为空
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
// 开始渲染场景
g_D3DDevice->BeginScene();
// 设置字体的位置并显示出来
g_DemoFontPosition.top = 32;
g_DemoFont->DrawText( NULL, "我的游戏:走迷宫 蒋轶民制作",
-1, &g_DemoFontPosition, DT_CENTER,
D3DCOLOR_XRGB(185, 220, 0 ) );
g_DemoFontPosition.top = 60;
g_DemoFont->DrawText( NULL,str,
-1, &g_DemoFontPosition, DT_CENTER,
D3DCOLOR_XRGB(185, 220, 0 ) );
// 渲染物体
g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,
sizeof(stD3DMaze));
g_D3DDevice->SetFVF(D3DFVF_VERTEX);
g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, M*N*2);
// 场景结束,停止渲染
g_D3DDevice->EndScene();
// 显示场景
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}
//释放空间的函数
void Shutdown( void )
{
//释放空间
if(g_D3DDevice != NULL) g_D3DDevice->Release();
if(g_D3D != NULL) g_D3D->Release();
if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
// 将指针置为空
g_D3DDevice = NULL;
g_D3D = NULL;
g_VertexBuffer = NULL;
}