0.1.0-0-pre.5-fix.1
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
# file: crates/apps/game-reflex-poc-desktop/Cargo.toml
|
||||
# version: 2
|
||||
# version: 4
|
||||
|
||||
[package]
|
||||
name = "game-reflex-poc-desktop"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
authors.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
engine-v1-common = { path = "../../engines/engine-v1-common" }
|
||||
engine-v1-platform-api = { path = "../../engines/engine-v1-platform-api" }
|
||||
engine-v1-sdl = { path = "../../engines/engine-v1-sdl" }
|
||||
game-logging-lib = { path = "../../common/game-logging-lib" }
|
||||
game-reflex-poc = { path = "../../games/game-reflex-poc" }
|
||||
tracing.workspace = true
|
||||
|
||||
[lints.rust]
|
||||
missing_docs = "warn"
|
||||
unsafe_code = "forbid"
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
// file: crates/apps/game-reflex-poc-desktop/src/main.rs
|
||||
// version: 3
|
||||
// version: 4
|
||||
|
||||
use engine_v1_sdl::SdlRuntime;
|
||||
use game_reflex_poc::ReflexGame;
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unreachable_pub)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let _guard = game_logging_lib::init_default_tracing("games::runner").map_err(|error| error.to_string())?;
|
||||
tracing::info!(game = "reflex", "desktop SDL3 runner started");
|
||||
//! SDL3 Desktop development runner for the Reflex POC game library.
|
||||
|
||||
let runtime = SdlRuntime::new("Reflex POC", 720, 1280, 16);
|
||||
let mut game = ReflexGame::default();
|
||||
return runtime.run(&mut game);
|
||||
fn main() {
|
||||
let _logging_guard = match game_logging_lib::init_console_tracing() {
|
||||
std::result::Result::Ok(guard) => guard,
|
||||
std::result::Result::Err(error) => {
|
||||
eprintln!("failed to initialize tracing: {error}");
|
||||
return;
|
||||
},
|
||||
};
|
||||
tracing::info!(target: "games::runner", game = "reflex", "desktop SDL3 runner started");
|
||||
let _monetization = engine_v1_platform_api::MonetizationCapabilities::disabled();
|
||||
let runtime = engine_v1_sdl::SdlRuntime::new("Reflex POC", 720, 1280, std::time::Duration::from_millis(16));
|
||||
let mut state = game_reflex_poc::ReflexState::new();
|
||||
match runtime.run(&mut state) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => {
|
||||
tracing::error!(target: "games::runner", game = "reflex", %error, "desktop SDL3 runner failed");
|
||||
eprintln!("Reflex POC desktop runner failed: {error}");
|
||||
},
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
# file: crates/apps/game-snake-poc-desktop/Cargo.toml
|
||||
# version: 2
|
||||
# version: 4
|
||||
|
||||
[package]
|
||||
name = "game-snake-poc-desktop"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
authors.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
engine-v1-common = { path = "../../engines/engine-v1-common" }
|
||||
engine-v1-platform-api = { path = "../../engines/engine-v1-platform-api" }
|
||||
engine-v1-sdl = { path = "../../engines/engine-v1-sdl" }
|
||||
game-logging-lib = { path = "../../common/game-logging-lib" }
|
||||
game-snake-poc = { path = "../../games/game-snake-poc" }
|
||||
tracing.workspace = true
|
||||
|
||||
[lints.rust]
|
||||
missing_docs = "warn"
|
||||
unsafe_code = "forbid"
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
// file: crates/apps/game-snake-poc-desktop/src/main.rs
|
||||
// version: 3
|
||||
// version: 4
|
||||
|
||||
use engine_v1_sdl::SdlRuntime;
|
||||
use game_snake_poc::SnakeGame;
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unreachable_pub)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let _guard = game_logging_lib::init_default_tracing("games::runner").map_err(|error| error.to_string())?;
|
||||
tracing::info!(game = "snake", "desktop SDL3 runner started");
|
||||
//! SDL3 Desktop development runner for the Snake POC game library.
|
||||
|
||||
let runtime = SdlRuntime::new("Snake POC", 720, 1280, 16);
|
||||
let mut game = SnakeGame::default();
|
||||
return runtime.run(&mut game);
|
||||
fn main() {
|
||||
let _logging_guard = match game_logging_lib::init_console_tracing() {
|
||||
std::result::Result::Ok(guard) => guard,
|
||||
std::result::Result::Err(error) => {
|
||||
eprintln!("failed to initialize tracing: {error}");
|
||||
return;
|
||||
},
|
||||
};
|
||||
tracing::info!(target: "games::runner", game = "snake", "desktop SDL3 runner started");
|
||||
let _monetization = engine_v1_platform_api::MonetizationCapabilities::disabled();
|
||||
let runtime = engine_v1_sdl::SdlRuntime::new("Snake POC", 720, 1280, std::time::Duration::from_millis(16));
|
||||
let mut state = game_snake_poc::SnakeState::new();
|
||||
match runtime.run(&mut state) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => {
|
||||
tracing::error!(target: "games::runner", game = "snake", %error, "desktop SDL3 runner failed");
|
||||
eprintln!("Snake POC desktop runner failed: {error}");
|
||||
},
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
# file: crates/engines/engine-v1-sdl/Cargo.toml
|
||||
# version: 2
|
||||
# version: 3
|
||||
|
||||
[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.rust]
|
||||
missing_docs = "warn"
|
||||
unsafe_code = "forbid"
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
game-logging-lib = { path = "../../common/game-logging-lib" }
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/lib.rs
|
||||
// version: 2
|
||||
// version: 3
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unreachable_pub)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
//! SDL3 integration for engine generation V1.
|
||||
|
||||
mod runtime;
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests;
|
||||
|
||||
pub use crate::runtime::SdlRuntime;
|
||||
/// Re-export of the minimal SDL3 runtime used by Desktop POC runners.
|
||||
pub use self::runtime::SdlRuntime;
|
||||
|
||||
@@ -1,84 +1,68 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/runtime.rs
|
||||
// version: 1
|
||||
|
||||
use sdl3::event::Event;
|
||||
use sdl3::keyboard::Keycode;
|
||||
use sdl3::pixels::Color;
|
||||
|
||||
use engine_v1_common::{Action, EngineGame, FixedStepRunner, InputState};
|
||||
// version: 2
|
||||
|
||||
/// Minimal SDL3 runtime used by Desktop POC runners.
|
||||
pub struct SdlRuntime {
|
||||
title: String,
|
||||
title: std::string::String,
|
||||
width: u32,
|
||||
height: u32,
|
||||
frame_duration_ms: u64,
|
||||
frame_duration: std::time::Duration,
|
||||
}
|
||||
|
||||
impl SdlRuntime {
|
||||
/// Creates a minimal SDL3 runtime configuration.
|
||||
#[must_use]
|
||||
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,
|
||||
};
|
||||
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: 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())?;
|
||||
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().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 = 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"
|
||||
);
|
||||
|
||||
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);
|
||||
tracing::info!(title = %self.title, width = self.width, height = self.height, "SDL3 runtime started");
|
||||
'running: loop {
|
||||
let mut input = InputState::default();
|
||||
|
||||
let mut input = engine_v1_common::InputState::none();
|
||||
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);
|
||||
}
|
||||
_ => {}
|
||||
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);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
runner.tick(game, &input);
|
||||
|
||||
canvas.set_draw_color(Color::RGB(18, 18, 24));
|
||||
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!("SDL3 runtime stopped");
|
||||
return Ok(());
|
||||
tracing::info!(frames = runner.next_frame_index(), "SDL3 runtime stopped");
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../unit_tests/runtime.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/unit_tests.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../unit_tests/runtime.rs"]
|
||||
mod runtime;
|
||||
mod tests;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
// file: crates/engines/engine-v1-sdl/unit_tests/runtime.rs
|
||||
// version: 1
|
||||
|
||||
//! Unit tests for SDL runtime configuration.
|
||||
|
||||
use engine_v1_sdl::SdlRuntime;
|
||||
// version: 2
|
||||
|
||||
#[test]
|
||||
fn runtime_configuration_constructs() {
|
||||
let _runtime = SdlRuntime::new("test", 320, 240, 16);
|
||||
game_logging_lib::with_test_tracing("runtime_configuration_constructs", || {
|
||||
let _runtime = crate::SdlRuntime::new("test", 320, 240, std::time::Duration::from_millis(16));
|
||||
tracing::info!("SDL3 runtime configuration constructed");
|
||||
return;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user