From c3d275ada8dc8c67e9930d23587e0c203761a056 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Verschelde?= Date: Sun, 29 Jan 2017 15:08:42 +0100 Subject: [PATCH] GameTick: Move input processing to a dedicated method Gameplay-related input is still in the main Tick() though as it is tightly coupled to the game logic. --- Source/Game.hpp | 5 + Source/GameTick.cpp | 242 +++++++++++++++++++++----------------- Source/Objects/Person.cpp | 4 +- 3 files changed, 144 insertions(+), 107 deletions(-) diff --git a/Source/Game.hpp b/Source/Game.hpp index d14037f..6307092 100644 --- a/Source/Game.hpp +++ b/Source/Game.hpp @@ -146,9 +146,14 @@ void playdialoguescenesound(); int findClosestPlayer(); bool LoadLevel(int which); bool LoadLevel(const std::string& name, bool tutorial = false); + +void ProcessInput(); +void ProcessDevInput(); + void Tick(); void TickOnce(); void TickOnceAfter(); + void SetUpLighting(); GLvoid ReSizeGLScene(float fov, float near); diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 284438a..85cf0e9 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -959,13 +959,144 @@ bool Game::LoadLevel(const std::string& name, bool tutorial) return true; } -void doDevKeys() +/* Process input events that impact settings, console, devtools, etc. + * Gameplay-related input processing is still done in Game::Tick() for now + * as it is tightly coupled to the game logic. + */ +void Game::ProcessInput() { - float headprop, bodyprop, armprop, legprop; - if (!devtools) { + /* Pump SDL input events */ + Input::Tick(); + + /* Menu handling (main menu, leave game) */ + if (Input::isKeyPressed(SDL_SCANCODE_ESCAPE) && + (gameon || mainmenu == 0)) { + selected = -1; + if (mainmenu == 0 && !winfreeze) { + mainmenu = 2; // Pause + } else if (mainmenu == 1 || mainmenu == 2) { + mainmenu = 0; // Unpause + } + // Play menu theme + if (musictoggle && (mainmenu == 1 || mainmenu == 2)) { + OPENAL_SetFrequency(OPENAL_ALL); + emit_stream_np(stream_menutheme); + pause_sound(leveltheme); + } + // On resume, play level music + if (!mainmenu) { + pause_sound(stream_menutheme); + resume_stream(leveltheme); + } + } + + /* Challenge mode */ + if (!campaign && !mainmenu) { + if ((Input::isKeyPressed(jumpkey) || Input::isKeyPressed(SDL_SCANCODE_SPACE))) { + if (winfreeze) { + winfreeze = 0; + } + } + + if ((Input::isKeyDown(SDL_SCANCODE_ESCAPE)) && gameon) { + if (console) { + console = false; + freeze = 0; + } else if (winfreeze) { + mainmenu = 9; + gameon = 0; + } + } + } + + + /* Tutorial mode hotkeys */ + if (Tutorial::active) { + // Skip current tutorial stage + if (Input::isKeyPressed(SDL_SCANCODE_TAB)) { + if (Tutorial::stage != 51) { + Tutorial::stagetime = Tutorial::maxtime; + } + emit_sound_np(consolefailsound, 128.); + } + } + + + /* Screenshot */ + if (Input::isKeyPressed(SDL_SCANCODE_F1)) { + Screenshot(); + } + + + /* Stereo video mode hotkeys */ + if (Input::isKeyPressed(SDL_SCANCODE_F6)) { + if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { + stereoreverse = true; + } else { + stereoreverse = false; + } + + if (stereoreverse) { + printf("Stereo reversed\n"); + } else { + printf("Stereo unreversed\n"); + } + } + + if (Input::isKeyDown(SDL_SCANCODE_F7)) { + if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { + stereoseparation -= 0.001; + } else { + stereoseparation -= 0.010; + } + printf("Stereo decreased increased to %f\n", stereoseparation); + } + + if (Input::isKeyDown(SDL_SCANCODE_F8)) { + if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { + stereoseparation += 0.001; + } else { + stereoseparation += 0.010; + } + printf("Stereo separation increased to %f\n", stereoseparation); + } + + + /* Devtools */ + if (devtools && !mainmenu) { + ProcessDevInput(); + } +} + +void Game::ProcessDevInput() +{ + if (!devtools || mainmenu) { return; } + float headprop, bodyprop, armprop, legprop; + + if (!mainmenu) { + // Console + if (Input::isKeyPressed(consolekey)) { + console = !console; + if (console) { + OPENAL_SetFrequency(OPENAL_ALL); + } else { + freeze = 0; + waiting = false; + } + } + + // Freeze + if (Input::isKeyDown(SDL_SCANCODE_LALT) && Input::isKeyPressed(SDL_SCANCODE_V)) { + freeze = !freeze; + if (freeze) { + OPENAL_SetFrequency(OPENAL_ALL); + } + } + } + if (Input::isKeyDown(SDL_SCANCODE_LALT)) { /* Invicible */ if (Input::isKeyPressed(SDL_SCANCODE_H)) { @@ -2948,46 +3079,8 @@ void Game::Tick() static XYZ facing, flatfacing; static int target; - Input::Tick(); - - if (Input::isKeyPressed(SDL_SCANCODE_F6)) { - if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { - stereoreverse = true; - } else { - stereoreverse = false; - } - - if (stereoreverse) { - printf("Stereo reversed\n"); - } else { - printf("Stereo unreversed\n"); - } - } - - if (Input::isKeyDown(SDL_SCANCODE_F7)) { - if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { - stereoseparation -= 0.001; - } else { - stereoseparation -= 0.010; - } - printf("Stereo decreased increased to %f\n", stereoseparation); - } - - if (Input::isKeyDown(SDL_SCANCODE_F8)) { - if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { - stereoseparation += 0.001; - } else { - stereoseparation += 0.010; - } - printf("Stereo separation increased to %f\n", stereoseparation); - } - - if (Input::isKeyPressed(SDL_SCANCODE_TAB) && Tutorial::active) { - if (Tutorial::stage != 51) { - Tutorial::stagetime = Tutorial::maxtime; - } - emit_sound_np(consolefailsound, 128.); - } + /* Pump SDL input events and process non-gameplay related ones */ + ProcessInput(); /* Values of mainmenu : @@ -3024,27 +3117,6 @@ void Game::Tick() } Menu::Load(); } - //escape key pressed - if (Input::isKeyPressed(SDL_SCANCODE_ESCAPE) && - (gameon || mainmenu == 0)) { - selected = -1; - if (mainmenu == 0 && !winfreeze) { - mainmenu = 2; //pause - } else if (mainmenu == 1 || mainmenu == 2) { - mainmenu = 0; //unpause - } - //play menu theme - if (musictoggle && (mainmenu == 1 || mainmenu == 2)) { - OPENAL_SetFrequency(OPENAL_ALL); - emit_stream_np(stream_menutheme); - pause_sound(leveltheme); - } - //on resume, play level music - if (!mainmenu) { - pause_sound(stream_menutheme); - resume_stream(leveltheme); - } - } } if (mainmenu) { @@ -3061,28 +3133,9 @@ void Game::Tick() leveltime += multiplier; } - //keys - if (Input::isKeyDown(SDL_SCANCODE_LALT) && Input::isKeyPressed(SDL_SCANCODE_V) && devtools) { - freeze = !freeze; - if (freeze) { - OPENAL_SetFrequency(OPENAL_ALL); - } - } - - if (Input::isKeyPressed(consolekey) && devtools) { - console = !console; - if (console) { - OPENAL_SetFrequency(OPENAL_ALL); - } else { - freeze = 0; - waiting = false; - } - } - if (console) { freeze = 1; - } - if (console && !Input::isKeyDown(SDL_SCANCODE_LGUI)) { + inputText(consoletext[0], &consoleselected); if (!waiting) { if (!consoletext[0].empty()) { @@ -3113,21 +3166,6 @@ void Game::Tick() oldwinfreeze++; } - if ((Input::isKeyPressed(jumpkey) || Input::isKeyPressed(SDL_SCANCODE_SPACE)) && !campaign) { - if (winfreeze) { - winfreeze = 0; - } - } - if ((Input::isKeyDown(SDL_SCANCODE_ESCAPE)) && !campaign && gameon) { - if (console) { - console = false; - freeze = 0; - } else if (winfreeze) { - mainmenu = 9; - gameon = 0; - } - } - if (!freeze && !winfreeze && !(mainmenu && gameon) && (gameon || !gamestarted)) { //dialogues @@ -3492,8 +3530,6 @@ void Game::Tick() hawkcalldelay = 16 + abs(Random() % 8); } - doDevKeys(); - doAttacks(); doPlayerCollisions(); @@ -4538,10 +4574,6 @@ void Game::Tick() oldviewer = viewer; } } - - if (Input::isKeyPressed(SDL_SCANCODE_F1)) { - Screenshot(); - } } void Game::TickOnce() diff --git a/Source/Objects/Person.cpp b/Source/Objects/Person.cpp index 55b6209..e1a1e71 100644 --- a/Source/Objects/Person.cpp +++ b/Source/Objects/Person.cpp @@ -578,8 +578,8 @@ void Person::CheckKick() /* EFFECT * * USES: - * GameTick/doPlayerCollisions - spread fire between players - * GameTick/doDevKeys - press f to ignite + * GameTick::doPlayerCollisions - spread fire between players + * GameTick::ProcessDevKeys - press f to ignite * Person::DoStuff - spread fire from lit campfires and bushes */ void Person::CatchFire() -- 2.39.5