Get rid of some more manual memory management

master
Piotr Dziwinski 2015-06-26 00:24:05 +02:00
parent bed9c19fc8
commit 7e21d3bd79
6 changed files with 83 additions and 111 deletions

View File

@ -100,18 +100,12 @@ struct ApplicationPrivate
CApplication::CApplication() CApplication::CApplication()
: m_private(new ApplicationPrivate())
, m_eventQueue(new CEventQueue())
, m_profile(new CProfile())
, m_input(new CInput())
, m_pathManager(new CPathManager())
{ {
m_private = new ApplicationPrivate();
m_pathManager = new CPathManager();
m_eventQueue = new CEventQueue();
m_profile = new CProfile();
m_input = new CInput();
m_engine = nullptr;
m_device = nullptr;
m_controller = nullptr;
m_sound = nullptr;
m_exitCode = 0; m_exitCode = 0;
m_active = false; m_active = false;
m_debugModes = 0; m_debugModes = 0;
@ -164,21 +158,6 @@ CApplication::CApplication()
CApplication::~CApplication() CApplication::~CApplication()
{ {
delete m_private;
m_private = nullptr;
delete m_input;
m_input = nullptr;
delete m_eventQueue;
m_eventQueue = nullptr;
delete m_profile;
m_profile = nullptr;
delete m_pathManager;
m_pathManager = nullptr;
GetSystemUtils()->DestroyTimeStamp(m_baseTimeStamp); GetSystemUtils()->DestroyTimeStamp(m_baseTimeStamp);
GetSystemUtils()->DestroyTimeStamp(m_curTimeStamp); GetSystemUtils()->DestroyTimeStamp(m_curTimeStamp);
GetSystemUtils()->DestroyTimeStamp(m_lastTimeStamp); GetSystemUtils()->DestroyTimeStamp(m_lastTimeStamp);
@ -192,12 +171,12 @@ CApplication::~CApplication()
CEventQueue* CApplication::GetEventQueue() CEventQueue* CApplication::GetEventQueue()
{ {
return m_eventQueue; return m_eventQueue.get();
} }
CSoundInterface* CApplication::GetSound() CSoundInterface* CApplication::GetSound()
{ {
return m_sound; return m_sound.get();
} }
ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
@ -399,7 +378,8 @@ bool CApplication::Create()
GetLogger()->Info("Creating CApplication\n"); GetLogger()->Info("Creating CApplication\n");
m_errorMessage = m_pathManager->VerifyPaths(); m_errorMessage = m_pathManager->VerifyPaths();
if(!m_errorMessage.empty()) { if (!m_errorMessage.empty())
{
m_exitCode = 1; m_exitCode = 1;
return false; return false;
} }
@ -425,10 +405,13 @@ bool CApplication::Create()
//Create the sound instance. //Create the sound instance.
#ifdef OPENAL_SOUND #ifdef OPENAL_SOUND
if(!m_headless) { if (!m_headless)
m_sound = static_cast<CSoundInterface *>(new ALSound()); {
} else { m_sound.reset(new ALSound());
m_sound = new CSoundInterface(); }
else
{
m_sound.reset(new CSoundInterface());
} }
#else #else
GetLogger()->Info("No sound support.\n"); GetLogger()->Info("No sound support.\n");
@ -474,7 +457,8 @@ bool CApplication::Create()
return false; return false;
} }
if(!m_headless) { if (!m_headless)
{
// load settings from profile // load settings from profile
int iValue; int iValue;
std::string sValue; std::string sValue;
@ -485,7 +469,7 @@ bool CApplication::Create()
std::getline(resolution, ws, 'x'); std::getline(resolution, ws, 'x');
std::getline(resolution, hs, 'x'); std::getline(resolution, hs, 'x');
int w = 800, h = 600; int w = 800, h = 600;
if(!ws.empty() && !hs.empty()) { if (!ws.empty() && !hs.empty()) {
w = atoi(ws.c_str()); w = atoi(ws.c_str());
h = atoi(hs.c_str()); h = atoi(hs.c_str());
} }
@ -494,7 +478,7 @@ bool CApplication::Create()
std::vector<Math::IntPoint> modes; std::vector<Math::IntPoint> modes;
GetVideoResolutionList(modes, true, true); GetVideoResolutionList(modes, true, true);
for(auto it = modes.begin(); it != modes.end(); ++it) { for(auto it = modes.begin(); it != modes.end(); ++it) {
if(it->x == w && it->y == h) { if (it->x == w && it->y == h) {
m_deviceConfig.size = *it; m_deviceConfig.size = *it;
break; break;
} }
@ -528,19 +512,19 @@ bool CApplication::Create()
// Don't generate joystick events // Don't generate joystick events
SDL_JoystickEventState(SDL_IGNORE); SDL_JoystickEventState(SDL_IGNORE);
if(!m_headless) if (!m_headless)
{ {
m_device = Gfx::CreateDevice(m_deviceConfig, m_graphics.c_str()); m_device = Gfx::CreateDevice(m_deviceConfig, m_graphics.c_str());
if (m_device == nullptr) if (m_device == nullptr)
{ {
m_device = new Gfx::CNullDevice(); m_device.reset(new Gfx::CNullDevice());
GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str()); GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str());
} }
} }
else else
{ {
m_device = new Gfx::CNullDevice(); m_device.reset(new Gfx::CNullDevice());
} }
if (! m_device->Create() ) if (! m_device->Create() )
@ -551,9 +535,9 @@ bool CApplication::Create()
} }
// Create the 3D engine // Create the 3D engine
m_engine = new Gfx::CEngine(this); m_engine.reset(new Gfx::CEngine(this));
m_engine->SetDevice(m_device); m_engine->SetDevice(m_device.get());
if (! m_engine->Create() ) if (! m_engine->Create() )
{ {
@ -563,11 +547,12 @@ bool CApplication::Create()
} }
// Create the robot application. // Create the robot application.
m_controller = new CController(this, !defaultValues); m_controller.reset(new CController(this, !defaultValues));
if (m_runSceneName.empty()) if (m_runSceneName.empty())
m_controller->StartApp(); m_controller->StartApp();
else { else
{
m_controller->GetRobotMain()->ChangePhase(PHASE_USER); // To load userlevel list - TODO: this is ugly m_controller->GetRobotMain()->ChangePhase(PHASE_USER); // To load userlevel list - TODO: this is ugly
m_controller->GetRobotMain()->SetExitAfterMission(true); m_controller->GetRobotMain()->SetExitAfterMission(true);
m_controller->StartGame(m_runSceneName, m_runSceneRank/100, m_runSceneRank%100); m_controller->StartGame(m_runSceneName, m_runSceneRank/100, m_runSceneRank%100);
@ -631,26 +616,21 @@ void CApplication::Destroy()
{ {
m_joystickEnabled = false; m_joystickEnabled = false;
delete m_controller; m_controller.reset();
m_controller = nullptr; m_sound.reset();
delete m_sound;
m_sound = nullptr;
if (m_engine != nullptr) if (m_engine != nullptr)
{ {
m_engine->Destroy(); m_engine->Destroy();
delete m_engine; m_engine.reset();
m_engine = nullptr;
} }
if (m_device != nullptr) if (m_device != nullptr)
{ {
m_device->Destroy(); m_device->Destroy();
delete m_device; m_device.reset();
m_device = nullptr;
} }
if (m_private->joystick != nullptr) if (m_private->joystick != nullptr)
@ -1044,10 +1024,10 @@ Event CApplication::ProcessSystemEvent()
// Some keyboards return numerical enter keycode instead of normal enter // Some keyboards return numerical enter keycode instead of normal enter
// See issue #427 for details // See issue #427 for details
if(event.key.key == KEY(KP_ENTER)) if (event.key.key == KEY(KP_ENTER))
event.key.key = KEY(RETURN); event.key.key = KEY(RETURN);
if(event.key.key == KEY(TAB) && ((event.kmodState & KEY_MOD(ALT)) != 0)) if (event.key.key == KEY(TAB) && ((event.kmodState & KEY_MOD(ALT)) != 0))
{ {
GetLogger()->Debug("Minimize to taskbar\n"); GetLogger()->Debug("Minimize to taskbar\n");
SDL_WM_IconifyWindow(); SDL_WM_IconifyWindow();
@ -1196,7 +1176,7 @@ Event CApplication::CreateVirtualEvent(const Event& sourceEvent)
virtualEvent.key.key = GetVirtualKey(sourceEvent.key.key); virtualEvent.key.key = GetVirtualKey(sourceEvent.key.key);
virtualEvent.key.virt = true; virtualEvent.key.virt = true;
if(virtualEvent.key.key == sourceEvent.key.key) if (virtualEvent.key.key == sourceEvent.key.key)
virtualEvent.type = EVENT_NULL; virtualEvent.type = EVENT_NULL;
} }
else if ((sourceEvent.type == EVENT_JOY_BUTTON_DOWN) || (sourceEvent.type == EVENT_JOY_BUTTON_UP)) else if ((sourceEvent.type == EVENT_JOY_BUTTON_DOWN) || (sourceEvent.type == EVENT_JOY_BUTTON_UP))
@ -1681,14 +1661,14 @@ void CApplication::SetLanguage(Language language)
{ {
std::locale::global(std::locale(systemLocale)); std::locale::global(std::locale(systemLocale));
} }
catch(...) catch (...)
{ {
GetLogger()->Warn("Failed to update locale, possibly incorect system configuration. Will fallback to classic locale.\n"); GetLogger()->Warn("Failed to update locale, possibly incorect system configuration. Will fallback to classic locale.\n");
try try
{ {
std::locale::global(std::locale::classic()); std::locale::global(std::locale::classic());
} }
catch(...) catch (...)
{ {
GetLogger()->Warn("Failed to set classic locale. Something is really messed up in your system configuration. Translations might not work.\n"); GetLogger()->Warn("Failed to set classic locale. Something is really messed up in your system configuration. Translations might not work.\n");
} }

View File

@ -354,23 +354,23 @@ protected:
protected: protected:
//! Private (SDL-dependent data) //! Private (SDL-dependent data)
ApplicationPrivate* m_private; std::unique_ptr<ApplicationPrivate> m_private;
//! Global event queue //! Global event queue
CEventQueue* m_eventQueue; std::unique_ptr<CEventQueue> m_eventQueue;
//! Graphics engine //! Graphics engine
Gfx::CEngine* m_engine; std::unique_ptr<Gfx::CEngine> m_engine;
//! Graphics device //! Graphics device
Gfx::CDevice* m_device; std::unique_ptr<Gfx::CDevice> m_device;
//! Sound subsystem //! Sound subsystem
CSoundInterface* m_sound; std::unique_ptr<CSoundInterface> m_sound;
//! Game controller - game engine and UI //! Game controller - game engine and UI
CController* m_controller; std::unique_ptr<CController> m_controller;
//! Profile (INI) reader/writer //! Profile (INI) reader/writer
CProfile* m_profile; std::unique_ptr<CProfile> m_profile;
//! Input manager //! Input manager
CInput* m_input; std::unique_ptr<CInput> m_input;
//! Path manager //! Path manager
CPathManager* m_pathManager; std::unique_ptr<CPathManager> m_pathManager;
//! Code to return at exit //! Code to return at exit
int m_exitCode; int m_exitCode;

View File

@ -25,15 +25,12 @@
#include "ui/maindialog.h" #include "ui/maindialog.h"
template<> CController* CSingleton<CController>::m_instance = nullptr;
CController::CController(CApplication* app, bool loadProfile) CController::CController(CApplication* app, bool loadProfile)
: m_app(app)
, m_main(new CRobotMain(this))
, m_dialog(new Ui::CMainDialog())
{ {
m_app = app;
m_main = new CRobotMain(this);
m_dialog = new Ui::CMainDialog();
m_main->Create(loadProfile); m_main->Create(loadProfile);
m_dialog->Create(); m_dialog->Create();
if (!loadProfile) if (!loadProfile)
@ -44,13 +41,6 @@ CController::CController(CApplication* app, bool loadProfile)
CController::~CController() CController::~CController()
{ {
delete m_dialog;
m_dialog = nullptr;
delete m_main;
m_main = nullptr;
m_app = nullptr;
} }
CApplication* CController::GetApplication() CApplication* CController::GetApplication()
@ -60,12 +50,12 @@ CApplication* CController::GetApplication()
CRobotMain* CController::GetRobotMain() CRobotMain* CController::GetRobotMain()
{ {
return m_main; return m_main.get();
} }
Ui::CMainDialog* CController::GetMainDialog() Ui::CMainDialog* CController::GetMainDialog()
{ {
return m_dialog; return m_dialog.get();
} }
void CController::StartApp() void CController::StartApp()
@ -83,6 +73,6 @@ void CController::StartGame(std::string cat, int chap, int lvl)
void CController::ProcessEvent(Event& event) void CController::ProcessEvent(Event& event)
{ {
bool passOn = m_dialog->EventProcess(event); bool passOn = m_dialog->EventProcess(event);
if(passOn) if (passOn)
m_main->ProcessEvent(event); m_main->ProcessEvent(event);
} }

View File

@ -24,11 +24,12 @@
#pragma once #pragma once
#include "common/event.h" #include <memory>
#include "common/singleton.h" #include <string>
class CApplication; class CApplication;
class CRobotMain; class CRobotMain;
struct Event;
namespace Ui { namespace Ui {
class CMainDialog; class CMainDialog;
} }
@ -37,7 +38,7 @@ class CMainDialog;
* \class CController * \class CController
* \brief Entry point into CRobotMain and CMainDialog * \brief Entry point into CRobotMain and CMainDialog
*/ */
class CController : public CSingleton<CController> class CController
{ {
public: public:
CController(CApplication* app, bool loadProfile = true); CController(CApplication* app, bool loadProfile = true);
@ -59,7 +60,7 @@ public:
void StartGame(std::string cat, int chap, int lvl); void StartGame(std::string cat, int chap, int lvl);
private: private:
CApplication* m_app; CApplication* m_app;
CRobotMain* m_main; std::unique_ptr<CRobotMain> m_main;
Ui::CMainDialog* m_dialog; std::unique_ptr<Ui::CMainDialog> m_dialog;
}; };

View File

@ -39,21 +39,20 @@ FramebufferSupport DetectFramebufferSupport()
return FBS_NONE; return FBS_NONE;
} }
CDevice* CreateDevice(const DeviceConfig &config, const char *name) std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name)
{ {
if (name == nullptr) return nullptr; if (name == "default") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (std::strcmp(name, "default") == 0) return new CGLDevice(config); else if (name == "opengl") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (std::strcmp(name, "opengl") == 0) return new CGLDevice(config); else if (name == "gl14") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (std::strcmp(name, "gl14") == 0) return new CGLDevice(config); else if (name == "gl21") return std::unique_ptr<CDevice>{new CGL21Device(config)};
else if (std::strcmp(name, "gl21") == 0) return new CGL21Device(config); else if (name == "gl33") return std::unique_ptr<CDevice>{new CGL33Device(config)};
else if (std::strcmp(name, "gl33") == 0) return new CGL33Device(config); else if (name == "auto")
else if (std::strcmp(name, "auto") == 0)
{ {
int version = GetOpenGLVersion(); int version = GetOpenGLVersion();
if (version >= 33) return new CGL33Device(config); if (version >= 33) return std::unique_ptr<CDevice>{new CGL33Device(config)};
else if (version >= 21) return new CGL21Device(config); else if (version >= 21) return std::unique_ptr<CDevice>{new CGL21Device(config)};
else return new CGLDevice(config); else return std::unique_ptr<CDevice>{new CGLDevice(config)};
} }
return nullptr; return nullptr;

View File

@ -26,6 +26,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <memory>
// Graphics module namespace // Graphics module namespace
namespace Gfx namespace Gfx
@ -41,7 +43,7 @@ enum FramebufferSupport
FramebufferSupport DetectFramebufferSupport(); FramebufferSupport DetectFramebufferSupport();
//! Creates OpenGL device //! Creates OpenGL device
CDevice* CreateDevice(const DeviceConfig &config, const char *name); std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name);
//! Returns OpenGL version as one number. //! Returns OpenGL version as one number.
// First digit is major part, second digit is minor part. // First digit is major part, second digit is minor part.