Quick start
Install SDL2 (with SDL2_ttf and SDL2_image), add pushrod, pushrod-widgets and pushrod-sdl2 to Cargo.toml, copy a font into assets/, paste one of the two programs below into src/main.rs, and run cargo run. Pick the style you prefer: callbacks (Pushrod runs the loop and calls your code) or your own event loop (you ask Pushrod for the next event and match on it).
You'll build this: a label and a button; each click updates the label.
1. Install SDL2
Ubuntu/Debian:
sudo apt install libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev
macOS (Homebrew): brew install sdl2 sdl2_image sdl2_ttf. Other systems: see Installation.
2. Create a project
cargo new hello-pushrod
cd hello-pushrod
3. Add the dependencies
In Cargo.toml:
[dependencies]
pushrod = { git = "https://github.com/KenSuenobu/rust-pushrod" }
pushrod-widgets = { git = "https://github.com/KenSuenobu/rust-pushrod" }
pushrod-sdl2 = { git = "https://github.com/KenSuenobu/rust-pushrod" }
pushrod is the core (engine, events, drawing); pushrod-widgets has the standard widgets; pushrod-sdl2 is the SDL2 backend that opens the window and draws. Your application does not depend on sdl2 itself. Pushrod requires Rust 1.100 or newer (edition 2024).
4. Add a font
Pushrod draws text with a TrueType font. Copy the one that ships with Pushrod:
mkdir assets
curl -L -o assets/OpenSans-Regular.ttf https://github.com/KenSuenobu/rust-pushrod/raw/main/assets/OpenSans-Regular.ttf
5. Write the program
Replace src/main.rs with either version. Both build the same window and behave the same way.
- Callbacks
- Event loop
Pushrod runs the event loop; you register a callback for the button.
use pushrod_widgets::button_widget::ButtonWidget;
use pushrod::engine::Engine;
use pushrod_sdl2::WindowSettings;
use pushrod::geometry::{point, size};
use pushrod_widgets::text_widget::{TextJustify, TextWidget};
use pushrod::color::{Color, FontStyle};
const FONT: &str = "assets/OpenSans-Regular.ttf";
/// Application state: passed to every callback as `&mut Counter`.
#[derive(Default)]
struct Counter {
clicks: u32,
}
fn main() -> Result<(), pushrod::error::Error> {
let mut engine: Engine<Counter> = Engine::new(size(400, 140), 60);
let label = engine.add_widget(TextWidget::new(
point(20, 20),
size(360, 24),
FONT.into(),
FontStyle::NORMAL,
16,
Color::BLACK,
TextJustify::Left,
"Not clicked yet.".into(),
));
let button = engine.add_widget(ButtonWidget::new(
point(20, 70),
size(120, 30),
FONT.into(),
FontStyle::NORMAL,
16,
Color::BLACK,
TextJustify::Center,
"Click me".into(),
2,
));
engine.on_click(button, move |state, ui| {
state.clicks += 1;
if let Some(text) = ui.get_mut::<TextWidget>(label) {
text.set_text(format!("Clicked {} time(s).", state.clicks));
}
});
let state = pushrod_sdl2::run(engine, WindowSettings::new("Hello, Pushrod", size(400, 140)), Counter::default())?;
println!("Clicked {} time(s) in total.", state.clicks);
Ok(())
}
You run the loop: next_event waits for input, keeps the screen up to date, and returns each event the widgets generate; match on the ones you care about. It returns None when the window closes.
use pushrod_widgets::button_widget::ButtonWidget;
use pushrod::engine::Engine;
use pushrod_sdl2::WindowSettings;
use pushrod::event::Event;
use pushrod::geometry::{point, size};
use pushrod_widgets::text_widget::{TextJustify, TextWidget};
use pushrod::color::{Color, FontStyle};
const FONT: &str = "assets/OpenSans-Regular.ttf";
fn main() -> Result<(), pushrod::error::Error> {
let mut engine: Engine = Engine::new(size(400, 140), 60);
let label = engine.add_widget(TextWidget::new(
point(20, 20),
size(360, 24),
FONT.into(),
FontStyle::NORMAL,
16,
Color::BLACK,
TextJustify::Left,
"Not clicked yet.".into(),
));
let button = engine.add_widget(ButtonWidget::new(
point(20, 70),
size(120, 30),
FONT.into(),
FontStyle::NORMAL,
16,
Color::BLACK,
TextJustify::Center,
"Click me".into(),
2,
));
let mut clicks = 0;
let mut events = pushrod_sdl2::event_loop(&mut engine, WindowSettings::new("Hello, Pushrod", size(400, 140)))?;
// Your own event loop: next_event() waits for input, keeps the screen up to date,
// and returns each event the widgets generate. It returns None when the window closes.
while let Some(event) = engine.next_event(&mut events) {
match event {
Event::Clicked { id, .. } if id == button => {
clicks += 1;
if let Some(text) = engine.widgets().get_as_mut::<TextWidget>(label) {
text.set_text(format!("Clicked {} time(s).", clicks));
}
}
_ => {}
}
}
println!("Clicked {} time(s) in total.", clicks);
Ok(())
}
6. Run it
cargo run
Click the button: the label counts your clicks. Close the window and the final count is printed, because engine.run returns your application state.
What just happened
Engineowns the window's widget tree. With callbacks, its type parameter is your state type (Engine<Counter>); in the event-loop version you keep state in local variables, so it's plainEngine.add_widgetadded each widget to the window and returned itsWidgetId.- Callbacks:
on_click(button, ..)registered code that runs for that button. It receives&mut Counterand aUihandle, which finds widgets by ID (ui.get_mut::<TextWidget>(label)).pushrod_sdl2::runran the loop until the window closed, then gave the state back. - Event loop:
pushrod_sdl2::event_loop(&mut engine, settings)opened the window for drawing, and eachengine.next_event(&mut events)returned the next event, such asEvent::Clicked { id, button, count }. You change widgets directly throughengine.widgets(). The call blocks while nothing happens, so the loop doesn't spin.
Both styles route input through the same widgets and produce the same events; see Handling events for when to choose which.
Next: Creating an application.