Crates.io | pixel_loop |
lib.rs | pixel_loop |
version | 0.3.0 |
source | src |
created_at | 2024-11-04 18:08:18.250905 |
updated_at | 2024-11-13 23:53:47.269819 |
description | A pixel based drawing engine based on the idea of a self stabilizing update loop. |
homepage | |
repository | https://github.com/jakobwesthoff/pixel_loop |
max_upload_size | |
id | 1435419 |
size | 97,109 |
This crate/library is still heavily being worked on. The API is not considered to be stable at this point in time. If you want to follow the development check out the following youtube channel MrJakob.
A Rust game loop implementation providing a solid foundation for building games and interactive applications. Inspired by the concepts from Fix Your Timestep, it offers fixed timestep updates with variable rendering, supporting both windowed and terminal-based applications.
The idea behind Pixel Loop resonated with me as I have often faced challenges with timing aspects while working on animations from scratch. This project serves as a practical exploration of fixed time game/update loops and lays the groundwork for future experiments and projects.
Add Pixel Loop to your Cargo.toml
:
[dependencies]
pixel_loop = "*"
winit
- Enable window-based renderingcrossterm
- Enable terminal-based renderingstb-image
- Enable image loading support for InMemoryCanvas via stb_imageBy default all flags are currently enabled. If you only need a specific one, you may only use enable the backend/feature you specifically need, to cut down on compilation time and filesize.
Create a simple moving box in your terminal:
use pixel_loop::{run, canvas::CrosstermCanvas, input::CrosstermInputState};
use pixel_loop::color::Color;
use pixel_loop::input::KeyboardKey;
use anyhow::Result;
struct GameState {
box_pos: (i64, i64),
}
fn main() -> Result<()> {
let mut canvas = CrosstermCanvas::new(); // Terminal size
let state = GameState { box_pos: (0, 0) };
let input = CrosstermInputState::new();
run(
60, // Updates per second
state,
input,
canvas,
// Update function - fixed timestep
|_env, state, input, _canvas| {
if input.is_key_down(KeyboardKey::Right) {
state.box_pos.0 += 1;
}
if input.is_key_pressed(KeyboardKey::Q) {
std::process::exit(0);
}
Ok(())
},
// Render function - variable timestep
|_env, state, _input, canvas, _dt| {
canvas.clear_screen(&Color::from_rgb(0, 0, 0));
canvas.filled_rect(
state.box_pos.0,
state.box_pos.1,
5,
5,
&Color::from_rgb(255, 0, 0),
);
canvas.render()?;
Ok(())
},
)
}
Pixel Loop implements a fixed timestep game loop that:
The library provides (currently) three canvas implementations:
PixelsCanvas
: Hardware-accelerated window renderingCrosstermCanvas
: Terminal-based rendering using Unicode charactersInMemoryCanvas
: In-memory buffer for image manipulationEach canvas (currently) supports:
Input handling is abstracted through traits:
KeyboardState
for basic keyboard inputInputState
for game loop integrationNote: Mouse integration for window rendering can currently be archieved, but an abstraction has not yet been implemented
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
This repository housed a couple of different experiment implementations based on
pixel_loop
. Those have mostly have been moved to their own repositories as
the library is now published on crates.io.
You can find the old subprojects here: