From 986cf98aafcfc1522212fd51c666aa1b826bde7b Mon Sep 17 00:00:00 2001 From: krzys-h Date: Mon, 22 Dec 2014 10:35:05 +0100 Subject: [PATCH] CController - entry point into CRobotMain and CMainDialog --- src/CMakeLists.txt | 1 + src/app/app.cpp | 48 ++++----- src/app/app.h | 6 +- src/app/controller.cpp | 88 +++++++++++++++++ src/app/controller.h | 65 +++++++++++++ src/object/robotmain.cpp | 205 ++++++++++++++++++++++----------------- src/object/robotmain.h | 13 ++- src/ui/maindialog.cpp | 35 +++++-- src/ui/maindialog.h | 2 + 9 files changed, 333 insertions(+), 130 deletions(-) create mode 100644 src/app/controller.cpp create mode 100644 src/app/controller.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e234937..a1e1c66a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -61,6 +61,7 @@ endif() # Source files set(BASE_SOURCES app/app.cpp + app/controller.cpp app/input.cpp app/pausemanager.cpp app/system.cpp diff --git a/src/app/app.cpp b/src/app/app.cpp index bdea2d7f..d598c0e5 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -21,6 +21,7 @@ #include "app/app.h" +#include "app/controller.h" #include "app/input.h" #include "app/system.h" @@ -108,16 +109,16 @@ CApplication::CApplication() m_profile = new CProfile(); m_input = new CInput(); - m_engine = nullptr; - m_device = nullptr; - m_modelManager = nullptr; - m_robotMain = nullptr; - m_sound = nullptr; + m_engine = nullptr; + m_device = nullptr; + m_modelManager = nullptr; + m_controller = nullptr; + m_sound = nullptr; - m_exitCode = 0; - m_active = false; - m_debugModes = 0; - m_restart = false; + m_exitCode = 0; + m_active = false; + m_debugModes = 0; + m_restart = false; m_windowTitle = "Colobot: Gold Edition"; @@ -588,14 +589,15 @@ bool CApplication::Create() m_modelManager = new Gfx::CModelManager(m_engine); // Create the robot application. - m_robotMain = new CRobotMain(this, !defaultValues); + m_controller = new CController(this, !defaultValues); - if (defaultValues) m_robotMain->CreateIni(); - - if (! m_runSceneName.empty()) - m_robotMain->LoadSceneOnStart(m_runSceneName, m_runSceneRank); - else - m_robotMain->ChangePhase(PHASE_WELCOME1); + if (m_runSceneName.empty()) + m_controller->StartApp(); + else { + m_controller->GetRobotMain()->ChangePhase(PHASE_USER); // To load userlevel list - TODO: this is ugly + m_controller->GetRobotMain()->SetExitAfterMission(true); + m_controller->StartGame(m_runSceneName, m_runSceneRank/100, m_runSceneRank%100); + } return true; } @@ -672,8 +674,8 @@ void CApplication::Destroy() { m_joystickEnabled = false; - delete m_robotMain; - m_robotMain = nullptr; + delete m_controller; + m_controller = nullptr; delete m_sound; m_sound = nullptr; @@ -775,7 +777,7 @@ bool CApplication::ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig) ( static_cast(m_device) )->ConfigChanged(m_deviceConfig); m_engine->ResetAfterDeviceChanged(); - m_robotMain->ResetAfterDeviceChanged(); + m_controller->GetRobotMain()->ResetAfterDeviceChanged(); return true; } @@ -993,8 +995,8 @@ int CApplication::Run() if (m_engine != nullptr) passOn = m_engine->ProcessEvent(event); - if (passOn && m_robotMain != nullptr) - m_robotMain->ProcessEvent(event); + if (passOn && m_controller != nullptr) + m_controller->ProcessEvent(event); } StopPerformanceCounter(PCNT_EVENT_PROCESSING); @@ -1003,14 +1005,14 @@ int CApplication::Run() // Prepare and process step simulation event event = CreateUpdateEvent(); - if (event.type != EVENT_NULL && m_robotMain != nullptr) + if (event.type != EVENT_NULL && m_controller != nullptr) { LogEvent(event); m_sound->FrameMove(m_relTime); StartPerformanceCounter(PCNT_UPDATE_GAME); - m_robotMain->ProcessEvent(event); + m_controller->ProcessEvent(event); StopPerformanceCounter(PCNT_UPDATE_GAME); StartPerformanceCounter(PCNT_UPDATE_ENGINE); diff --git a/src/app/app.h b/src/app/app.h index a8ca340a..818eaf46 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -40,7 +40,7 @@ class CInstanceManager; class CEventQueue; -class CRobotMain; +class CController; class CSoundInterface; class CInput; class CObjectManager; @@ -373,8 +373,8 @@ protected: Gfx::CModelManager* m_modelManager; //! Sound subsystem CSoundInterface* m_sound; - //! Main class of the proper game engine - CRobotMain* m_robotMain; + //! Game controller - game engine and UI + CController* m_controller; //! Profile (INI) reader/writer CProfile* m_profile; //! Input manager diff --git a/src/app/controller.cpp b/src/app/controller.cpp new file mode 100644 index 00000000..b7eb670f --- /dev/null +++ b/src/app/controller.cpp @@ -0,0 +1,88 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsiteс.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#include "app/controller.h" + + +#include "object/robotmain.h" + +#include "ui/maindialog.h" + + +template<> CController* CSingleton::m_instance = nullptr; + + +CController::CController(CApplication* app, bool loadProfile) +{ + m_app = app; + m_main = new CRobotMain(this); + m_dialog = new Ui::CMainDialog(); + + m_main->Create(loadProfile); + m_dialog->Create(); + if (!loadProfile) + m_main->CreateIni(); + else + m_main->LoadIni(); +} + +CController::~CController() +{ + delete m_dialog; + m_dialog = nullptr; + + delete m_main; + m_main = nullptr; + + m_app = nullptr; +} + +CApplication* CController::GetApplication() +{ + return m_app; +} + +CRobotMain* CController::GetRobotMain() +{ + return m_main; +} + +Ui::CMainDialog* CController::GetMainDialog() +{ + return m_dialog; +} + +void CController::StartApp() +{ + m_main->ChangePhase(PHASE_WELCOME1); +} + +void CController::StartGame(std::string cat, int chap, int lvl) +{ + m_dialog->SetSceneName(cat.c_str()); + m_dialog->SetSceneRank(chap*100+lvl); + m_main->ChangePhase(PHASE_LOADING); +} + +void CController::ProcessEvent(Event& event) +{ + bool passOn = m_dialog->EventProcess(event); + if(passOn) + m_main->ProcessEvent(event); +} \ No newline at end of file diff --git a/src/app/controller.h b/src/app/controller.h new file mode 100644 index 00000000..8a84a5f4 --- /dev/null +++ b/src/app/controller.h @@ -0,0 +1,65 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsiteс.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +/** + * \file app/controller.h + * \brief CController class + */ + +#pragma once + +#include "common/event.h" +#include "common/singleton.h" + +class CApplication; +class CRobotMain; +namespace Ui { +class CMainDialog; +} + +/** + * \class CController + * \brief Entry point into CRobotMain and CMainDialog + */ +class CController : public CSingleton +{ +public: + CController(CApplication* app, bool loadProfile = true); + ~CController(); + + //! Return CApplication instance + CApplication* GetApplication(); + //! Return CRobotMain instance + CRobotMain* GetRobotMain(); + //! Return CMainDialog instance + Ui::CMainDialog* GetMainDialog(); + + //! Event processing + void ProcessEvent(Event &event); + + //! Start the application + void StartApp(); + //! Starts the simulation, loading the given scene + void StartGame(std::string cat, int chap, int lvl); + +private: + CApplication* m_app; + CRobotMain* m_main; + Ui::CMainDialog* m_dialog; +}; \ No newline at end of file diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 69ee3c7e..caee647d 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -23,6 +23,7 @@ #include "CBot/CBotDll.h" #include "app/app.h" +#include "app/controller.h" #include "app/input.h" #include "common/event.h" @@ -120,35 +121,34 @@ float g_unit; // conversion factor //! Constructor of robot application -CRobotMain::CRobotMain(CApplication* app, bool loadProfile) +CRobotMain::CRobotMain(CController* controller) { - m_app = app; + m_ctrl = controller; + m_app = nullptr; - m_eventQueue = m_app->GetEventQueue(); - m_sound = m_app->GetSound(); + m_eventQueue = nullptr; + m_sound = nullptr; - m_engine = Gfx::CEngine::GetInstancePointer(); - m_lightMan = m_engine->GetLightManager(); - m_particle = m_engine->GetParticle(); - m_water = m_engine->GetWater(); - m_cloud = m_engine->GetCloud(); - m_lightning = m_engine->GetLightning(); - m_planet = m_engine->GetPlanet(); - m_pause = CPauseManager::GetInstancePointer(); - m_input = CInput::GetInstancePointer(); + m_engine = nullptr; + m_lightMan = nullptr; + m_particle = nullptr; + m_water = nullptr; + m_cloud = nullptr; + m_lightning = nullptr; + m_planet = nullptr; + m_pause = nullptr; + m_input = nullptr; - m_interface = new Ui::CInterface(); - m_terrain = new Gfx::CTerrain(); - m_camera = new Gfx::CCamera(); - m_displayText = new Ui::CDisplayText(); - m_movie = new CMainMovie(); - m_dialog = new Ui::CMainDialog(); - m_short = new Ui::CMainShort(); - m_map = new Ui::CMainMap(); + m_interface = nullptr; + m_terrain = nullptr; + m_camera = nullptr; + m_displayText = nullptr; + m_movie = nullptr; + m_dialog = nullptr; + m_short = nullptr; + m_map = nullptr; m_displayInfo = nullptr; - m_engine->SetTerrain(m_terrain); - m_time = 0.0f; m_gameTime = 0.0f; @@ -199,10 +199,7 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile) m_friendAim = false; m_resetCreate = false; m_shortCut = true; - - m_engine->SetMovieLock(m_movieLock); - - m_movie->Flush(); + m_movieInfoIndex = -1; m_tooltipPos = Math::Point(0.0f, 0.0f); @@ -220,43 +217,6 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile) m_autosaveSlots = 3; m_autosaveLast = 0.0f; - FlushDisplayInfo(); - - m_fontSize = 19.0f; - m_windowPos = Math::Point(0.15f, 0.17f); - m_windowDim = Math::Point(0.70f, 0.66f); - - float fValue; - int iValue; - - if (loadProfile) - { - if (GetProfile().GetFloatProperty("Edit", "FontSize", fValue)) m_fontSize = fValue; - if (GetProfile().GetFloatProperty("Edit", "WindowPosX", fValue)) m_windowPos.x = fValue; - if (GetProfile().GetFloatProperty("Edit", "WindowPosY", fValue)) m_windowPos.y = fValue; - if (GetProfile().GetFloatProperty("Edit", "WindowDimX", fValue)) m_windowDim.x = fValue; - if (GetProfile().GetFloatProperty("Edit", "WindowDimY", fValue)) m_windowDim.y = fValue; - } - - m_IOPublic = false; - m_IODim = Math::Point(320.0f/640.0f, (121.0f+18.0f*8)/480.0f); - m_IOPos.x = (1.0f-m_IODim.x)/2.0f; // in the middle - m_IOPos.y = (1.0f-m_IODim.y)/2.0f; - - if (loadProfile) - { - if (GetProfile().GetIntProperty ("Edit", "IOPublic", iValue)) m_IOPublic = iValue; - if (GetProfile().GetFloatProperty("Edit", "IOPosX", fValue)) m_IOPos.x = fValue; - if (GetProfile().GetFloatProperty("Edit", "IOPosY", fValue)) m_IOPos.y = fValue; - if (GetProfile().GetFloatProperty("Edit", "IODimX", fValue)) m_IODim.x = fValue; - if (GetProfile().GetFloatProperty("Edit", "IODimY", fValue)) m_IODim.y = fValue; - } - - m_short->FlushShortcuts(); - InitEye(); - - m_engine->SetTracePrecision(1.0f); - m_cameraPan = 0.0f; m_cameraZoom = 0.0f; @@ -267,10 +227,6 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile) g_unit = UNIT; m_gamerName = ""; - if (loadProfile) GetProfile().GetStringProperty("Gamer", "LastName", m_gamerName); - SetGlobalGamerName(m_gamerName); - ReadFreeParam(); - if (loadProfile) m_dialog->SetupRecall(); for (int i = 0; i < MAXSHOWLIMIT; i++) { @@ -278,6 +234,81 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile) m_showLimit[i].total = 0; m_showLimit[i].link = 0; } +} + +void CRobotMain::Create(bool loadProfile) +{ + m_app = m_ctrl->GetApplication(); + + m_eventQueue = m_app->GetEventQueue(); + m_sound = m_app->GetSound(); + + m_engine = Gfx::CEngine::GetInstancePointer(); + m_lightMan = m_engine->GetLightManager(); + m_particle = m_engine->GetParticle(); + m_water = m_engine->GetWater(); + m_cloud = m_engine->GetCloud(); + m_lightning = m_engine->GetLightning(); + m_planet = m_engine->GetPlanet(); + m_pause = CPauseManager::GetInstancePointer(); + m_input = CInput::GetInstancePointer(); + + m_interface = new Ui::CInterface(); + m_terrain = new Gfx::CTerrain(); + m_camera = new Gfx::CCamera(); + m_displayText = new Ui::CDisplayText(); + m_movie = new CMainMovie(); + m_dialog = m_ctrl->GetMainDialog(); + m_short = new Ui::CMainShort(); + m_map = new Ui::CMainMap(); + m_displayInfo = nullptr; + + m_engine->SetTerrain(m_terrain); + + m_engine->SetMovieLock(m_movieLock); + + m_movie->Flush(); + + FlushDisplayInfo(); + + m_fontSize = 19.0f; + m_windowPos = Math::Point(0.15f, 0.17f); + m_windowDim = Math::Point(0.70f, 0.66f); + + float fValue; + int iValue; + + if (loadProfile) + { + if (GetProfile().GetFloatProperty("Edit", "FontSize", fValue)) m_fontSize = fValue; + if (GetProfile().GetFloatProperty("Edit", "WindowPosX", fValue)) m_windowPos.x = fValue; + if (GetProfile().GetFloatProperty("Edit", "WindowPosY", fValue)) m_windowPos.y = fValue; + if (GetProfile().GetFloatProperty("Edit", "WindowDimX", fValue)) m_windowDim.x = fValue; + if (GetProfile().GetFloatProperty("Edit", "WindowDimY", fValue)) m_windowDim.y = fValue; + } + + m_IOPublic = false; + m_IODim = Math::Point(320.0f/640.0f, (121.0f+18.0f*8)/480.0f); + m_IOPos.x = (1.0f-m_IODim.x)/2.0f; // in the middle + m_IOPos.y = (1.0f-m_IODim.y)/2.0f; + + if (loadProfile) + { + if (GetProfile().GetIntProperty ("Edit", "IOPublic", iValue)) m_IOPublic = iValue; + if (GetProfile().GetFloatProperty("Edit", "IOPosX", fValue)) m_IOPos.x = fValue; + if (GetProfile().GetFloatProperty("Edit", "IOPosY", fValue)) m_IOPos.y = fValue; + if (GetProfile().GetFloatProperty("Edit", "IODimX", fValue)) m_IODim.x = fValue; + if (GetProfile().GetFloatProperty("Edit", "IODimY", fValue)) m_IODim.y = fValue; + } + + m_short->FlushShortcuts(); + InitEye(); + + m_engine->SetTracePrecision(1.0f); + + if (loadProfile) GetProfile().GetStringProperty("Gamer", "LastName", m_gamerName); + SetGlobalGamerName(m_gamerName); + ReadFreeParam(); CScriptFunctions::m_filesDir = CResourceManager::GetSaveLocation()+"/"+m_dialog->GetFilesDir(); //TODO: Refactor to PHYSFS while rewriting CBot engine CScriptFunctions::Init(); @@ -304,18 +335,17 @@ CRobotMain::~CRobotMain() delete m_movie; m_movie = nullptr; - delete m_dialog; - m_dialog = nullptr; - delete m_short; m_short = nullptr; delete m_map; m_map = nullptr; - + + m_dialog = nullptr; m_input = nullptr; m_pause = nullptr; m_app = nullptr; + m_ctrl = nullptr; } Gfx::CCamera* CRobotMain::GetCamera() @@ -338,16 +368,6 @@ Ui::CDisplayText* CRobotMain::GetDisplayText() return m_displayText; } -void CRobotMain::LoadSceneOnStart(const std::string& name, int rank) -{ - m_exitAfterMission = true; - // TODO: fix this ugly dependency :( - ChangePhase(PHASE_USER); // To load userlevel list - m_dialog->SetSceneName(name.c_str()); - m_dialog->SetSceneRank(rank); - ChangePhase(PHASE_LOADING); -} - void CRobotMain::ResetAfterDeviceChanged() { if(m_phase == PHASE_SETUPds || @@ -382,6 +402,11 @@ void CRobotMain::CreateIni() GetProfile().Save(); } +void CRobotMain::LoadIni() +{ + m_dialog->SetupRecall(); +} + //! Changes phase void CRobotMain::ChangePhase(Phase phase) { @@ -649,7 +674,6 @@ bool CRobotMain::ProcessEvent(Event &event) } } - m_dialog->EventProcess(event); m_displayText->EventProcess(event); RemoteCamera(m_cameraPan, m_cameraZoom, event.rTime); @@ -696,14 +720,10 @@ bool CRobotMain::ProcessEvent(Event &event) if (event.type == EVENT_SPEED) SetSpeed(1.0f); - if (!m_dialog->EventProcess(event)) + if (event.type == EVENT_MOUSE_MOVE) { - if (event.type == EVENT_MOUSE_MOVE) - { - m_lastMousePos = event.mousePos; - HiliteObject(event.mousePos); - } - return false; + m_lastMousePos = event.mousePos; + HiliteObject(event.mousePos); } if (!m_displayText->EventProcess(event)) @@ -6186,4 +6206,9 @@ void CRobotMain::Autosave() IOWriteScene(savegameFileName.c_str(), fileCBot.c_str(), const_cast((std::string("[AUTOSAVE] ")+timestr).c_str())); m_dialog->MakeSaveScreenshot(dir + "/screen.png"); +} + +void CRobotMain::SetExitAfterMission(bool exit) +{ + m_exitAfterMission = exit; } \ No newline at end of file diff --git a/src/object/robotmain.h b/src/object/robotmain.h index f84f53e6..9c4281cb 100644 --- a/src/object/robotmain.h +++ b/src/object/robotmain.h @@ -74,6 +74,7 @@ enum Phase }; +class CController; class CEventQueue; class CSoundInterface; class CLevelParserLine; @@ -170,18 +171,18 @@ const int SATCOM_MAX = 6; class CRobotMain : public CSingleton { public: - CRobotMain(CApplication* app, bool loadProfile); + CRobotMain(CController* controller); virtual ~CRobotMain(); + + void Create(bool loadProfile = true); Gfx::CCamera* GetCamera(); Gfx::CTerrain* GetTerrain(); Ui::CInterface* GetInterface(); Ui::CDisplayText* GetDisplayText(); - //! Caused the given mission to be loaded immediately after start - void LoadSceneOnStart(const std::string& name, int rank); - void CreateIni(); + void LoadIni(); void ResetAfterDeviceChanged(); @@ -350,6 +351,9 @@ public: int GetAutosaveInterval(); void SetAutosaveSlots(int slots); int GetAutosaveSlots(); + + //! Enable mode where completing mission closes the game + void SetExitAfterMission(bool exit); protected: bool EventFrame(const Event &event); @@ -389,6 +393,7 @@ protected: protected: + CController* m_ctrl; CApplication* m_app; CEventQueue* m_eventQueue; CMainMovie* m_movie; diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp index 3240cfa1..40bec525 100644 --- a/src/ui/maindialog.cpp +++ b/src/ui/maindialog.cpp @@ -116,15 +116,15 @@ static int perso_color[3*10*3] = CMainDialog::CMainDialog() { - m_app = CApplication::GetInstancePointer(); - m_eventQueue = m_app->GetEventQueue(); - m_sound = m_app->GetSound(); - m_main = CRobotMain::GetInstancePointer(); - m_interface = m_main->GetInterface(); - m_camera = m_main->GetCamera(); - m_engine = Gfx::CEngine::GetInstancePointer(); - m_particle = m_engine->GetParticle(); - m_pause = CPauseManager::GetInstancePointer(); + m_app = nullptr; + m_eventQueue = nullptr; + m_sound = nullptr; + m_main = nullptr; + m_interface = nullptr; + m_camera = nullptr; + m_engine = nullptr; + m_particle = nullptr; + m_pause = nullptr; m_phase = PHASE_NAME; m_phaseSetup = PHASE_SETUPg; @@ -182,11 +182,26 @@ CMainDialog::CMainDialog() m_publicDir = "program"; m_filesDir = "files"; - m_setupFull = m_app->GetVideoConfig().fullScreen; + m_setupFull = false; m_bDialog = false; } +void CMainDialog::Create() +{ + m_app = CApplication::GetInstancePointer(); + m_eventQueue = m_app->GetEventQueue(); + m_sound = m_app->GetSound(); + m_main = CRobotMain::GetInstancePointer(); + m_interface = m_main->GetInterface(); + m_camera = m_main->GetCamera(); + m_engine = Gfx::CEngine::GetInstancePointer(); + m_particle = m_engine->GetParticle(); + m_pause = CPauseManager::GetInstancePointer(); + + m_setupFull = m_app->GetVideoConfig().fullScreen; +} + // Destructor of robot application. CMainDialog::~CMainDialog() diff --git a/src/ui/maindialog.h b/src/ui/maindialog.h index d0753df6..9d69e598 100644 --- a/src/ui/maindialog.h +++ b/src/ui/maindialog.h @@ -72,6 +72,8 @@ class CMainDialog public: CMainDialog(); ~CMainDialog(); + + void Create(); bool EventProcess(const Event &event); void ChangePhase(Phase phase);