1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Surface Rotozoomer

use get_error;
use libc::c_int;
pub use std::f64::consts::PI;
use surface::Surface;
use sys::gfx::rotozoom;

/// `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) -> Result<Surface, String>;
    /// Rotates and zooms a surface with different horizontal and vertical scaling factors and optional anti-aliasing.
    fn rotozoom_xy(
        &self,
        angle: f64,
        zoomx: f64,
        zoomy: f64,
        smooth: bool,
    ) -> Result<Surface, String>;
    /// Zoom a surface by independent horizontal and vertical factors with optional smoothing.
    fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>;
    /// Shrink a surface by an integer ratio using averaging.
    fn shrink(&self, factorx: i32, factory: i32) -> Result<Surface, String>;
    /// Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
    fn rotate_90deg(&self, turns: i32) -> Result<Surface, String>;
}

impl<'a> RotozoomSurface for Surface<'a> {
    fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result<Surface, String> {
        let raw = unsafe { rotozoom::rotozoomSurface(self.raw(), angle, zoom, smooth as c_int) };
        if (raw as *mut ()).is_null() {
            Err(get_error())
        } else {
            unsafe { Ok(Surface::from_ll(raw)) }
        }
    }
    fn rotozoom_xy(
        &self,
        angle: f64,
        zoomx: f64,
        zoomy: f64,
        smooth: bool,
    ) -> Result<Surface, String> {
        let raw = unsafe {
            rotozoom::rotozoomSurfaceXY(self.raw(), angle, zoomx, zoomy, smooth as c_int)
        };
        if (raw as *mut ()).is_null() {
            Err(get_error())
        } else {
            unsafe { Ok(Surface::from_ll(raw)) }
        }
    }
    fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String> {
        let raw = unsafe { rotozoom::zoomSurface(self.raw(), zoomx, zoomy, smooth as c_int) };
        if (raw as *mut ()).is_null() {
            Err(get_error())
        } else {
            unsafe { Ok(Surface::from_ll(raw)) }
        }
    }
    fn shrink(&self, factorx: i32, factory: i32) -> Result<Surface, String> {
        let raw =
            unsafe { rotozoom::shrinkSurface(self.raw(), factorx as c_int, factory as c_int) };
        if (raw as *mut ()).is_null() {
            Err(get_error())
        } else {
            unsafe { Ok(Surface::from_ll(raw)) }
        }
    }
    fn rotate_90deg(&self, turns: i32) -> Result<Surface, String> {
        let raw = unsafe { rotozoom::rotateSurface90Degrees(self.raw(), turns as c_int) };
        if (raw as *mut ()).is_null() {
            Err(get_error())
        } else {
            unsafe { Ok(Surface::from_ll(raw)) }
        }
    }
}

pub fn get_zoom_size(width: i32, height: i32, zoomx: f64, zoomy: f64) -> (i32, i32) {
    let mut w: c_int = 0;
    let mut h: c_int = 0;
    unsafe {
        rotozoom::zoomSurfaceSize(
            width as c_int,
            height as c_int,
            zoomx,
            zoomy,
            &mut w,
            &mut h,
        )
    }
    (w as i32, h as i32)
}

pub fn get_rotozoom_size(width: i32, height: i32, angle: f64, zoom: f64) -> (i32, i32) {
    let mut w: c_int = 0;
    let mut h: c_int = 0;
    unsafe {
        rotozoom::rotozoomSurfaceSize(width as c_int, height as c_int, angle, zoom, &mut w, &mut h)
    }
    (w as i32, h as i32)
}

pub fn get_rotozoom_xy_size(
    width: i32,
    height: i32,
    angle: f64,
    zoomx: f64,
    zoomy: f64,
) -> (i32, i32) {
    let mut w: c_int = 0;
    let mut h: c_int = 0;
    unsafe {
        rotozoom::rotozoomSurfaceSizeXY(
            width as c_int,
            height as c_int,
            angle,
            zoomx,
            zoomy,
            &mut w,
            &mut h,
        )
    }
    (w as i32, h as i32)
}