Merge pull request #12 from sbidin/master

Update to newest sdl2 and rust nightly.
This commit is contained in:
ShuYu Wang
2015-03-22 10:30:39 +08:00
4 changed files with 40 additions and 38 deletions
+1 -5
View File
@@ -1,21 +1,18 @@
[package]
name = "sdl2_ttf"
description = "SDL2_ttf bindings for Rust"
repository = "https://github.com/andelf/rust-sdl2_ttf"
version = "0.0.3"
license = "MIT"
readme = "README.md"
authors = [ "ShuYu Wang <andelf@gmail.com>" ]
authors = ["ShuYu Wang <andelf@gmail.com>"]
keywords = ["SDL", "windowing", "graphics", "font"]
[lib]
name = "sdl2_ttf"
path = "src/sdl2_ttf/lib.rs"
[dependencies]
bitflags = "0.1"
[dependencies.sdl2]
@@ -25,6 +22,5 @@ git = "https://github.com/AngryLawyer/rust-sdl2/"
git = "https://github.com/AngryLawyer/rust-sdl2/"
[[bin]]
name = "demo"
path = "src/demo/main.rs"
Regular → Executable
+8 -3
View File
@@ -3,16 +3,21 @@
extern crate sdl2;
extern crate sdl2_ttf;
use std::os;
use std::env;
use std::path::Path;
mod video;
fn main() {
let args = os::args();
let args: Vec<_> = env::args().collect();
println!("linked sdl2_ttf: {}", sdl2_ttf::get_linked_version());
if args.len() < 2 {
println!("Usage: ./demo font.[ttf|ttc|fon]")
} else {
video::main(&Path::new(os::args()[1].to_string()));
let path: &Path = Path::new(&args[1]);
video::main(path);
}
}
+12 -11
View File
@@ -1,5 +1,8 @@
use sdl2;
use sdl2_ttf;
use sdl2::event::Event;
use sdl2::keycode::KeyCode;
use std::path::Path;
static SCREEN_WIDTH : i32 = 800;
static SCREEN_HEIGHT : i32 = 600;
@@ -17,7 +20,7 @@ macro_rules! rect(
);
pub fn main(filename: &Path) {
sdl2::init(sdl2::INIT_VIDEO);
let sdl_context = sdl2::init(sdl2::INIT_VIDEO).unwrap();
sdl2_ttf::init();
let window = trying!(sdl2::video::Window::new(
@@ -44,19 +47,17 @@ pub fn main(filename: &Path) {
drawer.present();
'main : loop {
'event : loop {
match sdl2::event::poll_event() {
sdl2::event::Event::Quit{..} => break 'main,
sdl2::event::Event::KeyDown { keycode: key, .. } => {
if key == sdl2::keycode::KeyCode::Escape {
break 'main
}
}
let mut event_pump = sdl_context.event_pump();
'mainloop: loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit{..} => break 'mainloop,
Event::KeyDown {keycode: KeyCode::Escape, ..} => break 'mainloop,
_ => {}
}
}
}
sdl2_ttf::quit();
sdl2::quit();
}
+19 -19
View File
@@ -2,10 +2,9 @@
A binding for SDL2_ttf.
*/
#![feature(libc)]
#![crate_type = "lib"]
#![desc = "SDL2_ttf bindings and wrappers"]
#![comment = "SDL2_ttf bindings and wrappers"]
#![license = "MIT"]
extern crate libc;
extern crate sdl2;
@@ -15,8 +14,9 @@ extern crate "sdl2-sys" as sdl2_sys;
extern crate bitflags;
use libc::{c_int, c_long};
use std::ffi::{c_str_to_bytes, CString};
use std::ffi::{CString, CStr};
use std::num::FromPrimitive;
use std::path::Path;
use sdl2::surface::Surface;
use sdl2::get_error;
use sdl2::pixels;
@@ -66,7 +66,7 @@ bitflags! {
}
}
#[derive(Show, PartialEq, FromPrimitive)]
#[derive(Debug, PartialEq, FromPrimitive)]
pub enum Hinting {
HintingNormal = ffi::TTF_HINTING_NORMAL as isize,
HintingLight = ffi::TTF_HINTING_LIGHT as isize,
@@ -75,7 +75,7 @@ pub enum Hinting {
}
/// Glyph Metrics
#[derive(PartialEq, Clone, Show)]
#[derive(Debug, PartialEq, Clone)]
pub struct GlyphMetrics {
pub minx: isize,
pub maxx: isize,
@@ -115,7 +115,7 @@ pub fn quit() {
}
/// The opaque holder of a loaded font.
#[allow(raw_pointer_deriving)]
#[allow(raw_pointer_derive)]
#[derive(PartialEq)]
pub struct Font {
raw: *const ffi::TTF_Font,
@@ -143,7 +143,7 @@ impl Font {
pub fn from_file(filename: &Path, ptsize: isize) -> SdlResult<Font> {
//! Load file for use as a font, at ptsize size.
unsafe {
let cstring = CString::from_slice(filename.as_str().unwrap().as_bytes());
let cstring = CString::new(filename.to_str().unwrap()).unwrap();
let raw = ffi::TTF_OpenFont(cstring.as_ptr(), ptsize as c_int);
if raw.is_null() {
Err(get_error())
@@ -156,7 +156,7 @@ impl Font {
pub fn from_file_index(filename: &Path, ptsize: isize, index: isize) -> SdlResult<Font> {
//! Load file, face index, for use as a font, at ptsize size.
unsafe {
let cstring = CString::from_slice(filename.as_str().unwrap().as_bytes());
let cstring = CString::new(filename.to_str().unwrap().as_bytes()).unwrap();
let raw = ffi::TTF_OpenFontIndex(cstring.as_ptr(), ptsize as c_int, index as c_long);
if raw.is_null() {
Err(get_error())
@@ -273,7 +273,7 @@ impl Font {
if cname.is_null() {
None
} else {
Some(String::from_utf8_lossy(c_str_to_bytes(&cname)).to_string())
Some(String::from_utf8_lossy(CStr::from_ptr(cname).to_bytes()).to_string())
}
}
}
@@ -285,7 +285,7 @@ impl Font {
if cname.is_null() {
None
} else {
Some(String::from_utf8_lossy(c_str_to_bytes(&cname)).to_string())
Some(String::from_utf8_lossy(CStr::from_ptr(cname).to_bytes()).to_string())
}
}
}
@@ -327,7 +327,7 @@ impl Font {
let w = 0;
let h = 0;
let ret = unsafe {
let ctext = CString::from_slice(text).as_ptr();
let ctext = CString::new(text).unwrap().as_ptr();
ffi::TTF_SizeText(self.raw, ctext, &w, &h)
};
if ret != 0 {
@@ -342,7 +342,7 @@ impl Font {
let w = 0;
let h = 0;
let ret = unsafe {
let ctext = CString::from_slice(text.as_bytes());
let ctext = CString::new(text.as_bytes()).unwrap();
ffi::TTF_SizeUTF8(self.raw, ctext.as_ptr(), &w, &h)
};
if ret != 0 {
@@ -355,7 +355,7 @@ impl Font {
pub fn render_bytes_solid(&self, text: &[u8], fg: Color) -> SdlResult<Surface> {
//! Draw LATIN1 text in solid mode.
unsafe {
let ctext = CString::from_slice(text).as_ptr();
let ctext = CString::new(text).unwrap().as_ptr();
let raw = ffi::TTF_RenderText_Solid(self.raw, ctext, color_to_c_color(fg));
if raw.is_null() {
Err(get_error())
@@ -368,7 +368,7 @@ impl Font {
pub fn render_str_solid(&self, text: &str, fg: Color) -> SdlResult<Surface> {
//! Draw UTF8 text in solid mode.
unsafe {
let ctext = CString::from_slice(text.as_bytes());
let ctext = CString::new(text.as_bytes()).unwrap();
let raw = ffi::TTF_RenderUTF8_Solid(self.raw, ctext.as_ptr(), color_to_c_color(fg));
if raw.is_null() {
Err(get_error())
@@ -393,7 +393,7 @@ impl Font {
pub fn render_bytes_shaded(&self, text: &[u8], fg: Color, bg: Color) -> SdlResult<Surface> {
//! Draw LATIN1 text in shaded mode.
unsafe {
let ctext = CString::from_slice(text).as_ptr();
let ctext = CString::new(text).unwrap().as_ptr();
let raw = ffi::TTF_RenderText_Shaded(self.raw, ctext, color_to_c_color(fg), color_to_c_color(bg));
if raw.is_null() {
Err(get_error())
@@ -406,7 +406,7 @@ impl Font {
pub fn render_str_shaded(&self, text: &str, fg: Color, bg: Color) -> SdlResult<Surface> {
//! Draw UTF8 text in shaded mode.
unsafe {
let ctext = CString::from_slice(text.as_bytes());
let ctext = CString::new(text.as_bytes()).unwrap();
let raw = ffi::TTF_RenderUTF8_Shaded(self.raw, ctext.as_ptr(), color_to_c_color(fg), color_to_c_color(bg));
if raw.is_null() {
Err(get_error())
@@ -431,7 +431,7 @@ impl Font {
pub fn render_bytes_blended(&self, text: &[u8], fg: Color) -> SdlResult<Surface> {
//! Draw LATIN1 text in blended mode.
unsafe {
let ctext = CString::from_slice(text).as_ptr();
let ctext = CString::new(text).unwrap().as_ptr();
let raw = ffi::TTF_RenderText_Blended(self.raw, ctext, color_to_c_color(fg));
if raw.is_null() {
Err(get_error())
@@ -444,7 +444,7 @@ impl Font {
pub fn render_str_blended(&self, text: &str, fg: Color) -> SdlResult<Surface> {
//! Draw UTF8 text in blended mode.
unsafe {
let ctext = CString::from_slice(text.as_bytes());
let ctext = CString::new(text.as_bytes()).unwrap();
let raw = ffi::TTF_RenderUTF8_Blended(self.raw, ctext.as_ptr(), color_to_c_color(fg));
if raw.is_null() {
Err(get_error())