Application

template<WindowImplementationConcept WindowImplementation = DefaultWindowImplementation, Dispatchable EventDispatcher = Dispatcher, template<typename> typename Renderer = DefaultRendererImplementation>
class Application

Provides the entry point for any Obscura game or application.

Template Parameters:
  • WindowImplementation – The implementation of the window to use. Defaults to DefaultWindowImplementation.

  • EventDispatcher – The dispatcher to use for the window. Defaults to obscura::Dispatcher.

Public Types

using WindowType = Window<WindowImplementation>

Public Functions

inline Application()
inline void run()

This will start the main loop. This is a blocking call.

inline void onTick([[maybe_unused]] const TickEvent &event)

Callback that is triggered with every main loop iteration. This callback updates the window. Don’t call this function manually.

Parameters:

event – The tick event.

inline void stopMainLoop()

This will stop the main loop and hence the application. Can only be called from another thread as the run function is a blocking call.

inline auto getWindow() const -> WindowType&
inline auto getMainLoop() const -> MainLoop<Dispatcher>&
inline auto getWindowEventEmitter() const -> auto&

Examples

Opening a window

#include <obscura/obscura.hxx>

auto main() -> int {
    try {
        auto application = obscura::Application();
        application.run();
    } catch (const std::exception& e) {
        obscura::logError("Exception caught: {}", e.what());
    }
    return 0;
}

Opening a window and registering simple listeners

#include <iostream>
#include <obscura/obscura.hxx>

using namespace obscura;

template<class Application>
class InputListener: public DefaultApplicationListener<Application> {
  public:
    explicit InputListener(Application& app)
      : DefaultApplicationListener<Application>(app) {
    }

    [[maybe_unused]] void onKeyPressed(const KeyEvent<obscura::EventCause::KeyPressed>& event) {
        logInfo("Key pressed: {}", event.getKey());
    }

    [[maybe_unused]] void onKeyReleased(const KeyEvent<obscura::EventCause::KeyReleased>& event) {
        logInfo("Key released: {}", event.getKey());
    }

    [[maybe_unused]] void onMouseMoved(const MouseMovedEvent& event) {
        logInfo("Mouse moved to x: {}, y: {}", event.getCurrentPosition().x, event.getCurrentPosition().y);
    }
};

auto main() -> int {
    try {
        using CustomApplication = obscura::Application<DefaultWindowImplementation, Dispatcher>;
        CustomApplication application;
        auto defaultApplicationListener = DefaultApplicationListener<CustomApplication>(application);
        Scheduler<Dispatcher>::getInstance().registerListener(defaultApplicationListener);

        InputListener<CustomApplication> inputListener(application);
        Scheduler<Dispatcher>::getInstance().registerListener(inputListener);

        application.run();
    } catch (const std::exception& e) {
        logError("Exception caught: {}", e.what());
    }
    return EXIT_SUCCESS;
}