Loading...
Searching...
No Matches
ml::ApplicationBase Class Reference

Internal base class for all Malena applications. More...

#include <Malena/Engine/App/Application.h>

Inheritance diagram for ml::ApplicationBase:
[legend]

Public Types

enum  Architecture { MVC , EDA , ECS }
 Architectural style hint passed at construction. More...

Public Member Functions

 ApplicationBase (const sf::VideoMode &videoMode, const std::string &title, sf::RenderWindow &window=WindowManager::getWindow(), AppManager::Architecture architecture=AppManager::MVC, std::uint32_t windowStyle=sf::Style::Default)
 Construct with an explicit video mode and window title.
 ApplicationBase (unsigned int screenWidth, unsigned int screenHeight, unsigned int bitDepth, const std::string &title, std::uint32_t windowStyle=sf::Style::Default)
 Construct from pixel dimensions and bit depth.
 ~ApplicationBase ()
void addComponent (Core &component)
 Register a Core object with the application's component manager.
void clear ()
 Remove all registered objects.
Architecture getArchitecture () const
 Return the architectural mode set at construction.
const std::vector< Core * > & getComponents () const
 Return a read-only view of all currently registered objects.
bool isPaused () const
 Return true if the application is currently paused.
void onClose (std::function< bool()> handler)
 Register a callback invoked when the OS close button is pressed.
virtual void onInit ()
 Called once after construction, before the first frame.
void onPostRender (std::function< void()> hook)
 Register a callback invoked after all components are drawn and before display().
void onPreRender (std::function< void()> hook)
 Register a callback invoked after clear() and before drawing components.
virtual void onReady ()
 Called once after onInit(), when all components are registered.
void onResize (std::function< void(unsigned int, unsigned int)> handler)
 Register a callback invoked whenever the window is resized.
void onUpdate (std::function< void()> callback, bool overwrite=false)
 Register a no-argument callback invoked every frame.
void onUpdate (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=false)
 Register a callback invoked every frame, receiving the SFML event.
void onWindowFocusGained (std::function< void()> callback, bool overwrite=false)
 Register a no-argument callback invoked when the application window gains OS focus.
void onWindowFocusGained (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=false)
 Register a callback invoked when the application window gains OS focus, receiving the raw SFML event.
void onWindowFocusLost (std::function< void()> callback, bool overwrite=false)
 Register a no-argument callback invoked when the application window loses OS focus.
void onWindowFocusLost (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=false)
 Register a callback invoked when the application window loses OS focus, receiving the raw SFML event.
void onWindowResized (std::function< void()> callback, bool overwrite=false)
 Register a no-argument callback invoked when the application window is resized.
void onWindowResized (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=false)
 Register a callback invoked when the application window is resized, receiving the raw SFML event.
void pause ()
 Suspend per-frame update events.
