diff --git a/examples/demo.rs b/examples/demo.rs index 311ec40f498..e16215e738a 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -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)); diff --git a/examples/image_demo.rs b/examples/image_demo.rs index 47e1c726340..a022a9cda6d 100755 --- a/examples/image_demo.rs +++ b/examples/image_demo.rs @@ -2,7 +2,7 @@ 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; @@ -10,7 +10,6 @@ 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() @@ -18,8 +17,9 @@ pub fn run(png: &Path) { .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) diff --git a/src/sdl2/image/mod.rs b/src/sdl2/image/mod.rs index 1dbebc3ef5b..7e471f7c943 100755 --- a/src/sdl2/image/mod.rs +++ b/src/sdl2/image/mod.rs @@ -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: @@ -23,11 +23,11 @@ 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. @@ -35,17 +35,17 @@ use sys; 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)] @@ -152,39 +152,41 @@ impl<'a> SaveSurface for Surface<'a> { } } -/// Method extensions for creating Textures from a Renderer -pub trait LoadTexture { - fn load_texture>(&self, filename: P) -> Result; +/// 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>(&self, filename: P) -> Result { +impl<'a, 'c> Sdl2ImageContext<'a, 'c> { + pub fn load_texture>(&self, filename: P) -> Result { //! 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 { +pub fn init<'a, 'c>(flags: InitFlag, + render_context: &'c RenderContext<'a>) + -> Result, String> { let return_flags = unsafe { let used = ffi::IMG_Init(flags.bits() as c_int); InitFlag::from_bits_truncate(used as u32) @@ -199,15 +201,13 @@ pub fn init(flags: InitFlag) -> Result { } 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] @@ -238,7 +238,7 @@ pub trait ImageRWops { fn load_png(&self) -> Result; fn load_tga(&self) -> Result; fn load_lbm(&self) -> Result; - fn load_xv(&self) -> Result; + fn load_xv(&self) -> Result; fn load_webp(&self) -> Result; fn is_cur(&self) -> bool; @@ -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 { - 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 { @@ -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 { + fn load_xv(&self) -> Result { let raw = unsafe { ffi::IMG_LoadXV_RW(self.raw()) }; to_surface_result(raw) } - fn load_webp(&self) -> Result { + fn load_webp(&self) -> Result { let raw = unsafe { ffi::IMG_LoadWEBP_RW(self.raw()) }; to_surface_result(raw) } @@ -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 } } } diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 2f5ad59d38d..d0da319a388 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -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)) } } } @@ -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)) } } } } @@ -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(), } }