Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::platform Namespace Reference

Window and input on top of SDL3, independent of the graphics API (client only). More...

Classes

class  ActionMap
 Actions of the game and the keys and gamepad buttons that trigger them. More...
 
struct  CapturedKey
 End of a key capture. More...
 
struct  ControlLine
 One line of a controls reminder: an action and the keys (or gamepad buttons) that trigger it now. More...
 
struct  Extent
 Width and height of a window or a framebuffer. More...
 
class  Gamepad
 Buttons held down, and those pressed or released since the start of the frame, with the position of the sticks and triggers. More...
 
class  Keyboard
 Keys held down, and those pressed or released since the start of the frame. More...
 
class  KeyCapture
 Key binding step of a settings menu: "press a key for Fire". More...
 
class  LatchedAction
 An action the player can hold without holding a key, such as fire. More...
 
class  Mouse
 Position of the mouse, buttons held, and those pressed or released and the wheel turned since the start of the frame. More...
 
class  TextInput
 UTF-8 text typed during a frame, following the user's keyboard layout. More...
 
class  Window
 Desktop window: creation, size and events. Drawing is left to a render backend. More...
 
struct  WindowConfig
 Settings of a new window. More...
 
class  WindowError
 Thrown when the window cannot be created. More...
 

Typedefs

using ActionId = std::size_t
 Identifier of an action in its ActionMap, given by ActionMap::add().
 
using ActionLabels = std::map< std::string, std::string, std::less<> >
 Display labels of actions, by action name: {"up", "Move up"}.
 

Enumerations

enum class  GamepadButton : std::uint8_t {
  South , East , West , North ,
  Back , Guide , Start , LeftStick ,
  RightStick , LeftShoulder , RightShoulder , DpadUp ,
  DpadDown , DpadLeft , DpadRight , LeftTrigger ,
  RightTrigger , LeftStickUp , LeftStickDown , LeftStickLeft ,
  LeftStickRight , RightStickUp , RightStickDown , RightStickLeft ,
  RightStickRight
}
 Buttons of a gamepad, named by their position on the pad. More...
 
enum class  GamepadAxis : std::uint8_t {
  LeftX , LeftY , RightX , RightY ,
  LeftTrigger , RightTrigger
}
 Analog controls of a gamepad. More...
 
enum class  GamepadStyle : std::uint8_t { Xbox , PlayStation , Nintendo }
 Family of a gamepad, which sets the labels printed on its buttons. More...
 
enum class  Key : std::uint8_t {
  A , B , C , D ,
  E , F , G , H ,
  I , J , K , L ,
  M , N , O , P ,
  Q , R , S , T ,
  U , V , W , X ,
  Y , Z , Num0 , Num1 ,
  Num2 , Num3 , Num4 , Num5 ,
  Num6 , Num7 , Num8 , Num9 ,
  F1 , F2 , F3 , F4 ,
  F5 , F6 , F7 , F8 ,
  F9 , F10 , F11 , F12 ,
  Up , Down , Left , Right ,
  Space , Enter , Escape , Tab ,
  Backspace , Insert , Delete , Home ,
  End , PageUp , PageDown , LeftShift ,
  RightShift , LeftCtrl , RightCtrl , LeftAlt ,
  RightAlt , CapsLock , Minus , Equals ,
  LeftBracket , RightBracket , Backslash , Semicolon ,
  Apostrophe , Grave , Comma , Period ,
  Slash , Keypad0 , Keypad1 , Keypad2 ,
  Keypad3 , Keypad4 , Keypad5 , Keypad6 ,
  Keypad7 , Keypad8 , Keypad9 , KeypadPeriod ,
  KeypadDivide , KeypadMultiply , KeypadMinus , KeypadPlus ,
  KeypadEnter
}
 Keyboard key, independent of SDL. More...
 
enum class  MouseButton : std::uint8_t {
  Left , Middle , Right , X1 ,
  X2
}
 Buttons of a mouse. More...
 
enum class  GraphicsApi : std::uint8_t { None , OpenGL }
 Graphics API the window is prepared for. More...
 

Functions

std::string action_label (std::string_view name)
 Default label of an action name.
 
std::string keys_label (const ActionMap &actions, ActionId action)
 The keys of an action as shown to the player.
 
std::string buttons_label (const ActionMap &actions, ActionId action, GamepadStyle style)
 The gamepad buttons of an action as printed on the player's pad.
 
std::vector< ControlLine > control_lines (const ActionMap &actions, const ActionLabels &labels={})
 Every action with the keys that trigger it now, in the order they were declared.
 
