Skip to content

Commit 94e769a

Browse files
committed
Move SDL_image to sdl2-sys crate
Progresses on Rust-SDL2#647
1 parent d2bd9ed commit 94e769a

File tree

6 files changed

+67
-74
lines changed

6 files changed

+67
-74
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ optional = true
3838
unsafe_textures = []
3939
default = []
4040
ttf = []
41-
image = []
4241
gfx = ["c_vec"]
4342
mixer = ["sdl2-sys/mixer"]
43+
image = ["sdl2-sys/image"]
4444

4545
use-bindgen = ["sdl2-sys/use-bindgen"]
4646
use-pkgconfig = ["sdl2-sys/use-pkgconfig"]

sdl2-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ static-link = []
5151
use_mac_framework = []
5252
bundled = ["cmake", "reqwest", "tar", "flate2"]
5353
mixer = []
54+
image = []

sdl2-sys/build.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,23 @@ fn link_sdl2(target_os: &str) {
204204
// leaves it up to the user to make a symlink to the shared object so
205205
// -lSDL2_mixer can find it.
206206
#[cfg(all(not(feature = "use-pkgconfig"), not(feature = "static-link")))] {
207-
if cfg!(all(any(target_os="linux", target_os="freebsd"), feature = "mixer")) {
208-
println!("cargo:rustc-flags=-l SDL2_mixer");
209-
} else if cfg!(all(target_os="windows", feature = "mixer")) {
210-
println!("cargo:rustc-flags=-l SDL2_mixer");
211-
} else if cfg!(all(any(target_os="macos", feature="use_mac_framework"), feature = "mixer")) {
212-
println!("cargo:rustc-flags=-l framework=SDL2_mixer");
207+
if cfg!(feature = "mixer") {
208+
if cfg!(any(target_os="linux", target_os="freebsd")) {
209+
println!("cargo:rustc-flags=-l SDL2_mixer");
210+
} else if cfg!(target_os="windows") {
211+
println!("cargo:rustc-flags=-l SDL2_mixer");
212+
} else if cfg!(any(target_os="macos", feature="use_mac_framework")) {
213+
println!("cargo:rustc-flags=-l framework=SDL2_mixer");
214+
}
215+
}
216+
if cfg!(feature = "image") {
217+
if cfg!(any(target_os="linux", target_os="freebsd")) {
218+
println!("cargo:rustc-flags=-l SDL2_image");
219+
} else if cfg!(target_os="windows") {
220+
println!("cargo:rustc-flags=-l SDL2_image");
221+
} else if cfg!(any(target_os="macos", feature="use_mac_framework")) {
222+
println!("cargo:rustc-flags=-l framework=SDL2_image");
223+
}
213224
}
214225
}
215226
}

src/sdl2/image/ffi.rs renamed to sdl2-sys/src/image.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::os::raw::{c_int, c_char};
2-
use sys;
3-
use sys::{SDL_RWops, SDL_Surface, SDL_Renderer, SDL_Texture};
2+
use ::{SDL_RWops, SDL_Surface, SDL_Renderer, SDL_Texture};
43

