From 0d8a21dd475075c9618465bdba603af2acfc121f Mon Sep 17 00:00:00 2001 From: Mathieu Schroeter Date: Sat, 4 Feb 2017 18:57:37 +0100 Subject: [PATCH] Remove the main windows event loop stuff --- blupi.cpp | 214 ++++++++++++++++++++---------------------------------- event.cpp | 71 +++++++++--------- event.h | 4 +- 3 files changed, 115 insertions(+), 174 deletions(-) diff --git a/blupi.cpp b/blupi.cpp index 8b6fe4a..a592d74 100644 --- a/blupi.cpp +++ b/blupi.cpp @@ -309,124 +309,101 @@ static void FinishObjects(void) } } -LRESULT CALLBACK WindowProc2 (HWND hWnd, UINT message, - WPARAM wParam, LPARAM lParam, - const SDL_Event *event) +void WindowProc2 (const SDL_Event &event) { POINT totalDim, iconDim; - // La touche F10 envoie un autre message pour activer - // le menu dans les applications Windows standard ! - if ( message == WM_SYSKEYDOWN && wParam == VK_F10 ) - { - message = WM_KEYDOWN; - } - if ( message == WM_SYSKEYUP && wParam == VK_F10 ) - { - message = WM_KEYUP; - } - if ( g_pEvent != NULL && - g_pEvent->TreatEvent(event) ) return 0; + g_pEvent->TreatEvent(event) ) + return; - if (event) + switch (event.type) { - switch (event->type) + case SDL_WINDOWEVENT: + { + switch (event.window.event) { - case SDL_WINDOWEVENT: - { - switch (event->window.event) + case SDL_WINDOWEVENT_FOCUS_GAINED: + if (g_bFullScreen) { - case SDL_WINDOWEVENT_FOCUS_GAINED: - if (g_bFullScreen) - { - RestoreGame (); - g_lastPhase = 999; - } - if (!g_bFullScreen && g_bTermInit) - { - totalDim.x = 64; - totalDim.y = 66; - iconDim.x = 64; - iconDim.y = 66 / 2; - g_pPixmap->Cache (CHHILI, "image\\hili.blp", totalDim, iconDim, true); - g_pPixmap->SetTransparent (CHHILI, RGB (0, 0, 255)); // bleu - - g_pPixmap->SavePalette (); - g_pPixmap->InitSysPalette (); - } - SDL_SetWindowTitle (g_window, "Blupi"); - if (g_pSound != NULL) g_pSound->RestartMusic (); - return 0; - - case SDL_WINDOWEVENT_FOCUS_LOST: - if (g_bFullScreen) - { - FlushGame (); - } - SDL_SetWindowTitle (g_window, "Blupi -- stop"); - if (g_pSound != NULL) g_pSound->SuspendMusic (); - return 0; + RestoreGame (); + g_lastPhase = 999; } - break; - } - - case SDL_KEYDOWN: - switch (event->key.keysym.sym) + if (!g_bFullScreen && g_bTermInit) { - case SDLK_F5: - g_pEvent->SetSpeed (1); - break; - case SDLK_F6: - g_pEvent->SetSpeed (2); - break; - case SDLK_F7: - g_pEvent->SetSpeed (4); - break; - case SDLK_F8: - g_pEvent->SetSpeed (8); - break; - } - break; + totalDim.x = 64; + totalDim.y = 66; + iconDim.x = 64; + iconDim.y = 66 / 2; + g_pPixmap->Cache (CHHILI, "image\\hili.blp", totalDim, iconDim, true); + g_pPixmap->SetTransparent (CHHILI, RGB (0, 0, 255)); // bleu - case SDL_USEREVENT: - { - switch (event->user.code) + g_pPixmap->SavePalette (); + g_pPixmap->InitSysPalette (); + } + SDL_SetWindowTitle (g_window, "Blupi"); + if (g_pSound != NULL) g_pSound->RestartMusic (); + return; + + case SDL_WINDOWEVENT_FOCUS_LOST: + if (g_bFullScreen) { - case WM_TIMER: - case WM_UPDATE: - if (!g_pEvent->IsMovie ()) // pas de film en cours ? - { - if (g_bActive) - { - UpdateFrame (); - } - g_pPixmap->Display (); - } - break; - - case WM_MUSIC_STOP: - if (g_pSound->IsStoppedOnDemand ()) - break; - - if (g_pEvent->IsMovie ()) - g_pEvent->StopMovie (); - else - g_pSound->RestartMusic (); - break; + FlushGame (); } - break; - } + SDL_SetWindowTitle (g_window, "Blupi -- stop"); + if (g_pSound != NULL) g_pSound->SuspendMusic (); + return; } + break; } - return DefWindowProc(hWnd, message, wParam, lParam); -} + case SDL_KEYDOWN: + switch (event.key.keysym.sym) + { + case SDLK_F5: + g_pEvent->SetSpeed (1); + break; + case SDLK_F6: + g_pEvent->SetSpeed (2); + break; + case SDLK_F7: + g_pEvent->SetSpeed (4); + break; + case SDLK_F8: + g_pEvent->SetSpeed (8); + break; + } + break; -LRESULT CALLBACK WindowProc (HWND hWnd, UINT message, - WPARAM wParam, LPARAM lParam) -{ - return WindowProc2 (hWnd, message, wParam, lParam, nullptr); + case SDL_USEREVENT: + { + switch (event.user.code) + { + case WM_TIMER: + case WM_UPDATE: + if (!g_pEvent->IsMovie ()) // pas de film en cours ? + { + if (g_bActive) + { + UpdateFrame (); + } + g_pPixmap->Display (); + } + break; + + case WM_MUSIC_STOP: + if (g_pSound->IsStoppedOnDemand ()) + break; + + if (g_pEvent->IsMovie ()) + g_pEvent->StopMovie (); + else + g_pSound->RestartMusic (); + break; + } + break; + } + } } // Erreur dans DoInit. @@ -459,20 +436,6 @@ static bool DoInit(HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow) InitHInstance(hInstance); - // Set up and register window class. - wc.style = CS_HREDRAW|CS_VREDRAW; - wc.lpfnWndProc = WindowProc; - wc.cbClsExtra = 0; - wc.cbWndExtra = 0; - wc.hInstance = hInstance; -//? wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION); - wc.hIcon = LoadIcon(hInstance, "IDR_MAINFRAME"); - wc.hCursor = LoadCursor(hInstance, "IDC_POINTER"); - wc.hbrBackground = GetStockBrush(BLACK_BRUSH); - wc.lpszMenuName = NAME; - wc.lpszClassName = NAME; - RegisterClass(&wc); - SDL_SetMainReady (); auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER); if (res < 0) @@ -753,30 +716,11 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, while (SDL_TRUE) { - if ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) - { - if ( !GetMessage(&msg, NULL, 0, 0) ) - { - goto out; - } - TranslateMessage(&msg); - DispatchMessage(&msg); - } - else - { - // make sure we go to sleep if we have nothing else to do - if (!g_bActive) - WaitMessage (); - else - Sleep (1); - } - SDL_Event event; while (SDL_PollEvent (&event)) - WindowProc2 (nullptr, 0, 0, 0, &event); + WindowProc2 (event); } -out: if (g_window) SDL_DestroyWindow (g_window); diff --git a/event.cpp b/event.cpp index 890d52c..82f4000 100644 --- a/event.cpp +++ b/event.cpp @@ -4606,7 +4606,7 @@ void CEvent::DemoStep() SDL_Event event = { 0 }; CEvent::WinToSDLEvent (message, wParam, lParam, event); - TreatEventBase(&event); + TreatEventBase(event); if ( m_demoIndex >= m_demoEnd ) { @@ -4671,19 +4671,19 @@ POINT CEvent::GetLastMousePos() // Traitement d'un événement. -bool CEvent::TreatEvent(const SDL_Event *event) +bool CEvent::TreatEvent(const SDL_Event &event) { - if ( m_bDemoPlay && event) // démo en lecture ? + if (m_bDemoPlay) { - if (event->type == SDL_KEYDOWN || - event->type == SDL_KEYUP || - event->type == SDL_MOUSEBUTTONUP) // is the user clicking? + if (event.type == SDL_KEYDOWN || + event.type == SDL_KEYUP || + event.type == SDL_MOUSEBUTTONUP) // is the user clicking? { DemoPlayStop (); return true; } - if (event->type == SDL_MOUSEMOTION) // is the user moving? + if (event.type == SDL_MOUSEMOTION) // is the user moving? return true; } @@ -4692,7 +4692,7 @@ bool CEvent::TreatEvent(const SDL_Event *event) // Traitement d'un événement. -bool CEvent::TreatEventBase(const SDL_Event *event) +bool CEvent::TreatEventBase(const SDL_Event &event) { POINT pos; int i, sound; @@ -4701,20 +4701,17 @@ bool CEvent::TreatEventBase(const SDL_Event *event) //DemoRecEvent(message, wParam, lParam); XXX: use SDL_Event - if (!event) - return false; - - switch (event->type) + switch (event.type) { case SDL_KEYDOWN: - if ( event->key.keysym.sym >= SDLK_a && event->key.keysym.sym <= SDLK_z ) + if ( event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z ) { if ( m_posCheat == 0 ) // première lettre ? { m_rankCheat = -1; for ( i=0 ; i<9 ; i++ ) { - if ( (char) event->key.keysym.sym == cheat_code[i][0] ) + if ( (char) event.key.keysym.sym == cheat_code[i][0] ) { m_rankCheat = i; break; @@ -4725,7 +4722,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) { c = cheat_code[m_rankCheat][m_posCheat]; if ( m_posCheat != 0 && m_rankCheat == 8 ) c++; // CONSTRUIRE ? - if ( (char) event->key.keysym.sym == c ) + if ( (char) event.key.keysym.sym == c ) { m_posCheat ++; if ( cheat_code[m_rankCheat][m_posCheat] == 0 ) @@ -4833,7 +4830,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) SDL_PushEvent (&ev); } - switch (event->key.keysym.sym) + switch (event.key.keysym.sym) { case SDLK_END: DemoRecStop(); @@ -5021,7 +5018,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) break; case SDL_KEYUP: - switch (event->key.keysym.sym) + switch (event.key.keysym.sym) { case SDLK_LSHIFT: case SDLK_RSHIFT: @@ -5039,37 +5036,37 @@ bool CEvent::TreatEventBase(const SDL_Event *event) break; case SDL_MOUSEBUTTONDOWN: - if ( event->button.button != SDL_BUTTON_LEFT - && event->button.button != SDL_BUTTON_RIGHT) + if ( event.button.button != SDL_BUTTON_LEFT + && event.button.button != SDL_BUTTON_RIGHT) break; - pos.x = event->button.x; - pos.y = event->button.y; + pos.x = event.button.x; + pos.y = event.button.y; MouseSprite(pos); //? DecorAutoShift(pos); - if ( EventButtons(*event, pos) ) return true; + if ( EventButtons(event, pos) ) return true; if ( m_phase == WM_PHASE_BUILD ) { if ( BuildDown(pos, m_keymod) ) return true; } if ( m_phase == WM_PHASE_PLAY ) { - if ( PlayDown(pos, *event) ) return true; + if ( PlayDown(pos, event) ) return true; } break; case SDL_MOUSEMOTION: - pos.x = event->motion.x; - pos.y = event->motion.y; + pos.x = event.motion.x; + pos.y = event.motion.y; m_oldMousePos = pos; MouseSprite(pos); - if ( EventButtons(*event, pos) ) return true; + if ( EventButtons(event, pos) ) return true; if ( m_phase == WM_PHASE_BUILD ) { - if ( BuildMove(pos, m_keymod, *event) ) return true; + if ( BuildMove(pos, m_keymod, event) ) return true; } if ( m_phase == WM_PHASE_PLAY ) { @@ -5078,14 +5075,14 @@ bool CEvent::TreatEventBase(const SDL_Event *event) break; case SDL_MOUSEBUTTONUP: - if ( event->button.button != SDL_BUTTON_LEFT - && event->button.button != SDL_BUTTON_RIGHT) + if ( event.button.button != SDL_BUTTON_LEFT + && event.button.button != SDL_BUTTON_RIGHT) break; - pos.x = event->button.x; - pos.y = event->button.y; + pos.x = event.button.x; + pos.y = event.button.y; - if ( EventButtons(*event, pos) ) return true; + if ( EventButtons(event, pos) ) return true; if ( m_phase == WM_PHASE_BUILD ) { if ( BuildUp(pos) ) return true; @@ -5103,7 +5100,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) break; case SDL_USEREVENT: - switch (event->user.code) + switch (event.user.code) { case WM_PHASE_DEMO: m_demoNumber = 0; @@ -5160,7 +5157,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) case WM_PHASE_H2MOVIE: case WM_PHASE_WINMOVIE: case WM_PHASE_BYE: - if ( ChangePhase(event->user.code) ) return true; + if ( ChangePhase(event.user.code) ) return true; break; case WM_PHASE_UNDO: @@ -5320,7 +5317,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) case WM_BUTTON37: case WM_BUTTON38: case WM_BUTTON39: - ChangeButtons(event->user.code); + ChangeButtons(event.user.code); break; case WM_READ0: @@ -5333,7 +5330,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) case WM_READ7: case WM_READ8: case WM_READ9: - Read(event->user.code); + Read(event.user.code); ChangePhase(WM_PHASE_PLAY); // joue break; @@ -5347,7 +5344,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event) case WM_WRITE7: case WM_WRITE8: case WM_WRITE9: - Write(event->user.code); + Write(event.user.code); if ( m_phase == WM_PHASE_WRITEp ) { ChangePhase(WM_PHASE_PLAY); // joue diff --git a/event.h b/event.h index 63e7dc3..34e2bf1 100644 --- a/event.h +++ b/event.h @@ -88,8 +88,8 @@ public: void WaitMouse(bool bWait); void HideMouse(bool bHide); POINT GetLastMousePos(); - bool TreatEvent(const SDL_Event *event); - bool TreatEventBase(const SDL_Event *event); + bool TreatEvent(const SDL_Event &event); + bool TreatEventBase(const SDL_Event &event); void DecorAutoShift(POINT pos);