Ember
Ember is a simple and fun 2D rendering library for Rust, allowing you to quickly create graphics and interactive applications with ease. It uses the minifb
crate under the hood to handle window creation and input events.
Features
- Easy-to-use API for drawing basic shapes, such as circles, lines, and rectangles
- Support for drawing text with a built-in font
- Efficient pixel buffer manipulation
- Mouse and keyboard input handling
Examples
Basic Drawing Example
The following example demonstrates some of Ember's features:
use ember_rs::Ember;
fn main() {
let (width, height, fps) = (800, 600, 144.0);
let mut app = Ember::new("Ember - Simple Example", width, height, fps);
while !app.should_close() {
app.clear();
app.draw_text("ember is awesome", 20, 20, 4, 0xFFAAAA);
app.draw_circle_fill(700, 500, 40, 0xFFFFFF);
app.draw_line(310, 70, 630, 430, 0xAAFFAA);
app.update();
}
}
Mouse and Keyboard Input Example
The following example demonstrates mouse and keyboard input handling:
use ember_rs::{Ember, Key};
fn main() {
let (width, height, fps) = (800, 600, 144.0);
let mut app = Ember::new("Ember - Mouse and Keyboard Example", width, height, fps);
let mut circle_x = width / 2;
let mut circle_y = height / 2;
let circle_radius = 40;
while !app.should_close() {
app.clear();
app.process_keys(|key| match key {
Key::W => circle_y -= 5,
Key::A => circle_x -= 5,
Key::S => circle_y += 5,
Key::D => circle_x += 5,
_ => {}
});
let mouse_info = app.get_mouse_info();
if let Some((mouse_x, mouse_y)) = mouse_info.position {
if mouse_info.left_button {
circle_x = mouse_x as i32;
circle_y = mouse_y as i32;
}
}
app.draw_circle_fill(circle_x, circle_y, circle_radius, 0xFF0000);
app.update();
}
}
Filled Shapes Example
The following example demonstrates drawing various filled shapes:
app.set_pixel(x, y, color);
app.draw_line(x1, y1, x2, y2, color);
app.draw_line_width(x1, y1, x2, y2, width, color);
app.draw_rectangle(x, y, width, height, color);
app.draw_rectangle_fill(x, y, width, height, color);
app.draw_circle(x, y, radius, color);
app.draw_circle_fill(x, y, radius, color);
Draw Text Example
The following example demonstrates how to draw text to the screen:
use ember_rs::{Ember, helper};
fn main() {
let (width, height, fps) = (800, 600, 144.0);
let mut app = Ember::new("Ember - Mouse and Keyboard Example", width, height, fps);
while !app.should_close() {
app.clear();
app.draw_text("hello world", 50, 50, 4, 0xFFFFFF);
let text = format!("Width: {}", width);
let (x, y) = helper::center_text(&text, 400, 400, 4);
app.draw_text(&text, x, y, 4, 0xAAAAFF);
app.update();
}
}
Installation
To get started with Ember, type the following command inside of your project:
cargo add ember-rs