77 lines
3.7 KiB
Rust
77 lines
3.7 KiB
Rust
// file: crates/engines/engine-v1-sdl/src/runtime.rs
|
|
// version: 5
|
|
|
|
/// Minimal SDL3 runtime used by Desktop POC runners.
|
|
pub struct SdlRuntime {
|
|
title: std::string::String,
|
|
width: u32,
|
|
height: u32,
|
|
frame_duration: std::time::Duration,
|
|
}
|
|
|
|
impl SdlRuntime {
|
|
/// Creates a minimal SDL3 runtime configuration.
|
|
#[must_use]
|
|
pub fn new(title: impl std::convert::Into<std::string::String>, width: u32, height: u32, frame_duration: std::time::Duration) -> Self {
|
|
return Self { title: title.into(), width, height, frame_duration };
|
|
}
|
|
|
|
/// Runs a game until the user closes the window or presses Escape.
|
|
pub fn run<G>(&self, game: &mut G) -> std::result::Result<(), std::string::String>
|
|
where
|
|
G: engine_v1_common::EngineGame,
|
|
{
|
|
let sdl = match sdl3::init() {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
|
};
|
|
let video = match sdl.video() {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
|
};
|
|
let window = match video.window(&self.title, self.width, self.height).position_centered().resizable().build() {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
|
};
|
|
let mut canvas = window.into_canvas();
|
|
let mut events = match sdl.event_pump() {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
|
};
|
|
let mut runner = engine_v1_common::FixedStepRunner::new(self.frame_duration);
|
|
let mut pointer = engine_v1_common::PointerState::inactive();
|
|
tracing::info!(title = %self.title, width = self.width, height = self.height, "SDL3 runtime started");
|
|
'running: loop {
|
|
let mut input = engine_v1_common::InputState::none().with_pointer(pointer);
|
|
for event in events.poll_iter() {
|
|
match event {
|
|
sdl3::event::Event::Quit { .. } => break 'running,
|
|
sdl3::event::Event::KeyDown { keycode: Some(sdl3::keyboard::Keycode::Escape), .. } => break 'running,
|
|
sdl3::event::Event::KeyDown { keycode: Some(sdl3::keyboard::Keycode::Space), .. } => {
|
|
input = input.with_action(engine_v1_common::GameAction::Primary, true);
|
|
},
|
|
sdl3::event::Event::FingerDown { x, y, .. } | sdl3::event::Event::FingerMotion { x, y, .. } => {
|
|
pointer = engine_v1_common::PointerState::normalized(true, x, y);
|
|
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
|
|
},
|
|
sdl3::event::Event::FingerUp { x, y, .. } | sdl3::event::Event::FingerCanceled { x, y, .. } => {
|
|
pointer = engine_v1_common::PointerState::normalized(false, x, y);
|
|
input = input.with_pointer(pointer);
|
|
},
|
|
_ => {},
|
|
}
|
|
}
|
|
if pointer.active() {
|
|
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
|
|
}
|
|
runner.tick(game, input);
|
|
canvas.set_draw_color(sdl3::pixels::Color::RGB(18, 18, 24));
|
|
canvas.clear();
|
|
canvas.present();
|
|
std::thread::sleep(self.frame_duration);
|
|
}
|
|
tracing::info!(frames = runner.next_frame_index(), "SDL3 runtime stopped");
|
|
return std::result::Result::Ok(());
|
|
}
|
|
}
|