virtual void process (const std::string &key, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for key.
template<typename ENUM_TYPE>
void process (ENUM_TYPE eventName, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for eventName.
bool removeComponent (Core &component)
 Unregister a T object by reference.
void reset ()
 Full application reset — clears events, messages, and components.
void resume ()
 Resume update events after a pause().
void run ()
 Enter the main loop and run until the window is closed.
void setBackgroundColor (sf::Color color)
 Set the colour used to clear the window at the start of each frame.
void setFramerateLimit (unsigned int limit)
 Cap the frame rate.
void setIcon (const sf::Image &icon)
 Set the window icon from an sf::Image.
void setTitle (const std::string &title)
 Change the window title at runtime.
void setVSync (bool enabled)
 Enable or disable vertical synchronisation.
void setWindowPosition (int x, int y)
 Move the window to a position on the desktop.
void setWindowStyle (std::uint32_t style)
 Recreate the window with new decoration-style flags.

Static Public Member Functions

static void clearEvents ()
 Clear all event subscriptions across the entire application.
static void clearExclusiveOwner ()
 Lift any active exclusive-owner restriction.
static void clearPending ()
 Discard all pending operations without executing them.
static CoreexclusiveOwner ()
 The current exclusive-interaction owner, or nullptr.
static float getDeltaTime ()
 Return the elapsed time of the previous frame in seconds.
static bool isBusy ()
 Return true if the manager is currently iterating.
static void processPending ()
 Flush all pending operations immediately.
static void setExclusiveOwner (Core *owner)
 Restrict click and hover dispatch to owner and its descendants. Any component outside that subtree silently receives no events. Pass nullptr (or call clearExclusiveOwner) to lift the restriction.

Static Protected Member Functions

static void beginBusy ()
 Signal that iteration has begun.
static void deferOrExecute (std::function< void()> operation)
 Execute operation now if safe, otherwise queue it.
static void endBusy ()
 Signal that iteration has ended; flush pending operations.

Static Protected Attributes

static int busyDepth
 Iteration nesting depth. Operations are deferred while this is > 0.
static std::vector< std::function< void()> > pendingOperations
 Queue of operations pending until the current iteration completes.

Detailed Description

Internal base class for all Malena applications.

Do not inherit directly. Use ml::Application or ml::ApplicationWith.

See also
Application, ApplicationWith

Definition at line 36 of file Application.h.

Member Enumeration Documentation

◆ Architecture

Architectural style hint passed at construction.

Informs the framework which structural pattern the application follows. Defaults to MVC.

Enumerator
MVC 

Model-View-Controller.

EDA 

Event-Driven Architecture.

ECS 

Entity-Component-System.

Definition at line 116 of file AppManager.h.

Constructor & Destructor Documentation

◆ ApplicationBase() [1/2]

ml::ApplicationBase::ApplicationBase ( const sf::VideoMode & videoMode,
const std::string & title,
sf::RenderWindow & window = WindowManager::getWindow(),
AppManager::Architecture architecture = AppManager::MVC,
std::uint32_t windowStyle = sf::Style::Default )

Construct with an explicit video mode and window title.

Parameters
videoModeWindow size/bit-depth as an sf::VideoMode.
titleWindow title bar text.
windowRender window to drive. Defaults to the shared WindowManager window.
architectureApp architecture (AppManager::MVC by default).
windowStyleSFML style flags (e.g. sf::Style::Default, sf::Style::None). Defaults to sf::Style::Default.

◆ ApplicationBase() [2/2]

ml::ApplicationBase::ApplicationBase ( unsigned int screenWidth,
unsigned int screenHeight,
unsigned int bitDepth,
const std::string & title,
std::uint32_t windowStyle = sf::Style::Default )

Construct from pixel dimensions and bit depth.

Parameters
screenWidthWindow width in pixels.
screenHeightWindow height in pixels.
bitDepthColor bit depth (e.g. 32).
titleWindow title bar text.
windowStyleSFML style flags. Defaults to sf::Style::Default.

◆ ~ApplicationBase()

ml::ApplicationBase::~ApplicationBase ( )

Member Function Documentation

◆ addComponent()

void ml::ApplicationBase::addComponent ( Core & component)

Register a Core object with the application's component manager.

◆ beginBusy()

void ml::DeferredOperationsManager< CoreManager< Core > >::beginBusy ( )
staticprotectedinherited

Signal that iteration has begun.

Increments the busy-depth counter. Multiple nested calls are safe — operations remain deferred until the outermost endBusy() is reached.

◆ clear()

void ml::CoreManager< Core >::clear ( )
inherited

Remove all registered objects.

If the collection is currently being iterated, the clear is deferred. After this call (or after the deferred clear executes), getComponents() returns an empty vector.

◆ clearEvents()

void ml::ApplicationBase::clearEvents ( )
static

Clear all event subscriptions across the entire application.

◆ clearExclusiveOwner()

void ml::AppManager::clearExclusiveOwner ( )
staticinherited

Lift any active exclusive-owner restriction.

◆ clearPending()

Discard all pending operations without executing them.

Use during application shutdown or full reset when executing the queued operations would be unsafe (e.g., the objects they reference have already been destroyed).

◆ deferOrExecute()

void ml::DeferredOperationsManager< CoreManager< Core > >::deferOrExecute ( std::function< void()> operation)
staticprotectedinherited

Execute operation now if safe, otherwise queue it.

If busyDepth > 0 the operation is appended to pendingOperations and will run when the current iteration completes. If the manager is idle the operation executes immediately.

Parameters
operationThe callable to execute or defer.

◆ endBusy()

void ml::DeferredOperationsManager< CoreManager< Core > >::endBusy ( )
staticprotectedinherited

Signal that iteration has ended; flush pending operations.

Decrements the busy-depth counter. When the counter reaches zero, all operations in pendingOperations are executed in FIFO order and the queue is cleared.

◆ exclusiveOwner()

Core * ml::AppManager::exclusiveOwner ( )
inlinestaticinherited

The current exclusive-interaction owner, or nullptr.

Definition at line 330 of file AppManager.h.

◆ getArchitecture()

Architecture ml::AppManager::getArchitecture ( ) const
inlineinherited

Return the architectural mode set at construction.

Definition at line 317 of file AppManager.h.

◆ getComponents()

const std::vector< Core * > & ml::CoreManager< Core >::getComponents ( ) const
nodiscardinherited

Return a read-only view of all currently registered objects.

The returned reference is valid until the next structural modification (add or remove). Do not store the reference across frames.

Returns
Const reference to the internal component pointer vector.

◆ getDeltaTime()

float ml::AppManager::getDeltaTime ( )
inlinestaticinherited

Return the elapsed time of the previous frame in seconds.

Updated at the start of every main-loop iteration. Safe to call from any onUpdate callback.

Definition at line 257 of file AppManager.h.

◆ isBusy()

bool ml::DeferredOperationsManager< CoreManager< Core > >::isBusy ( )
staticinherited

Return true if the manager is currently iterating.

Can be used by external code that needs to know whether a destructive operation will be deferred.

Returns
true when busyDepth > 0.

◆ isPaused()

bool ml::AppManager::isPaused ( ) const
inlineinherited

Return true if the application is currently paused.

Definition at line 274 of file AppManager.h.

◆ onClose()

void ml::AppManager::onClose ( std::function< bool()> handler)
inherited

Register a callback invoked when the OS close button is pressed.

Parameters
handlerCallable with signature bool(). Return true to allow the window to close (default behaviour), or false to cancel the close (e.g. show a "save changes?" dialog).

◆ onInit()

virtual void ml::Lifecycle::onInit ( )
inlinevirtualinherited

Called once after construction, before the first frame.

Override to perform initial setup — configure component properties, load resources, and register objects with the framework. At this point no other sibling components are guaranteed to be fully registered.

Definition at line 82 of file Lifecycle.h.

◆ onPostRender()

void ml::AppManager::onPostRender ( std::function< void()> hook)
inherited

Register a callback invoked after all components are drawn and before display().

Use this for post-processing effects, HUD overlays, or any content that must appear on top of all components.

Parameters
hookCallable with signature void().

◆ onPreRender()

void ml::AppManager::onPreRender ( std::function< void()> hook)
inherited

Register a callback invoked after clear() and before drawing components.

Use this to draw background layers, debug overlays, or ImGui frames that should appear beneath the component layer.

Parameters
hookCallable with signature void().

◆ onReady()

virtual void ml::Lifecycle::onReady ( )
inlinevirtualinherited

Called once after onInit(), when all components are registered.

Override to wire logic that depends on other components already existing — event callbacks, cross-component bindings, or derived initial state.

Definition at line 90 of file Lifecycle.h.

◆ onResize()

void ml::AppManager::onResize ( std::function< void(unsigned int, unsigned int)> handler)
inherited

Register a callback invoked whenever the window is resized.

Parameters
handlerCallable with signature void(unsigned int width, unsigned int height).

◆ onUpdate() [1/2]

void ml::Updatable::onUpdate ( std::function< void()> callback,
bool overwrite = false )
inherited

Register a no-argument callback invoked every frame.

The callback fires once per frame before the draw pass. All active components receive this regardless of hover or focus state.

Parameters
callbackFunction invoked with no arguments each frame.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onUpdate() [2/2]

void ml::Updatable::onUpdate ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = false )
inherited

Register a callback invoked every frame, receiving the SFML event.

For frame events the std::optional<sf::Event> is always std::nullopt — use the no-argument overload unless you specifically need the optional parameter in your callback signature.

Parameters
callbackFunction invoked with the SFML event (always nullopt) each frame.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onWindowFocusGained() [1/2]

void ml::Updatable::onWindowFocusGained ( std::function< void()> callback,
bool overwrite = false )
inherited

Register a no-argument callback invoked when the application window gains OS focus.

Parameters
callbackFunction invoked with no arguments when focus is gained.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onWindowFocusGained() [2/2]

void ml::Updatable::onWindowFocusGained ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = false )
inherited

