Skip to content

Commit dd9b847

Browse files
committed
update to use unboxed type; fix #2
1 parent 8eb26da commit dd9b847

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

src/sdl2_ttf/lib.rs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use std::c_str::CString;
1818
use std::num::FromPrimitive;
1919
use sdl2::surface::Surface;
2020
use sdl2::get_error;
21-
use sdl2::pixels::ToColor;
21+
use sdl2::pixels;
22+
use sdl2::pixels::Color;
23+
use sdl2::pixels::ll::SDL_Color;
2224
use sdl2::rwops::RWops;
2325
use sdl2::version::Version;
2426

@@ -46,6 +48,14 @@ mod others {
4648
mod ffi;
4749
mod flag;
4850

51+
#[inline]
52+
fn color_to_c_color(color: Color) -> SDL_Color {
53+
match color {
54+
pixels::RGB(r, g, b) => SDL_Color { r: r, g: g, b: b, a: 255 },
55+
pixels::RGBA(r, g, b, a) => SDL_Color { r: r, g: g, b: b, a: a }
56+
}
57+
}
58+
4959
/// Font Style
5060
#[deriving(Show)]
5161
flag_type!(FontStyle : c_int {
@@ -126,26 +136,26 @@ impl Drop for Font {
126136
}
127137

128138
impl Font {
129-
pub fn from_file(filename: &Path, ptsize: int) -> Result<~Font, ~str> {
139+
pub fn from_file(filename: &Path, ptsize: int) -> Result<Font, ~str> {
130140
//! Load file for use as a font, at ptsize size.
131141
unsafe {
132142
let raw = ffi::TTF_OpenFont(filename.to_c_str().unwrap(), ptsize as c_int);
133143
if raw.is_null() {
134144
Err(get_error())
135145
} else {
136-
Ok(~Font { raw: raw, owned: true })
146+
Ok(Font { raw: raw, owned: true })
137147
}
138148
}
139149
}
140150

141-
pub fn from_file_index(filename: &Path, ptsize: int, index: int) -> Result<~Font, ~str> {
151+
pub fn from_file_index(filename: &Path, ptsize: int, index: int) -> Result<Font, ~str> {
142152
//! Load file, face index, for use as a font, at ptsize size.
143153
unsafe {
144154
let raw = ffi::TTF_OpenFontIndex(filename.to_c_str().unwrap(), ptsize as c_int, index as c_long);
145155
if raw.is_null() {
146156
Err(get_error())
147157
} else {
148-
Ok(~Font { raw: raw, owned: true })
158+
Ok(Font { raw: raw, owned: true })
149159
}
150160
}
151161
}
@@ -336,11 +346,11 @@ impl Font {
336346
}
337347
}
338348

339-
pub fn render_bytes_solid<C: ToColor>(&self, text: &[u8], fg: C) -> Result<~Surface, ~str> {
349+
pub fn render_bytes_solid(&self, text: &[u8], fg: Color) -> Result<~Surface, ~str> {
340350
//! Draw LATIN1 text in solid mode.
341351
unsafe {
342352
let raw = text.with_c_str(|ctext| {
343-
ffi::TTF_RenderText_Solid(self.raw, ctext, fg.to_color())
353+
ffi::TTF_RenderText_Solid(self.raw, ctext, color_to_c_color(fg))
344354
});
345355
if raw.is_null() {
346356
Err(get_error())
@@ -350,11 +360,11 @@ impl Font {
350360
}
351361
}
352362

353-
pub fn render_str_solid<C: ToColor>(&self, text: &str, fg: C) -> Result<~Surface, ~str> {
363+
pub fn render_str_solid(&self, text: &str, fg: Color) -> Result<~Surface, ~str> {
354364
//! Draw UTF8 text in solid mode.
355365
unsafe {
356366
let raw = text.with_c_str(|ctext| {
357-
ffi::TTF_RenderUTF8_Solid(self.raw, ctext, fg.to_color())
367+
ffi::TTF_RenderUTF8_Solid(self.raw, ctext, color_to_c_color(fg))
358368
});
359369
if raw.is_null() {
360370
Err(get_error())
@@ -364,10 +374,10 @@ impl Font {
364374
}
365375
}
366376

367-
pub fn render_char_solid<C: ToColor>(&self, ch: char, fg: C) -> Result<~Surface, ~str> {
377+
pub fn render_char_solid(&self, ch: char, fg: Color) -> Result<~Surface, ~str> {
368378
//! Draw a UNICODE glyph in solid mode.
369379
unsafe {
370-
let raw = ffi::TTF_RenderGlyph_Solid(self.raw, ch as u16, fg.to_color());
380+
let raw = ffi::TTF_RenderGlyph_Solid(self.raw, ch as u16, color_to_c_color(fg));
371381
if raw.is_null() {
372382
Err(get_error())
373383
} else {
@@ -376,11 +386,11 @@ impl Font {
376386
}
377387
}
378388

379-
pub fn render_bytes_shaded<C: ToColor>(&self, text: &[u8], fg: C, bg: C) -> Result<~Surface, ~str> {
389+
pub fn render_bytes_shaded(&self, text: &[u8], fg: Color, bg: Color) -> Result<~Surface, ~str> {
380390
//! Draw LATIN1 text in shaded mode.
381391
unsafe {
382392
let raw = text.with_c_str(|ctext| {
383-
ffi::TTF_RenderText_Shaded(self.raw, ctext, fg.to_color(), bg.to_color())
393+
ffi::TTF_RenderText_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg))
384394
});
385395
if raw.is_null() {
386396
Err(get_error())
@@ -390,11 +400,11 @@ impl Font {
390400
}
391401
}
392402

393-
pub fn render_str_shaded<C: ToColor>(&self, text: &str, fg: C, bg: C) -> Result<~Surface, ~str> {
403+
pub fn render_str_shaded(&self, text: &str, fg: Color, bg: Color) -> Result<~Surface, ~str> {
394404
//! Draw UTF8 text in shaded mode.
395405
unsafe {
396406
let raw = text.with_c_str(|ctext| {
397-
ffi::TTF_RenderUTF8_Shaded(self.raw, ctext, fg.to_color(), bg.to_color())
407+
ffi::TTF_RenderUTF8_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg))
398408
});
399409
if raw.is_null() {
400410
Err(get_error())
@@ -404,10 +414,10 @@ impl Font {
404414
}
405415
}
406416

407-
pub fn render_char_shaded<C: ToColor>(&self, ch: char, fg: C, bg: C) -> Result<~Surface, ~str> {
417+
pub fn render_char_shaded(&self, ch: char, fg: Color, bg: Color) -> Result<~Surface, ~str> {
408418
//! Draw a UNICODE glyph in shaded mode.
409419
unsafe {
410-
let raw = ffi::TTF_RenderGlyph_Shaded(self.raw, ch as u16, fg.to_color(), bg.to_color());
420+
let raw = ffi::TTF_RenderGlyph_Shaded(self.raw, ch as u16, color_to_c_color(fg), color_to_c_color(bg));
411421
if raw.is_null() {
412422
Err(get_error())
413423
} else {
@@ -416,11 +426,11 @@ impl Font {
416426
}
417427
}
418428

419-
pub fn render_bytes_blended<C: ToColor>(&self, text: &[u8], fg: C) -> Result<~Surface, ~str> {
429+
pub fn render_bytes_blended(&self, text: &[u8], fg: Color) -> Result<~Surface, ~str> {
420430
//! Draw LATIN1 text in blended mode.
421431
unsafe {
422432
let raw = text.with_c_str(|ctext| {
423-
ffi::TTF_RenderText_Blended(self.raw, ctext, fg.to_color())
433+
ffi::TTF_RenderText_Blended(self.raw, ctext, color_to_c_color(fg))
424434
});
425435
if raw.is_null() {
426436
Err(get_error())
@@ -430,11 +440,11 @@ impl Font {
430440
}
431441
}
432442

433-
pub fn render_str_blended<C: ToColor>(&self, text: &str, fg: C) -> Result<~Surface, ~str> {
443+
pub fn render_str_blended(&self, text: &str, fg: Color) -> Result<~Surface, ~str> {
434444
//! Draw UTF8 text in blended mode.
435445
unsafe {
436446
let raw = text.with_c_str(|ctext| {
437-
ffi::TTF_RenderUTF8_Blended(self.raw, ctext, fg.to_color())
447+
ffi::TTF_RenderUTF8_Blended(self.raw, ctext, color_to_c_color(fg))
438448
});
439449
if raw.is_null() {
440450
Err(get_error())
@@ -444,10 +454,10 @@ impl Font {
444454
}
445455
}
446456

447-
pub fn render_char_blended<C: ToColor>(&self, ch: char, fg: C) -> Result<~Surface, ~str> {
457+
pub fn render_char_blended(&self, ch: char, fg: Color) -> Result<~Surface, ~str> {
448458
//! Draw a UNICODE glyph in blended mode.
449459
unsafe {
450-
let raw = ffi::TTF_RenderGlyph_Blended(self.raw, ch as u16, fg.to_color());
460+
let raw = ffi::TTF_RenderGlyph_Blended(self.raw, ch as u16, color_to_c_color(fg));
451461
if raw.is_null() {
452462
Err(get_error())
453463
} else {
@@ -461,30 +471,30 @@ impl Font {
461471
/// Loader trait for RWops
462472
pub trait LoaderRWops {
463473
/// Load src for use as a font.
464-
fn load_font(&self, ptsize: int) -> Result<~Font, ~str>;
474+
fn load_font(&self, ptsize: int) -> Result<Font, ~str>;
465475
/// Load src for use as a font.
466-
fn load_font_index(&self, ptsize: int, index: int) -> Result<~Font, ~str>;
476+
fn load_font_index(&self, ptsize: int, index: int) -> Result<Font, ~str>;
467477
}
468478

469479
impl LoaderRWops for RWops {
470-
fn load_font(&self, ptsize: int) -> Result<~Font, ~str> {
480+
fn load_font(&self, ptsize: int) -> Result<Font, ~str> {
471481
let raw = unsafe {
472482
ffi::TTF_OpenFontRW(self.raw, 0, ptsize as c_int)
473483
};
474484
if raw.is_null() {
475485
Err(get_error())
476486
} else {
477-
Ok(~Font { raw: raw, owned: true })
487+
Ok(Font { raw: raw, owned: true })
478488
}
479489
}
480-
fn load_font_index(&self, ptsize: int, index: int) -> Result<~Font, ~str> {
490+
fn load_font_index(&self, ptsize: int, index: int) -> Result<Font, ~str> {
481491
let raw = unsafe {
482492
ffi::TTF_OpenFontIndexRW(self.raw, 0, ptsize as c_int, index as c_long)
483493
};
484494
if raw.is_null() {
485495
Err(get_error())
486496
} else {
487-
Ok(~Font { raw: raw, owned: true })
497+
Ok(Font { raw: raw, owned: true })
488498
}
489499
}
490500
}

0 commit comments

Comments
 (0)