diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b561d637..b777c3db 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,7 +16,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install libsdl - run: sudo apt-get update && sudo apt-get -y install libsdl2-dev + run: sudo apt-get update && sudo apt-get -y install libsdl3-dev - name: Build core, no features run: cargo build -p retrofire-core --verbose --all-targets --no-default-features diff --git a/README b/README index 772014cc..95c7885f 100644 --- a/README +++ b/README @@ -99,7 +99,7 @@ dependencies. However, because `no_std` lacks most floating-point functions, the package is not fully functional unless either the `std`, `libm`, or `mm` feature is enabled. Activating `std` additionally enables APIs that do I/O. -The `front` package depends on either `sdl2`, `minifb`, or `wasm-bindgen` and +The `front` package depends on either `sdl3`, `minifb`, or `wasm-bindgen` and `web-sys`, depending on enabled features. The `geom` package has no external dependencies. It only requires `alloc`; diff --git a/README.md b/README.md index 2e264210..87abd795 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ functions, the package is not fully functional unless either the `std`, `libm`, or `mm` feature is enabled. Activating `std` additionally enables APIs that do I/O. -The `retrofire-front` package depends on either `sdl2`, `minifb`, or +The `retrofire-front` package depends on either `sdl3`, `minifb`, or `wasm-bindgen` and `web-sys`, depending on enabled features. The `retrofire-geom` package has no external dependencies. It only requires diff --git a/demos/Cargo.toml b/demos/Cargo.toml index 4e6e3ac5..a73099b1 100644 --- a/demos/Cargo.toml +++ b/demos/Cargo.toml @@ -26,17 +26,17 @@ re = { version = "0.4.0", path = "..", package = "retrofire" } re-front = { version = "0.4.0", path = "../front", package = "retrofire-front" } minifb = { version = "0.27.0", optional = true } -sdl2 = { version = "0.35.2", optional = true } +sdl3 = { version = "0.16.2", optional = true, features = ["build-from-source"] } pancurses = { version = "0.17.0", optional = true } [features] default = ["minifb"] minifb = ["dep:minifb", "re-front/minifb"] -sdl2 = ["dep:sdl2", "re-front/sdl2"] +sdl3 = ["dep:sdl3", "re-front/sdl3"] [[bin]] name = "crates" -required-features = ["sdl2"] +required-features = ["sdl3"] [[bin]] name = "curses" diff --git a/demos/src/bin/crates.rs b/demos/src/bin/crates.rs index 555659b3..0f84b30c 100644 --- a/demos/src/bin/crates.rs +++ b/demos/src/bin/crates.rs @@ -13,7 +13,7 @@ use re::core::render::{ // Try also Rgb565 or Rgba4444 use re::core::util::{pixfmt::Rgba8888, pnm::read_pnm}; -use re::front::sdl2::Window; +use re::front::sdl3::Window; use re::geom::solids::{Build, Cube}; fn main() { @@ -66,7 +66,7 @@ fn main() { let ep = &frame.win.ev_pump; for key in ep.keyboard_state().pressed_scancodes() { - use sdl2::keyboard::Scancode as Sc; + use sdl3::keyboard::Scancode as Sc; match key { Sc::W => cam_vel[2] += 4.0, Sc::S => cam_vel[2] -= 2.0, diff --git a/front/Cargo.toml b/front/Cargo.toml index 2d22f058..2d54cef7 100644 --- a/front/Cargo.toml +++ b/front/Cargo.toml @@ -28,7 +28,7 @@ wasm-dev = ["wasm", "dep:console_error_panic_hook"] [dependencies] retrofire-core = { version = "0.4.0", path = "../core" } minifb = { version = "0.27.0", optional = true } -sdl2 = { version = "0.35.2", optional = true } +sdl3 = { version = "0.16.2", optional = true, features = ["build-from-source"] } wasm-bindgen = { version = "0.2.92", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true } diff --git a/front/README.md b/front/README.md index d62ea872..656a3fba 100644 --- a/front/README.md +++ b/front/README.md @@ -19,14 +19,14 @@ Simple frontends for [`retrofire`][1]. ## Crate features * `minifb`: Enables a frontend using the [`minifb`][2] library. -* `sdl2`: Enables a frontend using the [`sdl2`][3] library. +* `sdl3`: Enables a frontend using the [`sdl3`][3] library. * `wasm` Enables a frontend using WebAssembly and [`wasm-bindgen`][4]. All features are disabled by default. [2]: https://crates.io/crates/minifb -[3]: https://crates.io/crates/sdl2 +[3]: https://crates.io/crates/sdl3 [4]: https://crates.io/crates/wasm-bindgen diff --git a/front/src/lib.rs b/front/src/lib.rs index 8e4507df..86521d2e 100644 --- a/front/src/lib.rs +++ b/front/src/lib.rs @@ -14,8 +14,8 @@ use retrofire_core::{ #[cfg(feature = "minifb")] pub mod minifb; -#[cfg(feature = "sdl2")] -pub mod sdl2; +#[cfg(feature = "sdl3")] +pub mod sdl3; #[no_std] #[cfg(feature = "wasm")] diff --git a/front/src/sdl2.rs b/front/src/sdl3.rs similarity index 87% rename from front/src/sdl2.rs rename to front/src/sdl3.rs index d340c3bc..96e683c0 100644 --- a/front/src/sdl2.rs +++ b/front/src/sdl3.rs @@ -1,12 +1,12 @@ -//! Frontend using the `sdl2` crate for window creation and event handling. +//! Frontend using the `sdl3` crate for window creation and event handling. use core::{cell::RefCell, fmt, mem::replace, ops::ControlFlow}; use std::time::Instant; -use sdl2::{ +use sdl3::{ EventPump, IntegerOrSdlError, Sdl, event::Event, keyboard::Keycode, - pixels::PixelFormatEnum, + pixels::PixelFormat, render::{Texture, TextureValueError, WindowCanvas}, video::{FullscreenType, Window as SdlWindow, WindowBuildError}, }; @@ -27,7 +27,7 @@ use super::{Frame, dims}; /// Helper trait to support different pixel format types. pub trait PixelFmt: Copy + Default { type Pixel: AsRef<[u8]> + Copy + Sized; - const SDL_FMT: PixelFormatEnum; + const SDL_FMT: PixelFormat; fn encode>(self, color: C) -> Self::Pixel { color.into_pixel_fmt(self) @@ -59,7 +59,7 @@ pub struct Builder<'title, PF> { pub title: &'title str, pub vsync: bool, pub hidpi: bool, - pub fs: FullscreenType, + pub fullscreen: bool, pub pixfmt: PF, } @@ -98,8 +98,8 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> { self } /// Sets the fullscreen state of the window. - pub fn fullscreen(mut self, fs: FullscreenType) -> Self { - self.fs = fs; + pub fn fullscreen(mut self, fs: bool) -> Self { + self.fullscreen = fs; self } /// Sets the framebuffer pixel format. @@ -112,10 +112,10 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> { /// Creates the window. pub fn build(self) -> Result, Error> { - let sdl = sdl2::init()?; + let sdl = sdl3::init()?; let win = self.create_window(&sdl)?; - self.set_mouse_mode(&sdl); + self.set_mouse_mode(&sdl, &win); let canvas = self.create_canvas(win)?; let ev_pump = sdl.event_pump()?; @@ -133,28 +133,34 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> { fn create_window(&self, sdl: &Sdl) -> Result { let Self { - dims: (w, h), title, fs, hidpi, .. + dims: (w, h), + title, + fullscreen, + hidpi, + .. } = *self; - let mut win = sdl.video()?.window(title, w, h); + let mut bld = sdl.video()?.window(title, w, h); if hidpi { - win.allow_highdpi(); + bld.high_pixel_density(); } - let mut win = win.build()?; - win.set_fullscreen(fs)?; - Ok(win) + if fullscreen { + bld.fullscreen(); + } + Ok(bld.build()?) } fn create_canvas(&self, w: SdlWindow) -> Result { - let mut canvas = w.into_canvas(); + let canvas = w.into_canvas(); if self.vsync { - canvas = canvas.present_vsync(); + //let _ok = canvas.present(); + //TODO vsync? canvas = canvas.present_vsync(); } - Ok(canvas.accelerated().build()?) + Ok(canvas) } - fn set_mouse_mode(&self, sdl: &Sdl) { + fn set_mouse_mode(&self, sdl: &Sdl, win: &SdlWindow) { let m = sdl.mouse(); - m.set_relative_mouse_mode(true); + m.set_relative_mouse_mode(win, true); m.capture(true); m.show_cursor(true); } @@ -186,7 +192,7 @@ impl, const N: usize> Window { F: FnMut(&mut Frame>>) -> ControlFlow<()>, Color4: IntoPixel, { - let dims @ (w, h) = self.canvas.window().drawable_size(); + let dims @ (w, h) = self.canvas.window().size_in_pixels(); let tc = self.canvas.texture_creator(); let mut tex = tc.create_texture_streaming(PF::SDL_FMT, w, h)?; @@ -256,15 +262,15 @@ impl, const N: usize> Window { impl PixelFmt for Rgba8888 { type Pixel = [u8; 4]; - const SDL_FMT: PixelFormatEnum = PixelFormatEnum::RGBA32; + const SDL_FMT: PixelFormat = PixelFormat::RGBA32; } impl PixelFmt for Rgb565 { type Pixel = [u8; 2]; - const SDL_FMT: PixelFormatEnum = PixelFormatEnum::RGB565; + const SDL_FMT: PixelFormat = PixelFormat::RGB565; } impl PixelFmt for Rgba4444 { type Pixel = [u8; 2]; - const SDL_FMT: PixelFormatEnum = PixelFormatEnum::RGBA4444; + const SDL_FMT: PixelFormat = PixelFormat::RGBA4444; } impl<'a, PF, const N: usize> Target for Framebuf<'a, PF> @@ -295,7 +301,7 @@ impl Default for Builder<'_, PF> { dims: dims::SVGA_800_600, title: "// retrofire application //", vsync: true, - fs: FullscreenType::Off, + fullscreen: false, pixfmt: PF::default(), hidpi: false, } @@ -320,7 +326,7 @@ macro_rules! impl_from_error { impl_from_error! { String - sdl2::Error + sdl3::Error WindowBuildError TextureValueError IntegerOrSdlError