Skip to content

Commit 7b31873

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

File tree

8 files changed

+73
-82
lines changed

8 files changed

+73
-82
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ optional = true
3737

3838
unsafe_textures = []
3939
default = []
40-
ttf = []
4140
gfx = ["c_vec"]
4241
mixer = ["sdl2-sys/mixer"]
4342
image = ["sdl2-sys/image"]
43+
ttf = ["sdl2-sys/ttf"]
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
@@ -52,3 +52,4 @@ use_mac_framework = []
5252
bundled = ["cmake", "reqwest", "tar", "flate2"]
5353
mixer = []
5454
image = []
55+
ttf = []

sdl2-sys/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ fn link_sdl2(target_os: &str) {
222222
println!("cargo:rustc-flags=-l framework=SDL2_image");
223223
}
224224
}
225+
if cfg!(feature = "ttf") {
226+
if cfg!(any(target_os="linux", target_os="freebsd")) {
227+
println!("cargo:rustc-flags=-l SDL2_ttf");
228+
} else if cfg!(target_os="windows") {
229+
println!("cargo:rustc-flags=-l SDL2_ttf");
230+
} else if cfg!(any(target_os="macos", feature="use_mac_framework")) {
231+
println!("cargo:rustc-flags=-l framework=SDL2_ttf");
232+
}
233+
}
225234
}
226235
}
227236

sdl2-sys/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ pub mod mixer;
1515

1616
#[cfg(feature = "image")]
1717
pub mod image;
18+
19+
#[cfg(feature = "ttf")]
20+
pub mod ttf;

src/sdl2/ttf/ffi.rs renamed to sdl2-sys/src/ttf.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, c_long, c_void};
2-
use sys;
3-
use sys::{SDL_Surface, SDL_Color, SDL_RWops};
2+
use ::{SDL_Surface, SDL_Color, SDL_RWops};
43

54

65
pub const TTF_STYLE_NORMAL : c_int = 0x00;
@@ -16,7 +15,7 @@ pub const TTF_HINTING_NONE : c_int = 3;
1615

1716
pub type TTF_Font = c_void;
1817
extern "C" {
19-
pub fn TTF_Linked_Version() -> *const sys::SDL_version;
18+
pub fn TTF_Linked_Version() -> *const ::SDL_version;
2019
pub fn TTF_ByteSwappedUNICODE(swapped: c_int);
2120
pub fn TTF_Init() -> c_int;
2221
pub fn TTF_OpenFont(file: *const c_char, ptsize: c_int) -> *const TTF_Font;

src/sdl2/ttf/context.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::Path;
66
use ::get_error;
77
use ::rwops::RWops;
88
use ::version::Version;
9+
use sys;
910

1011
use super::font::{
1112
internal_load_font,
@@ -14,16 +15,14 @@ use super::font::{
1415
Font,
1516
};
1617

17-
use super::ffi;
18-
1918
/// A context manager for `SDL2_TTF` to manage C code initialization and clean-up.
2019
#[must_use]
2120
pub struct Sdl2TtfContext;
2221

2322
// Clean up the context once it goes out of scope
2423
impl Drop for Sdl2TtfContext {
2524
fn drop(&mut self) {
26-
unsafe { ffi::TTF_Quit(); }
25+
unsafe { sys::ttf::TTF_Quit(); }
2726
}
2827
}
2928

@@ -45,7 +44,7 @@ impl Sdl2TtfContext {
4544
pub fn load_font_from_rwops<'ttf,'r>(&'ttf self, rwops: RWops<'r>, point_size: u16)
4645
-> Result<Font<'ttf,'r>, String> {
4746
let raw = unsafe {
48-
ffi::TTF_OpenFontRW(rwops.raw(), 0, point_size as c_int)
47+
sys::ttf::TTF_OpenFontRW(rwops.raw(), 0, point_size as c_int)
4948
};
5049
if (raw as *mut ()).is_null() {
5150
Err(get_error())
@@ -59,7 +58,7 @@ impl Sdl2TtfContext {
5958
pub fn load_font_at_index_from_rwops<'ttf,'r>(&'ttf self, rwops: RWops<'r>, index: u32,
6059
point_size: u16) -> Result<Font<'ttf,'r>, String> {
6160
let raw = unsafe {
62-
ffi::TTF_OpenFontIndexRW(rwops.raw(), 0, point_size as c_int,
61+
sys::ttf::TTF_OpenFontIndexRW(rwops.raw(), 0, point_size as c_int,
6362
index as c_long)
6463
};
6564
if (raw as *mut ()).is_null() {
@@ -73,7 +72,7 @@ impl Sdl2TtfContext {
7372
/// Returns the version of the dynamically linked `SDL_TTF` library
7473
pub fn get_linked_version() -> Version {
7574
unsafe {
76-
Version::from_ll(*ffi::TTF_Linked_Version())
75+
Version::from_ll(*sys::ttf::TTF_Linked_Version())
7776
}
7877
}
7978

@@ -119,9 +118,9 @@ impl fmt::Display for InitError {
119118
/// clean up the library once it goes out of scope.
120119
pub fn init() -> Result<Sdl2TtfContext, InitError> {
121120
unsafe {
122-
if ffi::TTF_WasInit() == 1 {
121+
if sys::ttf::TTF_WasInit() == 1 {
123122
Err(InitError::AlreadyInitializedError)
124-
} else if ffi::TTF_Init() == 0 {
123+
} else if sys::ttf::TTF_Init() == 0 {
125124
Ok(Sdl2TtfContext)
126125
} else {
127126
Err(InitError::InitializationError(
@@ -134,6 +133,6 @@ pub fn init() -> Result<Sdl2TtfContext, InitError> {
134133
/// Returns whether library has been initialized already.
135134
pub fn has_been_initialized() -> bool {
136135
unsafe {
137-
ffi::TTF_WasInit() == 1
136+
sys::ttf::TTF_WasInit() == 1
138137
}
139138
}

0 commit comments

Comments
 (0)