|
SBgl 0.1.0
A graphics framework in C99
|
The input system provides access to physical device state through the engine context. It utilizes an internal "key map" pattern tied to the engine context.
Unlike systems that use global state or OS-specific polling, SBgl stores all physical input state within the opaque sbgl_InternalContext.
This design ensures that:
Internally, the sbgl_InputState structure contains fixed-size boolean arrays for every possible physical key and mouse button.
keysDown: A bit-array or boolean array representing the current physical state of keys.keysPressed: A specialized array that is set to true only on the frame a key was first pressed.mouseDown: Current state of the mouse buttons.When the OS sends a message (e.g., WM_KEYDOWN on Windows or wl_keyboard.key on Wayland), the Platform Layer translates the native scancode into an SBgl scancode (defined in sbgl.h) and updates the corresponding index in these internal arrays.
The Platform Layer (Wayland, X11, Win32) is responsible for filling the key map during the sbgl_os_PollEvents phase.
Mouse movement is tracked using absolute coordinates (mouseX, mouseY) and relative deltas. Deltas are calculated during the event poll by comparing the current position with the position from the previous frame stored in internal tracking variables within the sbgl_InputState (_internalMouseX, _internalMouseY). This ensures deltas are calculated accurately even when frames are skipped or delayed.
Pressed states (keysPressed) are automatically reset at the end of every frame in sbgl_EndDrawing. This ensures that a "pressed" event is only visible to the application for exactly one frame.
The system provides a Data-Oriented API for batch processing.
The sbgl_GetInputState function provides a read-only pointer to the contiguous sbgl_InputState structure. Fetching the pointer once per frame enables batch processing.
The keysDown (held) or keysPressed (one-shot) boolean arrays are queried directly.
Access real-time coordinates, deltas, and button states.
The engine supports three distinct modes for controlling the cursor's visibility and movement behavior.
| Mode | Behavior | Use Case |
|---|---|---|
SBGL_MOUSE_MODE_NORMAL | Standard OS cursor, visible and free to move. | 2D UIs, Desktop-style apps. |
SBGL_MOUSE_MODE_HIDDEN | Cursor is invisible but moves freely. | Custom software cursors. |
SBGL_MOUSE_MODE_CAPTURED | Cursor is invisible and locked to the window center. | 3D navigation, FPS cameras. |
Use sbgl_SetMouseMode to switch between behaviors. The engine ensures that modes like CAPTURED are persistent; if the window loses and then regains focus, the engine automatically re-locks the cursor.
The design adheres to Data-Oriented Design principles by:
keysDown, keysPressed, mouseDown) allows the CPU to prefetch data effectively during batch processing loops.By exposing the state as a contiguous block of data, the engine enables iteration and transformation of input data without the overhead of object-oriented getters.
The Data-Oriented API facilitates direct mapping of scancodes to transformation data, enabling efficient camera or entity controls without branching logic.
| Feature | Implementation | Benefit |
|---|---|---|
| State Storage | Embedded in sbgl_InternalContext | Strict encapsulation and thread-safety. |
| Polling | Event-driven (Async) | Zero OS overhead during frame logic. |
| Data Layout | Tightly packed fixed arrays | Cache efficiency and batch-processing support. |
| Memory | O(1) Allocation (Arena) | No dynamic allocation during input events. |