Loading...
Searching...
No Matches
ml::FrameDispatcher Class Referenceabstract

Base class for per-frame dispatchers in the Malena event system. More...

#include <FrameDispatcher.h>

Inheritance diagram for ml::FrameDispatcher:

Public Member Functions

 FrameDispatcher ()
void fire () override=0
 Deliver the per-frame callback to all matching components.
bool occurred () override=0
 Return true when this dispatcher should fire this frame.

Detailed Description

Base class for per-frame dispatchers in the Malena event system.

FrameDispatcher is the frame-tick counterpart to EventDispatcher. Where EventDispatcher responds to SFML input events (mouse clicks, key presses, etc.), FrameDispatcher fires unconditionally once per frame — before drawing — making it the correct base for traits like Updatable that need to run every tick regardless of user input.

The constructor automatically passes DispatchType::FRAME to Fireable, so the framework routes this dispatcher through the per-frame loop rather than the per-event loop. No manual setup is needed.

The SFML-event–taking overloads inherited from Fireable are sealed final so you only need to implement the two no-argument versions.

How it fits together

The same three-piece pattern as EventDispatcher applies:

  1. A receiver class — inherits EventReceiver and exposes a friendly callback registration method that stores a callback via Fireable::addCallback.
  2. A dispatcher class — inherits FrameDispatcher and implements occurred() and fire(). Registered as a singleton via ML_EXPORT.
  3. ML_EXPORT(MyDispatcher) — placed after the class definition.

What each method does

Method Purpose
occurred() Return true when this dispatcher should fire this frame. Return false to skip the pass entirely. Typically always returns true for unconditional per-frame traits.
fire() Iterate registered components and deliver the per-frame callback.

Minimal example

// ── MyFrameTrait.h ────────────────────────────────────────────────────────
// 1. Receiver
class MyFrameTrait : public ml::EventReceiver
{
public:
void onTick(std::function<void()> f)
{
Fireable::addCallback(ml::Event::UPDATE, this,
[f](const std::optional<sf::Event>&){ f(); });
}
};
// 2. Dispatcher
class MyFrameDispatcher : public ml::FrameDispatcher
{
public:
bool occurred() override
{
return true; // fire every frame
}
void fire() override
{
for (auto* c : ml::EventsManager::getComponents())
c->process(ml::Event::UPDATE, std::nullopt);
}
};
// 3. Register the singleton
ML_EXPORT(MyFrameDispatcher)
#define ML_EXPORT(ClassName)
Register a Malena type with the framework.
Definition Export.h:38
Base class for all event-receiving traits.
virtual void process(const std::string &key, const std::optional< sf::Event > &event)
Invoke all callbacks registered for key.
void fire() override=0
Deliver the per-frame callback to all matching components.
bool occurred() override=0
Return true when this dispatcher should fire this frame.
@ UPDATE
Every frame, before drawing.
Definition Event.h:57
See also
Fireable, EventDispatcher, EventReceiver, Updatable, ML_EXPORT

Definition at line 86 of file FrameDispatcher.h.

Constructor & Destructor Documentation

◆ FrameDispatcher()

ml::FrameDispatcher::FrameDispatcher ( )
inline

Definition at line 88 of file FrameDispatcher.h.

Member Function Documentation

◆ fire()

void ml::FrameDispatcher::fire ( )
overridepure virtual

Deliver the per-frame callback to all matching components.

Called by the framework each frame after occurred() returns true. Typically iterates the component list and calls process() on each relevant component.

◆ occurred()

bool ml::FrameDispatcher::occurred ( )
overridepure virtual

Return true when this dispatcher should fire this frame.

Called once per frame by the framework. Return false to skip the entire delivery pass for this frame. Most per-frame traits simply return true unconditionally.

Returns
true if fire() should be called this frame.

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