1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* Beginning game programming, Third Edition
Chapter 4
Create_Surface Program */
#include <windows.h>
#include <d3d9.h>
#include <time.h>
#include <iostream>
using namespace std;
// Application title
const string APPTITLE = "Create Surface Program";
//macro to read the keyboard
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0)
//screen resolution
#define SCREENW 1024
#define SCREENH 768
//Direct3d objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9 surface = NULL;
bool gameover = false;
/* Game Initialization function */
bool Game_Init(HWND hWnd)
{
//initialize Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d = NULL)
{
MessageBox(hWnd, "Error Initializing Direct3D","Error", MB_OK);
return false;
}
//set the Direct3D presentation paramaters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = SCREENW;
d3dpp.BackBufferHeight = SCREENH;
d3dpp.hDeviceWindow = hWnd;
//create the direct3d device
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
if(!d3ddev)
{
MessageBox(hWnd, "Error Creating Direct3D device", "Error", MB_OK);
}
//set random number seed
srand(time(NULL));
//clear the back buffer
d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
//create pointer to the back buffer
d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer);
//create surface
HRESULT result = d3ddev->CreateOffscreenPlainSurface(
100, //width of the surface
100, // height of the surface
D3DFMT_X8R8G8B8, // surface format
D3DPOOL_DEFAULT, // memory pool to use
&surface,
NULL);
if(!SUCCEEDED(result)) return false;
return true;
}
/*GAME UPDATE FUNCTION */
void Game_Run(HWND hWnd)
{
// make sure the Direct3D device
if(!d3ddev) return;
//start rendering
if(d3ddev->BeginScene())
{
// fill the surface with random color
int r = rand() % 255;
int g = rand() % 255;
int b = rand() % 255;
d3ddev->ColorFill(surface,NULL,D3DCOLOR_XRGB(r,g,b));
//copy the surface to the back buffer
RECT rect;
rect.left = rand() % SCREENW/2;
rect.right = rect.left + rand() % SCREENW/2;
rect.top = rand() % SCREENH/2;
rect.bottom = rect.top + rand() % SCREENH/2;
d3ddev->StretchRect(surface,NULL,backbuffer, &rect,D3DTEXF_NONE);
//stop rendering
d3ddev->EndScene();
//display the back buffer on the screen
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(surface)surface->Release();
if(d3ddev) d3ddev->Release();
if(d3d) d3d->Release();
}
//windows event callback function
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
gameover = true;
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
/** Windows entry point function **/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
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 = APPTITLE.c_str();
wc.hIconSm = NULL;
RegisterClassEx(&wc);
//create a new window
HWND window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
//was there an error creating the window?
if(window == 0) return 0;
//display the window
ShowWindow(window, nCmdShow);
UpdateWindow(window);
//initialize the game
if(!Game_Init(window)) return 0;
// main message loop
MSG message;
while(!gameover)
{
if(PeekMessage(&message, NULL,0,0,PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
Game_Run(window);
}
return message.wParam;
}
|