Skip to content

Commit 1328100

Browse files
committed
use new style flag implementation
1 parent 54032be commit 1328100

File tree

3 files changed

+152
-60
lines changed

3 files changed

+152
-60
lines changed

src/demo/video.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ macro_rules! rect(
1717
)
1818

1919
pub fn main(filename: &Path) {
20-
sdl2::init([sdl2::InitVideo]);
20+
sdl2::init(sdl2::InitVideo);
2121
sdl2_ttf::init();
2222

2323
let window = trying!(sdl2::video::Window::new(
2424
"rust-sdl2 demo: Video", sdl2::video::PosCentered,
25-
sdl2::video::PosCentered, SCREEN_WIDTH, SCREEN_HEIGHT, [sdl2::video::OpenGL]));
25+
sdl2::video::PosCentered, SCREEN_WIDTH, SCREEN_HEIGHT, sdl2::video::OpenGL));
2626

2727
let renderer = trying!(sdl2::render::Renderer::from_window(
28-
window, sdl2::render::DriverAuto, [sdl2::render::Accelerated]));
28+
window, sdl2::render::DriverAuto, sdl2::render::Accelerated));
2929

3030
// Load a font
3131
let font = trying!(sdl2_ttf::Font::from_file(filename, 128));
3232

3333
// render a surface, and convert it to a texture bound to the renderer
34-
let surface = trying!(font.render_str_blended("Hello Rust!", sdl2::pixels::RGB(255, 0, 0)));
34+
let surface = trying!(font.render_str_blended("Hello Rust!", sdl2::pixels::Color::new(255, 0, 0, 255)));
3535
let texture = trying!(renderer.create_texture_from_surface(surface));
3636

37-
renderer.set_draw_color(sdl2::pixels::RGB(195, 217, 255));
37+
renderer.set_draw_color(sdl2::pixels::Color::new(195, 217, 255, 255));
3838
renderer.clear();
3939

