Skip to content

Commit

Permalink
split load_texture from the renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
nrxus committed Apr 11, 2017
1 parent 0dd1d48 commit 24b4666
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn main() {
.build()
.unwrap();

let mut context = window.renderer().build().unwrap();
let context = window.renderer().build().unwrap();
let mut renderer = context.renderer();

renderer.set_draw_color(Color::RGB(255, 0, 0));
Expand Down
6 changes: 3 additions & 3 deletions examples/image_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ extern crate sdl2;

use std::env;
use std::path::Path;
use sdl2::image::{LoadTexture, INIT_PNG, INIT_JPG};
use sdl2::image::{INIT_PNG, INIT_JPG};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;

pub fn run(png: &Path) {

let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let _image_context = sdl2::image::init(INIT_PNG | INIT_JPG).unwrap();
let window = video_subsystem
.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.build()
.unwrap();

let context = window.renderer().software().build().unwrap();
let image_context = sdl2::image::init(INIT_PNG | INIT_JPG, &context).unwrap();
let mut renderer = context.renderer();
let texture = renderer.load_texture(png).unwrap();
let texture = image_context.load_texture(png).unwrap();

renderer
.copy(&texture, None, None)
Expand Down
80 changes: 39 additions & 41 deletions src/sdl2/image/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//!
//! A binding for the library `SDL2_image`
//!
//!
//!
//! Note that you need to build with the
//! feature `image` for this module to be enabled,
//! like so:
Expand All @@ -23,29 +23,29 @@
use std::os::raw::{c_int, c_char};
use std::ffi::CString;
use std::path::Path;
use ::surface::Surface;
use ::render::{Renderer, Texture};
use ::rwops::RWops;
use ::version::Version;
use ::get_error;
use surface::Surface;
use render::{RenderContext, Texture};
use rwops::RWops;
use version::Version;
use get_error;
use sys;

// Setup linking for all targets.
#[cfg(target_os="macos")]
mod mac {
#[cfg(any(mac_framework, feature="use_mac_framework"))]
#[link(kind="framework", name="SDL2_image")]
extern {}
extern "C" {}

#[cfg(not(any(mac_framework, feature="use_mac_framework")))]
#[link(name="SDL2_image")]
extern {}
extern "C" {}
}

#[cfg(any(target_os="windows", target_os="linux", target_os="freebsd"))]
mod others {
#[link(name="SDL2_image")]
extern {}
extern "C" {}
}

#[allow(non_camel_case_types, dead_code)]
Expand Down Expand Up @@ -152,39 +152,41 @@ impl<'a> SaveSurface for Surface<'a> {
}
}

/// Method extensions for creating Textures from a Renderer
pub trait LoadTexture {
fn load_texture<P: AsRef<Path>>(&self, filename: P) -> Result<Texture, String>;
/// Context manager for `sdl2_image` to manage quiting. Can't do much with it but
/// keep it alive while you are using it.
pub struct Sdl2ImageContext<'a: 'c, 'c> {
render_context: &'c RenderContext<'a>,
}

impl<'a, 'c> Drop for Sdl2ImageContext<'a, 'c> {
fn drop(&mut self) {
unsafe {
ffi::IMG_Quit();
}
}
}

impl<'a, 'c> LoadTexture for Renderer<'a, 'c> {
fn load_texture<P: AsRef<Path>>(&self, filename: P) -> Result<Texture, String> {
impl<'a, 'c> Sdl2ImageContext<'a, 'c> {
pub fn load_texture<P: AsRef<Path>>(&self, filename: P) -> Result<Texture, String> {
//! Loads an SDL Texture from a file
unsafe {
let c_filename = CString::new(filename.as_ref().to_str().unwrap()).unwrap();
let raw = ffi::IMG_LoadTexture(self.context.raw(), c_filename.as_ptr() as *const _);
let raw = ffi::IMG_LoadTexture(self.render_context.raw(),
c_filename.as_ptr() as *const _);
if (raw as *mut ()).is_null() {
Err(get_error())
} else {
Ok(Texture::from_ll(self, raw))
Ok(Texture::from_ll(self.render_context, raw))
}
}
}
}

/// Context manager for `sdl2_image` to manage quiting. Can't do much with it but
/// keep it alive while you are using it.
pub struct Sdl2ImageContext;

impl Drop for Sdl2ImageContext {
fn drop(&mut self) {
unsafe { ffi::IMG_Quit(); }
}
}

/// Initializes `SDL2_image` with `InitFlags`.
/// If not every flag is set it returns an error
pub fn init(flags: InitFlag) -> Result<Sdl2ImageContext, String> {
pub fn init<'a, 'c>(flags: InitFlag,
render_context: &'c RenderContext<'a>)
-> Result<Sdl2ImageContext<'a, 'c>, String> {
let return_flags = unsafe {
let used = ffi::IMG_Init(flags.bits() as c_int);
InitFlag::from_bits_truncate(used as u32)
Expand All @@ -199,15 +201,13 @@ pub fn init(flags: InitFlag) -> Result<Sdl2ImageContext, String> {
}
Err(error)
} else {
Ok(Sdl2ImageContext)
Ok(Sdl2ImageContext { render_context })
}
}

/// Returns the version of the dynamically linked `SDL_image` library
pub fn get_linked_version() -> Version {
unsafe {
Version::from_ll(*ffi::IMG_Linked_Version())
}
unsafe { Version::from_ll(*ffi::IMG_Linked_Version()) }
}

#[inline]
Expand Down Expand Up @@ -238,7 +238,7 @@ pub trait ImageRWops {
fn load_png(&self) -> Result<Surface, String>;
fn load_tga(&self) -> Result<Surface, String>;
fn load_lbm(&self) -> Result<Surface, String>;
fn load_xv(&self) -> Result<Surface, String>;
fn load_xv(&self) -> Result<Surface, String>;
fn load_webp(&self) -> Result<Surface, String>;

fn is_cur(&self) -> bool;
Expand All @@ -253,15 +253,13 @@ pub trait ImageRWops {
fn is_tif(&self) -> bool;
fn is_png(&self) -> bool;
fn is_lbm(&self) -> bool;
fn is_xv(&self) -> bool;
fn is_xv(&self) -> bool;
fn is_webp(&self) -> bool;
}

impl<'a> ImageRWops for RWops<'a> {
fn load(&self) -> Result<Surface, String> {
let raw = unsafe {
ffi::IMG_Load_RW(self.raw(), 0)
};
let raw = unsafe { ffi::IMG_Load_RW(self.raw(), 0) };
to_surface_result(raw)
}
fn load_typed(&self, _type: &str) -> Result<Surface, String> {
Expand Down Expand Up @@ -324,11 +322,11 @@ impl<'a> ImageRWops for RWops<'a> {
let raw = unsafe { ffi::IMG_LoadLBM_RW(self.raw()) };
to_surface_result(raw)
}
fn load_xv(&self) -> Result<Surface, String> {
fn load_xv(&self) -> Result<Surface, String> {
let raw = unsafe { ffi::IMG_LoadXV_RW(self.raw()) };
to_surface_result(raw)
}
fn load_webp(&self) -> Result<Surface, String> {
fn load_webp(&self) -> Result<Surface, String> {
let raw = unsafe { ffi::IMG_LoadWEBP_RW(self.raw()) };
to_surface_result(raw)
}
Expand Down Expand Up @@ -369,10 +367,10 @@ impl<'a> ImageRWops for RWops<'a> {
fn is_lbm(&self) -> bool {
unsafe { ffi::IMG_isLBM(self.raw()) == 1 }
}
fn is_xv(&self) -> bool {
unsafe { ffi::IMG_isXV(self.raw()) == 1 }
fn is_xv(&self) -> bool {
unsafe { ffi::IMG_isXV(self.raw()) == 1 }
}
fn is_webp(&self) -> bool {
unsafe { ffi::IMG_isWEBP(self.raw()) == 1 }
unsafe { ffi::IMG_isWEBP(self.raw()) == 1 }
}
}
8 changes: 4 additions & 4 deletions src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl<'a, 'c> Renderer<'a, 'c> {
if result == ptr::null_mut() {
Err(SdlError(get_error()))
} else {
unsafe { Ok(Texture::from_ll(self, result)) }
unsafe { Ok(Texture::from_ll(self.context, result)) }
}
}

Expand Down Expand Up @@ -477,7 +477,7 @@ impl<'a, 'c> Renderer<'a, 'c> {
if result == ptr::null_mut() {
Err(SdlError(get_error()))
} else {
unsafe { Ok(Texture::from_ll(self, result)) }
unsafe { Ok(Texture::from_ll(self.context, result)) }
}
}
}
Expand Down Expand Up @@ -1624,10 +1624,10 @@ impl Texture {
}
}

pub unsafe fn from_ll(renderer: &Renderer, raw: *mut ll::SDL_Texture) -> Texture {
pub unsafe fn from_ll(context: &RenderContext, raw: *mut ll::SDL_Texture) -> Texture {
Texture {
raw: raw,
is_renderer_alive: renderer.context.is_alive.clone(),
is_renderer_alive: context.is_alive.clone(),
}
}

Expand Down

0 comments on commit 24b4666

Please sign in to comment.