Skip to content

Commit d096769

Browse files
committed
Convert SDL_image bindings into autogenerated ones
I'm not sure if this is the intended way of using bindgen, but the SDL_image bindings now piggy-back on the core SDL bindings by blacklisting some types that gets dragged into the image module. The solution probably does not scale, but it might be preferable to have autoupdateable bindings rather than having to do it manually. Progresses on Rust-SDL2#647
1 parent c6b78a1 commit d096769

File tree

5 files changed

+226
-90
lines changed

5 files changed

+226
-90
lines changed

sdl2-sys/build.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ fn copy_pregenerated_bindings() {
281281
let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
282282
fs::copy(crate_path.join("pregenerated_bindings.rs"), out_path.join("bindings.rs"))
283283
.expect("Couldn't find pregenerated bindings!");
284+
285+
fs::copy(crate_path.join("sdl_image_bindings.rs"), out_path.join("image_bindings.rs"))
286+
.expect("Couldn't find pregenerated SDL_image bindings!");
284287
}
285288

286289
#[cfg(feature = "bindgen")]
@@ -289,11 +292,15 @@ fn copy_pregenerated_bindings() {
289292
fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str, headers_paths: &[S]) {
290293
let target_os = get_os_from_triple(target).unwrap();
291294
let mut bindings = bindgen::Builder::default();
295+
let mut image_bindings = bindgen::Builder::default();
292296

293297
// Set correct target triple for bindgen when cross-compiling
294298
if target != host {
295299
bindings = bindings.clang_arg("-target");
296300
bindings = bindings.clang_arg(target.clone());
301+
302+
image_bindings = image_bindings.clang_arg("-target");
303+
image_bindings = image_bindings.clang_arg(target.clone());
297304
}
298305

299306
if headers_paths.len() == 0 {
@@ -302,10 +309,12 @@ fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str
302309
include_path.push(format!("SDL2-{}", SDL2_HEADERS_BUNDLED_VERSION));
303310
include_path.push("include");
304311
bindings = bindings.clang_arg(format!("-I{}", include_path.display()));
312+
image_bindings = image_bindings.clang_arg(format!("-I{}", include_path.display()));
305313
} else {
306314
// if paths are included, use them for bindgen. Bindgen should use the first one.
307315
for headers_path in headers_paths {
308-
bindings = bindings.clang_arg(format!("-I{}", headers_path.as_ref()))
316+
bindings = bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
317+
image_bindings = image_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
309318
}
310319
}
311320

@@ -315,12 +324,20 @@ fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str
315324
bindings = bindings.clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.10240.0/ucrt"));
316325
bindings = bindings.clang_arg(format!("-IC:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"));
317326
bindings = bindings.clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/8.1/Include/um"));
327+
328+
image_bindings = image_bindings.clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/8.1/Include/shared"));
329+
image_bindings = image_bindings.clang_arg(format!("-IC:/Program Files/LLVM/lib/clang/5.0.0/include"));
330+
image_bindings = image_bindings.clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.10240.0/ucrt"));
331+
image_bindings = image_bindings.clang_arg(format!("-IC:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"));
332+
image_bindings = image_bindings.clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/8.1/Include/um"));
318333
};
319334

320335
// SDL2 hasn't a default configuration for Linux
321336
if target_os == "linux-gnu" {
322337
bindings = bindings.clang_arg("-DSDL_VIDEO_DRIVER_X11");
323338
bindings = bindings.clang_arg("-DSDL_VIDEO_DRIVER_WAYLAND");
339+
image_bindings = image_bindings.clang_arg("-DSDL_VIDEO_DRIVER_X11");
340+
image_bindings = image_bindings.clang_arg("-DSDL_VIDEO_DRIVER_WAYLAND");
324341
}
325342

326343
let bindings = bindings
@@ -335,6 +352,24 @@ fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str
335352
bindings
336353
.write_to_file(out_path.join("bindings.rs"))
337354
.expect("Couldn't write bindings!");
355+
356+
let image_bindings = image_bindings
357+
.header("wrapper_image.h")
358+
.blacklist_type("max_align_t") // Until https://github.com/rust-lang-nursery/rust-bindgen/issues/550 gets fixed
359+
.whitelist_type("IMG.*")
360+
.whitelist_function("IMG.*")
361+
.whitelist_var("IMG.*")
362+
.blacklist_type("SDL_.*")
363+
.blacklist_type("_IO.*|FILE")
364+
.generate()
365+
.expect("Unable to generate image_bindings!");
366+
367+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
368+
369+
image_bindings
370+
.write_to_file(out_path.join("image_bindings.rs"))
371+
.expect("Couldn't write image_bindings!");
372+
338373
}
339374

