![]() |
ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
|
Read the keyboard, mouse, and gamepad through named actions.
Use this when the player needs to control anything: moving, jumping, pausing, navigating menus, ect.
Include: Imago/Engine/Game.hpp and Imago/Engine/Scene.hpp
In ImagoEngine you do not check for specific keys, instead you created named actions, like "jump" or "left", and add inputs to each one. Inputs currently could be a key, a mouse button, a gamepad button, or a direction on a stick. Systems then ask about the action instead of inputs:
With this system, that line works for any input registered to the "jump" action, it could be Space, a mouse click, or a gamepad button. Your systems never need to know which device the player is using.
Actions are set up once, in main, before the game runs. They are available in every scene.
Register each action, then add as many inputs to it as you like. Do this in main, after creating the Game and before calling run().
Register an action before adding inputs to it. The kinds of inputs you can add include:
| Input | Example | Notes |
|---|---|---|
| Keyboard key | sf::Keyboard::Scancode::Space | By physical key position, so it works on any keyboard layout |
| Mouse button | sf::Mouse::Button::Left | |
| Gamepad button | GamepadButton::FaceDown | Named by position: FaceDown is A on Xbox, X on PlayStation |
| Stick direction or trigger | GamepadAxis::LeftStickLeft | Each stick direction is separate |
There are four questions you can ask about any action:
Keys and buttons always have a strength of 0 or 1. Triggers and sticks can be anything in between.
direction combines four actions into one movement vector:
The vector’s length is never more than 1, so moving diagonally is not faster than moving straight. A stick tilted partway gives a shorter vector, for slower movement. Positive y points down the screen.
For a single axis, use axis instead:
Each direction of a stick is its own input, so you add them to actions just like keys:
Stick and trigger inputs take two optional:
Every query by name looks the name up. In a system that checks the same action many times, look it up once and reuse the result:
action_id returns INVALID_ACTION if the name does not exist.
Setup in main before game.run():
| Function | What it does | Notes |
|---|---|---|
| register_action(name) | Creates an action | Returns its ActionId |
| add_input(name, key) | Adds a keyboard key | sf::Keyboard::Scancode |
| add_input(name, button) | Adds a mouse or gamepad button | sf::Mouse::Button or GamepadButton |
| add_input(name, axis, deadZone, threshold) | Adds a stick direction or trigger | Defaults: 0.2f, 0.5f |
In systems on ctx.input:
| Function | Returns | Notes |
|---|---|---|
| held(action) | bool | Currently pressed |
| began(action) | bool | Just pressed |
| ended(action) | bool | Just let go |
| strength(action) | float | 0 to 1 |
| axis(negative, positive) | float | -1 to 1 |
| direction(left, right, up, down) | glm::vec2 | Length never above 1 |
| action_id(name) | ActionId | INVALID_ACTION if not found |
Every query accepts either a name or an ActionId.