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()
: 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_active = false;
m_debugModes = 0;
@ -164,21 +158,6 @@ 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_curTimeStamp);
GetSystemUtils()->DestroyTimeStamp(m_lastTimeStamp);
@ -192,12 +171,12 @@ CApplication::~CApplication()
CEventQueue* CApplication::GetEventQueue()
{
return m_eventQueue;
return m_eventQueue.get();
}
CSoundInterface* CApplication::GetSound()
{
return m_sound;
return m_sound.get();
}
ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
@ -399,7 +378,8 @@ bool CApplication::Create()
GetLogger()->Info("Creating CApplication\n");
m_errorMessage = m_pathManager->VerifyPaths();
if(!m_errorMessage.empty()) {
if (!m_errorMessage.empty())
{
m_exitCode = 1;
return false;
}
@ -425,10 +405,13 @@ bool CApplication::Create()
//Create the sound instance.
#ifdef OPENAL_SOUND
if(!m_headless) {
m_sound = static_cast<CSoundInterface *>(new ALSound());
} else {
m_sound = new CSoundInterface();
if (!m_headless)
{
m_sound.reset(new ALSound());
}
else
{
m_sound.reset(new CSoundInterface());
}
#else
GetLogger()->Info("No sound support.\n");
@ -474,7 +457,8 @@ bool CApplication::Create()
return false;
}
if(!m_headless) {
if (!m_headless)
{
// load settings from profile
int iValue;
std::string sValue;
@ -485,7 +469,7 @@ bool CApplication::Create()
std::getline(resolution, ws, 'x');
std::getline(resolution, hs, 'x');
int w = 800, h = 600;
if(!ws.empty() && !hs.empty()) {
if (!ws.empty() && !hs.empty()) {
w = atoi(ws.c_str());
h = atoi(hs.c_str());
}
@ -494,7 +478,7 @@ bool CApplication::Create()
std::vector<Math::IntPoint> modes;
GetVideoResolutionList(modes, true, true);
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;
break;
}
@ -528,19 +512,19 @@ bool CApplication::Create()
// Don't generate joystick events
SDL_JoystickEventState(SDL_IGNORE);
if(!m_headless)
if (!m_headless)
{
m_device = Gfx::CreateDevice(m_deviceConfig, m_graphics.c_str());
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());
}
}
else
{
m_device = new Gfx::CNullDevice();
m_device.reset(new Gfx::CNullDevice());
}
if (! m_device->Create() )
@ -551,9 +535,9 @@ bool CApplication::Create()
}
// 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() )
{
@ -563,11 +547,12 @@ bool CApplication::Create()
}
// Create the robot application.
m_controller = new CController(this, !defaultValues);
m_controller.reset(new CController(this, !defaultValues));
if (m_runSceneName.empty())
m_controller->StartApp();
else {
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);
@ -631,26 +616,21 @@ void CApplication::Destroy()
{
m_joystickEnabled = false;
delete m_controller;
m_controller = nullptr;
delete m_sound;
m_sound = nullptr;
m_controller.reset();
m_sound.reset();
if (m_engine != nullptr)
{
m_engine->Destroy();
delete m_engine;
m_engine = nullptr;
m_engine.reset();
}
if (m_device != nullptr)
{
m_device->Destroy();
delete m_device;
m_device = nullptr;
m_device.reset();
}
if (m_private->joystick != nullptr)
@ -1044,10 +1024,10 @@ Event CApplication::ProcessSystemEvent()
// Some keyboards return numerical enter keycode instead of normal enter
// See issue #427 for details
if(event.key.key == KEY(KP_ENTER))
if (event.key.key == KEY(KP_ENTER))
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");
SDL_WM_IconifyWindow();
@ -1196,7 +1176,7 @@ Event CApplication::CreateVirtualEvent(const Event& sourceEvent)
virtualEvent.key.key = GetVirtualKey(sourceEvent.key.key);
virtualEvent.key.virt = true;
if(virtualEvent.key.key == sourceEvent.key.key)
if (virtualEvent.key.key == sourceEvent.key.key)
virtualEvent.type = EVENT_NULL;
}
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));
}
catch(...)
catch (...)
{
GetLogger()->Warn("Failed to update locale, possibly incorect system configuration. Will fallback to classic locale.\n");
try
{
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");
}

View File

@ -210,7 +210,7 @@ public:
//! Cleans up before exit
void Destroy();
//! Restart
void Restart();
//! Should we restart after app quits?
@ -321,7 +321,7 @@ public:
void StopPerformanceCounter(PerformanceCounter counter);
float GetPerformanceCounterData(PerformanceCounter counter) const;
//@}
bool GetSceneTestMode();
protected:
@ -354,23 +354,23 @@ protected:
protected:
//! Private (SDL-dependent data)
ApplicationPrivate* m_private;
std::unique_ptr<ApplicationPrivate> m_private;
//! Global event queue
CEventQueue* m_eventQueue;
std::unique_ptr<CEventQueue> m_eventQueue;
//! Graphics engine
Gfx::CEngine* m_engine;
std::unique_ptr<Gfx::CEngine> m_engine;
//! Graphics device
Gfx::CDevice* m_device;
std::unique_ptr<Gfx::CDevice> m_device;
//! Sound subsystem
CSoundInterface* m_sound;
std::unique_ptr<CSoundInterface> m_sound;
//! Game controller - game engine and UI
CController* m_controller;
std::unique_ptr<CController> m_controller;
//! Profile (INI) reader/writer
CProfile* m_profile;
std::unique_ptr<CProfile> m_profile;
//! Input manager
CInput* m_input;
std::unique_ptr<CInput> m_input;
//! Path manager
CPathManager* m_pathManager;
std::unique_ptr<CPathManager> m_pathManager;
//! Code to return at exit
int m_exitCode;
@ -430,13 +430,13 @@ protected:
std::vector<int> m_joyAxeState;
//! Current state of joystick buttons; may be updated from another thread
std::vector<bool> m_joyButtonState;
//@{
//! Scene to run on startup
std::string m_runSceneName;
int m_runSceneRank;
//@}
//! Scene test mode
bool m_sceneTest;
@ -445,10 +445,10 @@ protected:
//! Low cpu mode
bool m_lowCPU;
//! Screen resoultion overriden by commandline
bool m_resolutionOverride;
//! Headles mode
bool m_headless;
};

