Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions demos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions demos/src/bin/crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion front/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions front/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions front/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
58 changes: 32 additions & 26 deletions front/src/sdl2.rs → front/src/sdl3.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand All @@ -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<C: IntoPixel<Self::Pixel, Self>>(self, color: C) -> Self::Pixel {
color.into_pixel_fmt(self)
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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.
Expand All @@ -112,10 +112,10 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> {

/// Creates the window.
pub fn build(self) -> Result<Window<PF>, 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()?;
Expand All @@ -133,28 +133,34 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> {

fn create_window(&self, sdl: &Sdl) -> Result<SdlWindow, Error> {
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<WindowCanvas, Error> {
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);
}
Expand Down Expand Up @@ -186,7 +192,7 @@ impl<PF: PixelFmt<Pixel = [u8; N]>, const N: usize> Window<PF> {
F: FnMut(&mut Frame<Self, &RefCell<Framebuf<PF>>>) -> ControlFlow<()>,
Color4: IntoPixel<PF::Pixel, PF>,
{
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)?;
Expand Down Expand Up @@ -256,15 +262,15 @@ impl<PF: PixelFmt<Pixel = [u8; N]>, const N: usize> Window<PF> {

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>
Expand Down Expand Up @@ -295,7 +301,7 @@ impl<PF: PixelFmt> Default for Builder<'_, PF> {
dims: dims::SVGA_800_600,
title: "// retrofire application //",
vsync: true,
fs: FullscreenType::Off,
fullscreen: false,
pixfmt: PF::default(),
hidpi: false,
}
Expand All @@ -320,7 +326,7 @@ macro_rules! impl_from_error {

impl_from_error! {
String
sdl2::Error
sdl3::Error
WindowBuildError
TextureValueError
IntegerOrSdlError
Expand Down
Loading