0.1.0-0-pre.10
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// file: crates/engines/engine-v1-sdl/src/runtime.rs
|
||||
// version: 5
|
||||
// version: 6
|
||||
|
||||
/// Minimal SDL3 runtime used by Desktop POC runners.
|
||||
/// Minimal SDL3 runtime used by Desktop and Android POC runners.
|
||||
pub struct SdlRuntime {
|
||||
title: std::string::String,
|
||||
width: u32,
|
||||
@@ -47,13 +47,26 @@ impl SdlRuntime {
|
||||
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::MouseButtonDown { mouse_btn: sdl3::mouse::MouseButton::Left, x, y, .. } => {
|
||||
pointer = normalize_mouse_pointer(&canvas, true, x, y);
|
||||
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
|
||||
},
|
||||
sdl3::event::Event::FingerDown { x, y, .. } | sdl3::event::Event::FingerMotion { x, y, .. } => {
|
||||
sdl3::event::Event::MouseButtonUp { mouse_btn: sdl3::mouse::MouseButton::Left, x, y, .. } => {
|
||||
pointer = normalize_mouse_pointer(&canvas, false, x, y);
|
||||
input = input.with_pointer(pointer);
|
||||
},
|
||||
sdl3::event::Event::MouseMotion { x, y, .. } if pointer.active() => {
|
||||
pointer = normalize_mouse_pointer(&canvas, true, x, y);
|
||||
input = input.with_pointer(pointer);
|
||||
},
|
||||
sdl3::event::Event::FingerDown { 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::FingerMotion { x, y, .. } => {
|
||||
pointer = engine_v1_common::PointerState::normalized(true, x, y);
|
||||
input = input.with_pointer(pointer);
|
||||
},
|
||||
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);
|
||||
@@ -61,16 +74,48 @@ impl SdlRuntime {
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
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();
|
||||
match render_scene(&mut canvas, game.scene()) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
}
|
||||
std::thread::sleep(self.frame_duration);
|
||||
}
|
||||
tracing::info!(frames = runner.next_frame_index(), "SDL3 runtime stopped");
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_mouse_pointer(canvas: &sdl3::render::WindowCanvas, active: bool, x: f32, y: f32) -> engine_v1_common::PointerState {
|
||||
let (width, height) = canvas.window().size();
|
||||
if width == 0 || height == 0 {
|
||||
return engine_v1_common::PointerState::normalized(active, 0.0, 0.0);
|
||||
}
|
||||
return engine_v1_common::PointerState::normalized(active, x / width as f32, y / height as f32);
|
||||
}
|
||||
|
||||
fn render_scene(canvas: &mut sdl3::render::WindowCanvas, scene: engine_v1_common::EngineScene) -> std::result::Result<(), std::string::String> {
|
||||
let background = scene.background();
|
||||
canvas.set_draw_color(sdl3::pixels::Color::RGBA(background.red(), background.green(), background.blue(), background.alpha()));
|
||||
canvas.clear();
|
||||
let (width, height) = match canvas.output_size() {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
||||
};
|
||||
for rectangle in scene.rectangles() {
|
||||
let rectangle = match rectangle {
|
||||
Some(value) => *value,
|
||||
None => continue,
|
||||
};
|
||||
let color = rectangle.color();
|
||||
let rect = rectangle.rect();
|
||||
canvas.set_draw_color(sdl3::pixels::Color::RGBA(color.red(), color.green(), color.blue(), color.alpha()));
|
||||
let physical = sdl3::rect::FRect::new(rect.x() * width as f32, rect.y() * height as f32, rect.width() * width as f32, rect.height() * height as f32);
|
||||
match canvas.fill_rect(physical) {
|
||||
std::result::Result::Ok(()) => {},
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
canvas.present();
|
||||
return std::result::Result::Ok(());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user