std::vector< ControlLine > control_lines (const ActionMap &actions, GamepadStyle style, const ActionLabels &labels={})
 Every action with the gamepad buttons that trigger it now, for a player using a pad, in the order they were declared.
 
std::string_view button_name (GamepadButton button) noexcept
 Stable name of a button, as written in settings files.
 
std::optional< GamepadButton > button_from_name (std::string_view name)
 Button whose name is name, ignoring case.
 
std::string button_label (GamepadButton button, GamepadStyle style={})
 Name of a button as printed on a pad of the given family, for a controls reminder.
 
std::string_view key_name (Key key) noexcept
 Stable name of a key, as written in settings files.
 
std::optional< Key > key_from_name (std::string_view name)
 Key whose name is name, ignoring case.
 
std::string key_label (Key key)
 Name of a key as shown to the player, such as in a controls reminder.
 
std::string_view name () noexcept
 Name of the module, used to check that it is linked.
 
std::filesystem::path settings_dir (std::string_view organization, std::string_view application)
 Folder of the player's settings for one game, created if needed.
 
core::Config read_settings (const std::filesystem::path &file)
 Reads the player's settings file.
 
bool pop_back_utf8 (std::string &text) noexcept
 Removes the last UTF-8 code point of a string, as Backspace does.
 

Variables

constexpr std::size_t gamepad_button_count
 Number of GamepadButton values.
 
constexpr std::size_t gamepad_axis_count
 Number of GamepadAxis values.
 
constexpr std::size_t key_count = static_cast<std::size_t>(Key::KeypadEnter) + 1
 Number of keys in Key, to size per-key tables.
 
constexpr std::size_t mouse_button_count = static_cast<std::size_t>(MouseButton::X2) + 1
 Number of MouseButton values.
 

Detailed Description

Window and input on top of SDL3, independent of the graphics API (client only).

Typedef Documentation

◆ ActionId

using aegis2d::platform::ActionId = typedef std::size_t

Identifier of an action in its ActionMap, given by ActionMap::add().

◆ ActionLabels

using aegis2d::platform::ActionLabels = typedef std::map<std::string, std::string, std::less<> >

Display labels of actions, by action name: {"up", "Move up"}.

Enumeration Type Documentation

◆ GamepadAxis

enum class aegis2d::platform::GamepadAxis : std::uint8_t
strong

Analog controls of a gamepad.

Enumerator
LeftX 

Left stick, from -1 (left) to 1 (right).

LeftY 

Left stick, from -1 (up) to 1 (down).

RightX 

Right stick, from -1 (left) to 1 (right).

RightY 

Right stick, from -1 (up) to 1 (down).

LeftTrigger 

Left trigger, from 0 (released) to 1 (pulled all the way).

RightTrigger 

Right trigger, from 0 (released) to 1 (pulled all the way).

◆ GamepadButton

enum class aegis2d::platform::GamepadButton : std::uint8_t
strong

Buttons of a gamepad, named by their position on the pad.

The four face buttons are named by side: South is A on an Xbox pad, Cross on a PlayStation pad and B on a Nintendo pad. Triggers and stick directions also act as buttons, held while pushed far enough (Gamepad::press_threshold), so actions bind to them like to any button: the left stick moves the player as the D-pad does.

Enumerator
South 

Bottom face button.

East 

Right face button.

West 

Left face button.

North 

Top face button.

Back 

Left center button (View, Share, Minus).

Guide 

Middle center button (Xbox, PS, Home).

Start 

Right center button (Menu, Options, Plus).

LeftStick 

Left stick pressed in.

RightStick 

Right stick pressed in.

LeftShoulder 

Left shoulder button.

RightShoulder 

Right shoulder button.

DpadUp 

D-pad up.

DpadDown 

D-pad down.

DpadLeft 

D-pad left.

DpadRight 

D-pad right.

LeftTrigger 

Left trigger pulled.

RightTrigger 

Right trigger pulled.

LeftStickUp 

Left stick pushed up.

LeftStickDown 

Left stick pushed down.

LeftStickLeft 

Left stick pushed left.

LeftStickRight 

Left stick pushed right.

RightStickUp 

Right stick pushed up.

RightStickDown 

Right stick pushed down.

RightStickLeft 

Right stick pushed left.

RightStickRight 

Right stick pushed right.

◆ GamepadStyle

enum class aegis2d::platform::GamepadStyle : std::uint8_t
strong

Family of a gamepad, which sets the labels printed on its buttons.

Enumerator
Xbox 

Xbox pads, and any pad of unknown family: A, B, X, Y, LB, RT...

PlayStation 

PlayStation pads: Cross, Circle, Square, Triangle, L1, R2...

Nintendo 

