0.1.0-0-pre.11-fix.5
This commit is contained in:
@@ -1,11 +1,19 @@
|
||||
// file: crates/engines/engine-v1-common/src/game_loop.rs
|
||||
// version: 3
|
||||
// version: 4
|
||||
|
||||
/// Minimal update contract implemented by a game state consumed by engine V1.
|
||||
pub trait EngineGame {
|
||||
/// Advances the game by one engine update using platform-independent input.
|
||||
fn update(&mut self, frame: crate::EngineFrame, input: crate::InputState);
|
||||
|
||||
/// Handles a platform-independent request to leave the current runtime.
|
||||
///
|
||||
/// Games may mutate their state and return [`crate::QuitDecision::Continue`] to implement
|
||||
/// pause/confirmation screens, navigation, score submission, saving, or other workflows.
|
||||
fn quit_requested(&mut self, _request: crate::QuitRequest) -> crate::QuitDecision {
|
||||
return crate::QuitDecision::Exit;
|
||||
}
|
||||
|
||||
/// Produces the platform-independent scene rendered after the update.
|
||||
fn scene(&self) -> crate::EngineScene {
|
||||
return crate::EngineScene::empty(crate::RenderColor::rgb(18, 18, 24));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/engines/engine-v1-common/src/lib.rs
|
||||
// version: 4
|
||||
// version: 5
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unreachable_pub)]
|
||||
@@ -12,6 +12,7 @@ mod frame;
|
||||
mod game_loop;
|
||||
mod input;
|
||||
mod pointer;
|
||||
mod quit;
|
||||
mod render;
|
||||
|
||||
/// Re-export of the canonical logical input action used by engine V1 games.
|
||||
@@ -26,6 +27,12 @@ pub use self::game_loop::FixedStepRunner;
|
||||
pub use self::input::InputState;
|
||||
/// Re-export of the normalized primary pointer snapshot.
|
||||
pub use self::pointer::PointerState;
|
||||
/// Re-export of a game's response to a quit request.
|
||||
pub use self::quit::QuitDecision;
|
||||
/// Re-export of the platform-independent quit request.
|
||||
pub use self::quit::QuitRequest;
|
||||
/// Re-export of the platform-level source of a quit request.
|
||||
pub use self::quit::QuitSource;
|
||||
/// Re-export of the maximum engine scene rectangle capacity.
|
||||
pub use self::render::ENGINE_SCENE_RECT_CAPACITY;
|
||||
/// Re-export of the platform-independent scene snapshot.
|
||||
|
||||
42
crates/engines/engine-v1-common/src/quit.rs
Normal file
42
crates/engines/engine-v1-common/src/quit.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
// file: crates/engines/engine-v1-common/src/quit.rs
|
||||
// version: 1
|
||||
|
||||
/// Origin of a platform-independent request to leave the current game runtime.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum QuitSource {
|
||||
/// The operating-system or window manager requested the window to close.
|
||||
WindowClose,
|
||||
/// The Desktop Escape key requested leaving the runtime.
|
||||
Escape,
|
||||
/// The platform-native Back action requested leaving the runtime.
|
||||
PlatformBack,
|
||||
}
|
||||
|
||||
/// Decision returned by a game when the runtime requests to leave.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum QuitDecision {
|
||||
/// Leave the current runtime immediately.
|
||||
Exit,
|
||||
/// Keep the runtime active after the game handled the request.
|
||||
Continue,
|
||||
}
|
||||
|
||||
/// Platform-independent quit request delivered to a game.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct QuitRequest {
|
||||
source: QuitSource,
|
||||
}
|
||||
|
||||
impl QuitRequest {
|
||||
/// Creates a quit request from the supplied source.
|
||||
#[must_use]
|
||||
pub const fn new(source: QuitSource) -> Self {
|
||||
return Self { source };
|
||||
}
|
||||
|
||||
/// Returns the physical or platform-level source of the request.
|
||||
#[must_use]
|
||||
pub const fn source(self) -> QuitSource {
|
||||
return self.source;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/engines/engine-v1-common/unit_tests/game_loop.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
#[derive(Default)]
|
||||
struct CountingGame {
|
||||
@@ -28,3 +28,20 @@ fn fixed_step_runner_advances_index_and_elapsed_time() {
|
||||
assert_eq!(game.last_frame.map(crate::EngineFrame::index), std::option::Option::Some(1));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_quit_policy_exits() {
|
||||
game_logging_lib::with_test_tracing("default_quit_policy_exits", || {
|
||||
let mut game = CountingGame::default();
|
||||
let request = crate::QuitRequest::new(crate::QuitSource::Escape);
|
||||
assert_eq!(crate::EngineGame::quit_requested(&mut game, request), crate::QuitDecision::Exit);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quit_request_preserves_its_source() {
|
||||
game_logging_lib::with_test_tracing("quit_request_preserves_its_source", || {
|
||||
let request = crate::QuitRequest::new(crate::QuitSource::PlatformBack);
|
||||
assert_eq!(request.source(), crate::QuitSource::PlatformBack);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/runtime.rs
|
||||
// version: 10
|
||||
// version: 11
|
||||
|
||||
/// Minimal SDL3 runtime used by Desktop and Android POC runners.
|
||||
pub struct SdlRuntime {
|
||||
@@ -50,9 +50,22 @@ impl SdlRuntime {
|
||||
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::AcBack), .. } => break 'running,
|
||||
sdl3::event::Event::Quit { .. } => {
|
||||
if quit_requested(game, engine_v1_common::QuitSource::WindowClose) {
|
||||
break 'running;
|
||||
}
|
||||
},
|
||||
sdl3::event::Event::KeyDown { keycode: Some(sdl3::keyboard::Keycode::Escape), .. } => {
|
||||
if quit_requested(game, engine_v1_common::QuitSource::Escape) {
|
||||
break 'running;
|
||||
}
|
||||
},
|
||||
sdl3::event::Event::KeyDown { keycode: Some(sdl3::keyboard::Keycode::AcBack), .. }
|
||||
| sdl3::event::Event::KeyDown { scancode: Some(sdl3::keyboard::Scancode::AcBack), .. } => {
|
||||
if quit_requested(game, engine_v1_common::QuitSource::PlatformBack) {
|
||||
break 'running;
|
||||
}
|
||||
},
|
||||
sdl3::event::Event::KeyDown { keycode: Some(sdl3::keyboard::Keycode::Left), repeat: false, .. } => {
|
||||
input = input.with_action(engine_v1_common::GameAction::Left, true);
|
||||
},
|
||||
@@ -67,9 +80,6 @@ impl SdlRuntime {
|
||||
},
|
||||
sdl3::event::Event::MouseButtonDown { mouse_btn: sdl3::mouse::MouseButton::Left, x, y, .. } => {
|
||||
pointer = normalize_mouse_pointer(&canvas, true, x, y);
|
||||
if platform_quit_control_hit(pointer) {
|
||||
break 'running;
|
||||
}
|
||||
gesture_start = Some(pointer);
|
||||
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
|
||||
},
|
||||
@@ -86,9 +96,6 @@ impl SdlRuntime {
|
||||
},
|
||||
sdl3::event::Event::FingerDown { x, y, .. } => {
|
||||
pointer = engine_v1_common::PointerState::normalized(true, x, y);
|
||||
if platform_quit_control_hit(pointer) {
|
||||
break 'running;
|
||||
}
|
||||
gesture_start = Some(pointer);
|
||||
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
|
||||
},
|
||||
@@ -153,70 +160,18 @@ fn render_scene(canvas: &mut sdl3::render::WindowCanvas, scene: engine_v1_common
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
match render_platform_controls(canvas, width, height) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
}
|
||||
canvas.present();
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
|
||||
const SWIPE_DIRECTION_THRESHOLD: f32 = 0.04;
|
||||
|
||||
fn platform_quit_control_hit(pointer: engine_v1_common::PointerState) -> bool {
|
||||
#[cfg(target_os = "android")]
|
||||
{
|
||||
return quit_control_hit(pointer);
|
||||
}
|
||||
#[cfg(not(target_os = "android"))]
|
||||
{
|
||||
let _ = pointer;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fn quit_control_hit(pointer: engine_v1_common::PointerState) -> bool {
|
||||
return pointer.x() >= 0.86 && pointer.x() <= 0.99 && pointer.y() >= 0.015 && pointer.y() <= 0.105;
|
||||
}
|
||||
|
||||
fn render_platform_controls(canvas: &mut sdl3::render::WindowCanvas, width: u32, height: u32) -> std::result::Result<(), std::string::String> {
|
||||
#[cfg(target_os = "android")]
|
||||
{
|
||||
let button = sdl3::render::FRect::new(0.86 * width as f32, 0.015 * height as f32, 0.13 * width as f32, 0.09 * height as f32);
|
||||
canvas.set_draw_color(sdl3::pixels::Color::RGBA(176, 48, 48, 232));
|
||||
match canvas.fill_rect(button) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
||||
}
|
||||
|
||||
canvas.set_draw_color(sdl3::pixels::Color::RGBA(255, 255, 255, 255));
|
||||
let marks = [
|
||||
(0.885_f32, 0.032_f32),
|
||||
(0.905_f32, 0.044_f32),
|
||||
(0.925_f32, 0.056_f32),
|
||||
(0.945_f32, 0.068_f32),
|
||||
(0.965_f32, 0.080_f32),
|
||||
(0.965_f32, 0.032_f32),
|
||||
(0.945_f32, 0.044_f32),
|
||||
(0.925_f32, 0.056_f32),
|
||||
(0.905_f32, 0.068_f32),
|
||||
(0.885_f32, 0.080_f32),
|
||||
];
|
||||
for (x, y) in marks {
|
||||
let mark = sdl3::render::FRect::new(x * width as f32, y * height as f32, 0.012 * width as f32, 0.012 * height as f32);
|
||||
match canvas.fill_rect(mark) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_os = "android"))]
|
||||
{
|
||||
let _ = canvas;
|
||||
let _ = width;
|
||||
let _ = height;
|
||||
}
|
||||
return std::result::Result::Ok(());
|
||||
fn quit_requested<G>(game: &mut G, source: engine_v1_common::QuitSource) -> bool
|
||||
where
|
||||
G: engine_v1_common::EngineGame,
|
||||
{
|
||||
let request = engine_v1_common::QuitRequest::new(source);
|
||||
return game.quit_requested(request) == engine_v1_common::QuitDecision::Exit;
|
||||
}
|
||||
|
||||
fn apply_swipe_direction(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/engines/engine-v1-sdl/unit_tests/swipe.rs
|
||||
// version: 2
|
||||
// version: 3
|
||||
|
||||
fn pointer(x: f32, y: f32) -> engine_v1_common::PointerState {
|
||||
return engine_v1_common::PointerState::normalized(true, x, y);
|
||||
@@ -26,9 +26,25 @@ fn vertical_swipe_uses_dominant_axis() {
|
||||
assert!(input.is_active(engine_v1_common::GameAction::Up));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quit_control_hit_is_limited_to_top_right_region() {
|
||||
assert!(super::quit_control_hit(pointer(0.92, 0.05)));
|
||||
assert!(!super::quit_control_hit(pointer(0.50, 0.05)));
|
||||
assert!(!super::quit_control_hit(pointer(0.92, 0.50)));
|
||||
#[derive(Default)]
|
||||
struct QuitAwareGame {
|
||||
requests: u64,
|
||||
}
|
||||
|
||||
impl engine_v1_common::EngineGame for QuitAwareGame {
|
||||
fn update(&mut self, _frame: engine_v1_common::EngineFrame, _input: engine_v1_common::InputState) {
|
||||
return;
|
||||
}
|
||||
|
||||
fn quit_requested(&mut self, _request: engine_v1_common::QuitRequest) -> engine_v1_common::QuitDecision {
|
||||
self.requests = self.requests.saturating_add(1);
|
||||
return engine_v1_common::QuitDecision::Continue;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_respects_game_quit_decision() {
|
||||
let mut game = QuitAwareGame::default();
|
||||
assert!(!super::quit_requested(&mut game, engine_v1_common::QuitSource::PlatformBack));
|
||||
assert_eq!(game.requests, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user