A tour of DreamEngine

When you click to swing a sword in Revenants, who decides that the Ember Lord loses health, and who redraws his health bar? Follow that click through the game and you can see how to build your own. DreamEngine runs the frame. Your scene decides who participates in it. A small C++ interface called a protocol lets an engine component work with a class you wrote.

Start in main with Application

The entry point for Revenants fits in two lines of C++ inside main.

Revenants::RevenantsExecutable game;
game.execute();

RevenantsExecutable inherits from DreamEngine::Application. Its constructor reads the window settings and passes them to the base class. Application creates the SDL window, renderer and camera, and queues the engine’s opening presentation. The game then loads its artwork and queues its own EmberScene. By the time execute() starts, the engine knows what to show first and what should follow. Game startup · Application implementation

Inside execute(), one frame runs in this order.

  1. Poll SDL for pending events. For each event, ask the scene manager for the active scene and call its onEvent(event) if one is available.
  2. Apply the camera’s resolution settings and update the camera. With an active scene, clear the renderer, call onUpdate(interval), then call onRender(*camera) and process collisions.
  3. Present the frame and sleep for any time left before the target frame duration.

interval is the measured time since the previous frame began, in seconds. The example targets 60 frames per second. A character moving at 70 world units per second advances by 70 * interval on each update. That keeps movement tied to elapsed time even when a frame takes longer. Frame loop · Character movement

Give the loop a Scene

A scene is one screen or playable situation, such as the arena where you meet the Ember Lord. To make one, publicly inherit from DreamEngine::Scene and implement its required methods with override. This is what conforming to a protocol means here. In the engine headers, interface expands to C++ class, and = delegated expands to = 0, a pure virtual method that a concrete class must supply. Engine vocabulary · Scene declaration

ProtocolWhat your scene suppliesWhat the arena does with it
Interactablevoid onEvent(const SDL_Event& event)Passes input to the characters.
Updatablevoid onUpdate(float interval)Advances the characters and UI, moves the fog and limits the camera’s travel.
Renderablevoid onRender(const Camera& camera) constDraws the background, characters, foreground fog and UI, in that order.
Identifiable<Identifier>Identifier getIdentifier() constSupplies a name the scene manager can log when the arena is queued.
Scenevoid onEntry() and void onExit()Sets a 576 × 128 virtual resolution on entry and disables it on exit.

GameObject combines the first three protocols and adds a Transform. Scene builds on GameObject and adds identity and the entry and exit hooks. The loop only needs a Scene* to call your implementation. GameObject · Arena implementation

There’s a second handoff inside the arena. EmberScene::onEvent loops over its characters and passes each one the event. SwordMaster interprets the click as an attack. In onUpdate, the scene also advances its views. This is why the boss’s health bar can animate without handling keyboard or mouse input. When adding an object, decide which of the scene’s calls it should receive and wire it into those methods. Arena dispatch · Sword Master input and updates

Change screens with SceneManager

SceneManager::getInstance()->switchCurrentScene(nextScene) puts a scene pointer at the back of a queue. A call to getCurrentScene() advances the transition. With a replacement waiting, one lookup calls the outgoing scene’s onExit() and clears the active pointer if transitions are unlocked. A later lookup takes the next scene from the queue and calls its onEntry(). These lookups can happen more than once in a frame.

You can see the queue at work on startup. The opening presentation locks transitions in onEntry(). When the video finishes, or the player skips it with Space, Return or Escape, its update unlocks the transition. The already-queued arena can then take its turn. If you add a second level, keep the scene objects alive while the manager can use them. An ordinary switch calls onExit() and leaves deletion to the owner. Scene transitions · Opening presentation

Load artwork with ResourceManager

The arena needs its backdrop as soon as its constructor runs. That explains the order of these calls in the game’s startup code.

resourceManager->loadFromDirectory(renderer, RESOURCES_PATH);
// The EmberScene constructor can now look up its background texture.
sceneManager->switchCurrentScene(new EmberScene(*camera));

The manager visits files directly inside the supplied directory and chooses a loader by extension. PNG textures, MP3 audio, TTF fonts and MP4 videos have separate pools. A filename’s stem is its lookup key, so ember-scene-background.png becomes getTexture("ember-scene-background"). Use distinct stems within a pool and load nested directories explicitly. A missing key returns nullptr. The manager owns the pooled assets, so scenes borrow the returned pointers. Resource loaders and lookups · Example assets

The background PNG contains several layers. The arena gives the texture and the size of one layer to a TextureSequence, selects a layer by index and passes it to Camera::renderTexture. It gives the sky a parallax factor of zero so it stays still, and the distant mountains a smaller factor than the nearby mountains so they scroll more slowly. Your scene chooses the composition. The camera converts each layer’s world position into a screen position. Arena drawing code · Camera coordinates

Follow one hit to the health bar

  1. The player clicks. The Sword Master records an attack request. On update, he asks his FiniteStateMachine to switch to the attack state. When the state changes, the machine calls the old state’s onExit() and the new state’s onEntry(). The character then passes the frame interval to the machine, which forwards it to the active state. The attack state chooses a strike animation, sets its damage and enables the sword’s hit box. Sword Master · Attack state · State machine
  2. The sword connects. The collision manager checks enabled boxes for overlap. The sword’s callback disables its hit box. The Ember Lord’s callback subtracts the strike’s damage from his health and calls notify(healthPoints). How much damage a strike does belongs to the game. Collision pass · Boss damage callback
  3. The bar changes. The boss conforms to Observable<float>. The arena connects the engine’s BarView with bossCharacter->registerObserver(bossHealthBar). When the boss publishes his health, the bar’s onNotified(const float&) receives the new value immediately. The next scene render shows it, and subsequent updates animate the trailing portion. The boss only publishes a number. The bar handles how losing health looks. Arena wiring · Observer protocol · Bar drawing and updates

Try a second listener

Suppose you want to see the boss’s remaining health in the console while tuning the fight. You can reuse the notification that already drives the bar. Define this class alongside the game’s scene code.

#include "engine/core.h"
#include <iostream>

class HealthLog final : public DreamEngine::Observer<float> {
public:
    void onNotified(const float& health) override {
        std::cout << "Ember Lord health: " << health << '\n';
    }
};

Give EmberScene a HealthLog healthLog; member. In its constructor, add bossCharacter->registerObserver(&healthLog); beside the health bar registration. Land a hit and both listeners receive the same value. If a listener has a shorter lifetime than the boss, call bossCharacter->removeObserver(&healthLog); before destroying it. The observable stores pointers to its listeners. Observer implementation

The camera uses the same idea with a different value. The Sword Master publishes a look-ahead Transform, and the camera observes it and smooths its movement toward the target. To find an extension point in your own game, start with the value or event you already have, then look for the protocol that lets another class receive it. Sword Master states · Camera following · Engine API guide

Explore the documentation

To explore beyond the Revenants demo, open the full DreamEngine documentation. Start with a component you saw in the tour, like the camera, or browse the API reference for your own game.