340375
fn get_os_from_triple(triple: &str) -> Option<&str>

sdl2-sys/sdl_image_bindings.rs

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
pub type __uint8_t = ::std::os::raw::c_uchar;
4+
pub type __uint32_t = ::std::os::raw::c_uint;
5+
pub type __int64_t = ::std::os::raw::c_long;
6+
pub type __off_t = ::std::os::raw::c_long;
7+
pub type __off64_t = ::std::os::raw::c_long;
8+
pub type Uint8 = u8;
9+
pub type Uint32 = u32;
10+
pub type Sint64 = i64;
11+
extern "C" {
12+
pub fn IMG_Linked_Version() -> *const SDL_version;
13+
}
14+
pub const IMG_InitFlags_IMG_INIT_JPG: IMG_InitFlags = 1;
15+
pub const IMG_InitFlags_IMG_INIT_PNG: IMG_InitFlags = 2;
16+
pub const IMG_InitFlags_IMG_INIT_TIF: IMG_InitFlags = 4;
17+
pub const IMG_InitFlags_IMG_INIT_WEBP: IMG_InitFlags = 8;
18+
pub type IMG_InitFlags = u32;
19+
extern "C" {
20+
pub fn IMG_Init(flags: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
21+
}
22+
extern "C" {
23+
pub fn IMG_Quit();
24+
}
25+
extern "C" {
26+
pub fn IMG_LoadTyped_RW(
27+
src: *mut SDL_RWops,
28+
freesrc: ::std::os::raw::c_int,
29+
type_: *const ::std::os::raw::c_char,
30+
) -> *mut SDL_Surface;
31+
}
32+
extern "C" {
33+
pub fn IMG_Load(file: *const ::std::os::raw::c_char) -> *mut SDL_Surface;
34+
}
35+
extern "C" {
36+
pub fn IMG_Load_RW(src: *mut SDL_RWops, freesrc: ::std::os::raw::c_int) -> *mut SDL_Surface;
37+
}
38+
extern "C" {
39+
pub fn IMG_LoadTexture(
40+
renderer: *mut SDL_Renderer,
41+
file: *const ::std::os::raw::c_char,
42+
) -> *mut SDL_Texture;
43+
}
44+
extern "C" {
45+
pub fn IMG_LoadTexture_RW(
46+
renderer: *mut SDL_Renderer,
47+
src: *mut SDL_RWops,
48+
freesrc: ::std::os::raw::c_int,
49+
) -> *mut SDL_Texture;
50+
}
51+
extern "C" {
52+
pub fn IMG_LoadTextureTyped_RW(
53+
renderer: *mut SDL_Renderer,
54+
src: *mut SDL_RWops,
55+
freesrc: ::std::os::raw::c_int,
56+
type_: *const ::std::os::raw::c_char,
57+
) -> *mut SDL_Texture;
58+
}
59+
extern "C" {
60+
pub fn IMG_isICO(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
61+
}
62+
extern "C" {
63+
pub fn IMG_isCUR(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
64+
}
65+
extern "C" {
66+
pub fn IMG_isBMP(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
67+
}
68+
extern "C" {
69+
pub fn IMG_isGIF(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
70+
}
71+
extern "C" {
72+
pub fn IMG_isJPG(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
73+
}
74+
extern "C" {
75+
pub fn IMG_isLBM(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
76+
}
77+
extern "C" {
78+
pub fn IMG_isPCX(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
79+
}
80+
extern "C" {
81+
pub fn IMG_isPNG(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
82+
}
83+
extern "C" {
84+
pub fn IMG_isPNM(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
85+
}
86+
extern "C" {
87+
pub fn IMG_isSVG(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
88+
}
89+
extern "C" {
90+
pub fn IMG_isTIF(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
91+
}
92+
extern "C" {
93+
pub fn IMG_isXCF(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
94+
}
95+
extern "C" {
96+
pub fn IMG_isXPM(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
97+
}
98+
extern "C" {
99+
pub fn IMG_isXV(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
100+
}
101+
extern "C" {
102+
pub fn IMG_isWEBP(src: *mut SDL_RWops) -> ::std::os::raw::c_int;
103+
}
104+
extern "C" {
105+
pub fn IMG_LoadICO_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
106+
}
107+
extern "C" {
108+
pub fn IMG_LoadCUR_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
109+
}
110+
extern "C" {
111+
pub fn IMG_LoadBMP_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
112+
}
113+
extern "C" {
114+
pub fn IMG_LoadGIF_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
115+
}
116+
extern "C" {
117+
pub fn IMG_LoadJPG_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
118+
}
119+
extern "C" {
120+
pub fn IMG_LoadLBM_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
121+
}
122+
extern "C" {
123+
pub fn IMG_LoadPCX_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
124+
}
125+
extern "C" {
126+
pub fn IMG_LoadPNG_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
127+
}
128+
extern "C" {
129+
pub fn IMG_LoadPNM_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
130+
}
131+
extern "C" {
132+
pub fn IMG_LoadSVG_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
133+
}
134+
extern "C" {
135+
pub fn IMG_LoadTGA_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
136+
}
137+
extern "C" {
138+
pub fn IMG_LoadTIF_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
139+
}
140+
extern "C" {
141+
pub fn IMG_LoadXCF_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
142+
}
143+
extern "C" {
144+
pub fn IMG_LoadXPM_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
145+
}
146+
extern "C" {
147+
pub fn IMG_LoadXV_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
148+
}
149+
extern "C" {
150+
pub fn IMG_LoadWEBP_RW(src: *mut SDL_RWops) -> *mut SDL_Surface;
151+
}
152+
extern "C" {
153+
pub fn IMG_ReadXPMFromArray(xpm: *mut *mut ::std::os::raw::c_char) -> *mut SDL_Surface;
154+
}
155+
extern "C" {
156+
pub fn IMG_SavePNG(
157+
surface: *mut SDL_Surface,
158+
file: *const ::std::os::raw::c_char,
159+
) -> ::std::os::raw::c_int;
160+
}
161+
extern "C" {
162+
pub fn IMG_SavePNG_RW(
163+
surface: *mut SDL_Surface,
164+
dst: *mut SDL_RWops,
165+
freedst: ::std::os::raw::c_int,
166+
) -> ::std::os::raw::c_int;
167+
}
168+
extern "C" {
169+
pub fn IMG_SaveJPG(
170+
surface: *mut SDL_Surface,
171+
file: *const ::std::os::raw::c_char,
172+
quality: ::std::os::raw::c_int,
173+
) -> ::std::os::raw::c_int;
174+
}
175+
extern "C" {
176+
pub fn IMG_SaveJPG_RW(
177+
surface: *mut SDL_Surface,
178+
dst: *mut SDL_RWops,
179+
freedst: ::std::os::raw::c_int,
180+
quality: ::std::os::raw::c_int,
181+
) -> ::std::os::raw::c_int;
182+
}

sdl2-sys/src/image.rs

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,2 @@
1-
use std::os::raw::{c_int, c_char};
2-
use ::{SDL_RWops, SDL_Surface, SDL_Renderer, SDL_Texture};
3-
4-
pub type IMG_InitFlags = c_int;
5-
pub const IMG_INIT_JPG: IMG_InitFlags = 0x00_00_00_01;
6-
pub const IMG_INIT_PNG: IMG_InitFlags = 0x00_00_00_02;
7-
pub const IMG_INIT_TIF: IMG_InitFlags = 0x00_00_00_04;
8-
pub const IMG_INIT_WEBP: IMG_InitFlags = 0x00_00_00_08;
9-
10-
extern "C" {
11-
12-
// This function gets the version of the dynamically linked SDL_image library.
13-
pub fn IMG_Linked_Version() -> *const ::SDL_version;
14-
15-
// Loads dynamic libraries and prepares them for use. Flags should be
16-
// one or more flags from IMG_InitFlags OR'd together.
17-
// It returns the flags successfully initialized, or 0 on failure.
18-
pub fn IMG_Init(flags: c_int) -> c_int;
19-
20-
// Unloads libraries loaded with IMG_Init
21-
pub fn IMG_Quit();
22-
23-
// Load an image from an SDL data source.
24-
// The 'type' may be one of: "BMP", "GIF", "PNG", etc.
25-
// If the image format supports a transparent pixel, SDL will set the
26-
// colorkey for the surface. You can enable RLE acceleration on the
27-
// surface afterwards by calling:
28-
// SDL_SetColorKey(image, SDL_RLEACCEL, image->format->colorkey);
29-
pub fn IMG_LoadTyped_RW(src: *const SDL_RWops, freesrc: c_int,
30-
fmt: *const c_char) -> *mut SDL_Surface;
31-
32-
// Convenience functions
33-
pub fn IMG_Load(file: *const c_char) -> *mut SDL_Surface;
34-
pub fn IMG_Load_RW(src: *const SDL_RWops, freesrc: c_int) -> *mut SDL_Surface;
35-
36-
// Load an image directly into a render texture.
37-
// Requires SDL2
38-
pub fn IMG_LoadTexture(renderer: *const SDL_Renderer,
39-
file: *const c_char) -> *mut SDL_Texture;
40-
pub fn IMG_LoadTexture_RW(renderer: *const SDL_Renderer, src: *const SDL_RWops,
41-
freesrc: c_int) -> *const SDL_Texture;
42-
pub fn IMG_LoadTextureTyped_RW(renderer: *const SDL_Renderer, src: *const SDL_RWops,
43-
freesrc: c_int, fmt: *const c_char) -> *const SDL_Texture;
44-
45-
// Functions to detect a file type, given a seekable source
46-
pub fn IMG_isICO(src: *const SDL_RWops) -> c_int;
47-
pub fn IMG_isCUR(src: *const SDL_RWops) -> c_int;
48-
pub fn IMG_isBMP(src: *const SDL_RWops) -> c_int;
49-
pub fn IMG_isGIF(src: *const SDL_RWops) -> c_int;
50-
pub fn IMG_isJPG(src: *const SDL_RWops) -> c_int;
51-
pub fn IMG_isLBM(src: *const SDL_RWops) -> c_int;
52-
pub fn IMG_isPCX(src: *const SDL_RWops) -> c_int;
53-
pub fn IMG_isPNG(src: *const SDL_RWops) -> c_int;
54-
pub fn IMG_isPNM(src: *const SDL_RWops) -> c_int;
55-
pub fn IMG_isTIF(src: *const SDL_RWops) -> c_int;
56-
pub fn IMG_isXCF(src: *const SDL_RWops) -> c_int;
57-
pub fn IMG_isXPM(src: *const SDL_RWops) -> c_int;
58-
pub fn IMG_isXV(src: *const SDL_RWops) -> c_int;
59-
pub fn IMG_isWEBP(src: *const SDL_RWops) -> c_int;
60-
61-
// Individual loading functions
62-
pub fn IMG_LoadICO_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
63-
pub fn IMG_LoadCUR_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
64-
pub fn IMG_LoadBMP_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
65-
pub fn IMG_LoadGIF_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
66-
pub fn IMG_LoadJPG_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
67-
pub fn IMG_LoadLBM_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
68-
pub fn IMG_LoadPCX_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
69-
pub fn IMG_LoadPNG_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
70-
pub fn IMG_LoadPNM_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
71-
pub fn IMG_LoadTGA_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
72-
pub fn IMG_LoadTIF_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
73-
pub fn IMG_LoadXCF_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
74-
pub fn IMG_LoadXPM_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
75-
pub fn IMG_LoadXV_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
76-
pub fn IMG_LoadWEBP_RW(src: *const SDL_RWops) -> *mut SDL_Surface;
77-
pub fn IMG_ReadXPMFromArray(xpm: *const *const c_char) -> *mut SDL_Surface;
78-
79-
// Individual saving functions
80-
pub fn IMG_SavePNG(surface: *mut SDL_Surface, file: *const c_char) -> c_int;
81-
pub fn IMG_SavePNG_RW(surface: *mut SDL_Surface, dst: *const SDL_RWops,
82-
freedst: c_int) -> c_int;
83-
84-
} // extern "C"
1+
use ::*;
2+
include!(concat!(env!("OUT_DIR"), "/image_bindings.rs"));

sdl2-sys/wrapper_image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <SDL2/SDL_image.h>

src/sdl2/image/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ use sys;
3434
/// functionality to load.
3535
bitflags! {
3636
pub flags InitFlag : 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
37+
const INIT_JPG = sys::image::IMG_InitFlags_IMG_INIT_JPG as u32,
38+
const INIT_PNG = sys::image::IMG_InitFlags_IMG_INIT_PNG as u32,
39+
const INIT_TIF = sys::image::IMG_InitFlags_IMG_INIT_TIF as u32,
40+
const INIT_WEBP = sys::image::IMG_InitFlags_IMG_INIT_WEBP as u32
4141
}
4242
}
4343

@@ -93,7 +93,7 @@ impl<'a> LoadSurface for Surface<'a> {
9393
fn from_xpm_array(xpm: *const *const i8) -> Result<Surface<'a>, String> {
9494
//! Loads an SDL Surface from XPM data
9595
unsafe {
96-
let raw = sys::image::IMG_ReadXPMFromArray(xpm as *const *const c_char);
96+
let raw = sys::image::IMG_ReadXPMFromArray(xpm as *mut *mut c_char);
9797
if (raw as *mut ()).is_null() {
9898
Err(get_error())
9999
} else {

0 commit comments

Comments
 (0)