Input and Controls
The oglr
library provides a comprehensive system for handling user input, enabling seamless integration with game logic. This system allows for efficient and flexible control over character movement, camera manipulation, and other interactive elements.
Input Handling
The oglr
library leverages the power of the glfw
library for capturing keyboard, mouse, and joystick input. This ensures cross-platform compatibility and a consistent user experience.
Example:
#include <oglr/Input.hpp>
// ...
// Check if the 'W' key is pressed
if (oglr::Input::isKeyDown(GLFW_KEY_W)) {
// Handle movement forward
}
// Check if the left mouse button is pressed
if (oglr::Input::isMouseButtonDown(GLFW_MOUSE_BUTTON_LEFT)) {
// Handle left-click actions
}
Source: src/oglr/Input.cpp
Control System
The oglr
library provides a robust control system that simplifies the mapping of input events to game actions. This system allows for:
- Customizable mappings: Define your own input bindings for different actions, tailoring them to your game’s requirements.
- Multiple input sources: Use keyboard, mouse, joystick, or a combination of these for more nuanced controls.
- State management: Keep track of button presses, axis values, and other input states.
Example:
#include <oglr/Controls.hpp>
// ...
// Create a control scheme for player movement
auto playerControls = std::make_shared<oglr::Controls::ControlScheme>();
playerControls->addBinding("MoveForward", oglr::Input::KeyBinding(GLFW_KEY_W));
playerControls->addBinding("MoveBackward", oglr::Input::KeyBinding(GLFW_KEY_S));
playerControls->addBinding("MoveLeft", oglr::Input::KeyBinding(GLFW_KEY_A));
playerControls->addBinding("MoveRight", oglr::Input::KeyBinding(GLFW_KEY_D));
// ...
// Update the control scheme in the game loop
playerControls->update();
// Retrieve movement input
float forward = playerControls->getAxisValue("MoveForward");
float backward = playerControls->getAxisValue("MoveBackward");
float left = playerControls->getAxisValue("MoveLeft");
float right = playerControls->getAxisValue("MoveRight");
// ...
Source: src/oglr/Controls.cpp
Key Bindings
The oglr
library offers flexible key binding options, allowing you to configure input to your preferences:
Simple bindings: Map a single key to an action.
Complex bindings: Combine multiple keys for advanced actions.
Modifiers: Use modifier keys like
Shift
,Control
, andAlt
to trigger different actions.
Example:
#include <oglr/Input.hpp>
// ...
// Bind the 'F' key to the 'Fire' action
oglr::Input::bindAction("Fire", oglr::Input::KeyBinding(GLFW_KEY_F));
// Bind the 'W' key and 'Shift' modifier to the 'Sprint' action
oglr::Input::bindAction("Sprint", oglr::Input::KeyBinding(GLFW_KEY_W, GLFW_MOD_SHIFT));
Source: src/oglr/Input.cpp
Mouse Input
The oglr
library supports mouse input for a variety of functionalities:
Cursor position: Track the mouse cursor’s position on the screen.
Button clicks: Detect mouse button presses and releases.
Scroll wheel: Handle scrolling events.
Example:
#include <oglr/Input.hpp>
// ...
// Get the current mouse cursor position
auto mousePos = oglr::Input::getMousePosition();
// Check if the left mouse button is pressed
if (oglr::Input::isMouseButtonDown(GLFW_MOUSE_BUTTON_LEFT)) {
// Handle left-click actions
}
// Handle scroll wheel events
oglr::Input::onScroll([&](float xOffset, float yOffset) {
// ...
});
Source: src/oglr/Input.cpp
Joystick Input
The oglr
library allows for seamless integration of joysticks, providing:
Axis values: Read joystick axis values (e.g., for movement or camera control).
Button presses: Detect joystick button presses and releases.
Example:
#include <oglr/Input.hpp>
// ...
// Get the value of the left joystick X-axis
float xAxis = oglr::Input::getJoystickAxis(0, GLFW_GAMEPAD_AXIS_LEFT_X);
// Check if the A button is pressed
if (oglr::Input::isJoystickButtonDown(0, GLFW_GAMEPAD_BUTTON_A)) {
// Handle button press
}
Source: src/oglr/Input.cpp
Advanced Features
The oglr
library offers a range of advanced features for fine-grained control over input:
Input events: Receive custom input events for specific actions, such as key presses, mouse clicks, and joystick button presses.
Input filtering: Filter input events based on specific criteria, such as key modifiers or button states.
Input state persistence: Maintain input state across multiple frames.
Source: src/oglr/Input.cpp