diff --git a/src/sdl2_gfx/framerate.rs b/src/sdl2_gfx/framerate.rs index b1f6ca1e..affe1f0d 100644 --- a/src/sdl2_gfx/framerate.rs +++ b/src/sdl2_gfx/framerate.rs @@ -1,9 +1,9 @@ //! Framerate control use libc; -use libc::{c_void, uint32_t, malloc, size_t}; +use libc::{c_void, uint32_t, size_t}; use std::mem; -use sdl2::{SdlResult, ErrorMessage}; +use sdl2::get_error; mod ll { @@ -44,10 +44,12 @@ impl FPSManager { } /// Set the framerate in Hz. - pub fn set_framerate(&mut self, rate: u32) -> SdlResult<()> { + pub fn set_framerate(&mut self, rate: u32) -> Result<(), String> { let ret = unsafe { ll::SDL_setFramerate(self.raw, rate as uint32_t) }; - if ret == 0 { Ok(()) } - else { Err(ErrorMessage("set_framerate error: beyond lower/upper limit.".into())) } + match ret { + 0 => Ok(()), + _ => Err(get_error()) + } } /// Return the current target framerate in Hz. diff --git a/src/sdl2_gfx/imagefilter.rs b/src/sdl2_gfx/imagefilter.rs index 0ff7c436..b00ce207 100644 --- a/src/sdl2_gfx/imagefilter.rs +++ b/src/sdl2_gfx/imagefilter.rs @@ -3,7 +3,7 @@ use std::mem; use libc; use libc::{size_t, c_void, c_uint, c_int}; -use sdl2::{SdlResult, ErrorMessage}; +use sdl2::get_error; use c_vec::CVec; mod ll { @@ -112,7 +112,7 @@ fn cvec_with_size(sz: usize) -> CVec { } /// Filter using Add: D = saturation255(S1 + S2). -pub fn add(src1: CVec, src2: CVec) -> SdlResult> { +pub fn add(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -121,11 +121,11 @@ pub fn add(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterAdd error".into())) } + else { Err(get_error()) } } /// Filter using Mean: D = S1/2 + S2/2. -pub fn mean(src1: CVec, src2: CVec) -> SdlResult> { +pub fn mean(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -134,11 +134,11 @@ pub fn mean(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterMean error".into())) } + else { Err(get_error()) } } /// Filter using Sub: D = saturation0(S1 - S2). -pub fn sub(src1: CVec, src2: CVec) -> SdlResult> { +pub fn sub(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -147,11 +147,11 @@ pub fn sub(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterSub error".into())) } + else { Err(get_error()) } } /// Filter using AbsDiff: D = | S1 - S2 |. -pub fn abs_diff(src1: CVec, src2: CVec) -> SdlResult> { +pub fn abs_diff(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -160,11 +160,11 @@ pub fn abs_diff(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterAbsDiff error".into())) } + else { Err(get_error()) } } /// Filter using Mult: D = saturation255(S1 * S2). -pub fn mult(src1: CVec, src2: CVec) -> SdlResult> { +pub fn mult(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -173,11 +173,11 @@ pub fn mult(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterMult error".into())) } + else { Err(get_error()) } } /// Filter using MultNor: D = S1 * S2. -pub fn mult_nor(src1: CVec, src2: CVec) -> SdlResult> { +pub fn mult_nor(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -186,11 +186,11 @@ pub fn mult_nor(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterMultNor error".into())) } + else { Err(get_error()) } } /// Filter using MultDivby2: D = saturation255(S1/2 * S2). -pub fn mult_div_by2(src1: CVec, src2: CVec) -> SdlResult> { +pub fn mult_div_by2(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -199,11 +199,11 @@ pub fn mult_div_by2(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterMultDivby2 error".into())) } + else { Err(get_error()) } } /// Filter using MultDivby4: D = saturation255(S1/2 * S2/2). -pub fn mult_div_by4(src1: CVec, src2: CVec) -> SdlResult> { +pub fn mult_div_by4(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -212,11 +212,11 @@ pub fn mult_div_by4(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterMultDivby4 error".into())) } + else { Err(get_error()) } } /// Filter using BitAnd: D = S1 & S2. -pub fn bit_and(src1: CVec, src2: CVec) -> SdlResult> { +pub fn bit_and(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -225,11 +225,11 @@ pub fn bit_and(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterBitAnd error".into())) } + else { Err(get_error()) } } /// Filter using BitOr: D = S1 | S2. -pub fn bit_or(src1: CVec, src2: CVec) -> SdlResult> { +pub fn bit_or(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -238,11 +238,11 @@ pub fn bit_or(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterBitOr error".into())) } + else { Err(get_error()) } } /// Filter using Div: D = S1 / S2. -pub fn div(src1: CVec, src2: CVec) -> SdlResult> { +pub fn div(src1: CVec, src2: CVec) -> Result, String> { assert_eq!(src1.len(), src2.len()); let size = src1.len(); let dest = cvec_with_size(size); @@ -251,176 +251,176 @@ pub fn div(src1: CVec, src2: CVec) -> SdlResult> { mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterDiv error".into())) } + else { Err(get_error()) } } /// Filter using BitNegation: D = !S. -pub fn bit_negation(src1: CVec) -> SdlResult> { +pub fn bit_negation(src1: CVec) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterBitNegation(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterBitNegation error".into())) } + else { Err(get_error()) } } /// Filter using AddByte: D = saturation255(S + C). -pub fn add_byte(src1: CVec, c: u8) -> SdlResult> { +pub fn add_byte(src1: CVec, c: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterAddByte(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterAddByte error".into())) } + else { Err(get_error()) } } /// Filter using AddUint: D = saturation255((S[i] + Cs[i % 4]), Cs=Swap32((uint)C). -pub fn add_uint(src1: CVec, c: u32) -> SdlResult> { +pub fn add_uint(src1: CVec, c: u32) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterAddUint(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterAddUint error".into())) } + else { Err(get_error()) } } /// Filter using AddByteToHalf: D = saturation255(S/2 + C). -pub fn add_byte_to_half(src1: CVec, c: u8) -> SdlResult> { +pub fn add_byte_to_half(src1: CVec, c: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterAddByteToHalf(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterAddByteToHalf error".into())) } + else { Err(get_error()) } } /// Filter using SubByte: D = saturation0(S - C). -pub fn sub_byte(src1: CVec, c: u8) -> SdlResult> { +pub fn sub_byte(src1: CVec, c: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterSubByte(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterSubByte error".into())) } + else { Err(get_error()) } } /// Filter using SubUint: D = saturation0(S[i] - Cs[i % 4]), Cs=Swap32((uint)C). -pub fn sub_uint(src1: CVec, c: u32) -> SdlResult> { +pub fn sub_uint(src1: CVec, c: u32) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterSubUint(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterSubUint error".into())) } + else { Err(get_error()) } } /// Filter using ShiftRight: D = saturation0(S >> N). -pub fn shift_right(src1: CVec, n: u8) -> SdlResult> { +pub fn shift_right(src1: CVec, n: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterShiftRight(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, n) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterShiftRight error".into())) } + else { Err(get_error()) } } /// Filter using ShiftRightUint: D = saturation0((uint)S[i] >> N). -pub fn shift_right_uint(src1: CVec, n: u8) -> SdlResult> { +pub fn shift_right_uint(src1: CVec, n: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterShiftRightUint(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, n) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterShiftRightUint error".into())) } + else { Err(get_error()) } } /// Filter using MultByByte: D = saturation255(S * C). -pub fn mult_by_byte(src1: CVec, c: u8) -> SdlResult> { +pub fn mult_by_byte(src1: CVec, c: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterMultByByte(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterMultByByte error".into())) } + else { Err(get_error()) } } /// Filter using ShiftRightAndMultByByte: D = saturation255((S >> N) * C). -pub fn shift_right_and_mult_by_byte(src1: CVec, n: u8, c: u8) -> SdlResult> { +pub fn shift_right_and_mult_by_byte(src1: CVec, n: u8, c: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterShiftRightAndMultByByte(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, n, c) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterShiftRightAndMultByByte error".into())) } + else { Err(get_error()) } } /// Filter using ShiftLeftByte: D = (S << N). -pub fn shift_left_byte(src1: CVec, n: u8) -> SdlResult> { +pub fn shift_left_byte(src1: CVec, n: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterShiftLeftByte(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, n) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterShiftLeftByte error".into())) } + else { Err(get_error()) } } /// Filter using ShiftLeftUint: D = ((uint)S << N). -pub fn shift_left_uint(src1: CVec, n: u8) -> SdlResult> { +pub fn shift_left_uint(src1: CVec, n: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterShiftLeftUint(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, n) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterShiftLeftUint error".into())) } + else { Err(get_error()) } } /// Filter ShiftLeft: D = saturation255(S << N). -pub fn shift_left(src1: CVec, n: u8) -> SdlResult> { +pub fn shift_left(src1: CVec, n: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterShiftLeft(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, n) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterShiftLeft error".into())) } + else { Err(get_error()) } } /// Filter using BinarizeUsingThreshold: D = (S >= T) ? 255:0. -pub fn binarize_using_threshold(src1: CVec, t: u8) -> SdlResult> { +pub fn binarize_using_threshold(src1: CVec, t: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterBinarizeUsingThreshold(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, t) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterBinarizeUsingThreshold error".into())) } + else { Err(get_error()) } } /// Filter using ClipToRange: D = (S >= Tmin) & (S <= Tmax) S:Tmin | Tmax. -pub fn clip_to_range(src1: CVec, tmin: u8, tmax: u8) -> SdlResult> { +pub fn clip_to_range(src1: CVec, tmin: u8, tmax: u8) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterClipToRange(mem::transmute(src1.get(0)), mem::transmute(dest.get(0)), size as c_uint, tmin, tmax) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterClipToRange error".into())) } + else { Err(get_error()) } } /// Filter using NormalizeLinear: D = saturation255((Nmax - Nmin)/(Cmax - Cmin)*(S - Cmin) + Nmin). -pub fn normalize_linear(src1: CVec, cmin: isize, cmax: isize, nmin: isize, nmax: isize) -> SdlResult> { +pub fn normalize_linear(src1: CVec, cmin: isize, cmax: isize, nmin: isize, nmax: isize) -> Result, String> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterNormalizeLinear(mem::transmute(src1.get(0)), @@ -429,5 +429,5 @@ pub fn normalize_linear(src1: CVec, cmin: isize, cmax: isize, nmin: isize, n cmin as c_int, cmax as c_int, nmin as c_int, nmax as c_int) }; if ret == 0 { Ok(dest) } - else { Err(ErrorMessage("SDL_imageFilterNormalizeLinear error".into())) } + else { Err(get_error()) } } diff --git a/src/sdl2_gfx/primitives.rs b/src/sdl2_gfx/primitives.rs index 0b3f4b19..14912d78 100644 --- a/src/sdl2_gfx/primitives.rs +++ b/src/sdl2_gfx/primitives.rs @@ -9,7 +9,6 @@ use sdl2::render::Renderer; use sdl2::surface::Surface; use sdl2::pixels; use sdl2::get_error; -use sdl2::SdlResult; #[allow(dead_code)] mod ll { @@ -241,186 +240,186 @@ impl ToColor for isize { /// For drawing with rust-sdl2 Renderer pub trait DrawRenderer { - fn pixel(&self, x: i16, y: i16, color: C) -> SdlResult<()>; - fn hline(&self, x1: i16, x2: i16, y: i16, color: C) -> SdlResult<()>; - fn vline(&self, x: i16, y1: i16, y2: i16, color: C) -> SdlResult<()>; - fn rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()>; - fn rounded_rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> SdlResult<()>; - fn box_(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()>; - fn rounded_box(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> SdlResult<()>; - fn line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()>; - fn aa_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()>; - fn thick_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, width: u8, color: C) -> SdlResult<()>; - fn circle(&self, x: i16, y: i16, rad: i16, color: C) -> SdlResult<()>; - fn aa_circle(&self, x: i16, y: i16, rad: i16, color: C) -> SdlResult<()>; - fn filled_circle(&self, x: i16, y: i16, rad: i16, color: C) -> SdlResult<()>; - fn arc(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> SdlResult<()>; - fn ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> SdlResult<()>; - fn aa_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> SdlResult<()>; - fn filled_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> SdlResult<()>; - fn pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> SdlResult<()>; - fn filled_pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> SdlResult<()>; - fn trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> SdlResult<()>; - fn aa_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> SdlResult<()>; - fn filled_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> SdlResult<()>; - fn polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>; - fn aa_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>; - fn filled_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>; - fn textured_polygon(&self, vx: &[i16], vy: &[i16], texture: &Surface, texture_dx: i16, texture_dy: i16, color: C) -> SdlResult<()>; - fn bezier(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> SdlResult<()>; - fn character(&self, x: i16, y: i16, c: char, color: C) -> SdlResult<()>; - fn string(&self, x: i16, y: i16, s: &str, color: C) -> SdlResult<()>; + fn pixel(&self, x: i16, y: i16, color: C) -> Result<(), String>; + fn hline(&self, x1: i16, x2: i16, y: i16, color: C) -> Result<(), String>; + fn vline(&self, x: i16, y1: i16, y2: i16, color: C) -> Result<(), String>; + fn rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String>; + fn rounded_rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> Result<(), String>; + fn box_(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String>; + fn rounded_box(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> Result<(), String>; + fn line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String>; + fn aa_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String>; + fn thick_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, width: u8, color: C) -> Result<(), String>; + fn circle(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String>; + fn aa_circle(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String>; + fn filled_circle(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String>; + fn arc(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> Result<(), String>; + fn ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> Result<(), String>; + fn aa_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> Result<(), String>; + fn filled_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> Result<(), String>; + fn pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> Result<(), String>; + fn filled_pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> Result<(), String>; + fn trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> Result<(), String>; + fn aa_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> Result<(), String>; + fn filled_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> Result<(), String>; + fn polygon(&self, vx: &[i16], vy: &[i16], color: C) -> Result<(), String>; + fn aa_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> Result<(), String>; + fn filled_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> Result<(), String>; + fn textured_polygon(&self, vx: &[i16], vy: &[i16], texture: &Surface, texture_dx: i16, texture_dy: i16, color: C) -> Result<(), String>; + fn bezier(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> Result<(), String>; + fn character(&self, x: i16, y: i16, c: char, color: C) -> Result<(), String>; + fn string(&self, x: i16, y: i16, s: &str, color: C) -> Result<(), String>; } impl<'a> DrawRenderer for Renderer<'a> { - fn pixel(&self, x: i16, y: i16, color: C) -> SdlResult<()> { + fn pixel(&self, x: i16, y: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::pixelColor(self.raw(), x, y, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn hline(&self, x1: i16, x2: i16, y: i16, color: C) -> SdlResult<()> { + fn hline(&self, x1: i16, x2: i16, y: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::hlineColor(self.raw(), x1, x2, y, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn vline(&self, x: i16, y1: i16, y2: i16, color: C) -> SdlResult<()> { + fn vline(&self, x: i16, y1: i16, y2: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::vlineColor(self.raw(), x, y1, y2, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()> { + fn rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::rectangleColor(self.raw(), x1, y1, x2, y2, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn rounded_rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> SdlResult<()> { + fn rounded_rectangle(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::roundedRectangleColor(self.raw(), x1, y1, x2, y2, rad, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn box_(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()> { + fn box_(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::boxColor(self.raw(), x1, y1, x2, y2, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn rounded_box(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> SdlResult<()> { + fn rounded_box(&self, x1: i16, y1: i16, x2: i16, y2: i16, rad: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::roundedBoxColor(self.raw(), x1, y1, x2, y2, rad, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()> { + fn line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::lineColor(self.raw(), x1, y1, x2, y2, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn aa_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> SdlResult<()> { + fn aa_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::aalineColor(self.raw(), x1, y1, x2, y2, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn thick_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, width: u8, color: C) -> SdlResult<()> { + fn thick_line(&self, x1: i16, y1: i16, x2: i16, y2: i16, width: u8, color: C) -> Result<(), String> { let ret = unsafe { ll::thickLineColor(self.raw(), x1, y1, x2, y2, width, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn circle(&self, x: i16, y: i16, rad: i16, color: C) -> SdlResult<()> { + fn circle(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::circleColor(self.raw(), x, y, rad, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn aa_circle(&self, x: i16, y: i16, rad: i16, color: C) -> SdlResult<()> { + fn aa_circle(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::aacircleColor(self.raw(), x, y, rad, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn filled_circle(&self, x: i16, y: i16, rad: i16, color: C) -> SdlResult<()> { + fn filled_circle(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::filledCircleColor(self.raw(), x, y, rad, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn arc(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> SdlResult<()> { + fn arc(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::arcColor(self.raw(), x, y, rad, start, end, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> SdlResult<()> { + fn ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::ellipseColor(self.raw(), x, y, rx, ry, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn aa_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> SdlResult<()> { + fn aa_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::aaellipseColor(self.raw(), x, y, rx, ry, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn filled_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> SdlResult<()> { + fn filled_ellipse(&self, x: i16, y: i16, rx: i16, ry: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::filledEllipseColor(self.raw(), x, y, rx, ry, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> SdlResult<()> { + fn pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::pieColor(self.raw(), x, y, rad, start, end, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn filled_pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> SdlResult<()> { + fn filled_pie(&self, x: i16, y: i16, rad: i16, start: i16, end: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::filledPieColor(self.raw(), x, y, rad, start, end, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> SdlResult<()> { + fn trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::trigonColor(self.raw(), x1, y1, x2, y2, x3, y3, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn aa_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> SdlResult<()> { + fn aa_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::aatrigonColor(self.raw(), x1, y1, x2, y2, x3, y3, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - fn filled_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> SdlResult<()> { + fn filled_trigon(&self, x1: i16, y1: i16, x2: i16, y2: i16, x3: i16, y3: i16, color: C) -> Result<(), String> { let ret = unsafe { ll::filledTrigonColor(self.raw(), x1, y1, x2, y2, x3, y3, color.as_u32()) }; @@ -428,7 +427,7 @@ impl<'a> DrawRenderer for Renderer<'a> { else { Err(get_error()) } } // FIXME: may we use pointer tuple? - fn polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()> { + fn polygon(&self, vx: &[i16], vy: &[i16], color: C) -> Result<(), String> { assert_eq!(vx.len(), vy.len()); let n = vx.len() as c_int; let ret = unsafe { @@ -438,7 +437,7 @@ impl<'a> DrawRenderer for Renderer<'a> { else { Err(get_error()) } } - fn aa_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()> { + fn aa_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> Result<(), String> { assert_eq!(vx.len(), vy.len()); let n = vx.len() as c_int; let ret = unsafe { @@ -448,7 +447,7 @@ impl<'a> DrawRenderer for Renderer<'a> { else { Err(get_error()) } } - fn filled_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()> { + fn filled_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> Result<(), String> { assert_eq!(vx.len(), vy.len()); let n = vx.len() as c_int; let ret = unsafe { @@ -458,11 +457,11 @@ impl<'a> DrawRenderer for Renderer<'a> { else { Err(get_error()) } } #[allow(unused_variables)] - fn textured_polygon(&self, vx: &[i16], vy: &[i16], texture: &Surface, texture_dx: i16, texture_dy: i16, color: C) -> SdlResult<()> { + fn textured_polygon(&self, vx: &[i16], vy: &[i16], texture: &Surface, texture_dx: i16, texture_dy: i16, color: C) -> Result<(), String> { unimplemented!() } - fn bezier(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> SdlResult<()> { + fn bezier(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> Result<(), String> { assert_eq!(vx.len(), vy.len()); let n = vx.len() as c_int; let ret = unsafe { @@ -472,7 +471,7 @@ impl<'a> DrawRenderer for Renderer<'a> { else { Err(get_error()) } } - fn character(&self, x: i16, y: i16, c: char, color: C) -> SdlResult<()> { + fn character(&self, x: i16, y: i16, c: char, color: C) -> Result<(), String> { let ret = unsafe { ll::characterColor(self.raw(), x, y, c as c_char, color.as_u32()) }; @@ -480,7 +479,7 @@ impl<'a> DrawRenderer for Renderer<'a> { else { Err(get_error()) } } - fn string(&self, x: i16, y: i16, s: &str, color: C) -> SdlResult<()> { + fn string(&self, x: i16, y: i16, s: &str, color: C) -> Result<(), String> { let ret = unsafe { let buf = CString::new(s).unwrap().as_bytes().as_ptr(); ll::stringColor(self.raw(), x, y, buf as *mut i8, color.as_u32()) diff --git a/src/sdl2_gfx/rotozoom.rs b/src/sdl2_gfx/rotozoom.rs index d2a57415..c41e79cc 100644 --- a/src/sdl2_gfx/rotozoom.rs +++ b/src/sdl2_gfx/rotozoom.rs @@ -2,7 +2,7 @@ use libc::c_int; use sdl2::surface::Surface; -use sdl2::{SdlResult, ErrorMessage}; +use sdl2::get_error; pub use std::f64::consts::PI; @@ -39,64 +39,64 @@ mod ll { /// RotozoomSurface for work with rust-sdl2 Surface type pub trait RotozoomSurface { /// Rotates and zooms a surface and optional anti-aliasing. - fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> SdlResult; + fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result; /// Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing. - fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> SdlResult; + fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result; /// Zoom a surface by independent horizontal and vertical factors with optional smoothing. - fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> SdlResult; + fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result; /// Shrink a surface by an integer ratio using averaging. - fn shrink(&self, factorx: isize, factory: isize) -> SdlResult; + fn shrink(&self, factorx: isize, factory: isize) -> Result; /// Rotates a 8/16/24/32 bit surface in increments of 90 degrees. - fn rotate_90deg(&self, turns: isize) -> SdlResult; + fn rotate_90deg(&self, turns: isize) -> Result; } impl<'a> RotozoomSurface for Surface<'a> { - fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> SdlResult { + fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result { let raw = unsafe { ll::rotozoomSurface(self.raw(), angle, zoom, smooth as c_int) }; if (raw as *mut ()).is_null() { - Err(ErrorMessage("rotozoomSurface: error.".into())) + Err(get_error()) } else { unsafe { Ok(Surface::from_ll(raw)) } } } - fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> SdlResult { + fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result { let raw = unsafe { ll::rotozoomSurfaceXY(self.raw(), angle, zoomx, zoomy, smooth as c_int) }; if (raw as *mut ()).is_null() { - Err(ErrorMessage("rotozoomSurfaceXY: error.".into())) + Err(get_error()) } else { unsafe { Ok(Surface::from_ll(raw)) } } } - fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> SdlResult { + fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result { let raw = unsafe { ll::zoomSurface(self.raw(), zoomx, zoomy, smooth as c_int) }; if (raw as *mut ()).is_null() { - Err(ErrorMessage("zoomSurface: error.".into())) + Err(get_error()) } else { unsafe { Ok(Surface::from_ll(raw)) } } } - fn shrink(&self, factorx: isize, factory: isize) -> SdlResult { + fn shrink(&self, factorx: isize, factory: isize) -> Result { let raw = unsafe { ll::shrinkSurface(self.raw(), factorx as c_int, factory as c_int) }; if (raw as *mut ()).is_null() { - Err(ErrorMessage("shrinkSurface: error.".into())) + Err(get_error()) } else { unsafe { Ok(Surface::from_ll(raw)) } } } - fn rotate_90deg(&self, turns: isize) -> SdlResult { + fn rotate_90deg(&self, turns: isize) -> Result { let raw = unsafe { ll::rotateSurface90Degrees(self.raw(), turns as c_int) }; if (raw as *mut ()).is_null() { - Err(ErrorMessage("rotateSurface90Degrees: error.".into())) + Err(get_error()) } else { unsafe { Ok(Surface::from_ll(raw)) } }