From b8f0ed31e34735657c7cdb649592f4e383c605f7 Mon Sep 17 00:00:00 2001 From: "Martin Felis (berta)" Date: Sun, 28 Nov 2010 01:13:45 +0100 Subject: [PATCH] sound volumes are now read from and saved to config.rc + bugfix - fixed calling of IMGUIClear() when the view state changed (was previously called only when the game state changed) --- .hgignore | 1 + asteroids/AsteroidsEvents.h | 2 +- asteroids/Controller.cc | 4 ++-- asteroids/Model.cc | 5 ----- asteroids/View.cc | 11 ++++------- asteroids/View.h | 15 +++++++++++++++ asteroids/main.cc | 12 ++++++++---- engine/VariablesCommands.cc | 14 ++++++++++++++ engine/doc/Mainpage.h | 6 +++--- 9 files changed, 48 insertions(+), 22 deletions(-) diff --git a/.hgignore b/.hgignore index 117c25c..88b8636 100644 --- a/.hgignore +++ b/.hgignore @@ -10,6 +10,7 @@ Makefile start runtests run_asteroids +config.rc ./doc/html/* diff --git a/asteroids/AsteroidsEvents.h b/asteroids/AsteroidsEvents.h index 550ab2f..57ee4b1 100644 --- a/asteroids/AsteroidsEvents.h +++ b/asteroids/AsteroidsEvents.h @@ -10,7 +10,7 @@ BEGIN_ENUM(Event) DECL_ENUM_ELEMENT(EventAccelerateStart), DECL_ENUM_ELEMENT(EventAccelerateStop), DECL_ENUM_ELEMENT(EventLevelComplete), - DECL_ENUM_ELEMENT(EventChangeGameState), + DECL_ENUM_ELEMENT(EventChangeViewState), DECL_ENUM_ELEMENT(EventGameOver), DECL_ENUM_ELEMENT(EventShipExplode), DECL_ENUM_ELEMENT(EventPlayerDied), diff --git a/asteroids/Controller.cc b/asteroids/Controller.cc index 6b35b42..f9166d8 100644 --- a/asteroids/Controller.cc +++ b/asteroids/Controller.cc @@ -17,13 +17,13 @@ int Controller::OnInit (int argc, char *argv[]) { mBindings[SDLK_F8] = "toggleconsole"; mBindings[SDLK_F9] = "set playerspeed 5.0"; - Engine::RegisterListener (this, EventChangeGameState); + Engine::RegisterListener (this, EventChangeViewState); return 0; } bool Controller::OnReceiveEvent (const Engine::EventBasePtr &event) { - if (event->mEventType == EventChangeGameState) { + if (event->mEventType == EventChangeViewState) { IMGUIClear(); } return false; diff --git a/asteroids/Model.cc b/asteroids/Model.cc index a4d5e36..8e1f46e 100644 --- a/asteroids/Model.cc +++ b/asteroids/Model.cc @@ -374,11 +374,6 @@ void Model::ProceedToNextLevel () { void Model::SetGameState (const unsigned int &state) { mLastGameState = mGameState; - - Engine::EventBasePtr changegamestate_event (new Engine::EventBase()); - changegamestate_event->mEventType = EventChangeGameState; - QueueEvent (changegamestate_event); - mGameState = state; } diff --git a/asteroids/View.cc b/asteroids/View.cc index b9ffc32..b5d8530 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -663,22 +663,19 @@ void View::DrawUiHighscore() { void View::DrawUiOptions() { DrawPageTitle ("Options"); SelectFont ("console.ttf size=23"); - - // Enter your name - std::string player_name = GetModel()->GetPlayerName(); - Engine::GUI::Label (1, "Effects Volume: ", screen_right * 0.5 - 150, 240); + Engine::GUI::Label (10, "Effects Volume: ", screen_right * 0.5 - 150, 240); float effects_volume = Engine::GetEffectsVolume(); - if (Engine::GUI::VerticalSlider (2, screen_right * 0.5 - 100, 250, 250, 16, 0., 1., effects_volume)) { + if (Engine::GUI::VerticalSlider (1, screen_right * 0.5 - 100, 250, 250, 16, 0., 1., effects_volume)) { Engine::LogDebug ("Setting effects volume to: %f", effects_volume); Engine::SetEffectsVolume(effects_volume); } - Engine::GUI::Label (3, "Music Volume: ", screen_right * 0.5 - 150, 300); + Engine::GUI::Label (11, "Music Volume: ", screen_right * 0.5 - 150, 300); float music_volume = Engine::GetMusicVolume(); - if (Engine::GUI::VerticalSlider (4, screen_right * 0.5 - 100, 310, 250, 16, 0., 1., music_volume)) { + if (Engine::GUI::VerticalSlider (2, screen_right * 0.5 - 100, 310, 250, 16, 0., 1., music_volume)) { Engine::LogDebug ("Setting music volume to: %f", music_volume); Engine::SetMusicVolume(music_volume); } diff --git a/asteroids/View.h b/asteroids/View.h index a8809f4..fb78600 100644 --- a/asteroids/View.h +++ b/asteroids/View.h @@ -75,6 +75,11 @@ class View : public Engine::ViewBase { void PushViewState (const ViewState state) { Engine::LogDebug ("Pushing ViewState %s", GetStringViewState(state)); mViewStateStack.push(state); + + // Fire the view state change event to properly clear the IMGUI state + Engine::EventBasePtr changeviewstate_event (new Engine::EventBase()); + changeviewstate_event->mEventType = EventChangeViewState; + QueueEvent (changeviewstate_event); } /** \brief Returns the current ViewState */ ViewState GetViewState () { @@ -92,12 +97,22 @@ class View : public Engine::ViewBase { current_name = GetStringViewState(mViewStateStack.top()); Engine::LogDebug("Popped ViewState: %s current %s remaining: %u", popped_name.c_str(), current_name.c_str(), mViewStateStack.size()); + + // Fire the view state change event to properly clear the IMGUI state + Engine::EventBasePtr changeviewstate_event (new Engine::EventBase()); + changeviewstate_event->mEventType = EventChangeViewState; + QueueEvent (changeviewstate_event); } /** \brief Removes all elements of the ViewState stack */ void ResetViewState() { Engine::LogDebug ("Resetting ViewState stack"); while (mViewStateStack.size()) mViewStateStack.pop(); + + // Fire the view state change event to properly clear the IMGUI state + Engine::EventBasePtr changeviewstate_event (new Engine::EventBase()); + changeviewstate_event->mEventType = EventChangeViewState; + QueueEvent (changeviewstate_event); } // \todo [high] add Resource Manager! diff --git a/asteroids/main.cc b/asteroids/main.cc index 7320f52..90b4925 100644 --- a/asteroids/main.cc +++ b/asteroids/main.cc @@ -44,14 +44,18 @@ int main (int argc, char* argv[]) { engine.GetView()->SetGridSize (8,8); dynamic_cast(engine.GetPhysics())->SetWorldSize (28, 20); + // run the default commands and load the configuration Engine::RunCommand ("exec asteroids.rc"); - - Engine::LogMessage("Warning: muting sound!"); - Engine::SetEffectsVolume(0.); - Engine::SetMusicVolume(0.); + Engine::RunCommand ("exec config.rc"); engine.MainLoop (); + // save the configuration + std::ofstream config_file ("config.rc"); + config_file << "set effects_volume " << Engine::GetEffectsVolume() << std::endl; + config_file << "set music_volume " << Engine::GetMusicVolume() << std::endl; + config_file.close(); + SDL_WM_SetIcon(NULL,NULL); SDL_FreeSurface (image); diff --git a/engine/VariablesCommands.cc b/engine/VariablesCommands.cc index a3ed9d9..ab03103 100644 --- a/engine/VariablesCommands.cc +++ b/engine/VariablesCommands.cc @@ -1,4 +1,5 @@ #include "Variables.h" +#include "SoundBaseGlobal.h" namespace Engine { @@ -8,6 +9,19 @@ bool Cmd_Set (const std::vector args) { return false; } + // special variable sound_volume + if (args[0] == "effects_volume") { + SetEffectsVolume(atof(args[1].c_str())); + + return true; + } + // special variable music_volume + if (args[0] == "music_volume") { + SetMusicVolume(atof(args[1].c_str())); + + return true; + } + Variable *test = GetVariable (args[0]); if (test) { test->SetStringValue (args[1]); diff --git a/engine/doc/Mainpage.h b/engine/doc/Mainpage.h index 29ee4a3..54189e3 100644 --- a/engine/doc/Mainpage.h +++ b/engine/doc/Mainpage.h @@ -72,7 +72,6 @@ * todos within the code have a look at the \ref todo. * * \todo [high] Create a simple racing or asteroids game - * \todo [high] Enable saving of music and effects volume * \todo [med] Use shared_ptr instead of raw pointers for the Entities * \todo [med] Clear all references of EntityVisualState and EntityGameState * \todo [med] Clarify functionalities of CreateEntity, KillEntity, RegisterEntity, UnregisterEntity (which frees memory, which does only change the state of the Model?) @@ -88,9 +87,10 @@ * Engine::Module::Init() * * Done: + * \todo [high] (28-11-2010) Enable saving of music and effects volume * \todo [high] (28-11-2010) Allow transitions when player dies -* \todo [high] (11-09-2010) Since introduction of the IMGUI pressing 'space' in the menu crashes the game -* - [high] In Physics remove dependancy on the Model to pass on collision + * \todo [high] (11-09-2010) Since introduction of the IMGUI pressing 'space' in the menu crashes the game + * - [high] In Physics remove dependancy on the Model to pass on collision * events * - [high] Fix Physics bug when two actors collide actively (i.e. velocity * towards each other)