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

Drives the application main loop and coordinates all framework managers. More...

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

Inheritance diagram for ml::AppManager:
[legend]

Public Member Functions

 AppManager (const sf::VideoMode &videoMode, const std::string &title, sf::RenderWindow &window=WindowManager::getWindow())
 Construct an AppManager and create the SFML window.
virtual ~AppManager ()=default
void addComponent (Core &component)
 Register a T object with this manager.
void clear ()
 Remove all registered objects.
const std::vector< Core * > & getComponents () const
 Return a read-only view of all currently registered objects.
virtual void onInit ()
 Called once after construction, before the first frame.
virtual void onReady ()
 Called once after onInit(), when all components are registered.
bool removeComponent (Core &component)
 Unregister a T object by reference.
void run ()
 Enter the main loop and run until the window is closed.

Static Public Member Functions

static void clearPending ()
 Discard all pending operations without executing them.
static bool isBusy ()
 Return true if the manager is currently iterating.
static void processPending ()
 Flush all pending operations immediately.

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

Drives the application main loop and coordinates all framework managers.

AppManager owns the SFML render window and runs the core application lifecycle:

  1. Calls Lifecycle::onInit() then Lifecycle::onReady() before entering the loop.
  2. Each frame, polls SFML events and distributes them via fireInputEvents().
  3. Fires the per-frame update event via fireUpdateEvents().
  4. Clears the window, calls draw(), and presents the frame.

AppManager implements the Manager interface, so it participates in the same event-distribution protocol as other framework managers.

Architecture modes

The Architecture enum lets the framework know which structural style the application uses.

Value Meaning
MVC Model-View-Controller — default for most UI applications
EDA Event-Driven Architecture — pure message/event flow
ECS Entity-Component-System — activates the ECS subsystem

Typical use

Most applications do not instantiate AppManager directly. Instead, inherit from ml::Application, which wraps AppManager into a single convenient base class.

For the common case, subclass ml::Application and override onInit() and onReady() directly:

class MyApp : public ml::Application
{
public:
MyApp() : ml::Application(1280, 720, 32, "My App") {}
void onInit() override { _box.setSize({200.f, 100.f}); addComponent(_box); }
void onReady() override { _box.onClick([]{ std::cout << "clicked!\n"; }); }
private:
ml::Rectangle _box;
};
void addComponent(Core &component)
Register a Core object with the application's component manager.
Primary entry point for Malena applications without a manifest.
virtual void onInit()
Called once after construction, before the first frame.
Definition Lifecycle.h:82
virtual void onReady()
Called once after onInit(), when all components are registered.
Definition Lifecycle.h:90
Definition Component.h:22
See also
Application, Manager, WindowManager

Definition at line 71 of file AppManager.h.

Constructor & Destructor Documentation

◆ AppManager()

ml::AppManager::AppManager ( const sf::VideoMode & videoMode,
const std::string & title,
sf::RenderWindow & window = WindowManager::getWindow() )

Construct an AppManager and create the SFML window.

onInit() and onReady() are called by run() before the main loop begins, not in the constructor. The window parameter defaults to WindowManager::getWindow() so the framework's centralized window is used unless an explicit one is provided.

Parameters
videoModeSFML video mode (resolution + bit depth).
titleWindow title string.
windowRender window to use. Defaults to the framework window.

◆ ~AppManager()

virtual ml::AppManager::~AppManager ( )
virtualdefault

Member Function Documentation

◆ addComponent()

void ml::CoreManager< Core >::addComponent ( Core & component)
inherited

Register a T object with this manager.

After registration, the object will receive update() and draw() calls each frame (for managers that drive those methods).

Parameters
componentThe object to register. Must outlive its registration.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ run()

void ml::AppManager::run ( )

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

Each iteration of the loop:

  1. Polls all pending SFML events and passes each to fireInputEvents().
  2. Calls fireUpdateEvents() for the per-frame update tick.
  3. Clears the window, calls draw() on all registered components, and displays the frame.

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

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: