mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
update to use unboxed type; fix #2
This commit is contained in:
+39
-29
@@ -18,7 +18,9 @@ use std::c_str::CString;
|
||||
use std::num::FromPrimitive;
|
||||
use sdl2::surface::Surface;
|
||||
use sdl2::get_error;
|
||||
use sdl2::pixels::ToColor;
|
||||
use sdl2::pixels;
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::pixels::ll::SDL_Color;
|
||||
use sdl2::rwops::RWops;
|
||||
use sdl2::version::Version;
|
||||
|
||||
@@ -46,6 +48,14 @@ mod others {
|
||||
mod ffi;
|
||||
mod flag;
|
||||
|
||||
#[inline]
|
||||
fn color_to_c_color(color: Color) -> SDL_Color {
|
||||
match color {
|
||||
pixels::RGB(r, g, b) => SDL_Color { r: r, g: g, b: b, a: 255 },
|
||||
pixels::RGBA(r, g, b, a) => SDL_Color { r: r, g: g, b: b, a: a }
|
||||
}
|
||||
}
|
||||
|
||||
/// Font Style
|
||||
#[deriving(Show)]
|
||||
flag_type!(FontStyle : c_int {
|
||||
@@ -126,26 +136,26 @@ impl Drop for Font {
|
||||
}
|
||||
|
||||
impl Font {
|
||||
pub fn from_file(filename: &Path, ptsize: int) -> Result<~Font, ~str> {
|
||||
pub fn from_file(filename: &Path, ptsize: int) -> Result<Font, ~str> {
|
||||
//! Load file for use as a font, at ptsize size.
|
||||
unsafe {
|
||||
let raw = ffi::TTF_OpenFont(filename.to_c_str().unwrap(), ptsize as c_int);
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
Ok(~Font { raw: raw, owned: true })
|
||||
Ok(Font { raw: raw, owned: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_file_index(filename: &Path, ptsize: int, index: int) -> Result<~Font, ~str> {
|
||||
pub fn from_file_index(filename: &Path, ptsize: int, index: int) -> Result<Font, ~str> {
|
||||
//! Load file, face index, for use as a font, at ptsize size.
|
||||
unsafe {
|
||||
let raw = ffi::TTF_OpenFontIndex(filename.to_c_str().unwrap(), ptsize as c_int, index as c_long);
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
Ok(~Font { raw: raw, owned: true })
|
||||
Ok(Font { raw: raw, owned: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -336,11 +346,11 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_bytes_solid<C: ToColor>(&self, text: &[u8], fg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_bytes_solid(&self, text: &[u8], fg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw LATIN1 text in solid mode.
|
||||
unsafe {
|
||||
let raw = text.with_c_str(|ctext| {
|
||||
ffi::TTF_RenderText_Solid(self.raw, ctext, fg.to_color())
|
||||
ffi::TTF_RenderText_Solid(self.raw, ctext, color_to_c_color(fg))
|
||||
});
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
@@ -350,11 +360,11 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_str_solid<C: ToColor>(&self, text: &str, fg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_str_solid(&self, text: &str, fg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw UTF8 text in solid mode.
|
||||
unsafe {
|
||||
let raw = text.with_c_str(|ctext| {
|
||||
ffi::TTF_RenderUTF8_Solid(self.raw, ctext, fg.to_color())
|
||||
ffi::TTF_RenderUTF8_Solid(self.raw, ctext, color_to_c_color(fg))
|
||||
});
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
@@ -364,10 +374,10 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_char_solid<C: ToColor>(&self, ch: char, fg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_char_solid(&self, ch: char, fg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw a UNICODE glyph in solid mode.
|
||||
unsafe {
|
||||
let raw = ffi::TTF_RenderGlyph_Solid(self.raw, ch as u16, fg.to_color());
|
||||
let raw = ffi::TTF_RenderGlyph_Solid(self.raw, ch as u16, color_to_c_color(fg));
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
@@ -376,11 +386,11 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_bytes_shaded<C: ToColor>(&self, text: &[u8], fg: C, bg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_bytes_shaded(&self, text: &[u8], fg: Color, bg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw LATIN1 text in shaded mode.
|
||||
unsafe {
|
||||
let raw = text.with_c_str(|ctext| {
|
||||
ffi::TTF_RenderText_Shaded(self.raw, ctext, fg.to_color(), bg.to_color())
|
||||
ffi::TTF_RenderText_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg))
|
||||
});
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
@@ -390,11 +400,11 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_str_shaded<C: ToColor>(&self, text: &str, fg: C, bg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_str_shaded(&self, text: &str, fg: Color, bg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw UTF8 text in shaded mode.
|
||||
unsafe {
|
||||
let raw = text.with_c_str(|ctext| {
|
||||
ffi::TTF_RenderUTF8_Shaded(self.raw, ctext, fg.to_color(), bg.to_color())
|
||||
ffi::TTF_RenderUTF8_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg))
|
||||
});
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
@@ -404,10 +414,10 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_char_shaded<C: ToColor>(&self, ch: char, fg: C, bg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_char_shaded(&self, ch: char, fg: Color, bg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw a UNICODE glyph in shaded mode.
|
||||
unsafe {
|
||||
let raw = ffi::TTF_RenderGlyph_Shaded(self.raw, ch as u16, fg.to_color(), bg.to_color());
|
||||
let raw = ffi::TTF_RenderGlyph_Shaded(self.raw, ch as u16, color_to_c_color(fg), color_to_c_color(bg));
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
@@ -416,11 +426,11 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_bytes_blended<C: ToColor>(&self, text: &[u8], fg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_bytes_blended(&self, text: &[u8], fg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw LATIN1 text in blended mode.
|
||||
unsafe {
|
||||
let raw = text.with_c_str(|ctext| {
|
||||
ffi::TTF_RenderText_Blended(self.raw, ctext, fg.to_color())
|
||||
ffi::TTF_RenderText_Blended(self.raw, ctext, color_to_c_color(fg))
|
||||
});
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
@@ -430,11 +440,11 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_str_blended<C: ToColor>(&self, text: &str, fg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_str_blended(&self, text: &str, fg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw UTF8 text in blended mode.
|
||||
unsafe {
|
||||
let raw = text.with_c_str(|ctext| {
|
||||
ffi::TTF_RenderUTF8_Blended(self.raw, ctext, fg.to_color())
|
||||
ffi::TTF_RenderUTF8_Blended(self.raw, ctext, color_to_c_color(fg))
|
||||
});
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
@@ -444,10 +454,10 @@ impl Font {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_char_blended<C: ToColor>(&self, ch: char, fg: C) -> Result<~Surface, ~str> {
|
||||
pub fn render_char_blended(&self, ch: char, fg: Color) -> Result<~Surface, ~str> {
|
||||
//! Draw a UNICODE glyph in blended mode.
|
||||
unsafe {
|
||||
let raw = ffi::TTF_RenderGlyph_Blended(self.raw, ch as u16, fg.to_color());
|
||||
let raw = ffi::TTF_RenderGlyph_Blended(self.raw, ch as u16, color_to_c_color(fg));
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
@@ -461,30 +471,30 @@ impl Font {
|
||||
/// Loader trait for RWops
|
||||
pub trait LoaderRWops {
|
||||
/// Load src for use as a font.
|
||||
fn load_font(&self, ptsize: int) -> Result<~Font, ~str>;
|
||||
fn load_font(&self, ptsize: int) -> Result<Font, ~str>;
|
||||
/// Load src for use as a font.
|
||||
fn load_font_index(&self, ptsize: int, index: int) -> Result<~Font, ~str>;
|
||||
fn load_font_index(&self, ptsize: int, index: int) -> Result<Font, ~str>;
|
||||
}
|
||||
|
||||
impl LoaderRWops for RWops {
|
||||
fn load_font(&self, ptsize: int) -> Result<~Font, ~str> {
|
||||
fn load_font(&self, ptsize: int) -> Result<Font, ~str> {
|
||||
let raw = unsafe {
|
||||
ffi::TTF_OpenFontRW(self.raw, 0, ptsize as c_int)
|
||||
};
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
Ok(~Font { raw: raw, owned: true })
|
||||
Ok(Font { raw: raw, owned: true })
|
||||
}
|
||||
}
|
||||
fn load_font_index(&self, ptsize: int, index: int) -> Result<~Font, ~str> {
|
||||
fn load_font_index(&self, ptsize: int, index: int) -> Result<Font, ~str> {
|
||||
let raw = unsafe {
|
||||
ffi::TTF_OpenFontIndexRW(self.raw, 0, ptsize as c_int, index as c_long)
|
||||
};
|
||||
if raw.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
Ok(~Font { raw: raw, owned: true })
|
||||
Ok(Font { raw: raw, owned: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user