Register a callback invoked when the application window gains OS focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event when focus is gained.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onWindowFocusLost() [1/2]

void ml::Updatable::onWindowFocusLost ( std::function< void()> callback,
bool overwrite = false )
inherited

Register a no-argument callback invoked when the application window loses OS focus.

Parameters
callbackFunction invoked with no arguments when focus is lost.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onWindowFocusLost() [2/2]

void ml::Updatable::onWindowFocusLost ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = false )
inherited

Register a callback invoked when the application window loses OS focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event when focus is lost.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onWindowResized() [1/2]

void ml::Updatable::onWindowResized ( std::function< void()> callback,
bool overwrite = false )
inherited

Register a no-argument callback invoked when the application window is resized.

Parameters
callbackFunction invoked with no arguments on window resize.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onWindowResized() [2/2]

void ml::Updatable::onWindowResized ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = false )
inherited

Register a callback invoked when the application window is resized, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event on window resize.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ pause()

void ml::AppManager::pause ( )
inherited

Suspend per-frame update events.

After pause(), fireUpdateEvents() is skipped each iteration. The render loop continues running so the window stays responsive and the last rendered frame remains visible.

◆ process() [1/2]

virtual void ml::EventReceiver::process ( const std::string & key,
const std::optional< sf::Event > & event )
virtualinherited

Invoke all callbacks registered for key.

