(1)创建字体:
DirectX提供一个字体类,它为我们抽象了整个过程,从而让我们可以少关注流程(比如满载字体的位图图像)而花更多的时间在游戏的代码上。ID3DXFont接口用于创建字
体,其指针版本已经预定义好了。
1 LPD3DFONT font;
我们将使用一个名为D3DXCreateFontIndirect的函数来创建字体并且为字体打印做准备。不过在这之前,我们必须先使用D3DXFONT_DESC结构来时
只想要的字体属性。
(2)字体描述符:
D3DXFONT_DESC结构由以下属性组成:
* INT Height
* UINT Width
* UINT Weight
* UINT MipLevels
* BOOL Italic
* BYTE CharSet
* BYTE OutputPrecision
* BYTE Quality
* BYTE PitchAndFamily
* CHAR FaceName[LF_FACESIZE]
不要被吓到,因为大多数都设为0或者是默认值。只有两个属性真正重要的:Height和FaceName。一下是一个示例,他被初始化成Arial 24点
字体的值
D3DXFONT_DESC desc = { 24, //height 0, //width 0, //weight 0, //miplevels false, //italic DEFAULT_CHARSET, //charset OUT_TT_PRECIS, //output precision CLIP_DEFAULT_PRECIS, //quality DEFAULT_PITCH, //pitch and family "Arial" //font name };
(3)创建字体对象:
在设置完字体描述符后,就可以使用D3DCreateFontIndirect函数来创建字体对象了。这个函数有三个参数:
* Direct3D设备
* D3DXFONT_DESC
* LPD3DXFONT
(4)可重用的MakeFont函数:
我们来将所有的这些代码放在一个可重用的函数中,然后添加到游戏库中。这个函数需要字体名称以及自体点尺寸作为参数,它返回一个指向
LPD3DXFONT对象的指针。
1 LPD3DXFONT MakeFont(string name, int size) 2 { 3 LPD3DXFONT font = NULL; 4 D3DXFONT_DESC desc = { 5 size, //height 6 0, //width 7 0, //weight 8 0, //miplevels 9 false, //italic 10 DEFAULT_CHARSET, //charset 11 OUT_TT_PRECIS, //output precision 12 CLIP_DEFAULT_PRECIS, //quality 13 DEFAULT_PITCH, //pitch and family 14 "" //font name 15 }; 16 strcpy(desc.FaceName, name.c_str()); 17 D3DXCreateFontIndirect(d3ddev, &desc, &font); 18 return font; 19 }
(5)使用ID3DXFont打印文本:
我们已经学习了创建字体对象的方法,但还不知道如何用它将文本打印到屏幕上。这是将要学习的内容。巍峨使用一个已有的字体来打印文本(
这个字体先前创建并初始化过),可以使用ID3DXFont::DrawText()函数。此函数需要的属性如下:
* LPD3DXSPRITE pSprite 精灵渲染对象
* LPCSTR pString 要打印的文本
* INT count 文本长度
* LPRECT pRect 指定位置和边界的矩形
* DWORD format 比如DT_WORDBREAK这样的格式化选项
* D3DCOLOR color 文本的输出颜色
(6)使用DrawText打印:
我们假设已经创建了名为“font”的字体对象,并且使用DrawText函数打印一些东西.当然一个好的方法是将这些代码包装成一个可重用的函数。
如下所示:
1 void FontPrint(LPD3DXFONT font, int x, int y, string text, D3DCOLOR color) 2 { 3 //figure out the text boundary 4 RECT rect = { x, y, 0, 0 }; 5 font->DrawText(NULL, text.c_str(), text.length(), &rect, DT_CALCRECT, color); 6 //print the text 7 font->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_LEFT, color); 8 }
(7)文本折行:
1 RECT rect = { 60,250,350,700 }; //分别是左,上,右,下 2 D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255); 3 string text = "This is a long string that will be "; 4 text += "wrapped inside a rectangle."; 5 fontTimesNewRoman40->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_WORDBREAK, white);
(8)总代码:
MyDirectX.h:
1 #pragma once 2 //header files 3 #define WIN32_EXTRA_LEAN 4 #define DIRECTINPUT_VERSION 0x0800 5 #include <windows.h> 6 #include <d3d9.h> 7 #include <d3dx9.h> 8 #include <dinput.h> 9 #include <xinput.h> 10 #include <ctime> 11 #include <iostream> 12 #include <iomanip> 13 using namespace std; 14 15 //libraries 16 #pragma comment(lib,"winmm.lib") 17 #pragma comment(lib,"user32.lib") 18 #pragma comment(lib,"gdi32.lib") 19 #pragma comment(lib,"dxguid.lib") 20 #pragma comment(lib,"d3d9.lib") 21 #pragma comment(lib,"d3dx9.lib") 22 #pragma comment(lib,"dinput8.lib") 23 #pragma comment(lib,"xinput.lib") 24 25 //program values 26 extern const string APPTITLE; 27 extern const int SCREENW; 28 extern const int SCREENH; 29 extern bool gameover; 30 31 //macro to detect key presses 32 #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) 33 34 //Direct3D objects 35 extern LPDIRECT3D9 d3d; 36 extern LPDIRECT3DDEVICE9 d3ddev; 37 extern LPDIRECT3DSURFACE9 backbuffer; 38 extern LPD3DXSPRITE spriteobj; 39 40 //Direct3D functions 41 bool Direct3D_Init(HWND hwnd, int width, int height, bool fullscreen); 42 void Direct3D_Shutdown(); 43 LPDIRECT3DSURFACE9 LoadSurface(string filename); 44 void DrawSurface(LPDIRECT3DSURFACE9 dest, float x, float y, LPDIRECT3DSURFACE9 source); 45 D3DXVECTOR2 GetBitmapSize(string filename); 46 LPDIRECT3DTEXTURE9 LoadTexture(string filename, D3DCOLOR transcolor = D3DCOLOR_XRGB(0, 0, 0)); 47 void Sprite_Draw_Frame(LPDIRECT3DTEXTURE9 texture, int destx, int desty, int framenum, int framew, int frameh, int columns); 48 void Sprite_Animate(int &frame, int startframe, int endframe, int direction, int &starttime, int delay); 49 void Sprite_Transform_Draw(LPDIRECT3DTEXTURE9 image, int x, int y, int width, int height, int frame = 0, int columns = 1, 50 float rotation = 0.0f, float scaling = 1.0f, D3DCOLOR color = D3DCOLOR_XRGB(255, 255, 255)); 51 52 //DirectInput objects, devices, and states 53 extern LPDIRECTINPUT8 dinput; 54 extern LPDIRECTINPUTDEVICE8 dimouse; 55 extern LPDIRECTINPUTDEVICE8 dikeyboard; 56 extern DIMOUSESTATE mouse_state; 57 extern XINPUT_GAMEPAD controllers[4]; 58 59 //DirectInput functions 60 bool DirectInput_Init(HWND); 61 void DirectInput_Update(); 62 void DirectInput_Shutdown(); 63 bool Key_Down(int); 64 int Mouse_Button(int); 65 int Mouse_X(); 66 int Mouse_Y(); 67 void XInput_Vibrate(int contNum = 0, int amount = 65535); 68 bool XInput_Controller_Found(); 69 70 //game functions 71 bool Game_Init(HWND window); 72 void Game_Run(HWND window); 73 void Game_End(); 74 75 //sprite structure 76 struct SPRITE { 77 float x, y; 78 int frame, columns; 79 int width, height; 80 float scaling, rotation; 81 int startframe, endframe; 82 int starttime, delay; 83 int direction; 84 float velx, vely; 85 D3DCOLOR color; 86 SPRITE() 87 { 88 frame = 0; 89 columns = 1; 90 width = height = 0; 91 scaling = 1.0f; 92 rotation = 0.0f; 93 startframe = endframe = 0; 94 direction = 1; 95 starttime = delay = 0; 96 velx = vely = 0.0f; 97 color = D3DCOLOR_XRGB(255, 255, 255); 98 } 99 }; 100 //bouding box collision detection 101 int Collision(SPRITE sprite1, SPRITE sprite2); 102 103 //font function 104 LPD3DXFONT MakeFont(string name, int size); 105 void FontPrint(LPD3DXFONT font, int x, int y, string text, D3DCOLOR color = D3DCOLOR_XRGB(255, 255, 255));
MyDirectX.cpp:
1 #include "MyDirectX.h" 2 #include <iostream> 3 using namespace std; 4 5 //Direct3D variables 6 LPDIRECT3D9 d3d = NULL; 7 LPDIRECT3DDEVICE9 d3ddev = NULL; 8 LPDIRECT3DSURFACE9 backbuffer = NULL; 9 LPD3DXSPRITE spriteobj; 10 11 //DirectInput variables 12 LPDIRECTINPUT8 dinput = NULL; 13 LPDIRECTINPUTDEVICE8 dimouse = NULL; 14 LPDIRECTINPUTDEVICE8 dikeyboard = NULL; 15 DIMOUSESTATE mouse_state; 16 char keys[256]; 17 XINPUT_GAMEPAD controllers[4]; 18 19 20 bool Direct3D_Init(HWND window, int width, int height, bool fullscreen) 21 { 22 //initialize Direct3D 23 d3d = Direct3DCreate9(D3D_SDK_VERSION); 24 if (!d3d) return false; 25 26 //set Direct3D presentation parameters 27 D3DPRESENT_PARAMETERS d3dpp; 28 ZeroMemory(&d3dpp, sizeof(d3dpp)); 29 d3dpp.hDeviceWindow = window; 30 d3dpp.Windowed = (!fullscreen); 31 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 32 d3dpp.EnableAutoDepthStencil = 1; 33 d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 34 d3dpp.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL; 35 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 36 d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 37 d3dpp.BackBufferCount = 1; 38 d3dpp.BackBufferWidth = width; 39 d3dpp.BackBufferHeight = height; 40 41 //create Direct3D device 42 d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, 43 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); 44 if (!d3ddev) return false; 45 46 47 //get a pointer to the back buffer surface 48 d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); 49 50 //create sprite object 51 D3DXCreateSprite(d3ddev, &spriteobj); 52 53 return 1; 54 } 55 56 void Direct3D_Shutdown() 57 { 58 if (spriteobj) spriteobj->Release(); 59 60 if (d3ddev) d3ddev->Release(); 61 if (d3d) d3d->Release(); 62 } 63 64 void DrawSurface(LPDIRECT3DSURFACE9 dest, float x, float y, LPDIRECT3DSURFACE9 source) 65 { 66 //get width/height from source surface 67 D3DSURFACE_DESC desc; 68 source->GetDesc(&desc); 69 70 //create rects for drawing 71 RECT source_rect = { 0, 0, (long)desc.Width, (long)desc.Height }; 72 RECT dest_rect = { (long)x, (long)y, (long)x + desc.Width, (long)y + desc.Height }; 73 74 //draw the source surface onto the dest 75 d3ddev->StretchRect(source, &source_rect, dest, &dest_rect, D3DTEXF_NONE); 76 77 } 78 79 LPDIRECT3DSURFACE9 LoadSurface(string filename) 80 { 81 LPDIRECT3DSURFACE9 image = NULL; 82 83 //get width and height from bitmap file 84 D3DXIMAGE_INFO info; 85 HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info); 86 if (result != D3D_OK) 87 return NULL; 88 89 //create surface 90 result = d3ddev->CreateOffscreenPlainSurface( 91 info.Width, //width of the surface 92 info.Height, //height of the surface 93 D3DFMT_X8R8G8B8, //surface format 94 D3DPOOL_DEFAULT, //memory pool to use 95 &image, //pointer to the surface 96 NULL); //reserved (always NULL) 97 98 if (result != D3D_OK) return NULL; 99 100 //load surface from file into newly created surface 101 result = D3DXLoadSurfaceFromFile( 102 image, //destination surface 103 NULL, //destination palette 104 NULL, //destination rectangle 105 filename.c_str(), //source filename 106 NULL, //source rectangle 107 D3DX_DEFAULT, //controls how image is filtered 108 D3DCOLOR_XRGB(0, 0, 0), //for transparency (0 for none) 109 NULL); //source image info (usually NULL) 110 111 //make sure file was loaded okay 112 if (result != D3D_OK) return NULL; 113 114 return image; 115 } 116 117 118 D3DXVECTOR2 GetBitmapSize(string filename) 119 { 120 D3DXIMAGE_INFO info; 121 D3DXVECTOR2 size = D3DXVECTOR2(0.0f, 0.0f); 122 123 HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info); 124 125 if (result == D3D_OK) 126 size = D3DXVECTOR2((float)info.Width, (float)info.Height); 127 else 128 size = D3DXVECTOR2((float)info.Width, (float)info.Height); 129 130 return size; 131 } 132 133 LPDIRECT3DTEXTURE9 LoadTexture(std::string filename, D3DCOLOR transcolor) 134 { 135 LPDIRECT3DTEXTURE9 texture = NULL; 136 137 //get width and height from bitmap file 138 D3DXIMAGE_INFO info; 139 HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info); 140 if (result != D3D_OK) return NULL; 141 142 //create the new texture by loading a bitmap image file 143 D3DXCreateTextureFromFileEx( 144 d3ddev, //Direct3D device object 145 filename.c_str(), //bitmap filename 146 info.Width, //bitmap image width 147 info.Height, //bitmap image height 148 1, //mip-map levels (1 for no chain) 149 D3DPOOL_DEFAULT, //the type of surface (standard) 150 D3DFMT_UNKNOWN, //surface format (default) 151 D3DPOOL_DEFAULT, //memory class for the texture 152 D3DX_DEFAULT, //image filter 153 D3DX_DEFAULT, //mip filter 154 transcolor, //color key for transparency 155 &info, //bitmap file info (from loaded file) 156 NULL, //color palette 157 &texture); //destination texture 158 159 //make sure the bitmap textre was loaded correctly 160 if (result != D3D_OK) return NULL; 161 162 return texture; 163 } 164 165 166 bool DirectInput_Init(HWND hwnd) 167 { 168 //initialize DirectInput object 169 DirectInput8Create( 170 GetModuleHandle(NULL), 171 DIRECTINPUT_VERSION, 172 IID_IDirectInput8, 173 (void**)&dinput, 174 NULL); 175 176 //initialize the keyboard 177 dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL); 178 dikeyboard->SetDataFormat(&c_dfDIKeyboard); 179 dikeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); 180 dikeyboard->Acquire(); 181 182 //initialize the mouse 183 dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL); 184 dimouse->SetDataFormat(&c_dfDIMouse); 185 dimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); 186 dimouse->Acquire(); 187 d3ddev->ShowCursor(false); 188 189 return true; 190 } 191 192 void DirectInput_Update() 193 { 194 //update mouse 195 dimouse->Poll(); 196 if (!SUCCEEDED(dimouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouse_state))) 197 { 198 //mouse device lose, try to re-acquire 199 dimouse->Acquire(); 200 } 201 202 //update keyboard 203 dikeyboard->Poll(); 204 if (!SUCCEEDED(dikeyboard->GetDeviceState(256, (LPVOID)&keys))) 205 { 206 //keyboard device lost, try to re-acquire 207 dikeyboard->Acquire(); 208 } 209 210 //update controllers 211 for (int i = 0; i< 4; i++) 212 { 213 ZeroMemory(&controllers[i], sizeof(XINPUT_STATE)); 214 215 //get the state of the controller 216 XINPUT_STATE state; 217 DWORD result = XInputGetState(i, &state); 218 219 //store state in global controllers array 220 if (result == 0) controllers[i] = state.Gamepad; 221 } 222 } 223 224 225 int Mouse_X() 226 { 227 return mouse_state.lX; 228 } 229 230 int Mouse_Y() 231 { 232 return mouse_state.lY; 233 } 234 235 int Mouse_Button(int button) 236 { 237 return mouse_state.rgbButtons[button] & 0x80; 238 } 239 240 bool Key_Down(int key) 241 { 242 return (bool)(keys[key] & 0x80); 243 } 244 245 246 void DirectInput_Shutdown() 247 { 248 if (dikeyboard) 249 { 250 dikeyboard->Unacquire(); 251 dikeyboard->Release(); 252 dikeyboard = NULL; 253 } 254 if (dimouse) 255 { 256 dimouse->Unacquire(); 257 dimouse->Release(); 258 dimouse = NULL; 259 } 260 } 261 262 bool XInput_Controller_Found() 263 { 264 XINPUT_CAPABILITIES caps; 265 ZeroMemory(&caps, sizeof(XINPUT_CAPABILITIES)); 266 XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps); 267 if (caps.Type != 0) return false; 268 269 return true; 270 } 271 272 void XInput_Vibrate(int contNum, int amount) 273 { 274 XINPUT_VIBRATION vibration; 275 ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION)); 276 vibration.wLeftMotorSpeed = amount; 277 vibration.wRightMotorSpeed = amount; 278 XInputSetState(contNum, &vibration); 279 } 280 void Sprite_Draw_Frame(LPDIRECT3DTEXTURE9 texture, int destx, int desty, int framenum, int framew, int frameh, int columns) 281 { 282 D3DXVECTOR3 position((float)destx, (float)desty, 0); 283 D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255); 284 285 RECT rect; 286 rect.left = (framenum % columns) * framew; 287 rect.top = (framenum / columns) * frameh; 288 rect.right = rect.left + framew; 289 rect.bottom = rect.top + frameh; 290 291 spriteobj->Draw(texture, &rect, NULL, &position, white); 292 } 293 294 void Sprite_Animate(int &frame, int startframe, int endframe, int direction, int &starttime, int delay) 295 { 296 if ((int)GetTickCount() > starttime + delay) 297 { 298 starttime = GetTickCount(); 299 300 frame += direction; 301 if (frame > endframe) frame = startframe; 302 if (frame < startframe) frame = endframe; 303 } 304 } 305 void Sprite_Transform_Draw(LPDIRECT3DTEXTURE9 image, int x, int y, int width, int height, int frame, int columns, float rotation, float scaling, D3DCOLOR color) 306 { 307 //create a scale vector 308 D3DXVECTOR2 scale(scaling, scaling); 309 //create a translate vector 310 D3DXVECTOR2 trans(x, y); 311 //set center by dividing width and height by two 312 D3DXVECTOR2 center((float)(width * scaling) / 2, (float)(height * scaling) / 2); 313 //create 2D transformation matrix 314 D3DXMATRIX mat; 315 D3DXMatrixTransformation2D(&mat, NULL, 0, &scale, ¢er, rotation, &trans); 316 317 //tell sprite object to use the transform 318 spriteobj->SetTransform(&mat); 319 //calculate frame location in source image 320 int fx = (frame % columns) * width; 321 int fy = (frame / columns) * height; 322 RECT srcRect = { fx, fy, fx + width, fy + height }; 323 //draw the sprite frame 324 spriteobj->Draw(image, &srcRect, NULL, NULL, color); 325 } 326 327 //bouding box collision detection 328 int Collision(SPRITE sprite1, SPRITE sprite2) 329 { 330 RECT rect1; 331 rect1.left = (long)sprite1.x; 332 rect1.top = (long)sprite1.y; 333 rect1.right = (long)sprite1.x + sprite1.width + sprite1.scaling; 334 rect1.bottom = (long)sprite1.y + sprite1.height + sprite1.scaling; 335 336 RECT rect2; 337 rect2.left = (long)sprite2.x; 338 rect2.top = (long)sprite2.y; 339 rect2.right = (long)sprite2.x + sprite2.width + sprite2.scaling; 340 rect2.bottom = (long)sprite2.y + sprite2.height + sprite2.scaling; 341 342 RECT dest; //ignored 343 return IntersectRect(&dest, &rect1, &rect2); 344 } 345 LPD3DXFONT MakeFont(string name, int size) 346 { 347 LPD3DXFONT font = NULL; 348 D3DXFONT_DESC desc = { 349 size, //height 350 0, //width 351 0, //weight 352 0, //miplevels 353 false, //italic 354 DEFAULT_CHARSET, //charset 355 OUT_TT_PRECIS, //output precision 356 CLIP_DEFAULT_PRECIS, //quality 357 DEFAULT_PITCH, //pitch and family 358 "" //font name 359 }; 360 strcpy(desc.FaceName, name.c_str()); 361 D3DXCreateFontIndirect(d3ddev, &desc, &font); 362 return font; 363 } 364 void FontPrint(LPD3DXFONT font, int x, int y, string text, D3DCOLOR color) 365 { 366 //figure out the text boundary 367 RECT rect = { x, y, 0, 0 }; 368 font->DrawText(NULL, text.c_str(), text.length(), &rect, DT_CALCRECT, color); 369 //print the text 370 font->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_LEFT, color); 371 }
MyGame.cpp:
1 #include "MyDirectX.h" 2 3 const string APPTITLE = "Font Demo"; 4 const int SCREENW = 1024; 5 const int SCREENH = 768; 6 //declare some font objects 7 LPD3DXFONT fontArial124 = NULL; 8 LPD3DXFONT fontGaramond36 = NULL; 9 LPD3DXFONT fontTimesNewRoman40 = NULL; 10 11 bool Game_Init(HWND window) 12 { 13 //initialize Direct3D 14 if (!Direct3D_Init(window, SCREENW, SCREENH, false)) 15 { 16 MessageBox(0, "Error initializing Direct3D", "ERROR", 0); 17 return false; 18 } 19 20 //initialize DirectInput 21 if (!DirectInput_Init(window)) 22 { 23 MessageBox(0, "Error initializing DirectInput", "ERROR", 0); 24 return false; 25 } 26 //create some fonts 27 fontArial124 = MakeFont("Arial", 24); 28 fontGaramond36 = MakeFont("Garamond", 36); 29 fontTimesNewRoman40 = MakeFont("Times New Roman", 40); 30 return true; 31 } 32 33 void Game_Run(HWND window) 34 { 35 //make sure the Direct3D device is valid 36 if (!d3ddev) return; 37 38 //update input devices 39 DirectInput_Update(); 40 41 //clear the scene 42 d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 100), 1.0f, 0); 43 44 //start rendering 45 if (d3ddev->BeginScene()) 46 { 47 //begin sprite rendering 48 spriteobj->Begin(D3DXSPRITE_ALPHABLEND); 49 //demonstrate font output 50 FontPrint(fontArial124, 60, 50, "This is the Arial 24 font printed with ID3DXFont"); 51 FontPrint(fontGaramond36, 60, 100, "The text can be printed in any color like this magenta!", D3DCOLOR_XRGB(255, 0, 255)); 52 FontPrint(fontTimesNewRoman40, 60, 150, "Or how about bright green instead?", D3DCOLOR_XRGB(0, 255, 0)); 53 54 //demonstrate text wrapping inside a rectangular region 55 RECT rect = { 60,250,350,700 }; 56 D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255); 57 string text = "This is a long string that will be "; 58 text += "wrapped inside a rectangle."; 59 fontTimesNewRoman40->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_WORDBREAK, white); 60 61 //stop sprite rendering 62 spriteobj->End(); 63 //stop rendering 64 d3ddev->EndScene(); 65 d3ddev->Present(NULL, NULL, NULL, NULL); 66 } 67 68 //Escape key ends program 69 if (KEY_DOWN(VK_ESCAPE)) gameover = true; 70 71 //controller Back button also ends 72 if (controllers[0].wButtons & XINPUT_GAMEPAD_BACK) 73 gameover = true; 74 } 75 76 void Game_End() 77 { 78 //free memory and shut down 79 if (fontArial124) fontArial124->Release(); 80 if (fontGaramond36) fontGaramond36->Release(); 81 if (fontTimesNewRoman40) fontTimesNewRoman40->Release(); 82 83 DirectInput_Shutdown(); 84 Direct3D_Shutdown(); 85 }
MyWindows.cpp:
1 #include "MyDirectX.h" 2 using namespace std; 3 bool gameover = false; 4 5 6 LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 7 { 8 switch (msg) 9 { 10 case WM_DESTROY: 11 gameover = true; 12 PostQuitMessage(0); 13 return 0; 14 } 15 return DefWindowProc(hWnd, msg, wParam, lParam); 16 } 17 18 19 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 20 { 21 //initialize window settings 22 WNDCLASSEX wc; 23 wc.cbSize = sizeof(WNDCLASSEX); 24 wc.style = CS_HREDRAW | CS_VREDRAW; 25 wc.lpfnWndProc = (WNDPROC)WinProc; 26 wc.cbClsExtra = 0; 27 wc.cbWndExtra = 0; 28 wc.hInstance = hInstance; 29 wc.hIcon = NULL; 30 wc.hCursor = LoadCursor(NULL, IDC_ARROW); 31 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 32 wc.lpszMenuName = NULL; 33 wc.lpszClassName = APPTITLE.c_str(); 34 wc.hIconSm = NULL; 35 RegisterClassEx(&wc); 36 37 //create a new window 38 HWND window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(), 39 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 40 SCREENW, SCREENH, NULL, NULL, hInstance, NULL); 41 if (window == 0) return 0; 42 43 //display the window 44 ShowWindow(window, nCmdShow); 45 UpdateWindow(window); 46 47 //initialize the game 48 if (!Game_Init(window)) return 0; 49 50 // main message loop 51 MSG message; 52 while (!gameover) 53 { 54 if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) 55 { 56 TranslateMessage(&message); 57 DispatchMessage(&message); 58 } 59 60 //process game loop 61 Game_Run(window); 62 } 63 64 //shutdown 65 Game_End(); 66 return message.wParam; 67 }
显示结果:
原文地址:https://www.cnblogs.com/Trojan00/p/9568789.html