Engine+Example: Improve entrypoint via manual singleton
This commit is contained in:
+20
-11
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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[]);
|
||||
@@ -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