// PhilVaz Direct3D Demo // Go Full Screen // April 19, 2005 -- Habemus Papam! // INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #include // include all the windows headers #include // include useful macros #include // for multimedia #include // for rand functions #include #include #include // include main Direct3D version 9 // DEFINES //////////////////////////////////////////////// // defines for windows #define WINDOW_CLASS_NAME "WINCLASS1" #define WINDOW_WIDTH 640 // size of game window #define WINDOW_HEIGHT 480 #define WINDOW_BPP 16 // bits per pixel #define GAME_SPEED 15 // speed of game (about 60 FPS) // MACROS ///////////////////////////////////////////////// #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // GLOBALS //////////////////////////////////////////////// HWND game_window = NULL; // global game window handle HINSTANCE game_instance = NULL; // global game instance handle // Direct3D variables LPDIRECT3D9 d3d_main = NULL; // d3d main object LPDIRECT3DDEVICE9 d3d_dev = NULL; // d3d device D3DPRESENT_PARAMETERS d3d_pp; // d3d parameters bool d3d_ok; // whether Direct3D init okay // FUNCTIONS ////////////////////////////////////////////// bool GameInit(); void GameMain(); void GameQuit(); // WINPROC //////////////////////////////////////////////// LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { // this is the main message handler of the system HDC hdc; // handle to a device context PAINTSTRUCT ps; // used in WM_PAINT switch(msg) // what is the message { case WM_CREATE: { // do initialization stuff here return(0); // return success } break; case WM_PAINT: { hdc = BeginPaint(hwnd, &ps); // validate the window EndPaint(hwnd, &ps); return(0); // return success } break; case WM_DESTROY: { PostQuitMessage(0); // kill the application, sends a WM_QUIT message return(0); // return success } break; default:break; } // end switch // process any messages that we didn't take care of return (DefWindowProc(hwnd, msg, wparam, lparam)); } // end WinProc // WINMAIN //////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASSEX winclass; // this will hold the class we create HWND hwnd; // generic window handle MSG msg; // generic message // first fill in the window class structure winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WinProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; // save the game instance handle game_instance = hinstance; // register the window class if (!RegisterClassEx(&winclass)) return(0); // create the window if (!(hwnd = CreateWindowEx(NULL, // extended style WINDOW_CLASS_NAME, // class "Direct3D Full Screen", // title WS_POPUP | WS_VISIBLE, // use POPUP for full screen 0,0, // initial game window x,y WINDOW_WIDTH, // initial game width WINDOW_HEIGHT, // initial game height NULL, // handle to parent NULL, // handle to menu game_instance, // instance of this application NULL))) // extra creation parms return(0); // save the game window handle game_window = hwnd; d3d_ok = GameInit(); // game initialization function called here if (!d3d_ok) SendMessage(game_window, WM_CLOSE, 0, 0); // close if failed // enter main event loop using PeekMessage() to retrieve messages while(TRUE) { // get initial tick count to keep game speed constant DWORD start_tick = GetTickCount(); // is there a message in queue, if so get it if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // test if this is a quit if (msg.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&msg); // send the message to WinProc DispatchMessage(&msg); } // end if GameMain(); // game main processing function called here // check for key and send quit game if (KEYDOWN(VK_ESCAPE)) SendMessage (game_window, WM_CLOSE, 0, 0); // wait until we hit correct game speed frame rate while ((GetTickCount() - start_tick) < GAME_SPEED); } // end while GameQuit(); // game quit function and clean up before exit called here return(msg.wParam); // return to Windows } // end WinMain // BEGIN GAME CODE //////////////////////////////////////// /////////////////////////////////////////////////////////// // // GAME INITIALIZATION // /////////////////////////////////////////////////////////// bool GameInit() { // first create main Direct3D interface if (!(d3d_main = Direct3DCreate9(D3D_SDK_VERSION))) { MessageBox(game_window, "Direct3D could not be initialized -- Create Error","Direct3D Error",MB_OK); return FALSE; // error returned } // set Direct3D parameters ZeroMemory(&d3d_pp, sizeof(d3d_pp)); d3d_pp.Windowed = FALSE; d3d_pp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3d_pp.BackBufferFormat = D3DFMT_X8R8G8B8; d3d_pp.BackBufferCount = 1; d3d_pp.BackBufferWidth = WINDOW_WIDTH; d3d_pp.BackBufferHeight = WINDOW_HEIGHT; d3d_pp.hDeviceWindow = game_window; // create Direct3D device IDirect3D9_CreateDevice(d3d_main, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, game_window,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3d_pp, &d3d_dev); if (!d3d_dev) { MessageBox(game_window, "Direct3D could not be initialized -- Device Init Error","Direct3D Error",MB_OK); return FALSE; // error returned } MessageBox(game_window, "Direct3D Initialized Okay -- Entering GameMain Loop","Direct3D OK",MB_OK); return TRUE; // success since the previous if's all true } // END OF GameInit /////////////////////////////////////////////////////////// // // GAME MAIN LOOP AND PROCESSING // /////////////////////////////////////////////////////////// void GameMain() { if (!d3d_ok) return; // make sure d3d init ok // this is the main loop of the game, do all your processing here // clear the back buffer to black IDirect3DDevice9_Clear(d3d_dev, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); // game and graphics processing here // display (present) the back buffer IDirect3DDevice9_Present(d3d_dev, NULL, NULL, NULL, NULL); } // END OF GameMain /////////////////////////////////////////////////////////// // // GAME QUIT AND CLEAN UP // /////////////////////////////////////////////////////////// void GameQuit() { // release/delete Direct3D device if (d3d_dev) { IDirect3DDevice9_Release(d3d_dev); d3d_dev = NULL; } // release/delete Direct3D main if (d3d_main) { IDirect3D9_Release(d3d_main); d3d_main = NULL; } } // END OF GameQuit // END GAME CODE //////////////////////////////////////////