Engine+Example: Improve entrypoint via manual singleton

This commit is contained in:
Riyyi
2022-09-26 13:07:05 +02:00
parent 1dcb25ea5e
commit ae8bb46a7d
5 changed files with 67 additions and 22 deletions
+20 -11
View File
@@ -4,17 +4,26 @@
* SPDX-License-Identifier: MIT
*/
#include "inferno.h"
#include "inferno/entrypoint.h"
#include "game.h"
class Game : public Inferno::Application
Game::Game()
: Application()
{
public:
Game() : Application({}) {}
~Game() {}
};
Inferno::Application& Inferno::createApplication()
{
return Game::the();
}
Game::~Game()
{
}
void Game::update()
{
}
void Game::render()
{
}
Inferno::Application* Inferno::createApplication(int argc, char* argv[])
{
return new Game;
}
+19
View File
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include "inferno.h"
#include "inferno/entrypoint.h"
class Game final : public Inferno::Application {
public:
Game();
~Game();
void update() override;
void render() override;
};
Inferno::Application* Inferno::createApplication(int argc, char* argv[]);
+13 -1
View File
@@ -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();
+12 -3
View File
@@ -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
+3 -7
View File
@@ -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;
}