From 8fb6c8e56bc619a3212f54a11cfe27d89fb89f79 Mon Sep 17 00:00:00 2001 From: XProger Date: Mon, 18 Dec 2017 00:45:36 +0300 Subject: [PATCH] #15 exit game from menu --- src/core.h | 7 +++++++ src/inventory.h | 7 ++++++- src/platform/nix/main.cpp | 4 ++-- src/platform/osx/main.cpp | 5 ++--- src/platform/rpi/main.cpp | 5 ++--- src/platform/win/main.cpp | 12 +++++++++--- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/core.h b/src/core.h index a4e5632..5948df7 100644 --- a/src/core.h +++ b/src/core.h @@ -246,6 +246,7 @@ namespace Core { } settings; bool resetState; + bool isQuit; int getTime() { return osGetTime(); @@ -255,6 +256,10 @@ namespace Core { lastTime = getTime(); resetState = true; } + + void quit() { + isQuit = true; + } } #include "input.h" @@ -550,6 +555,8 @@ namespace Core { tinf_init(); #endif + isQuit = false; + Input::init(); #ifdef ANDROID void *libGL = dlopen("libGLESv2.so", RTLD_LAZY); diff --git a/src/inventory.h b/src/inventory.h index e84e7d6..8ce4931 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -514,7 +514,12 @@ struct Inventory { switch (item->value) { case 0 : nextLevel = passportSlots[slot]; break; case 1 : nextLevel = level->isTitle() ? level->getStartId() : id; break; - case 2 : nextLevel = level->isTitle() ? TR::LVL_MAX : level->getTitleId(); break; + case 2 : + if (!level->isTitle()) + nextLevel = level->getTitleId(); + else + Core::quit(); // exit game + break; } if (nextLevel != TR::LVL_MAX) { diff --git a/src/platform/nix/main.cpp b/src/platform/nix/main.cpp index 4fca2d4..4e0899c 100644 --- a/src/platform/nix/main.cpp +++ b/src/platform/nix/main.cpp @@ -214,12 +214,12 @@ int main(int argc, char **argv) { sndInit(); Game::init(argc > 1 ? argv[1] : NULL); - while (1) { + while (!Core::isQuit) { if (XPending(dpy)) { XEvent event; XNextEvent(dpy, &event); if (event.type == ClientMessage && *event.xclient.data.l == WM_DELETE_WINDOW) - break; + Core::quit(); WndProc(event,dpy,wnd); } else { pthread_mutex_lock(&sndMutex); diff --git a/src/platform/osx/main.cpp b/src/platform/osx/main.cpp index 939a9af..3afb2d8 100644 --- a/src/platform/osx/main.cpp +++ b/src/platform/osx/main.cpp @@ -1,6 +1,5 @@ #include "game.h" -bool isQuit = false; WindowRef window; AGLContext context; @@ -71,7 +70,7 @@ OSStatus eventHandler(EventHandlerCallRef handler, EventRef event, void* userDat case kEventClassWindow : switch (eventKind) { case kEventWindowClosed : - isQuit = true; + Core::quit(); break; case kEventWindowBoundsChanged : { aglUpdateContext(context); @@ -202,7 +201,7 @@ int main() { ShowWindow(window); EventRecord event; - while (!isQuit) + while (!Core::isQuit) if (!GetNextEvent(0xffff, &event) && Game::update()) { Game::render(); aglSwapBuffers(context); diff --git a/src/platform/rpi/main.cpp b/src/platform/rpi/main.cpp index cf6389d..7327643 100644 --- a/src/platform/rpi/main.cpp +++ b/src/platform/rpi/main.cpp @@ -468,13 +468,12 @@ int main(int argc, char **argv) { sndInit(); char *lvlName = argc > 1 ? argv[1] : NULL; - char *sndName = argc > 2 ? argv[2] : NULL; - Game::init(lvlName, sndName); + Game::init(lvlName); inputInit(); // initialize and grab input devices - while (!Input::down[ikEscape]) { + while (!Core::isQuit) { inputUpdate(); pthread_mutex_lock(&sndMutex); diff --git a/src/platform/win/main.cpp b/src/platform/win/main.cpp index a351d0d..369935a 100644 --- a/src/platform/win/main.cpp +++ b/src/platform/win/main.cpp @@ -16,6 +16,8 @@ #include "game.h" +int osStartTime = 0; + int osGetTime() { #ifdef DEBUG LARGE_INTEGER Freq, Count; @@ -24,7 +26,7 @@ int osGetTime() { return int(Count.QuadPart * 1000L / Freq.QuadPart); #else timeBeginPeriod(0); - return int(timeGetTime()); + return int(timeGetTime()) - osStartTime; #endif } @@ -357,6 +359,8 @@ int main(int argc, char** argv) { Sound::channelsCount = 0; + osStartTime = osGetTime(); + touchInit(hWnd); joyInit(); sndInit(hWnd); @@ -371,10 +375,12 @@ int main(int argc, char** argv) { MSG msg; - do { + while (!Core::isQuit) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); + if (msg.message == WM_QUIT) + Core::quit(); } else { joyUpdate(); if (Game::update()) { @@ -385,7 +391,7 @@ int main(int argc, char** argv) { Sleep(20); #endif } - } while (msg.message != WM_QUIT); + }; sndFree(); Game::deinit();