54
pub type IMG_InitFlags = c_int;
65
pub const IMG_INIT_JPG: IMG_InitFlags = 0x00_00_00_01;
@@ -11,7 +10,7 @@ pub const IMG_INIT_WEBP: IMG_InitFlags = 0x00_00_00_08;
1110
extern "C" {
1211

1312
// This function gets the version of the dynamically linked SDL_image library.
14-
pub fn IMG_Linked_Version() -> *const sys::SDL_version;
13+
pub fn IMG_Linked_Version() -> *const ::SDL_version;
1514

1615
// Loads dynamic libraries and prepares them for use. Flags should be
1716
// one or more flags from IMG_InitFlags OR'd together.

sdl2-sys/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
1212

1313
#[cfg(feature = "mixer")]
1414
pub mod mixer;
15+
16+
#[cfg(feature = "image")]
17+
pub mod image;

src/sdl2/image/mod.rs

100755100644
Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,14 @@ use version::Version;
3030
use get_error;
3131
use sys;
3232

33-
// Setup linking for all targets.
34-
#[cfg(target_os="macos")]
35-
mod mac {
36-
#[cfg(any(mac_framework, feature="use_mac_framework"))]
37-
#[link(kind="framework", name="SDL2_image")]
38-
extern "C" {}
39-
40-
#[cfg(not(any(mac_framework, feature="use_mac_framework")))]
41-
#[link(name="SDL2_image")]
42-
extern "C" {}
43-
}
44-
45-
#[cfg(any(target_os="windows", target_os="linux", target_os="freebsd"))]
46-
mod others {
47-
#[link(name="SDL2_image")]
48-
extern "C" {}
49-
}
50-
51-
#[allow(non_camel_case_types, dead_code)]
52-
mod ffi;
53-
5433
/// InitFlags are passed to init() to control which subsystem
5534
/// functionality to load.
5635
bitflags! {
5736
pub flags InitFlag : u32 {
58-
const INIT_JPG = ffi::IMG_INIT_JPG as u32,
59-
const INIT_PNG = ffi::IMG_INIT_PNG as u32,
60-
const INIT_TIF = ffi::IMG_INIT_TIF as u32,
61-
const INIT_WEBP = ffi::IMG_INIT_WEBP as u32
37+
const INIT_JPG = sys::image::IMG_INIT_JPG as u32,
38+
const INIT_PNG = sys::image::IMG_INIT_PNG as u32,
39+
const INIT_TIF = sys::image::IMG_INIT_TIF as u32,
40+
const INIT_WEBP = sys::image::IMG_INIT_WEBP as u32
6241
}
6342
}
6443

@@ -102,7 +81,7 @@ impl<'a> LoadSurface for Surface<'a> {
10281
//! Loads an SDL Surface from a file
10382
unsafe {
10483
let c_filename = CString::new(filename.as_ref().to_str().unwrap()).unwrap();
105-
let raw = ffi::IMG_Load(c_filename.as_ptr() as *const _);
84+
let raw = sys::image::IMG_Load(c_filename.as_ptr() as *const _);
10685
if (raw as *mut ()).is_null() {
10786
Err(get_error())
10887
} else {
@@ -114,7 +93,7 @@ impl<'a> LoadSurface for Surface<'a> {
11493
fn from_xpm_array(xpm: *const *const i8) -> Result<Surface<'a>, String> {
11594
//! Loads an SDL Surface from XPM data
11695
unsafe {
117-
let raw = ffi::IMG_ReadXPMFromArray(xpm as *const *const c_char);
96+
let raw = sys::image::IMG_ReadXPMFromArray(xpm as *const *const c_char);
11897
if (raw as *mut ()).is_null() {
11998
Err(get_error())
12099
} else {
@@ -129,7 +108,7 @@ impl<'a> SaveSurface for Surface<'a> {
129108
//! Saves an SDL Surface to a file
130109
unsafe {
131110
let c_filename = CString::new(filename.as_ref().to_str().unwrap()).unwrap();
132-
let status = ffi::IMG_SavePNG(self.raw(), c_filename.as_ptr() as *const _);
111+
let status = sys::image::IMG_SavePNG(self.raw(), c_filename.as_ptr() as *const _);
133112
if status != 0 {
134113
Err(get_error())
135114
} else {
@@ -141,7 +120,7 @@ impl<'a> SaveSurface for Surface<'a> {
141120
fn save_rw(&self, dst: &mut RWops) -> Result<(), String> {
142121
//! Saves an SDL Surface to an RWops
143122
unsafe {
144-
let status = ffi::IMG_SavePNG_RW(self.raw(), dst.raw(), 0);
123+
let status = sys::image::IMG_SavePNG_RW(self.raw(), dst.raw(), 0);
145124

146125
if status != 0 {
147126
Err(get_error())
@@ -162,7 +141,7 @@ impl<T> LoadTexture for TextureCreator<T> {
162141
//! Loads an SDL Texture from a file
163142
unsafe {
164143
let c_filename = CString::new(filename.as_ref().to_str().unwrap()).unwrap();
165-
let raw = ffi::IMG_LoadTexture(self.raw(), c_filename.as_ptr() as *const _);
144+
let raw = sys::image::IMG_LoadTexture(self.raw(), c_filename.as_ptr() as *const _);
166145
if (raw as *mut ()).is_null() {
167146
Err(get_error())
168147
} else {
@@ -179,7 +158,7 @@ pub struct Sdl2ImageContext;
179158
impl Drop for Sdl2ImageContext {
180159
fn drop(&mut self) {
181160
unsafe {
182-
ffi::IMG_Quit();
161+
sys::image::IMG_Quit();
183162
}
184163
}
185164
}
@@ -188,7 +167,7 @@ impl Drop for Sdl2ImageContext {
188167
/// If not every flag is set it returns an error
189168
pub fn init(flags: InitFlag) -> Result<Sdl2ImageContext, String> {
190169
let return_flags = unsafe {
191-
let used = ffi::IMG_Init(flags.bits() as c_int);
170+
let used = sys::image::IMG_Init(flags.bits() as c_int);
192171
InitFlag::from_bits_truncate(used as u32)
193172
};
194173
if !flags.intersects(return_flags) {
@@ -207,7 +186,7 @@ pub fn init(flags: InitFlag) -> Result<Sdl2ImageContext, String> {
207186

208187
/// Returns the version of the dynamically linked `SDL_image` library
209188
pub fn get_linked_version() -> Version {
210-
unsafe { Version::from_ll(*ffi::IMG_Linked_Version()) }
189+
unsafe { Version::from_ll(*sys::image::IMG_Linked_Version()) }
211190
}
212191

213192
#[inline]
@@ -259,118 +238,118 @@ pub trait ImageRWops {
259238

260239
impl<'a> ImageRWops for RWops<'a> {
261240
fn load(&self) -> Result<Surface, String> {
262-
let raw = unsafe { ffi::IMG_Load_RW(self.raw(), 0) };
241+
let raw = unsafe { sys::image::IMG_Load_RW(self.raw(), 0) };
263242
to_surface_result(raw)
264243
}
265244
fn load_typed(&self, _type: &str) -> Result<Surface, String> {
266245
let raw = unsafe {
267246
let c_type = CString::new(_type.as_bytes()).unwrap();
268-
ffi::IMG_LoadTyped_RW(self.raw(), 0, c_type.as_ptr() as *const _)
247+
sys::image::IMG_LoadTyped_RW(self.raw(), 0, c_type.as_ptr() as *const _)
269248
};
270249
to_surface_result(raw)
271250
}
272251

273252
fn load_cur(&self) -> Result<Surface, String> {
274-
let raw = unsafe { ffi::IMG_LoadCUR_RW(self.raw()) };
253+
let raw = unsafe { sys::image::IMG_LoadCUR_RW(self.raw()) };
275254
to_surface_result(raw)
276255
}
277256
fn load_ico(&self) -> Result<Surface, String> {
278-
let raw = unsafe { ffi::IMG_LoadICO_RW(self.raw()) };
257+
let raw = unsafe { sys::image::IMG_LoadICO_RW(self.raw()) };
279258
to_surface_result(raw)
280259
}
281260
fn load_bmp(&self) -> Result<Surface, String> {
282-
let raw = unsafe { ffi::IMG_LoadBMP_RW(self.raw()) };
261+
let raw = unsafe { sys::image::IMG_LoadBMP_RW(self.raw()) };
283262
to_surface_result(raw)
284263
}
285264
fn load_pnm(&self) -> Result<Surface, String> {
286-
let raw = unsafe { ffi::IMG_LoadPNM_RW(self.raw()) };
265+
let raw = unsafe { sys::image::IMG_LoadPNM_RW(self.raw()) };
287266
to_surface_result(raw)
288267
}
289268
fn load_xpm(&self) -> Result<Surface, String> {
290-
let raw = unsafe { ffi::IMG_LoadXPM_RW(self.raw()) };
269+
let raw = unsafe { sys::image::IMG_LoadXPM_RW(self.raw()) };
291270
to_surface_result(raw)
292271
}
293272
fn load_xcf(&self) -> Result<Surface, String> {
294-
let raw = unsafe { ffi::IMG_LoadXCF_RW(self.raw()) };
273+
let raw = unsafe { sys::image::IMG_LoadXCF_RW(self.raw()) };
295274
to_surface_result(raw)
296275
}
297276
fn load_pcx(&self) -> Result<Surface, String> {
298-
let raw = unsafe { ffi::IMG_LoadPCX_RW(self.raw()) };
277+
let raw = unsafe { sys::image::IMG_LoadPCX_RW(self.raw()) };
299278
to_surface_result(raw)
300279
}
301280
fn load_gif(&self) -> Result<Surface, String> {
302-
let raw = unsafe { ffi::IMG_LoadGIF_RW(self.raw()) };
281+
let raw = unsafe { sys::image::IMG_LoadGIF_RW(self.raw()) };
303282
to_surface_result(raw)
304283
}
305284
fn load_jpg(&self) -> Result<Surface, String> {
306-
let raw = unsafe { ffi::IMG_LoadJPG_RW(self.raw()) };
285+
let raw = unsafe { sys::image::IMG_LoadJPG_RW(self.raw()) };
307286
to_surface_result(raw)
308287
}
309288
fn load_tif(&self) -> Result<Surface, String> {
310-
let raw = unsafe { ffi::IMG_LoadTIF_RW(self.raw()) };
289+
let raw = unsafe { sys::image::IMG_LoadTIF_RW(self.raw()) };
311290
to_surface_result(raw)
312291
}
313292
fn load_png(&self) -> Result<Surface, String> {
314-
let raw = unsafe { ffi::IMG_LoadPNG_RW(self.raw()) };
293+
let raw = unsafe { sys::image::IMG_LoadPNG_RW(self.raw()) };
315294
to_surface_result(raw)
316295
}
317296
fn load_tga(&self) -> Result<Surface, String> {
318-
let raw = unsafe { ffi::IMG_LoadTGA_RW(self.raw()) };
297+
let raw = unsafe { sys::image::IMG_LoadTGA_RW(self.raw()) };
319298
to_surface_result(raw)
320299
}
321300
fn load_lbm(&self) -> Result<Surface, String> {
322-
let raw = unsafe { ffi::IMG_LoadLBM_RW(self.raw()) };
301+
let raw = unsafe { sys::image::IMG_LoadLBM_RW(self.raw()) };
323302
to_surface_result(raw)
324303
}
325304
fn load_xv(&self) -> Result<Surface, String> {
326-
let raw = unsafe { ffi::IMG_LoadXV_RW(self.raw()) };
305+
let raw = unsafe { sys::image::IMG_LoadXV_RW(self.raw()) };
327306
to_surface_result(raw)
328307
}
329308
fn load_webp(&self) -> Result<Surface, String> {
330-
let raw = unsafe { ffi::IMG_LoadWEBP_RW(self.raw()) };
309+
let raw = unsafe { sys::image::IMG_LoadWEBP_RW(self.raw()) };
331310
to_surface_result(raw)
332311
}
333312

334313
fn is_cur(&self) -> bool {
335-
unsafe { ffi::IMG_isCUR(self.raw()) == 1 }
314+
unsafe { sys::image::IMG_isCUR(self.raw()) == 1 }
336315
}
337316
fn is_ico(&self) -> bool {
338-
unsafe { ffi::IMG_isICO(self.raw()) == 1 }
317+
unsafe { sys::image::IMG_isICO(self.raw()) == 1 }
339318
}
340319
fn is_bmp(&self) -> bool {
341-
unsafe { ffi::IMG_isBMP(self.raw()) == 1 }
320+
unsafe { sys::image::IMG_isBMP(self.raw()) == 1 }
342321
}
343322
fn is_pnm(&self) -> bool {
344-
unsafe { ffi::IMG_isPNM(self.raw()) == 1 }
323+
unsafe { sys::image::IMG_isPNM(self.raw()) == 1 }
345324
}
346325
fn is_xpm(&self) -> bool {
347-
unsafe { ffi::IMG_isXPM(self.raw()) == 1 }
326+
unsafe { sys::image::IMG_isXPM(self.raw()) == 1 }
348327
}
349328
fn is_xcf(&self) -> bool {
350-
unsafe { ffi::IMG_isXCF(self.raw()) == 1 }
329+
unsafe { sys::image::IMG_isXCF(self.raw()) == 1 }
351330
}
352331
fn is_pcx(&self) -> bool {
353-
unsafe { ffi::IMG_isPCX(self.raw()) == 1 }
332+
unsafe { sys::image::IMG_isPCX(self.raw()) == 1 }
354333
}
355334
fn is_gif(&self) -> bool {
356-
unsafe { ffi::IMG_isGIF(self.raw()) == 1 }
335+
unsafe { sys::image::IMG_isGIF(self.raw()) == 1 }
357336
}
358337
fn is_jpg(&self) -> bool {
359-
unsafe { ffi::IMG_isJPG(self.raw()) == 1 }
338+
unsafe { sys::image::IMG_isJPG(self.raw()) == 1 }
360339
}
361340
fn is_tif(&self) -> bool {
362-
unsafe { ffi::IMG_isTIF(self.raw()) == 1 }
341+
unsafe { sys::image::IMG_isTIF(self.raw()) == 1 }
363342
}
364343
fn is_png(&self) -> bool {
365-
unsafe { ffi::IMG_isPNG(self.raw()) == 1 }
344+
unsafe { sys::image::IMG_isPNG(self.raw()) == 1 }
366345
}
367346
fn is_lbm(&self) -> bool {
368-
unsafe { ffi::IMG_isLBM(self.raw()) == 1 }
347+
unsafe { sys::image::IMG_isLBM(self.raw()) == 1 }
369348
}
370349
fn is_xv(&self) -> bool {
371-
unsafe { ffi::IMG_isXV(self.raw()) == 1 }
350+
unsafe { sys::image::IMG_isXV(self.raw()) == 1 }
372351
}
373352
fn is_webp(&self) -> bool {
374-
unsafe { ffi::IMG_isWEBP(self.raw()) == 1 }
353+
unsafe { sys::image::IMG_isWEBP(self.raw()) == 1 }
375354
}
376355
}

0 commit comments

Comments
 (0)