0.1.0-0-pre.5
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
# file: crates/engines/engine-v1-sdl/Cargo.toml
|
||||
# version: 1
|
||||
# version: 2
|
||||
|
||||
[package]
|
||||
name = "engine-v1-sdl"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
authors.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
engine-v1-common = { path = "../engine-v1-common" }
|
||||
sdl3.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
[lints.rust]
|
||||
missing_docs = "warn"
|
||||
unsafe_code = "forbid"
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/lib.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unreachable_pub)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
//! SDL boundary for engine generation V1.
|
||||
//!
|
||||
//! The baseline intentionally contains only the boundary type. The external SDL3 dependency is introduced in the dedicated SDL integration delta.
|
||||
//! SDL3 integration for engine generation V1.
|
||||
|
||||
mod runtime;
|
||||
|
||||
/// Re-export of the SDL runtime boundary marker.
|
||||
pub use self::runtime::SdlRuntimeBoundary;
|
||||
#[cfg(test)]
|
||||
mod unit_tests;
|
||||
|
||||
pub use crate::runtime::SdlRuntime;
|
||||
|
||||
@@ -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(());
|
||||
}
|
||||
}
|
||||
|
||||
5
crates/engines/engine-v1-sdl/src/unit_tests.rs
Normal file
5
crates/engines/engine-v1-sdl/src/unit_tests.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/unit_tests.rs
|
||||
// version: 1
|
||||
|
||||
#[path = "../unit_tests/runtime.rs"]
|
||||
mod runtime;
|
||||
11
crates/engines/engine-v1-sdl/unit_tests/runtime.rs
Normal file
11
crates/engines/engine-v1-sdl/unit_tests/runtime.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
// file: crates/engines/engine-v1-sdl/unit_tests/runtime.rs
|
||||
// version: 1
|
||||
|
||||
//! Unit tests for SDL runtime configuration.
|
||||
|
||||
use engine_v1_sdl::SdlRuntime;
|
||||
|
||||
#[test]
|
||||
fn runtime_configuration_constructs() {
|
||||
let _runtime = SdlRuntime::new("test", 320, 240, 16);
|
||||
}
|
||||
Reference in New Issue
Block a user