@@ -18,7 +18,9 @@ use std::c_str::CString;
18
18
use std:: num:: FromPrimitive ;
19
19
use sdl2:: surface:: Surface ;
20
20
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 ;
22
24
use sdl2:: rwops:: RWops ;
23
25
use sdl2:: version:: Version ;
24
26
@@ -46,6 +48,14 @@ mod others {
46
48
mod ffi;
47
49
mod flag;
48
50
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
+
49
59
/// Font Style
50
60
#[ deriving( Show ) ]
51
61
flag_type ! ( FontStyle : c_int {
@@ -126,26 +136,26 @@ impl Drop for Font {
126
136
}
127
137
128
138
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 > {
130
140
//! Load file for use as a font, at ptsize size.
131
141
unsafe {
132
142
let raw = ffi:: TTF_OpenFont ( filename. to_c_str ( ) . unwrap ( ) , ptsize as c_int ) ;
133
143
if raw. is_null ( ) {
134
144
Err ( get_error ( ) )
135
145
} else {
136
- Ok ( ~ Font { raw : raw, owned : true } )
146
+ Ok ( Font { raw : raw, owned : true } )
137
147
}
138
148
}
139
149
}
140
150
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 > {
142
152
//! Load file, face index, for use as a font, at ptsize size.
143
153
unsafe {
144
154
let raw = ffi:: TTF_OpenFontIndex ( filename. to_c_str ( ) . unwrap ( ) , ptsize as c_int , index as c_long ) ;
145
155
if raw. is_null ( ) {
146
156
Err ( get_error ( ) )
147
157
} else {
148
- Ok ( ~ Font { raw : raw, owned : true } )
158
+ Ok ( Font { raw : raw, owned : true } )
149
159
}
150
160
}
151
161
}
@@ -336,11 +346,11 @@ impl Font {
336
346
}
337
347
}
338
348
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 > {
340
350
//! Draw LATIN1 text in solid mode.
341
351
unsafe {
342
352
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 ) )
344
354
} ) ;
345
355
if raw. is_null ( ) {
346
356
Err ( get_error ( ) )
@@ -350,11 +360,11 @@ impl Font {
350
360
}
351
361
}
352
362
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 > {
354
364
//! Draw UTF8 text in solid mode.
355
365
unsafe {
356
366
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 ) )
358
368
} ) ;
359
369
if raw. is_null ( ) {
360
370
Err ( get_error ( ) )
@@ -364,10 +374,10 @@ impl Font {
364
374
}
365
375
}
366
376
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 > {
368
378
//! Draw a UNICODE glyph in solid mode.
369
379
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 ) ) ;
371
381
if raw. is_null ( ) {
372
382
Err ( get_error ( ) )
373
383
} else {
@@ -376,11 +386,11 @@ impl Font {
376
386
}
377
387
}
378
388
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 > {
380
390
//! Draw LATIN1 text in shaded mode.
381
391
unsafe {
382
392
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 ) )
384
394
} ) ;
385
395
if raw. is_null ( ) {
386
396
Err ( get_error ( ) )
@@ -390,11 +400,11 @@ impl Font {
390
400
}
391
401
}
392
402
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 > {
394
404
//! Draw UTF8 text in shaded mode.
395
405
unsafe {
396
406
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 ) )
398
408
} ) ;
399
409
if raw. is_null ( ) {
400
410
Err ( get_error ( ) )
@@ -404,10 +414,10 @@ impl Font {
404
414
}
405
415
}
406
416
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 > {
408
418
//! Draw a UNICODE glyph in shaded mode.
409
419
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 ) ) ;
411
421
if raw. is_null ( ) {
412
422
Err ( get_error ( ) )
413
423
} else {
@@ -416,11 +426,11 @@ impl Font {
416
426
}
417
427
}
418
428
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 > {
420
430
//! Draw LATIN1 text in blended mode.
421
431
unsafe {
422
432
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 ) )
424
434
} ) ;
425
435
if raw. is_null ( ) {
426
436
Err ( get_error ( ) )
@@ -430,11 +440,11 @@ impl Font {
430
440
}
431
441
}
432
442
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 > {
434
444
//! Draw UTF8 text in blended mode.
435
445
unsafe {
436
446
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 ) )
438
448
} ) ;
439
449
if raw. is_null ( ) {
440
450
Err ( get_error ( ) )
@@ -444,10 +454,10 @@ impl Font {
444
454
}
445
455
}
446
456
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 > {
448
458
//! Draw a UNICODE glyph in blended mode.
449
459
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 ) ) ;
451
461
if raw. is_null ( ) {
452
462
Err ( get_error ( ) )
453
463
} else {
@@ -461,30 +471,30 @@ impl Font {
461
471
/// Loader trait for RWops
462
472
pub trait LoaderRWops {
463
473
/// 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 > ;
465
475
/// 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 > ;
467
477
}
468
478
469
479
impl LoaderRWops for RWops {
470
- fn load_font ( & self , ptsize : int ) -> Result < ~ Font , ~str > {
480
+ fn load_font ( & self , ptsize : int ) -> Result < Font , ~str > {
471
481
let raw = unsafe {
472
482
ffi:: TTF_OpenFontRW ( self . raw , 0 , ptsize as c_int )
473
483
} ;
474
484
if raw. is_null ( ) {
475
485
Err ( get_error ( ) )
476
486
} else {
477
- Ok ( ~ Font { raw : raw, owned : true } )
487
+ Ok ( Font { raw : raw, owned : true } )
478
488
}
479
489
}
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 > {
481
491
let raw = unsafe {
482
492
ffi:: TTF_OpenFontIndexRW ( self . raw , 0 , ptsize as c_int , index as c_long )
483
493
} ;
484
494
if raw. is_null ( ) {
485
495
Err ( get_error ( ) )
486
496
} else {
487
- Ok ( ~ Font { raw : raw, owned : true } )
497
+ Ok ( Font { raw : raw, owned : true } )
488
498
}
489
499
}
490
500
}
0 commit comments