void Level::Paint(bool cameraControl, bool HUDControl) { // Paint depending on the level state switch (m_LevelState) { case LevelState::PLAYING: GamePaint(cameraControl, HUDControl); break; case LevelState::OBJECTIVE: GamePaint(cameraControl, HUDControl); ObjectivePaint(); break; case LevelState::COMPLETED: GamePaint(cameraControl, HUDControl); CompletedPaint(); break; case LevelState::FAILED: GamePaint(cameraControl, HUDControl); FailedPaint(); break; case LevelState::PAUSED: GamePaint(cameraControl, HUDControl); MenuPaint(); break; default: break; } }
void T_Engine::StartEngine() { MSG msg; static int TickElapsed = 0; int nowTick; GameWinInit(); srand( (unsigned)time( NULL ) ); while (TRUE) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { if (!GetSleep()) { nowTick = GetTickCount(); if (nowTick > TickElapsed) { TickElapsed = nowTick + GetInterval(); GameLogic(); GamePaint(bufferDC); HDC hDC = GetDC(m_hWnd); BitBlt(hDC, 0, 0, WIN_WIDTH, WIN_HEIGHT, bufferDC, 0, 0, SRCCOPY); ReleaseDC(m_hWnd, hDC); } } } } pEngine->GameEnd(); }
void GameCycle() { if(!g_bGameOver) { if((rand() % g_iDifficulty) == 0) { AddMeteor(); } /* if(g_pBackgroundLayer -> GetViewport().bottom < 0) { g_pBackgroundLayer -> SetSpeed(0); } 要實現滾動到某一座標停止加上該段代碼(該段代碼為滾動至頂部停止,可根據需要修改)*/ g_pScrollingBackground -> Update(); g_pGame -> UpdateSprites(); HWND hWindow = g_pGame -> GetWindow(); HDC hDC = GetDC(hWindow); GamePaint(g_hOffscreenDC); BitBlt(hDC, 0, 0, g_pGame -> GetWidth(), g_pGame -> GetHeight(), g_hOffscreenDC, 0, 0, SRCCOPY); ReleaseDC(hWindow, hDC); } }
bool GameInit(HWND hwnd) { SetTimer(hwnd, 1, 100, nullptr); PlaySound(_T("Kalafina - believe.wav"), nullptr, SND_ASYNC | SND_FILENAME | SND_LOOP); /** * Initial DC * */ gHDC = GetDC(hwnd); gBufferDC = CreateCompatibleDC(gHDC); /** * Init images to form anim * */ std::wstring images[11]; for (int i = 0; i < 11; i++) { images[i] = _T("Loli"); WCHAR buffer[20]; wsprintf(buffer, L"%d.bmp", i); images[i] += buffer; gBmp[i] = (HBITMAP)LoadImage(nullptr, images[i].c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_DEFAULTSIZE); } GamePaint(hwnd); return true; }
LRESULT GEN::GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) { // Route Windows messages to game engine member functions switch (msg) { case WM_SETFOCUS: // Activate the game and update the Sleep status GameActivate(hWindow); SetSleep(FALSE); return 0; case WM_KILLFOCUS: // Deactivate the game and update the Sleep status GameDeactivate(hWindow); SetSleep(TRUE); return 0; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hWindow, &ps); // Paint the game GamePaint(hDC); EndPaint(hWindow, &ps); return 0; case MM_MCINOTIFY: if (LOWORD(wParam) == MCI_NOTIFY_SUCCESSFUL) { if (GetMIDIPlayer() != nullptr) GetMIDIPlayer()->Restart(hWindow); } return 0; // Mouse Input Cases case WM_LBUTTONDOWN: // handle mouse button MouseButtonDown(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_LBUTTONUP: MouseButtonUp(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_RBUTTONDOWN: MouseButtonDown(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_RBUTTONUP: MouseButtonUp(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_MOUSEMOVE: MouseMove(LOWORD(lParam), HIWORD(lParam)); return 0; } return DefWindowProc(hWindow, msg, wParam, lParam); }
void GameCycle() { g_pGame->UpdateSprites(); HWND hWindow = g_pGame->GetWindow(); HDC hDC = GetDC(hWindow); GamePaint(g_hOffscreenDC); // Blit the offscreen bitmap to the game screen BitBlt(hDC, 0, 0, g_pGame->GetWidth(), g_pGame->GetHeight(), g_hOffscreenDC, 0, 0, SRCCOPY); // Cleanup ReleaseDC(hWindow, hDC); }
void GEN::GameEngine::BltOffscreenBuffer() { HDC hdc = GetDC(GetWindow()); // Paint game to offscreen buffer GamePaint(m_hOffscreenDC); // blit the offscreen bitmap image to the game screen BitBlt(hdc, 0, 0, m_iWidth, m_iHeight, m_hOffscreenDC, 0, 0, SRCCOPY); // cleanup ReleaseDC(GetWindow(), hdc); }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX wndClass = { 0 }; wndClass.cbSize = sizeof( WNDCLASSEX ) ; wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor( NULL, IDC_ARROW ); wndClass.hbrBackground = GetStockBrush(WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = WINDOW_CLASS; if( !RegisterClassEx( &wndClass ) ) return -1; g_hInst = hInstance; HWND hWnd = InitInstance(hInstance, nShowCmd); if ( !hWnd) return -1; MSG msg = { 0 }; DWORD dwTick, dwPreTick = GetTickCount(); if ( !GameInit(hWnd)) return -1; while( msg.message != WM_QUIT ) { if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { dwTick = GetTickCount(); if (dwTick - dwPreTick > 40) { if (GameAction(hWnd)) GamePaint(hWnd); dwPreTick = GetTickCount(); } } } UnregisterClass(WINDOW_CLASS, wndClass.hInstance); return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; switch (message) { case WM_CREATE: GameInit(hwnd); break; case WM_TIMER: GamePaint(hwnd); break; case WM_DESTROY: GameClearup(hwnd); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; }
void GameCycle() { if (!_bGameOver) { if (!_bDemo) { // Randomly add aliens if ((rand() % _iDifficulty) == 0) AddAlien(); } // Update the background _pBackground->Update(); // Update the sprites _pGame->UpdateSprites(); // Obtain a device context for repainting the game HWND hWindow = _pGame->GetWindow(); HDC hDC = GetDC(hWindow); // Paint the game to the offscreen device context GamePaint(_hOffscreenDC); // Blit the offscreen bitmap to the game screen BitBlt(hDC, 0, 0, _pGame->GetWidth(), _pGame->GetHeight(), _hOffscreenDC, 0, 0, SRCCOPY); // Cleanup ReleaseDC(hWindow, hDC); } else if (--_iGameOverDelay == 0) { // Stop the music and switch to demo mode _pGame->PauseMIDISong(); _bDemo = TRUE; NewGame(); } }
LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) { //Route Windows messages to game engine member fucntions switch (msg) { case WM_CREATE: //Set the game window and start the game SetWindow(hWindow); GameStart(hWindow); return 0; case WM_ACTIVATE: //Activate and deactivate the game and update the Sleep status if (wParam != WA_INACTIVE) { GameActivate(hWindow); SetSleep(FALSE); } else { GameDeactivate(hWindow); SetSleep(TRUE); } return 0; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hWindow, &ps); //Paint the game GamePaint(hDC); EndPaint(hWindow, &ps); return 0; case WM_LBUTTONDOWN: //Handle left mouse button press MouseButtonDown(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_LBUTTONUP: //Handle left mouse button release MouseButtonUp(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_RBUTTONDOWN: //Handle right mouse button press MouseButtonDown(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_RBUTTONUP: //Handle right mouse button release MouseButtonUp(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_MOUSEMOVE: //Handle mouse movement MouseMove(LOWORD(lParam), HIWORD(lParam)); return 0; case WM_DESTROY: //End the game and exit the application GameEnd(); PostQuitMessage(0); return 0; } return DefWindowProc(hWindow, msg, wParam, lParam); }
LONG T_Engine::GameEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { HDC hdc; switch (msg) { case WM_SETFOCUS: SetSleep(FALSE); return 0; case WM_KILLFOCUS: SetSleep(TRUE); return 0; case WM_CREATE: m_hWnd = hWnd; GameInit(); return 0; case WM_LBUTTONDOWN: GameMouseAction(LOWORD(lParam), HIWORD(lParam), MOUSE_LCLICK); return 0; case WM_MOUSEMOVE: GameMouseAction(LOWORD(lParam), HIWORD(lParam), MOUSE_MOVE); return 0; case WM_KEYDOWN: keys[wParam] = true; GameKeyAction(KEY_DOWN); SubKeyAction(wParam); return 0; case WM_KEYUP: keys[wParam] = false; GameKeyAction(KEY_UP); return 0; case WM_NCLBUTTONDBLCLK: if(HTCAPTION==wParam) { return 0; } case WM_SYSCOMMAND: if(wParam == SC_MAXIMIZE) { m_bFullScreen=!m_bFullScreen; if(m_bFullScreen) { GetWindowRect(hWnd, &m_rcOld); style = GetWindowLong(hWnd,GWL_STYLE); ex_style = GetWindowLong(hWnd, GWL_EXSTYLE); p_disp->SaveMode(); //全屏幕显示 HWND hDesktop; RECT rc; hDesktop = GetDesktopWindow(); GetWindowRect(hDesktop, &rc); p_disp->ChangeMode(wndWidth, wndHeight); SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_WINDOWEDGE); SetWindowLong(hWnd, GWL_STYLE, WS_BORDER); SetWindowPos(hWnd, HWND_TOP, -1, -1, rc.right, rc.bottom, SWP_SHOWWINDOW); } } else if(wParam == SC_CLOSE) { if(IDOK==MessageBox(NULL,L"你确定要退出吗?", wndTitle, MB_OKCANCEL|MB_ICONQUESTION)) { DestroyWindow(hWnd); } } else { return DefWindowProc(hWnd, WM_SYSCOMMAND, wParam,lParam); } return 0; case WM_CLOSE: DestroyWindow(hWnd); return 0; case WM_PAINT: PAINTSTRUCT ps; hdc=BeginPaint(hWnd,&ps); GamePaint(hdc); EndPaint(hWnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); }
/******************************************************************************* * ########## 关于Callback回调函数 ########## * * 回调函数 Callback functions 是系统程序开发中常用的一种机制。 * * 在事件\消息驱动型的系统,我们会遇到以下情况: * (1)系统并不知道应用程序要如何处理事件(如鼠标点击); * (2)应用程序不知道何时会发生事件(如鼠标何时被点击)。 * 因此,应用程序定义好处理事件的函数,然后将这个函数的指针传递给操作系统,系统保存这个指针, * 当事件发生时,系统通过指针调用这个函数。 * * 因为很多情况下事件的发生都是硬件相关的,只有操作系统通过底层驱动才能获取硬件的IO情况。 * 现代操作系统的设计是不允许应用程序直接和硬件通讯的,必须全部由操作系统来进行管理。 * * 回调函数并不神秘,基本原理就是函数指针。 * 在消息和事件处理、多线程、异步IO等情况下经常会使用到。 * 回调函数机制是“消息驱动型”程序的基本技术。 * *******************************************************************************/ LONG CALLBACK MainWndProc( HWND hwnd, // UINT msg, // 消息 WPARAM wParam, // 消息参数,不同的消息有不同的意义,详见MSDN中每个消息的文档 LPARAM lParam) // 消息参数,不同的消息有不同的意义,详见MSDN中每个消息的文档 { // 注意,是switch-case, 每次这个函数被调用,只会落入到一个case中。 switch (msg) { // 当窗口被创建时,收到的第一个消息就是WM_CREATE, // 一般收到这个消息处理过程中,可以用来进行一些初始化的工作 case WM_CREATE: CreateGame(hwnd, INIT_TIMER_ELAPSE, ONE_LEVELS_SCORES, SPEEDUP_RATIO, MAX_X, MAX_Y, INIT_X, INIT_Y, INIT_SNAKE_LEN, INIT_DIR); ReSizeGameWnd(hwnd); break; // 当系统认为窗口上的GDI对象应该被重绘时,会向窗口发送一个WM_PAINT消息。 // 当然应用程序也可以通过调用 UpateWindow来主动向窗口发送一个WM_PAINT消息。 // 所有使用GDI在窗口上绘制图形的程序都 “必须” 写在这里。 // 如果不是在WM_PAINT消息的处理过程中绘制GDI图形,那么在窗口刷新时就会被新被抹除和覆盖 case WM_PAINT: GamePaint(hwnd); break; case WM_KEYDOWN: OnKeyDown(wParam); GamePaint(hwnd); break; case WM_LBUTTONDOWN: OnTimer(hwnd); GamePaint(hwnd); break; case WM_TIMER: OnTimer(hwnd); GamePaint(hwnd); break; case WM_DESTROY: ExitProcess(0); break; default: break; } return DefWindowProc(hwnd, msg, wParam, lParam); }