4040
let (w, h) = match texture.query() {

src/sdl2_ttf/flag.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Loosely based on zlib-licensed code from RustAllegro
2+
// https://github.com/SiegeLord/RustAllegro
3+
//
4+
// Implements efficient, type-safe flags with support for bitwise operators.
5+
//
6+
// Usage:
7+
//
8+
// flag_type!(FlagTypeName {
9+
// FlagName1 = FlagValue1,
10+
// FlagName2 = FlagValue2,
11+
// ...
12+
// FlagNameN = FlagValueN
13+
// })
14+
//
15+
// fn foo(flag: FlagTypeName) {
16+
// let raw = (flag | FlagNameN).get();
17+
// bar(raw);
18+
// }
19+
20+
#![macro_escape]
21+
22+
macro_rules! flag_type(
23+
($typename:ident : $supertype:ident { $($name:ident = $value:expr),* }) => {
24+
pub struct $typename {
25+
bits: $supertype
26+
}
27+
28+
impl $typename {
29+
#[inline]
30+
pub fn new(bits: $supertype) -> $typename {
31+
$typename { bits: bits }
32+
}
33+
34+
#[inline]
35+
pub fn get(self) -> $supertype {
36+
self.bits
37+
}
38+
}
39+
40+
impl ::std::default::Default for $typename {
41+
fn default() -> $typename {
42+
$typename::new(0)
43+
}
44+
}
45+
46+
impl ::std::cmp::Eq for $typename {
47+
fn eq(&self, other: &$typename) -> bool {
48+
self.bits == other.bits
49+
}
50+
}
51+
52+
impl ::std::cmp::TotalEq for $typename {}
53+
54+
impl ::std::cmp::Ord for $typename {
55+
fn lt(&self, other: &$typename) -> bool {
56+
self.bits < other.bits
57+
}
58+
}
59+
60+
impl ::std::cmp::TotalOrd for $typename {
61+
fn cmp(&self, other: &$typename) -> Ordering {
62+
self.bits.cmp(&other.bits)
63+
}
64+
}
65+
66+
impl ::std::ops::Not<$typename> for $typename {
67+
fn not(&self) -> $typename {
68+
$typename { bits: !self.bits }
69+
}
70+
}
71+
72+
impl ::std::ops::BitAnd<$typename, $typename> for $typename {
73+
fn bitand(&self, rhs: &$typename) -> $typename {
74+
$typename { bits: self.bits & rhs.bits }
75+
}
76+
}
77+
78+
impl ::std::ops::BitOr<$typename, $typename> for $typename {
79+
fn bitor(&self, rhs: &$typename) -> $typename {
80+
$typename { bits: self.bits | rhs.bits }
81+
}
82+
}
83+
84+
impl ::std::ops::BitXor<$typename, $typename> for $typename {
85+
fn bitxor(&self, rhs: &$typename) -> $typename {
86+
$typename { bits: self.bits ^ rhs.bits }
87+
}
88+
}
89+
90+
impl ::std::ops::Shl<$supertype, $typename> for $typename {
91+
fn shl(&self, rhs: &$supertype) -> $typename {
92+
$typename { bits: self.bits << *rhs }
93+
}
94+
}
95+
96+
impl ::std::ops::Shr<$supertype, $typename> for $typename {
97+
fn shr(&self, rhs: &$supertype) -> $typename {
98+
$typename { bits: self.bits >> *rhs }
99+
}
100+
}
101+
102+
$(
103+
pub static $name: $typename = $typename { bits: $value as $supertype };
104+
)+
105+
};
106+
($typename:ident { $($name:ident = $value:expr),* }) => {
107+
flag_type!($typename : u32 { $($name = $value),* })
108+
}
109+
)

src/sdl2_ttf/lib.rs

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
A binding for SDL2_ttf.
33
*/
44

5+
#![feature(macro_rules)]
6+
57
#![crate_id="sdl2_ttf#sdl2_ttf:0.1"]
68
#![crate_type = "lib"]
79
#![desc = "SDL2_ttf bindings and wrappers"]
@@ -12,14 +14,11 @@ extern crate libc;
1214
extern crate sdl2;
1315

1416
use libc::{c_int, c_long};
15-
use std::ptr;
1617
use std::c_str::CString;
1718
use std::num::FromPrimitive;
1819
use sdl2::surface::Surface;
1920
use sdl2::get_error;
20-
use sdl2::pixels;
2121
use sdl2::pixels::Color;
22-
use sdl2::pixels::ll::SDL_Color;
2322
use sdl2::rwops::RWops;
2423
use sdl2::version::Version;
2524

@@ -45,16 +44,17 @@ mod others {
4544

4645
#[allow(non_camel_case_types, dead_code)]
4746
mod ffi;
47+
mod flag;
4848

4949
/// Font Style
5050
#[deriving(Show)]
51-
pub enum FontStyle {
52-
StyleNormal = ffi::TTF_STYLE_NORMAL as int,
53-
StyleBold = ffi::TTF_STYLE_BOLD as int,
54-
StyleItalic = ffi::TTF_STYLE_ITALIC as int,
55-
StyleUnderline = ffi::TTF_STYLE_UNDERLINE as int,
56-
StyleStrikeThrough = ffi::TTF_STYLE_STRIKETHROUGH as int
57-
}
51+
flag_type!(FontStyle : c_int {
52+
StyleNormal = ffi::TTF_STYLE_NORMAL,
53+
StyleBold = ffi::TTF_STYLE_BOLD,
54+
StyleItalic = ffi::TTF_STYLE_ITALIC,
55+
StyleUnderline = ffi::TTF_STYLE_UNDERLINE,
56+
StyleStrikeThrough = ffi::TTF_STYLE_STRIKETHROUGH
57+
})
5858

5959
#[deriving(Show, Eq, FromPrimitive)]
6060
pub enum Hinting {
@@ -74,14 +74,6 @@ pub struct GlyphMetrics {
7474
pub advance: int
7575
}
7676

77-
#[inline]
78-
fn color_to_c_color(color: Color) -> SDL_Color {
79-
match color {
80-
pixels::RGB(r, g, b) => SDL_Color { r: r, g: g, b: b, a: 255 },
81-
pixels::RGBA(r, g, b, a) => SDL_Color { r: r, g: g, b: b, a: a }
82-
}
83-
}
84-
8577
/// Returns the version of the dynamically linked SDL_ttf library
8678
pub fn get_linked_version() -> Version {
8779
unsafe {
@@ -133,20 +125,12 @@ impl Drop for Font {
133125
}
134126
}
135127

136-
fn wrap_font_styles(bitflags: u32) -> Vec<FontStyle> {
137-
let styles = [StyleBold, StyleItalic, StyleUnderline, StyleStrikeThrough];
138-
styles.iter().filter_map(|&flag| {
139-
if bitflags & (flag as u32) != 0 { Some(flag) }
140-
else { None }
141-
}).collect()
142-
}
143-
144128
impl Font {
145129
pub fn from_file(filename: &Path, ptsize: int) -> Result<~Font, ~str> {
146130
//! Load file for use as a font, at ptsize size.
147131
unsafe {
148132
let raw = ffi::TTF_OpenFont(filename.to_c_str().unwrap(), ptsize as c_int);
149-
if raw == ptr::null() {
133+
if raw.is_null() {
150134
Err(get_error())
151135
} else {
152136
Ok(~Font { raw: raw, owned: true })
@@ -158,25 +142,24 @@ impl Font {
158142
//! Load file, face index, for use as a font, at ptsize size.
159143
unsafe {
160144
let raw = ffi::TTF_OpenFontIndex(filename.to_c_str().unwrap(), ptsize as c_int, index as c_long);
161-
if raw == ptr::null() {
145+
if raw.is_null() {
162146
Err(get_error())
163147
} else {
164148
Ok(~Font { raw: raw, owned: true })
165149
}
166150
}
167151
}
168152

169-
pub fn get_style(&self) -> Vec<FontStyle> {
153+
pub fn get_style(&self) -> FontStyle {
170154
//! Get font render style
171155
let raw = unsafe { ffi::TTF_GetFontStyle(self.raw) };
172-
wrap_font_styles(raw as u32)
156+
FontStyle::new(raw)
173157
}
174158

175-
pub fn set_style(&mut self, styles: &[FontStyle]) {
159+
pub fn set_style(&mut self, styles: FontStyle) {
176160
//! Set font render style.
177-
let flags = styles.iter().fold(0i32, |flags, flag| { flags | *flag as i32 });
178161
unsafe {
179-
ffi::TTF_SetFontStyle(self.raw, flags)
162+
ffi::TTF_SetFontStyle(self.raw, styles.get())
180163
}
181164
}
182165

@@ -269,7 +252,7 @@ impl Font {
269252
unsafe {
270253
// not owns buffer
271254
let cname = ffi::TTF_FontFaceFamilyName(self.raw);
272-
if cname == ptr::null() {
255+
if cname.is_null() {
273256
None
274257
} else {
275258
Some(CString::new(cname, false).as_str().unwrap().into_owned())
@@ -281,7 +264,7 @@ impl Font {
281264
//! Get current font face style name string.
282265
unsafe {
283266
let cname = ffi::TTF_FontFaceStyleName(self.raw);
284-
if cname == ptr::null() {
267+
if cname.is_null() {
285268
None
286269
} else {
287270
Some(CString::new(cname, false).as_str().unwrap().into_owned())
@@ -357,9 +340,9 @@ impl Font {
357340
//! Draw LATIN1 text in solid mode.
358341
unsafe {
359342
let raw = text.with_c_str(|ctext| {
360-
ffi::TTF_RenderText_Solid(self.raw, ctext, color_to_c_color(fg))
343+
ffi::TTF_RenderText_Solid(self.raw, ctext, fg)
361344
});
362-
if raw == ptr::null() {
345+
if raw.is_null() {
363346
Err(get_error())
364347
} else {
365348
Ok(~Surface { raw: raw, owned: true })
@@ -371,9 +354,9 @@ impl Font {
371354
//! Draw UTF8 text in solid mode.
372355
unsafe {
373356
let raw = text.with_c_str(|ctext| {
374-
ffi::TTF_RenderUTF8_Solid(self.raw, ctext, color_to_c_color(fg))
357+
ffi::TTF_RenderUTF8_Solid(self.raw, ctext, fg)
375358
});
376-
if raw == ptr::null() {
359+
if raw.is_null() {
377360
Err(get_error())
378361
} else {
379362
Ok(~Surface { raw: raw, owned: true })
@@ -384,8 +367,8 @@ impl Font {
384367
pub fn render_char_solid(&self, ch: char, fg: Color) -> Result<~Surface, ~str> {
385368
//! Draw a UNICODE glyph in solid mode.
386369
unsafe {
387-
let raw = ffi::TTF_RenderGlyph_Solid(self.raw, ch as u16, color_to_c_color(fg));
388-
if raw == ptr::null() {
370+
let raw = ffi::TTF_RenderGlyph_Solid(self.raw, ch as u16, fg);
371+
if raw.is_null() {
389372
Err(get_error())
390373
} else {
391374
Ok(~Surface { raw: raw, owned: true })
@@ -397,9 +380,9 @@ impl Font {
397380
//! Draw LATIN1 text in shaded mode.
398381
unsafe {
399382
let raw = text.with_c_str(|ctext| {
400-
ffi::TTF_RenderText_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg))
383+
ffi::TTF_RenderText_Shaded(self.raw, ctext, fg, bg)
401384
});
402-
if raw == ptr::null() {
385+
if raw.is_null() {
403386
Err(get_error())
404387
} else {
405388
Ok(~Surface { raw: raw, owned: true })
@@ -411,9 +394,9 @@ impl Font {
411394
//! Draw UTF8 text in shaded mode.
412395
unsafe {
413396
let raw = text.with_c_str(|ctext| {
414-
ffi::TTF_RenderUTF8_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg))
397+
ffi::TTF_RenderUTF8_Shaded(self.raw, ctext, fg, bg)
415398
});
416-
if raw == ptr::null() {
399+
if raw.is_null() {
417400
Err(get_error())
418401
} else {
419402
Ok(~Surface { raw: raw, owned: true })
@@ -424,8 +407,8 @@ impl Font {
424407
pub fn render_char_shaded(&self, ch: char, fg: Color, bg: Color) -> Result<~Surface, ~str> {
425408
//! Draw a UNICODE glyph in shaded mode.
426409
unsafe {
427-
let raw = ffi::TTF_RenderGlyph_Shaded(self.raw, ch as u16, color_to_c_color(fg), color_to_c_color(bg));
428-
if raw == ptr::null() {
410+
let raw = ffi::TTF_RenderGlyph_Shaded(self.raw, ch as u16, fg, bg);
411+
if raw.is_null() {
429412
Err(get_error())
430413
} else {
431414
Ok(~Surface { raw: raw, owned: true })
@@ -437,9 +420,9 @@ impl Font {
437420
//! Draw LATIN1 text in blended mode.
438421
unsafe {
439422
let raw = text.with_c_str(|ctext| {
440-
ffi::TTF_RenderText_Blended(self.raw, ctext, color_to_c_color(fg))
423+
ffi::TTF_RenderText_Blended(self.raw, ctext, fg)
441424
});
442-
if raw == ptr::null() {
425+
if raw.is_null() {
443426
Err(get_error())
444427
} else {
445428
Ok(~Surface { raw: raw, owned: true })
@@ -451,9 +434,9 @@ impl Font {
451434
//! Draw UTF8 text in blended mode.
452435
unsafe {
453436
let raw = text.with_c_str(|ctext| {
454-
ffi::TTF_RenderUTF8_Blended(self.raw, ctext, color_to_c_color(fg))
437+
ffi::TTF_RenderUTF8_Blended(self.raw, ctext, fg)
455438
});
456-
if raw == ptr::null() {
439+
if raw.is_null() {
457440
Err(get_error())
458441
} else {
459442
Ok(~Surface { raw: raw, owned: true })
@@ -464,8 +447,8 @@ impl Font {
464447
pub fn render_char_blended(&self, ch: char, fg: Color) -> Result<~Surface, ~str> {
465448
//! Draw a UNICODE glyph in blended mode.
466449
unsafe {
467-
let raw = ffi::TTF_RenderGlyph_Blended(self.raw, ch as u16, color_to_c_color(fg));
468-
if raw == ptr::null() {
450+
let raw = ffi::TTF_RenderGlyph_Blended(self.raw, ch as u16, fg);
451+
if raw.is_null() {
469452
Err(get_error())
470453
} else {
471454
Ok(~Surface { raw: raw, owned: true })
@@ -488,7 +471,7 @@ impl LoaderRWops for RWops {
488471
let raw = unsafe {
489472
ffi::TTF_OpenFontRW(self.raw, 0, ptsize as c_int)
490473
};
491-
if raw == ptr::null() {
474+
if raw.is_null() {
492475
Err(get_error())
493476
} else {
494477
Ok(~Font { raw: raw, owned: true })
@@ -498,7 +481,7 @@ impl LoaderRWops for RWops {
498481
let raw = unsafe {
499482
ffi::TTF_OpenFontIndexRW(self.raw, 0, ptsize as c_int, index as c_long)
500483
};
501-
if raw == ptr::null() {
484+
if raw.is_null() {
502485
Err(get_error())
503486
} else {
504487
Ok(~Font { raw: raw, owned: true })

0 commit comments

Comments
 (0)