Browse Source

Revert application and remove singleton class, it breaks object lifetime?

master
Riyyi 3 years ago
parent
commit
6103854503
  1. 6
      game/src/game.cpp
  2. 7
      inferno/src/inferno/application.cpp
  3. 21
      inferno/src/inferno/application.h
  4. 8
      inferno/src/inferno/entrypoint.h
  5. 4
      inferno/src/inferno/io/input.cpp
  6. 31
      inferno/src/inferno/singleton.h

6
game/src/game.cpp

@ -4,11 +4,11 @@
class Game : public Inferno::Application class Game : public Inferno::Application
{ {
public: public:
Game() : Application({}) {} Game() {}
~Game() {} ~Game() {}
}; };
Inferno::Application& Inferno::getApplication() Inferno::Application* Inferno::createApplication()
{ {
return Game::the(); return new Game();
} }

7
inferno/src/inferno/application.cpp

@ -21,8 +21,13 @@
namespace Inferno { namespace Inferno {
Application::Application(s) Application* Application::s_instance = nullptr;
Application::Application()
{ {
ASSERT(!s_instance, "Application already exists!");
s_instance = this;
// Initialize // Initialize
Settings::initialize(); Settings::initialize();

21
inferno/src/inferno/application.h

@ -3,8 +3,6 @@
#include <memory> // std::unique_ptr, std::shared_ptr #include <memory> // std::unique_ptr, std::shared_ptr
#include "inferno/singleton.h"
namespace Inferno { namespace Inferno {
class Event; class Event;
@ -16,9 +14,9 @@ namespace Inferno {
class WindowCloseEvent; class WindowCloseEvent;
class WindowResizeEvent; class WindowResizeEvent;
class Application : public Singleton<Application> { class Application {
public: public:
Application(s); Application();
virtual ~Application(); virtual ~Application();
int run(); int run();
@ -33,20 +31,23 @@ namespace Inferno {
inline Window& getWindow() const { return *m_window; } inline Window& getWindow() const { return *m_window; }
static inline Application& the() { return *s_instance; }
private: private:
int m_status = 0; int m_status { 0 };
float m_lastFrameTime { 0.0f }; float m_lastFrameTime { 0.0f };
std::unique_ptr<Window> m_window { nullptr }; std::unique_ptr<Window> m_window;
std::shared_ptr<Scene> m_scene { nullptr }; std::shared_ptr<Scene> m_scene;
// //
std::shared_ptr<Font> m_font { nullptr }; std::shared_ptr<Font> m_font;
//
static Application* s_instance;
}; };
// To be defined in the game // To be defined in the game
extern Application& getApplication(); extern Application* createApplication();
} }

8
inferno/src/inferno/entrypoint.h

@ -15,7 +15,13 @@ int main(int argc, char* argv[])
(void)argc; (void)argc;
(void)argv; (void)argv;
return Inferno::getApplication().run(); auto app = Inferno::createApplication();
int status = app->run();
delete app;
return status;
} }
#endif // ENTRYPOINT_H #endif // ENTRYPOINT_H

4
inferno/src/inferno/io/input.cpp

@ -16,6 +16,10 @@ namespace Inferno {
void Input::initialize() void Input::initialize()
{ {
// Set cursor in the middle of the screen
m_xPosLast = Application::the().getWindow().getWidth() / 2.0f;
m_yPosLast = Application::the().getWindow().getHeight() / 2.0f;
info() << "Input initialized"; info() << "Input initialized";
} }

31
inferno/src/inferno/singleton.h

@ -1,31 +0,0 @@
#ifndef SINGLETON_H
#define SINGLETON_H
#include <memory> // std::unique_ptr
namespace Inferno {
template<typename T>
class Singleton {
public:
static inline T& the() {
static const std::unique_ptr<T> instance { new T { s {} } };
return *instance;
}
// Remove copy constructor and copy assignment operator
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(Singleton&&) = delete;
protected:
Singleton() {}
// Constructor token
struct s {};
};
} // namespace Inferno
#endif // SINGLETON_H
Loading…
Cancel
Save