View File

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

View File

@ -24,11 +24,12 @@
#pragma once
#include "common/event.h"
#include "common/singleton.h"
#include <memory>
#include <string>
class CApplication;
class CRobotMain;
struct Event;
namespace Ui {
class CMainDialog;
}
@ -37,29 +38,29 @@ class CMainDialog;
* \class CController
* \brief Entry point into CRobotMain and CMainDialog
*/
class CController : public CSingleton<CController>
class CController
{
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;
};
CApplication* m_app;
std::unique_ptr<CRobotMain> m_main;
std::unique_ptr<Ui::CMainDialog> m_dialog;
};

View File

@ -39,21 +39,20 @@ FramebufferSupport DetectFramebufferSupport()
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;
else if (std::strcmp(name, "default") == 0) return new CGLDevice(config);
else if (std::strcmp(name, "opengl") == 0) return new CGLDevice(config);
else if (std::strcmp(name, "gl14") == 0) return new CGLDevice(config);
else if (std::strcmp(name, "gl21") == 0) return new CGL21Device(config);
else if (std::strcmp(name, "gl33") == 0) return new CGL33Device(config);
else if (std::strcmp(name, "auto") == 0)
if (name == "default") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (name == "opengl") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (name == "gl14") return std::unique_ptr<CDevice>{new CGLDevice(config)};
else if (name == "gl21") return std::unique_ptr<CDevice>{new CGL21Device(config)};
else if (name == "gl33") return std::unique_ptr<CDevice>{new CGL33Device(config)};
else if (name == "auto")
{
int version = GetOpenGLVersion();
if (version >= 33) return new CGL33Device(config);
else if (version >= 21) return new CGL21Device(config);
else return new CGLDevice(config);
if (version >= 33) return std::unique_ptr<CDevice>{new CGL33Device(config)};
else if (version >= 21) return std::unique_ptr<CDevice>{new CGL21Device(config)};
else return std::unique_ptr<CDevice>{new CGLDevice(config)};
}
return nullptr;

View File

@ -26,6 +26,8 @@
#include <GL/glew.h>
#include <memory>
// Graphics module namespace
namespace Gfx
@ -41,7 +43,7 @@ enum FramebufferSupport
FramebufferSupport DetectFramebufferSupport();
//! 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.
// First digit is major part, second digit is minor part.