0.1.0-0-pre.5
This commit is contained in:
@@ -1,14 +1,84 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/runtime.rs
|
||||
// version: 1
|
||||
|
||||
/// Marker representing the future SDL3 runtime boundary for engine V1.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub struct SdlRuntimeBoundary;
|
||||
use sdl3::event::Event;
|
||||
use sdl3::keyboard::Keycode;
|
||||
use sdl3::pixels::Color;
|
||||
|
||||
impl SdlRuntimeBoundary {
|
||||
/// Returns the engine generation served by this boundary.
|
||||
use engine_v1_common::{Action, EngineGame, FixedStepRunner, InputState};
|
||||
|
||||
/// Minimal SDL3 runtime used by Desktop POC runners.
|
||||
pub struct SdlRuntime {
|
||||
title: String,
|
||||
width: u32,
|
||||
height: u32,
|
||||
frame_duration_ms: u64,
|
||||
}
|
||||
|
||||
impl SdlRuntime {
|
||||
/// Creates a minimal SDL3 runtime configuration.
|
||||
#[must_use]
|
||||
pub const fn engine_generation() -> u16 {
|
||||
return 1;
|
||||
pub fn new(title: impl Into<String>, width: u32, height: u32, frame_duration_ms: u64) -> Self {
|
||||
return Self {
|
||||
title: title.into(),
|
||||
width,
|
||||
height,
|
||||
frame_duration_ms,
|
||||
};
|
||||
}
|
||||
|
||||
/// Runs a game until the user closes the window or presses Escape.
|
||||
pub fn run<G: EngineGame>(&self, game: &mut G) -> Result<(), String> {
|
||||
let sdl = sdl3::init().map_err(|error| error.to_string())?;
|
||||
let video = sdl.video().map_err(|error| error.to_string())?;
|
||||
let window = video
|
||||
.window(&self.title, self.width, self.height)
|
||||
.position_centered()
|
||||
.build()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut canvas = window.into_canvas();
|
||||
let mut events = sdl.event_pump().map_err(|error| error.to_string())?;
|
||||
let mut runner = FixedStepRunner::new(self.frame_duration_ms);
|
||||
|
||||
tracing::info!(
|
||||
title = %self.title,
|
||||
width = self.width,
|
||||
height = self.height,
|
||||
"SDL3 runtime started"
|
||||
);
|
||||
|
||||
'running: loop {
|
||||
let mut input = InputState::default();
|
||||
|
||||
for event in events.poll_iter() {
|
||||
match event {
|
||||
Event::Quit { .. } => {
|
||||
break 'running;
|
||||
}
|
||||
Event::KeyDown {
|
||||
keycode: Some(Keycode::Escape),
|
||||
..
|
||||
} => {
|
||||
break 'running;
|
||||
}
|
||||
Event::KeyDown {
|
||||
keycode: Some(Keycode::Space),
|
||||
..
|
||||
} => {
|
||||
input.set(Action::Primary, true);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
runner.tick(game, &input);
|
||||
|
||||
canvas.set_draw_color(Color::RGB(18, 18, 24));
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
}
|
||||
|
||||
tracing::info!("SDL3 runtime stopped");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user