Browse Source

Engine+Example: Improve entrypoint via manual singleton

master
Riyyi 2 years ago
parent
commit
ae8bb46a7d
  1. 27
      example/src/game.cpp
  2. 19
      example/src/game.h
  3. 14
      src/inferno/application.cpp
  4. 15
      src/inferno/application.h
  5. 10
      src/inferno/entrypoint.h

27
example/src/game.cpp

@ -4,17 +4,26 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include "inferno.h" #include "game.h"
#include "inferno/entrypoint.h"
class Game : public Inferno::Application Game::Game()
: Application()
{ {
public: }
Game() : Application({}) {}
~Game() {} Game::~Game()
}; {
}
void Game::update()
{
}
void Game::render()
{
}
Inferno::Application& Inferno::createApplication() Inferno::Application* Inferno::createApplication(int argc, char* argv[])
{ {
return Game::the(); return new Game;
} }

19
example/src/game.h

@ -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[]);

14
src/inferno/application.cpp

@ -31,8 +31,14 @@
namespace Inferno { 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 // Initialize
Settings::initialize(); Settings::initialize();
@ -78,6 +84,8 @@ Application::~Application()
// Input::destroy(); // Input::destroy();
Settings::destroy(); Settings::destroy();
s_instance = nullptr;
} }
int Application::run() int Application::run()
@ -143,12 +151,16 @@ int Application::run()
// Update // Update
update();
Input::update(); Input::update();
m_window->update(); m_window->update();
m_scene->update(deltaTime); m_scene->update(deltaTime);
// Render // Render
render();
RenderCommand::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f }); RenderCommand::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f });
RenderCommand::clear(); RenderCommand::clear();

15
src/inferno/application.h

@ -21,11 +21,13 @@ class Window;
class WindowCloseEvent; class WindowCloseEvent;
class WindowResizeEvent; class WindowResizeEvent;
class Application : public ruc::Singleton<Application> { class Application {
public: public:
explicit Application(s);
virtual ~Application(); virtual ~Application();
virtual void update() = 0;
virtual void render() = 0;
int run(); int run();
void onEvent(Event& e); void onEvent(Event& e);
@ -38,6 +40,11 @@ public:
inline Window& getWindow() const { return *m_window; } inline Window& getWindow() const { return *m_window; }
static Application& the() { return *s_instance; }
protected:
Application();
private: private:
int m_status { 0 }; int m_status { 0 };
float m_lastFrameTime { 0.0f }; float m_lastFrameTime { 0.0f };
@ -48,10 +55,12 @@ private:
// //
std::shared_ptr<Font> m_font; std::shared_ptr<Font> m_font;
// //
static Application* s_instance;
}; };
// To be defined in the game // To be defined in the game
extern Application& createApplication(); extern Application* createApplication(int argc, char* argv[]);
} // namespace Inferno } // namespace Inferno

10
src/inferno/entrypoint.h

@ -16,15 +16,11 @@
int main(int argc, char* argv[]) // NOLINT(misc-definitions-in-headers) int main(int argc, char* argv[]) // NOLINT(misc-definitions-in-headers)
{ {
// Suppress unused warning auto* app = Inferno::createApplication(argc, argv);
(void)argc;
(void)argv;
auto& app = Inferno::createApplication(); int status = app->run();
int status = app.run(); delete app;
app.destroy();
return status; return status;
} }

Loading…
Cancel
Save