Nintendo pads: B, A, Y, X (South is B), L, ZR...

◆ GraphicsApi

enum class aegis2d::platform::GraphicsApi : std::uint8_t
strong

Graphics API the window is prepared for.

Some APIs need the window to be created for them; the render backend then attaches its context or swapchain to the window.

Enumerator
None 

No API-specific setup (APIs that only need the native window, or none).

OpenGL 

The window can receive an OpenGL context (render::opengl::Context).

◆ Key

enum class aegis2d::platform::Key : std::uint8_t
strong

Keyboard key, independent of SDL.

Letters, digits and symbols follow the character printed on the key in the user's layout, so the key labeled Z on an AZERTY keyboard is Key::Z; keys whose character has no Key (such as & on the AZERTY digit row) fall back to their position on a US keyboard (Key::Num1). NumN are the digits of the main row, KeypadN those of the numeric keypad.

Enumerator
A 
B 
C 
D 
E 
F 
G 
H 
I 
J 
K 
L 
M 
N 
O 
P 
Q 
R 
S 
T 
U 
V 
W 
X 
Y 
Z 
Num0 
Num1 
Num2 
Num3 
Num4 
Num5 
Num6 
Num7 
Num8 
Num9 
F1 
F2 
F3 
F4 
F5 
F6 
F7 
F8 
F9 
F10 
F11 
F12 
Up 
Down 
Left 
Right 
Space 
Enter 
Escape 
Tab 
Backspace 
Insert 
Delete 
Home 
End 
PageUp 
PageDown 
LeftShift 
RightShift 
LeftCtrl 
RightCtrl 
LeftAlt 
RightAlt 
CapsLock 
Minus 
Equals 
LeftBracket 
RightBracket 
Backslash 
Semicolon 
Apostrophe 
Grave 
Comma 
Period 
Slash 
Keypad0 
Keypad1 
Keypad2 
Keypad3 
Keypad4 
Keypad5 
Keypad6 
Keypad7 
Keypad8 
Keypad9 
KeypadPeriod 
KeypadDivide 
KeypadMultiply 
KeypadMinus 
KeypadPlus 
KeypadEnter 

◆ MouseButton

enum class aegis2d::platform::MouseButton : std::uint8_t
strong

Buttons of a mouse.

Enumerator
Left 

Main button, under the index finger.

Middle 

Wheel pressed in.

Right 

Secondary button.

X1 

First side button (back in a browser).

X2 

Second side button (forward in a browser).

Function Documentation

◆ action_label()

std::string aegis2d::platform::action_label ( std::string_view  name)

Default label of an action name.

Parameters
nameAction name, such as "move-up".
Returns
The name with its first letter in upper case and - and _ as spaces: "Move up".

◆ button_from_name()

std::optional< GamepadButton > aegis2d::platform::button_from_name ( std::string_view  name)

Button whose name is name, ignoring case.

Parameters
nameName returned by button_name(), such as "south" or "RightTrigger".
Returns
The button, or nothing if no button has this name.

◆ button_label()

std::string aegis2d::platform::button_label ( GamepadButton  button,
GamepadStyle  style = {} 
)

Name of a button as printed on a pad of the given family, for a controls reminder.

Face, shoulder, trigger and center buttons get their family's label ("A", "Cross", "RB", "R2", "ZR", "Menu"); the D-pad and the sticks are spelled out ("D-pad up", "Left stick left", "LS" for the left stick pressed in).

Parameters
buttonButton to name.
styleFamily of the pad, such as Gamepad::style().
Returns
The label; "Unknown" for a value outside GamepadButton.

◆ button_name()

std::string_view aegis2d::platform::button_name ( GamepadButton  button)
noexcept

Stable name of a button, as written in settings files.

The enumerator's name: "South", "DpadUp", "LeftStickLeft".

Parameters
buttonButton to name.
Returns
A view of a static string; "Unknown" for a value outside GamepadButton.

◆ buttons_label()

std::string aegis2d::platform::buttons_label ( const ActionMap &  actions,
ActionId  action,
GamepadStyle  style 
)

The gamepad buttons of an action as printed on the player's pad.

Parameters
actionsActions of the game.
actionAction to describe.
styleFamily of the pad, usually window.gamepad().style().
Returns
Its current buttons' labels (button_label()) joined by " / ", in the order they were bound, or "not bound" when it has none.
Exceptions
std::out_of_rangeif action is not declared.

◆ control_lines() [1/2]

std::vector< ControlLine > aegis2d::platform::control_lines ( const ActionMap &  actions,
const ActionLabels &  labels = {} 
)

Every action with the keys that trigger it now, in the order they were declared.

