Skip to content

Commit 6135e88

Browse files
committed
Migrate to SDL 3
1 parent cce0d4f commit 6135e88

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

demos/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ re = { version = "0.4.0", path = "..", package = "retrofire" }
2626
re-front = { version = "0.4.0", path = "../front", package = "retrofire-front" }
2727

2828
minifb = { version = "0.27.0", optional = true }
29-
sdl2 = { version = "0.35.2", optional = true }
29+
sdl3 = { version = "0.16.2", optional = true }
3030
pancurses = { version = "0.17.0", optional = true }
3131

3232
[features]
33-
default = ["minifb"]
33+
default = ["minifb", "sdl2"]
3434
minifb = ["dep:minifb", "re-front/minifb"]
35-
sdl2 = ["dep:sdl2", "re-front/sdl2"]
35+
sdl2 = ["dep:sdl3", "re-front/sdl3"]
3636

3737
[[bin]]
3838
name = "crates"

demos/src/bin/crates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn main() {
6666
let ep = &frame.win.ev_pump;
6767

6868
for key in ep.keyboard_state().pressed_scancodes() {
69-
use sdl2::keyboard::Scancode as Sc;
69+
use sdl3::keyboard::Scancode as Sc;
7070
match key {
7171
Sc::W => cam_vel[2] += 4.0,
7272
Sc::S => cam_vel[2] -= 2.0,

front/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ wasm-dev = ["wasm", "dep:console_error_panic_hook"]
2828
[dependencies]
2929
retrofire-core = { version = "0.4.0", path = "../core" }
3030
minifb = { version = "0.27.0", optional = true }
31-
sdl2 = { version = "0.35.2", optional = true }
31+
sdl3 = { version = "0.16.2", optional = true, features = ["build-from-source-static"] }
3232

3333
wasm-bindgen = { version = "0.2.92", optional = true }
3434
console_error_panic_hook = { version = "0.1.7", optional = true }

front/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use retrofire_core::{
1414
#[cfg(feature = "minifb")]
1515
pub mod minifb;
1616

17-
#[cfg(feature = "sdl2")]
17+
#[cfg(feature = "sdl3")]
1818
pub mod sdl2;
1919

2020
#[no_std]

front/src/sdl2.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
use core::{cell::RefCell, fmt, mem::replace, ops::ControlFlow};
33
use std::time::Instant;
44

5-
use sdl2::{
5+
use sdl3::{
66
EventPump, IntegerOrSdlError, Sdl,
77
event::Event,
88
keyboard::Keycode,
9-
pixels::PixelFormatEnum,
9+
pixels::PixelFormat,
1010
render::{Texture, TextureValueError, WindowCanvas},
1111
video::{FullscreenType, Window as SdlWindow, WindowBuildError},
1212
};
@@ -27,7 +27,7 @@ use super::{Frame, dims};
2727
/// Helper trait to support different pixel format types.
2828
pub trait PixelFmt: Copy + Default {
2929
type Pixel: AsRef<[u8]> + Copy + Sized;
30-
const SDL_FMT: PixelFormatEnum;
30+
const SDL_FMT: PixelFormat;
3131

3232
fn encode<C: IntoPixel<Self::Pixel, Self>>(self, color: C) -> Self::Pixel {
3333
color.into_pixel_fmt(self)
@@ -59,7 +59,7 @@ pub struct Builder<'title, PF> {
5959
pub title: &'title str,
6060
pub vsync: bool,
6161
pub hidpi: bool,
62-
pub fs: FullscreenType,
62+
pub fullscreen: bool,
6363
pub pixfmt: PF,
6464
}
6565

@@ -98,8 +98,8 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> {
9898
self
9999
}
100100
/// Sets the fullscreen state of the window.
101-
pub fn fullscreen(mut self, fs: FullscreenType) -> Self {
102-
self.fs = fs;
101+
pub fn fullscreen(mut self, fs: bool) -> Self {
102+
self.fullscreen = fs;
103103
self
104104
}
105105
/// Sets the framebuffer pixel format.
@@ -112,10 +112,10 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> {
112112

113113
/// Creates the window.
114114
pub fn build(self) -> Result<Window<PF>, Error> {
115-
let sdl = sdl2::init()?;
115+
let sdl = sdl3::init()?;
116116
let win = self.create_window(&sdl)?;
117117

118-
self.set_mouse_mode(&sdl);
118+
self.set_mouse_mode(&sdl, &win);
119119

120120
let canvas = self.create_canvas(win)?;
121121
let ev_pump = sdl.event_pump()?;
@@ -133,28 +133,34 @@ impl<'t, PF: PixelFmt> Builder<'t, PF> {
133133

134134
fn create_window(&self, sdl: &Sdl) -> Result<SdlWindow, Error> {
135135
let Self {
136-
dims: (w, h), title, fs, hidpi, ..
136+
dims: (w, h),
137+
title,
138+
fullscreen,
139+
hidpi,
140+
..
137141
} = *self;
138-
let mut win = sdl.video()?.window(title, w, h);
142+
let mut bld = sdl.video()?.window(title, w, h);
139143
if hidpi {
140-
win.allow_highdpi();
144+
bld.high_pixel_density();
141145
}
142-
let mut win = win.build()?;
143-
win.set_fullscreen(fs)?;
144-
Ok(win)
146+
if fullscreen {
147+
bld.fullscreen();
148+
}
149+
Ok(bld.build()?)
145150
}
146151

147152
fn create_canvas(&self, w: SdlWindow) -> Result<WindowCanvas, Error> {
148-
let mut canvas = w.into_canvas();
153+
let canvas = w.into_canvas();
149154
if self.vsync {
150-
canvas = canvas.present_vsync();
155+
//let _ok = canvas.present();
156+
//TODO vsync? canvas = canvas.present_vsync();
151157
}
152-
Ok(canvas.accelerated().build()?)
158+
Ok(canvas)
153159
}
154160

155-
fn set_mouse_mode(&self, sdl: &Sdl) {
161+
fn set_mouse_mode(&self, sdl: &Sdl, win: &SdlWindow) {
156162
let m = sdl.mouse();
157-
m.set_relative_mouse_mode(true);
163+
m.set_relative_mouse_mode(win, true);
158164
m.capture(true);
159165
m.show_cursor(true);
160166
}
@@ -186,7 +192,7 @@ impl<PF: PixelFmt<Pixel = [u8; N]>, const N: usize> Window<PF> {
186192
F: FnMut(&mut Frame<Self, &RefCell<Framebuf<PF>>>) -> ControlFlow<()>,
187193
Color4: IntoPixel<PF::Pixel, PF>,
188194
{
189-
let dims @ (w, h) = self.canvas.window().drawable_size();
195+
let dims @ (w, h) = self.canvas.window().size_in_pixels();
190196

191197
let tc = self.canvas.texture_creator();
192198
let mut tex = tc.create_texture_streaming(PF::SDL_FMT, w, h)?;
@@ -256,15 +262,15 @@ impl<PF: PixelFmt<Pixel = [u8; N]>, const N: usize> Window<PF> {
256262

257263
impl PixelFmt for Rgba8888 {
258264
type Pixel = [u8; 4];
259-
const SDL_FMT: PixelFormatEnum = PixelFormatEnum::RGBA32;
265+
const SDL_FMT: PixelFormat = PixelFormat::RGBA32;
260266
}
261267
impl PixelFmt for Rgb565 {
262268
type Pixel = [u8; 2];
263-
const SDL_FMT: PixelFormatEnum = PixelFormatEnum::RGB565;
269+
const SDL_FMT: PixelFormat = PixelFormat::RGB565;
264270
}
265271
impl PixelFmt for Rgba4444 {
266272
type Pixel = [u8; 2];
267-
const SDL_FMT: PixelFormatEnum = PixelFormatEnum::RGBA4444;
273+
const SDL_FMT: PixelFormat = PixelFormat::RGBA4444;
268274
}
269275

270276
impl<'a, PF, const N: usize> Target for Framebuf<'a, PF>
@@ -295,7 +301,7 @@ impl<PF: PixelFmt> Default for Builder<'_, PF> {
295301
dims: dims::SVGA_800_600,
296302
title: "// retrofire application //",
297303
vsync: true,
298-
fs: FullscreenType::Off,
304+
fullscreen: false,
299305
pixfmt: PF::default(),
300306
hidpi: false,
301307
}
@@ -320,7 +326,7 @@ macro_rules! impl_from_error {
320326

321327
impl_from_error! {
322328
String
323-
sdl2::Error
329+
sdl3::Error
324330
WindowBuildError
325331
TextureValueError
326332
IntegerOrSdlError

0 commit comments

Comments
 (0)