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
{
public:
Game() : Application({}) {}
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 {
Application::Application(s)
Application* Application::s_instance = nullptr;
Application::Application()
{
ASSERT(!s_instance, "Application already exists!");
s_instance = this;
// Initialize
Settings::initialize();

21
inferno/src/inferno/application.h

@ -3,8 +3,6 @@
#include <memory> // std::unique_ptr, std::shared_ptr
#include "inferno/singleton.h"
namespace Inferno {
class Event;
@ -16,9 +14,9 @@ namespace Inferno {
class WindowCloseEvent;
class WindowResizeEvent;
class Application : public Singleton<Application> {
class Application {
public:
Application(s);
Application();
virtual ~Application();
int run();
@ -33,20 +31,23 @@ namespace Inferno {
inline Window& getWindow() const { return *m_window; }
static inline Application& the() { return *s_instance; }
private:
int m_status = 0;
int m_status { 0 };
float m_lastFrameTime { 0.0f };
std::unique_ptr<Window> m_window { nullptr };
std::shared_ptr<Scene> m_scene { nullptr };
std::unique_ptr<Window> m_window;
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
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)argv;
return Inferno::getApplication().run();
auto app = Inferno::createApplication();
int status = app->run();
delete app;
return status;
}
#endif // ENTRYPOINT_H

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

@ -16,6 +16,10 @@ namespace Inferno {
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";
}

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