Built from the bindings in use, so a reminder shows the player's own keys after a remap (#51), not the defaults.

Parameters
actionsActions of the game.
labelsDisplay labels by action name; other actions use action_label().
Returns
One line per action.

◆ control_lines() [2/2]

std::vector< ControlLine > aegis2d::platform::control_lines ( const ActionMap &  actions,
GamepadStyle  style,
const ActionLabels &  labels = {} 
)

Every action with the gamepad buttons that trigger it now, for a player using a pad, in the order they were declared.

Parameters
actionsActions of the game.
styleFamily of the player's pad, usually window.gamepad().style().
labelsDisplay labels by action name; other actions use action_label().
Returns
One line per action, its keys holding buttons_label().

◆ key_from_name()

std::optional< Key > aegis2d::platform::key_from_name ( std::string_view  name)

Key whose name is name, ignoring case.

Parameters
nameName returned by key_name(), such as "space" or "F5".
Returns
The key, or nothing if no key has this name.

◆ key_label()

std::string aegis2d::platform::key_label ( Key  key)

Name of a key as shown to the player, such as in a controls reminder.

key_name() split into words: "Left Shift", "Page Up", "Keypad 5"; single letters, digits and function keys stay as they are ("A", "7", "F5").

Parameters
keyKey to name.
Returns
The label; "Unknown" for a value outside Key.

◆ key_name()

std::string_view aegis2d::platform::key_name ( Key  key)
noexcept

Stable name of a key, as written in settings files.

Letters are upper case ("A"), digits are "0" to "9", the other names are the enumerator's ("Space", "LeftShift", "Keypad0").

Parameters
keyKey to name.
Returns
A view of a static string; "Unknown" for a value outside Key.

◆ keys_label()

std::string aegis2d::platform::keys_label ( const ActionMap &  actions,
ActionId  action 
)

The keys of an action as shown to the player.

Parameters
actionsActions of the game.
actionAction to describe.
Returns
Its current keys' labels (key_label()) joined by " / ", in the order they were bound, or "not bound" when it has none.
Exceptions
std::out_of_rangeif action is not declared.

◆ name()

std::string_view aegis2d::platform::name ( )
noexcept

Name of the module, used to check that it is linked.

Returns
"platform", a view of a static string.

◆ pop_back_utf8()

bool aegis2d::platform::pop_back_utf8 ( std::string &  text)
noexcept

Removes the last UTF-8 code point of a string, as Backspace does.

Parameters
textUTF-8 text; unchanged when empty.
Returns
true if something was removed.

◆ read_settings()

core::Config aegis2d::platform::read_settings ( const std::filesystem::path &  file)

Reads the player's settings file.

A missing file gives an empty configuration: it is the first start. A file that cannot be read or parsed is logged as a warning and gives an empty configuration too, so a broken settings file never stops the game; the next save replaces it.

Parameters
fileSettings file, usually settings_dir(...) / "settings.json".
Returns
The settings, or an empty configuration.

◆ settings_dir()

std::filesystem::path aegis2d::platform::settings_dir ( std::string_view  organization,
std::string_view  application 
)

Folder of the player's settings for one game, created if needed.

The per-user folder the operating system gives for application data (SDL's "pref path"), outside the game folder so settings survive an update and each player of a shared computer has their own: under APPDATA% on Windows, ~/Library/Application Support on macOS, $XDG_DATA_HOME (by default ~/.local/share) on Linux.

Parameters
organizationName of the studio, such as "Aegis2D": letters, digits and spaces.
applicationName of the game, such as "RType"; never change it once released.
Returns
Absolute path of the folder.
Exceptions
std::runtime_errorif the operating system gives no such folder.

Variable Documentation

◆ gamepad_axis_count

constexpr std::size_t aegis2d::platform::gamepad_axis_count
inlineconstexpr
Initial value:
=
static_cast<std::size_t>(GamepadAxis::RightTrigger) + 1

Number of GamepadAxis values.

◆ gamepad_button_count

constexpr std::size_t aegis2d::platform::gamepad_button_count
inlineconstexpr
Initial value:
=
static_cast<std::size_t>(GamepadButton::RightStickRight) + 1

Number of GamepadButton values.

◆ key_count

constexpr std::size_t aegis2d::platform::key_count = static_cast<std::size_t>(Key::KeypadEnter) + 1
inlineconstexpr

Number of keys in Key, to size per-key tables.

◆ mouse_button_count

constexpr std::size_t aegis2d::platform::mouse_button_count = static_cast<std::size_t>(MouseButton::X2) + 1
inlineconstexpr

Number of MouseButton values.