// PhilVaz Bitmap Demo // Displays different BMP Ghosts to screen // Full Screen GDI demo // Sept 25, 2003 // INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #include // include all the windows headers #include // include useful macros #include // for rand functions #include #include #include "VazPacGhost.h" // DEFINES //////////////////////////////////////////////// // defines for windows #define WINDOW_CLASS_NAME "WINCLASS1" #define WINDOW_WIDTH 800 // size of game window #define WINDOW_HEIGHT 600 #define GAME_SPEED 30 // speed of game (increase to go slower) #define GHOST_RIGHT 0 #define GHOST_DOWN 1 #define GHOST_LEFT 2 #define GHOST_UP 3 #define GHOST_BLUE 0 #define GHOST_GREEN 8 #define GHOST_RED 16 #define GHOST_WAIT 3 // 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 HDC game_dc = NULL; // global device context (for GDI) handle // for back (double) buffering HDC back_dc = NULL; HBITMAP back_bmp = NULL; HBITMAP old_bmp = NULL; RECT back_rect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; DEVMODE game_screen; // global for full screen mode // global pen and brush HPEN black_pen = CreatePen(PS_SOLID, 1, RGB(0,0,0)); HBRUSH black_brush = CreateSolidBrush(RGB(0,0,0)); int xlimit, ylimit; // BMP limits for x and y int ghost_feet; // switch between 0 and 4 for feet position int ghost_delay; // delay for feet int ghost_color[20]; // ghost color int xbmp[20]; // x position on screen int ybmp[20]; // y position on screen int xmove[20]; // x move position int ymove[20]; // y move position // for bitmap file HBITMAP hbmp[24]; BITMAP bmp[24]; HDC bmp_dc = NULL; // FUNCTIONS //////////////////////////////////////////////////////////// void 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 default messages 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 "VazPac Ghosts", // 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 hinstance, // instance of this application NULL))) // extra creation parms return(0); // save the game window handle game_window = hwnd; GameInit(); // game initialization function called here // 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 (hwnd, 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 // /////////////////////////////////////////////////////////// void GameInit() { int i, r; // temporary change to full screen mode ZeroMemory(&game_screen, sizeof(game_screen)); // clear out size of DEVMODE struct game_screen.dmSize = sizeof(game_screen); game_screen.dmPelsWidth = WINDOW_WIDTH; game_screen.dmPelsHeight = WINDOW_HEIGHT; game_screen.dmBitsPerPel = 16; game_screen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL; ChangeDisplaySettings(&game_screen, CDS_FULLSCREEN); game_dc = GetDC(game_window); // get the GDI device context // for back (double) buffering back_dc = CreateCompatibleDC(game_dc); back_bmp = CreateCompatibleBitmap(game_dc, WINDOW_WIDTH, WINDOW_HEIGHT); old_bmp = (HBITMAP)SelectObject(back_dc, back_bmp); // load the BMP file and set up display and struct bmp_dc = CreateCompatibleDC(game_dc); hbmp[0] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST00), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[0], sizeof(bmp[0]), &bmp[0]); hbmp[1] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST01), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[1], sizeof(bmp[1]), &bmp[1]); hbmp[2] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST02), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[2], sizeof(bmp[2]), &bmp[2]); hbmp[3] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST03), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[3], sizeof(bmp[3]), &bmp[3]); hbmp[4] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST04), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[4], sizeof(bmp[4]), &bmp[4]); hbmp[5] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST05), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[5], sizeof(bmp[5]), &bmp[5]); hbmp[6] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST06), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[6], sizeof(bmp[6]), &bmp[6]); hbmp[7] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST07), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[7], sizeof(bmp[7]), &bmp[7]); hbmp[8] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST08), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[8], sizeof(bmp[8]), &bmp[8]); hbmp[9] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST09), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[9], sizeof(bmp[9]), &bmp[9]); hbmp[10] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST10), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[10], sizeof(bmp[10]), &bmp[10]); hbmp[11] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST11), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[11], sizeof(bmp[11]), &bmp[11]); hbmp[12] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST12), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[12], sizeof(bmp[12]), &bmp[12]); hbmp[13] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST13), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[13], sizeof(bmp[13]), &bmp[13]); hbmp[14] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST14), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[14], sizeof(bmp[14]), &bmp[14]); hbmp[15] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST15), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[15], sizeof(bmp[15]), &bmp[15]); hbmp[16] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST16), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[16], sizeof(bmp[16]), &bmp[16]); hbmp[17] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST17), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[17], sizeof(bmp[17]), &bmp[17]); hbmp[18] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST18), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[18], sizeof(bmp[18]), &bmp[18]); hbmp[19] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST19), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[19], sizeof(bmp[19]), &bmp[19]); hbmp[20] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST20), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[20], sizeof(bmp[20]), &bmp[20]); hbmp[21] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST21), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[21], sizeof(bmp[21]), &bmp[21]); hbmp[22] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST22), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[22], sizeof(bmp[22]), &bmp[22]); hbmp[23] = (HBITMAP)LoadImage(game_instance, MAKEINTRESOURCE(BMP_GHOST23), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); GetObject(hbmp[23], sizeof(bmp[23]), &bmp[23]); // set 20 random bitmaps, positions, and movement for (i=0; i<20; i++) { r = rand()%3; if (r == 0) ghost_color[i] = GHOST_BLUE; if (r == 1) ghost_color[i] = GHOST_GREEN; if (r == 2) ghost_color[i] = GHOST_RED; xbmp[i] = rand()%(WINDOW_WIDTH - 50) + 1; ybmp[i] = rand()%(WINDOW_HEIGHT - 50) + 1; r = rand()%4; if (r == 0) { xmove[i] = rand()%3 + 1; ymove[i] = 0; } if (r == 1) { xmove[i] = 0; ymove[i] = rand()%3 + 1; } if (r == 2) { xmove[i] = rand()%3 - 3; ymove[i] = 0; } if (r == 3) { xmove[i] = 0; ymove[i] = rand()%3 - 3; } } xlimit = WINDOW_WIDTH - 31; ylimit = WINDOW_HEIGHT - 31; ghost_delay = GHOST_WAIT; ghost_feet = 0; } // END OF GameInit /////////////////////////////////////////////////////////// // // GAME MAIN LOOP AND PROCESSING // /////////////////////////////////////////////////////////// void GameMain() { int i, b, d; // erase the back buffer FillRect(back_dc, &back_rect, black_brush); for (i=0; i<20; i++) { xbmp[i] = xbmp[i] + xmove[i]; ybmp[i] = ybmp[i] + ymove[i]; if (xbmp[i] > xlimit) xbmp[i] = 1; if (xbmp[i] < 1) xbmp[i] = xlimit; if (ybmp[i] > ylimit) ybmp[i] = 1; if (ybmp[i] < 1) ybmp[i] = ylimit; // get direction of ghost if (xmove[i] > 0) d = GHOST_RIGHT; if (xmove[i] < 0) d = GHOST_LEFT; if (ymove[i] > 0) d = GHOST_DOWN; if (ymove[i] < 0) d = GHOST_UP; // find correct BMP position b = ghost_color[i] + d + ghost_feet; SelectObject(bmp_dc, hbmp[b]); BitBlt(back_dc, xbmp[i], ybmp[i], 30, 30, bmp_dc, 0, 0, SRCCOPY); } ghost_delay--; if (ghost_delay == 0) { ghost_feet += 4; if (ghost_feet > 4) ghost_feet = 0; ghost_delay = GHOST_WAIT; } // copy back buffer to front buffer BitBlt(game_dc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, back_dc, 0, 0, SRCCOPY); } // END OF GameMain /////////////////////////////////////////////////////////// // // GAME QUIT AND CLEAN UP // /////////////////////////////////////////////////////////// void GameQuit() { int i; DeleteObject(black_pen); DeleteObject(black_brush); // release bmps and dc for (i=0; i<24; i++) DeleteObject(hbmp[i]); DeleteDC(bmp_dc); // release the back buffer SelectObject(back_dc, old_bmp); DeleteObject(back_bmp); DeleteDC(back_dc); // release the device context (for GDI) from the game window ReleaseDC(game_window, game_dc); // return to original display settings ChangeDisplaySettings(NULL,NULL); } // END OF GameQuit // END GAME CODE //////////////////////////////////////////