1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Remove the main windows event loop stuff

This commit is contained in:
Mathieu Schroeter 2017-02-04 18:57:37 +01:00
parent 4787569104
commit 0d8a21dd47
3 changed files with 115 additions and 174 deletions

214
blupi.cpp
View File

@ -309,124 +309,101 @@ static void FinishObjects(void)
} }
} }
LRESULT CALLBACK WindowProc2 (HWND hWnd, UINT message, void WindowProc2 (const SDL_Event &event)
WPARAM wParam, LPARAM lParam,
const SDL_Event *event)
{ {
POINT totalDim, iconDim; 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 && 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: case SDL_WINDOWEVENT_FOCUS_GAINED:
{ if (g_bFullScreen)
switch (event->window.event)
{ {
case SDL_WINDOWEVENT_FOCUS_GAINED: RestoreGame ();
if (g_bFullScreen) g_lastPhase = 999;
{
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;
} }
break; if (!g_bFullScreen && g_bTermInit)
}
case SDL_KEYDOWN:
switch (event->key.keysym.sym)
{ {
case SDLK_F5: totalDim.x = 64;
g_pEvent->SetSpeed (1); totalDim.y = 66;
break; iconDim.x = 64;
case SDLK_F6: iconDim.y = 66 / 2;
g_pEvent->SetSpeed (2); g_pPixmap->Cache (CHHILI, "image\\hili.blp", totalDim, iconDim, true);
break; g_pPixmap->SetTransparent (CHHILI, RGB (0, 0, 255)); // bleu
case SDLK_F7:
g_pEvent->SetSpeed (4);
break;
case SDLK_F8:
g_pEvent->SetSpeed (8);
break;
}
break;
case SDL_USEREVENT: g_pPixmap->SavePalette ();
{ g_pPixmap->InitSysPalette ();
switch (event->user.code) }
SDL_SetWindowTitle (g_window, "Blupi");
if (g_pSound != NULL) g_pSound->RestartMusic ();
return;
case SDL_WINDOWEVENT_FOCUS_LOST:
if (g_bFullScreen)
{ {
case WM_TIMER: FlushGame ();
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; 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, case SDL_USEREVENT:
WPARAM wParam, LPARAM lParam) {
{ switch (event.user.code)
return WindowProc2 (hWnd, message, wParam, lParam, nullptr); {
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. // Erreur dans DoInit.
@ -459,20 +436,6 @@ static bool DoInit(HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow)
InitHInstance(hInstance); 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 (); SDL_SetMainReady ();
auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER); auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
if (res < 0) if (res < 0)
@ -753,30 +716,11 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
while (SDL_TRUE) 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; SDL_Event event;
while (SDL_PollEvent (&event)) while (SDL_PollEvent (&event))
WindowProc2 (nullptr, 0, 0, 0, &event); WindowProc2 (event);
} }
out:
if (g_window) if (g_window)
SDL_DestroyWindow (g_window); SDL_DestroyWindow (g_window);

View File

@ -4606,7 +4606,7 @@ void CEvent::DemoStep()
SDL_Event event = { 0 }; SDL_Event event = { 0 };
CEvent::WinToSDLEvent (message, wParam, lParam, event); CEvent::WinToSDLEvent (message, wParam, lParam, event);
TreatEventBase(&event); TreatEventBase(event);
if ( m_demoIndex >= m_demoEnd ) if ( m_demoIndex >= m_demoEnd )
{ {
@ -4671,19 +4671,19 @@ POINT CEvent::GetLastMousePos()
// Traitement d'un événement. // 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 || if (event.type == SDL_KEYDOWN ||
event->type == SDL_KEYUP || event.type == SDL_KEYUP ||
event->type == SDL_MOUSEBUTTONUP) // is the user clicking? event.type == SDL_MOUSEBUTTONUP) // is the user clicking?
{ {
DemoPlayStop (); DemoPlayStop ();
return true; return true;
} }
if (event->type == SDL_MOUSEMOTION) // is the user moving? if (event.type == SDL_MOUSEMOTION) // is the user moving?
return true; return true;
} }
@ -4692,7 +4692,7 @@ bool CEvent::TreatEvent(const SDL_Event *event)
// Traitement d'un événement. // Traitement d'un événement.
bool CEvent::TreatEventBase(const SDL_Event *event) bool CEvent::TreatEventBase(const SDL_Event &event)
{ {
POINT pos; POINT pos;
int i, sound; int i, sound;
@ -4701,20 +4701,17 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
//DemoRecEvent(message, wParam, lParam); XXX: use SDL_Event //DemoRecEvent(message, wParam, lParam); XXX: use SDL_Event
if (!event) switch (event.type)
return false;
switch (event->type)
{ {
case SDL_KEYDOWN: 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 ? if ( m_posCheat == 0 ) // première lettre ?
{ {
m_rankCheat = -1; m_rankCheat = -1;
for ( i=0 ; i<9 ; i++ ) 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; m_rankCheat = i;
break; break;
@ -4725,7 +4722,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
{ {
c = cheat_code[m_rankCheat][m_posCheat]; c = cheat_code[m_rankCheat][m_posCheat];
if ( m_posCheat != 0 && m_rankCheat == 8 ) c++; // CONSTRUIRE ? 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 ++; m_posCheat ++;
if ( cheat_code[m_rankCheat][m_posCheat] == 0 ) if ( cheat_code[m_rankCheat][m_posCheat] == 0 )
@ -4833,7 +4830,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
SDL_PushEvent (&ev); SDL_PushEvent (&ev);
} }
switch (event->key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_END: case SDLK_END:
DemoRecStop(); DemoRecStop();
@ -5021,7 +5018,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
break; break;
case SDL_KEYUP: case SDL_KEYUP:
switch (event->key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_LSHIFT: case SDLK_LSHIFT:
case SDLK_RSHIFT: case SDLK_RSHIFT:
@ -5039,37 +5036,37 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if ( event->button.button != SDL_BUTTON_LEFT if ( event.button.button != SDL_BUTTON_LEFT
&& event->button.button != SDL_BUTTON_RIGHT) && event.button.button != SDL_BUTTON_RIGHT)
break; break;
pos.x = event->button.x; pos.x = event.button.x;
pos.y = event->button.y; pos.y = event.button.y;
MouseSprite(pos); MouseSprite(pos);
//? DecorAutoShift(pos); //? DecorAutoShift(pos);
if ( EventButtons(*event, pos) ) return true; if ( EventButtons(event, pos) ) return true;
if ( m_phase == WM_PHASE_BUILD ) if ( m_phase == WM_PHASE_BUILD )
{ {
if ( BuildDown(pos, m_keymod) ) return true; if ( BuildDown(pos, m_keymod) ) return true;
} }
if ( m_phase == WM_PHASE_PLAY ) if ( m_phase == WM_PHASE_PLAY )
{ {
if ( PlayDown(pos, *event) ) return true; if ( PlayDown(pos, event) ) return true;
} }
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
pos.x = event->motion.x; pos.x = event.motion.x;
pos.y = event->motion.y; pos.y = event.motion.y;
m_oldMousePos = pos; m_oldMousePos = pos;
MouseSprite(pos); MouseSprite(pos);
if ( EventButtons(*event, pos) ) return true; if ( EventButtons(event, pos) ) return true;
if ( m_phase == WM_PHASE_BUILD ) 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 ) if ( m_phase == WM_PHASE_PLAY )
{ {
@ -5078,14 +5075,14 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if ( event->button.button != SDL_BUTTON_LEFT if ( event.button.button != SDL_BUTTON_LEFT
&& event->button.button != SDL_BUTTON_RIGHT) && event.button.button != SDL_BUTTON_RIGHT)
break; break;
pos.x = event->button.x; pos.x = event.button.x;
pos.y = event->button.y; pos.y = event.button.y;
if ( EventButtons(*event, pos) ) return true; if ( EventButtons(event, pos) ) return true;
if ( m_phase == WM_PHASE_BUILD ) if ( m_phase == WM_PHASE_BUILD )
{ {
if ( BuildUp(pos) ) return true; if ( BuildUp(pos) ) return true;
@ -5103,7 +5100,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
break; break;
case SDL_USEREVENT: case SDL_USEREVENT:
switch (event->user.code) switch (event.user.code)
{ {
case WM_PHASE_DEMO: case WM_PHASE_DEMO:
m_demoNumber = 0; m_demoNumber = 0;
@ -5160,7 +5157,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
case WM_PHASE_H2MOVIE: case WM_PHASE_H2MOVIE:
case WM_PHASE_WINMOVIE: case WM_PHASE_WINMOVIE:
case WM_PHASE_BYE: case WM_PHASE_BYE:
if ( ChangePhase(event->user.code) ) return true; if ( ChangePhase(event.user.code) ) return true;
break; break;
case WM_PHASE_UNDO: case WM_PHASE_UNDO:
@ -5320,7 +5317,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
case WM_BUTTON37: case WM_BUTTON37:
case WM_BUTTON38: case WM_BUTTON38:
case WM_BUTTON39: case WM_BUTTON39:
ChangeButtons(event->user.code); ChangeButtons(event.user.code);
break; break;
case WM_READ0: case WM_READ0:
@ -5333,7 +5330,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
case WM_READ7: case WM_READ7:
case WM_READ8: case WM_READ8:
case WM_READ9: case WM_READ9:
Read(event->user.code); Read(event.user.code);
ChangePhase(WM_PHASE_PLAY); // joue ChangePhase(WM_PHASE_PLAY); // joue
break; break;
@ -5347,7 +5344,7 @@ bool CEvent::TreatEventBase(const SDL_Event *event)
case WM_WRITE7: case WM_WRITE7:
case WM_WRITE8: case WM_WRITE8:
case WM_WRITE9: case WM_WRITE9:
Write(event->user.code); Write(event.user.code);
if ( m_phase == WM_PHASE_WRITEp ) if ( m_phase == WM_PHASE_WRITEp )
{ {
ChangePhase(WM_PHASE_PLAY); // joue ChangePhase(WM_PHASE_PLAY); // joue

View File

@ -88,8 +88,8 @@ public:
void WaitMouse(bool bWait); void WaitMouse(bool bWait);
void HideMouse(bool bHide); void HideMouse(bool bHide);
POINT GetLastMousePos(); POINT GetLastMousePos();
bool TreatEvent(const SDL_Event *event); bool TreatEvent(const SDL_Event &event);
bool TreatEventBase(const SDL_Event *event); bool TreatEventBase(const SDL_Event &event);
void DecorAutoShift(POINT pos); void DecorAutoShift(POINT pos);