Hardware-accelerated drawing of shapes, images, and text, with an easy to use API.
Speedy2D aims to be:
Supports Windows, Mac, and Linux. Should also work on Android and iOS (for rendering only).
By default, Speedy2D contains support for setting up a window with an OpenGL
context, and receiving input events. If you'd like to handle this yourself, and
use Speedy2D only for rendering, you can disable the windowing
feature.
The example projects can be run using cargo run --example=hello_world
(just
change hello_world
to the name of the example source file).
Step 1: Add Speedy2D to your Cargo.toml
dependencies:
toml
[dependencies]
speedy2d = "1.0"
Step 2: Create a window:
```rust use speedy2d::Window;
let window = Window::new_centered("Title", (640, 480)).unwrap(); ```
Step 3: Create a struct implementing the WindowHandler
trait. Override
whichever callbacks you're interested in, for example on_draw()
,
on_mouse_move()
, or on_key_down()
.
```rust use speedy2d::color::Color; use speedy2d::window::{WindowHandler, WindowHelper}; use speedy2d::Graphics2D;
struct MyWindowHandler {}
impl WindowHandler for MyWindowHandler { fn ondraw(&mut self, helper: &mut WindowHelper, graphics: &mut Graphics2D) { graphics.clearscreen(Color::fromrgb(0.8, 0.9, 1.0)); graphics.drawcircle((100.0, 100.0), 75.0, Color::BLUE);
// Request that we draw another frame once this one has finished
helper.request_redraw();
}
// If desired, onmousemove(), onkeydown(), etc... } ```
Step 4: Finally, start the event loop by passing your new WindowHandler
to the run_loop()
function.
rust
window.run_loop(MyWindowHandler{});
That's it!
For a more detailed getting started guide, including a full list of WindowHandler
callbacks, and how to render text, go to
docs.rs/speedy2d.
The full code of the above example is below for your convenience:
```rust use speedy2d::color::Color; use speedy2d::{Graphics2D, Window}; use speedy2d::window::{WindowHandler, WindowHelper};
fn main() { let window = Window::newcentered("Title", (640, 480)).unwrap(); window.runloop(MyWindowHandler{}); }
struct MyWindowHandler {}
impl WindowHandler for MyWindowHandler { fn ondraw(&mut self, helper: &mut WindowHelper, graphics: &mut Graphics2D) { graphics.clearscreen(Color::fromrgb(0.8, 0.9, 1.0)); graphics.drawcircle((100.0, 100.0), 75.0, Color::BLUE); helper.request_redraw(); } } ```
If you'd rather handle the window creation and OpenGL context management
yourself, simply disable Speedy2D's windowing
feature in your Cargo.toml
file, and create a context as follows:
```rust use speedy2d::GLRenderer;
let mut renderer = unsafe { GLRenderer::newforcurrent_context((640, 480)) }.unwrap(); ```
Then, draw a frame using GLRenderer::draw_frame()
:
rust
renderer.draw_frame(|graphics| {
graphics.clear_screen(Color::WHITE);
graphics.draw_circle((100.0, 100.0), 75.0, Color::BLUE);
});
Speedy2D is licensed under the Apache license, version 2.0. See LICENSE for more details.
Pull requests for Speedy2D are always welcome. Please ensure the following checks pass locally before submitting:
bash
cargo test
cargo test --no-default-features --lib --examples --tests
cargo clippy
cargo +nightly fmt -- --check
cargo doc
Some tests require the ability to create a headless OpenGL context.