Expand description
Core game loop and engine functionality.
This module provides the main game loop implementation with fixed timestep updates and rendering. It supports different input and canvas implementations through traits.
§Example
use pixel_loop::{run, EngineEnvironment};
use pixel_loop::canvas::{Canvas, CrosstermCanvas, RenderableCanvas};
use pixel_loop::color::Color;
use pixel_loop::input::{CrosstermInputState, KeyboardKey, KeyboardState};
use anyhow::Result;
// Game state definition
struct Box {
position: (i64, i64),
size: (u32, u32),
color: Color,
}
struct State {
box_entity: Box,
}
// Create initial state
let state = State {
box_entity: Box {
position: (0, 0),
size: (5, 5),
color: Color::from_rgb(156, 80, 182),
},
};
// Setup canvas and input
let canvas = CrosstermCanvas::new();
let input = CrosstermInputState::new();
// Update function - called at fixed timestep
fn update(env: &mut EngineEnvironment,
state: &mut State,
input: &CrosstermInputState,
canvas: &mut CrosstermCanvas) -> Result<()> {
// Handle input
if input.is_key_down(KeyboardKey::Up) {
state.box_entity.position.1 -= 1;
}
if input.is_key_down(KeyboardKey::Down) {
state.box_entity.position.1 += 1;
}
Ok(())
}
// Render function - called as often as possible
fn render(env: &mut EngineEnvironment,
state: &mut State,
input: &CrosstermInputState,
canvas: &mut CrosstermCanvas,
dt: std::time::Duration) -> Result<()> {
canvas.clear_screen(&Color::from_rgb(0, 0, 0));
canvas.filled_rect(
state.box_entity.position.0,
state.box_entity.position.1,
state.box_entity.size.0,
state.box_entity.size.1,
&state.box_entity.color,
);
canvas.render()?;
Ok(())
}
// Run the game loop
run(60, state, input, canvas, update, render)?;
Ok(())
Re-exports§
pub use crossterm;
pub use rand;
pub use rand_xoshiro;
Modules§
- canvas
- Canvas implementations for rendering pixels on different output targets
- color
- Color types and conversion utilities.
- input
- Input handling and keyboard state management.
Structs§
- Engine
Environment - Global engine state containing shared resources.
- Pixel
Loop - Main game loop handler.
Enums§
- Next
Loop State - Return type for the next loop state. Used to determine if the game loop should continue or exit.
Functions§
- run
- Runs the game loop with the provided state and implementations.