Engine+Example: Improve entrypoint via manual singleton
This commit is contained in:
@@ -31,8 +31,14 @@
|
||||
|
||||
namespace Inferno {
|
||||
|
||||
Application::Application(s)
|
||||
Application* Application::s_instance = nullptr;
|
||||
|
||||
Application::Application()
|
||||
{
|
||||
// Set singleton instance
|
||||
VERIFY(!s_instance, "reinstantiation of Application");
|
||||
s_instance = this;
|
||||
|
||||
// Initialize
|
||||
|
||||
Settings::initialize();
|
||||
@@ -78,6 +84,8 @@ Application::~Application()
|
||||
// Input::destroy();
|
||||
|
||||
Settings::destroy();
|
||||
|
||||
s_instance = nullptr;
|
||||
}
|
||||
|
||||
int Application::run()
|
||||
@@ -143,12 +151,16 @@ int Application::run()
|
||||
|
||||
// Update
|
||||
|
||||
update();
|
||||
|
||||
Input::update();
|
||||
m_window->update();
|
||||
m_scene->update(deltaTime);
|
||||
|
||||
// Render
|
||||
|
||||
render();
|
||||
|
||||
RenderCommand::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f });
|
||||
RenderCommand::clear();
|
||||
|
||||
|
||||
@@ -21,11 +21,13 @@ class Window;
|
||||
class WindowCloseEvent;
|
||||
class WindowResizeEvent;
|
||||
|
||||
class Application : public ruc::Singleton<Application> {
|
||||
class Application {
|
||||
public:
|
||||
explicit Application(s);
|
||||
virtual ~Application();
|
||||
|
||||
virtual void update() = 0;
|
||||
virtual void render() = 0;
|
||||
|
||||
int run();
|
||||
|
||||
void onEvent(Event& e);
|
||||
@@ -38,6 +40,11 @@ public:
|
||||
|
||||
inline Window& getWindow() const { return *m_window; }
|
||||
|
||||
static Application& the() { return *s_instance; }
|
||||
|
||||
protected:
|
||||
Application();
|
||||
|
||||
private:
|
||||
int m_status { 0 };
|
||||
float m_lastFrameTime { 0.0f };
|
||||
@@ -48,10 +55,12 @@ private:
|
||||
//
|
||||
std::shared_ptr<Font> m_font;
|
||||
//
|
||||
|
||||
static Application* s_instance;
|
||||
};
|
||||
|
||||
// To be defined in the game
|
||||
extern Application& createApplication();
|
||||
extern Application* createApplication(int argc, char* argv[]);
|
||||
|
||||
} // namespace Inferno
|
||||
|
||||
|
||||
@@ -16,15 +16,11 @@
|
||||
|
||||
int main(int argc, char* argv[]) // NOLINT(misc-definitions-in-headers)
|
||||
{
|
||||
// Suppress unused warning
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
auto* app = Inferno::createApplication(argc, argv);
|
||||
|
||||
auto& app = Inferno::createApplication();
|
||||
int status = app->run();
|
||||
|
||||
int status = app.run();
|
||||
|
||||
app.destroy();
|
||||
delete app;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user