Called by framework dispatchers. key is the string produced by EnumKey::get(eventEnum).

Parameters
keyString key identifying the event.
eventSFML event forwarded to each callback.

◆ process() [2/2]

template<typename ENUM_TYPE>
void ml::EventReceiver::process ( ENUM_TYPE eventName,
const std::optional< sf::Event > & event )
inherited

Invoke all callbacks registered for eventName.

Template overload that accepts any enum value directly, converting to a string key via EnumKey::get() internally.

Template Parameters
ENUM_TYPEAny enum type.
Parameters
eventNameThe enum value identifying the event.
eventSFML event forwarded to each callback.

◆ processPending()

Flush all pending operations immediately.

Normally called automatically by endBusy(). Provided as a public escape hatch for unusual teardown sequences.

Warning
Calling this while isBusy() is true can cause iterator invalidation in the currently running loop. Only call manually when you are certain it is safe.

◆ removeComponent()

bool ml::CoreManager< Core >::removeComponent ( Core & component)
inherited

Unregister a T object by reference.

Safe to call from inside an event or update callback. If the collection is currently being iterated, the removal is deferred until iteration completes.

Parameters
componentThe object to remove.
Returns
true if the object was found and removed (or queued for removal); false if it was not registered.

◆ reset()

void ml::ApplicationBase::reset ( )

Full application reset — clears events, messages, and components.

Tears down all framework state in the correct order:

  1. All event subscriptions
  2. All message subscriptions
  3. All registered components

After reset(), call onInit() and onReady() to rebuild the scene.

Note
Does NOT close the window or restart the main loop.

◆ resume()

void ml::AppManager::resume ( )
inherited

Resume update events after a pause().

◆ run()

void ml::AppManager::run ( )
inherited

Enter the main loop and run until the window is closed.

Each iteration of the loop:

  1. Measures delta time for the previous frame.
  2. Polls all pending SFML events (resize and close are handled internally).
  3. Calls fireUpdateEvents() for the per-frame update tick (skipped when paused).
  4. Clears the window, invokes onPreRender, draws all visible components, invokes onPostRender, and presents the frame.
  5. Flushes any pending deferred-unload operations.

Returns when the SFML window is closed or window.close() is called.

◆ setBackgroundColor()

void ml::AppManager::setBackgroundColor ( sf::Color color)
inherited

Set the colour used to clear the window at the start of each frame.

Parameters
colorAny sf::Color value. Defaults to sf::Color::Black.

◆ setExclusiveOwner()

void ml::AppManager::setExclusiveOwner ( Core * owner)
staticinherited

Restrict click and hover dispatch to owner and its descendants. Any component outside that subtree silently receives no events. Pass nullptr (or call clearExclusiveOwner) to lift the restriction.

◆ setFramerateLimit()

void ml::AppManager::setFramerateLimit ( unsigned int limit)
inherited

Cap the frame rate.

Calls sf::Window::setFramerateLimit. Pass 0 to remove the cap.

Parameters
limitMaximum frames per second.

◆ setIcon()

void ml::AppManager::setIcon ( const sf::Image & icon)
inherited

Set the window icon from an sf::Image.

Parameters
iconAn sf::Image loaded with your desired icon pixels.

◆ setTitle()

void ml::AppManager::setTitle ( const std::string & title)
inherited

Change the window title at runtime.

Parameters
titleNew UTF-8 window title string.

◆ setVSync()

void ml::AppManager::setVSync ( bool enabled)
inherited

Enable or disable vertical synchronisation.

Parameters
enabledtrue to enable VSync, false to disable.

◆ setWindowPosition()

void ml::AppManager::setWindowPosition ( int x,
int y )
inherited

Move the window to a position on the desktop.

Parameters
xHorizontal position in desktop pixels.
yVertical position in desktop pixels.

◆ setWindowStyle()

void ml::AppManager::setWindowStyle ( std::uint32_t style)
inherited

Recreate the window with new decoration-style flags.

Equivalent to calling sf::Window::create again with the stored video-mode and title but a new style. Any previously configured framerate limit is re-applied automatically.

Parameters
styleCombination of sf::Style flags, e.g. sf::Style::Titlebar | sf::Style::Close.

Member Data Documentation

◆ busyDepth

int ml::DeferredOperationsManager< CoreManager< Core > >::busyDepth
inlinestaticprotectedinherited

Iteration nesting depth. Operations are deferred while this is > 0.

Definition at line 70 of file DeferredOperationsManager.h.

◆ pendingOperations

std::vector<std::function<void()> > ml::DeferredOperationsManager< CoreManager< Core > >::pendingOperations
inlinestaticprotectedinherited

Queue of operations pending until the current iteration completes.

Definition at line 73 of file DeferredOperationsManager.h.


The documentation for this class was generated from the following file: