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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Texture used to draw the image to [`Renderer`].

use static_assertions::assert_not_impl_all;
use std::marker::PhantomData;
use std::ptr::NonNull;

use crate::color::Rgb;
use crate::geo::{Rect, Size};
use crate::renderer::Renderer;
use crate::surface::Surface;
use crate::{bind, EnumInt, Result, Sdl, SdlError};

pub mod lock;
mod query;

use lock::Lock;
pub use query::*;

/// An access type for the texture.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TextureAccess {
    /// Cannot mutate and lock.
    Static,
    /// Can mutate and lock.
    Streaming,
    /// Can use as the render target in [`Renderer`].
    Target,
}

impl TextureAccess {
    fn from_raw(raw: u32) -> Self {
        match raw as EnumInt {
            bind::SDL_TEXTUREACCESS_STATIC => TextureAccess::Static,
            bind::SDL_TEXTUREACCESS_STREAMING => TextureAccess::Streaming,
            bind::SDL_TEXTUREACCESS_TARGET => TextureAccess::Target,
            _ => unreachable!(),
        }
    }

    #[allow(clippy::unnecessary_cast)]
    fn as_raw(&self) -> u32 {
        (match self {
            TextureAccess::Static => bind::SDL_TEXTUREACCESS_STATIC,
            TextureAccess::Streaming => bind::SDL_TEXTUREACCESS_STREAMING,
            TextureAccess::Target => bind::SDL_TEXTUREACCESS_TARGET,
        }) as u32
    }
}

/// A texture used to draw the image to [`Renderer`].
pub struct Texture<'renderer> {
    texture: NonNull<bind::SDL_Texture>,
    clip: Option<Rect>,
    _phantom: PhantomData<&'renderer ()>,
}

impl std::fmt::Debug for Texture<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Texture")
            .field("clip", &self.clip)
            .finish_non_exhaustive()
    }
}

assert_not_impl_all!(Texture: Send, Sync);

impl<'renderer> Texture<'renderer> {
    /// Constructs a texture from the renderer with access type.
    ///
    /// # Errors
    ///
    /// Returns `Err` if failed to allocate, no rendering context was active, the format was unsupported, or the width or height were out of range.
    pub fn new(renderer: &'renderer Renderer<'renderer>, access: TextureAccess) -> Result<Self> {
        use super::window::ConfigExt;
        let Size { width, height } = renderer.window().size();
        let pixel_format = renderer.window().pixel_format();
        NonNull::new(unsafe {
            bind::SDL_CreateTexture(
                renderer.as_ptr(),
                pixel_format.as_raw(),
                access.as_raw() as i32,
                width as i32,
                height as i32,
            )
        })
        .map_or_else(
            || Err(SdlError::UnsupportedFeature),
            |texture| {
                Ok(Self {
                    texture,
                    clip: None,
                    _phantom: PhantomData,
                })
            },
        )
    }

    /// Constructs a texture from the [`Surface`]. The texture will be readonly and the access type will be [`TextureAccess::Static`].
    pub fn from_surface(
        renderer: &'renderer Renderer<'renderer>,
        surface: &'renderer impl Surface,
    ) -> Self {
        let ptr = unsafe {
            bind::SDL_CreateTextureFromSurface(renderer.as_ptr(), surface.as_ptr().as_ptr())
        };
        Self {
            texture: NonNull::new(ptr).unwrap(),
            clip: None,
            _phantom: PhantomData,
        }
    }

    pub(crate) fn as_ptr(&self) -> *mut bind::SDL_Texture {
        self.texture.as_ptr()
    }

    /// Returns the alpha mod of the texture.
    #[must_use]
    pub fn alpha_mod(&self) -> u8 {
        let mut alpha = 0;
        let ret = unsafe { bind::SDL_GetTextureAlphaMod(self.as_ptr(), &mut alpha) };
        if ret != 0 {
            Sdl::error_then_panic("Getting texture alpha mod");
        }
        alpha
    }

    /// Sets the alpha mod of the texture.
    ///
    /// # Errors
    ///
    /// Returns `Err` if setting the alpha mod is unsupported.
    ///
    /// # Panics
    ///
    /// Panics if some unrecoverable error is occurred.
    pub fn set_alpha_mod(&self, alpha: u8) -> Result<()> {
        let ret = unsafe { bind::SDL_SetTextureAlphaMod(self.as_ptr(), alpha) };
        if ret != 0 {
            let error = Sdl::error();
            if error == "That operation is not supported" {
                return Err(SdlError::UnsupportedFeature);
            }
            Sdl::error_then_panic("Setting texture alpha mod");
        }
        Ok(())
    }

    /// Returns the color mod of the texture.
    pub fn color_mod(&self) -> Rgb {
        let (mut r, mut g, mut b) = (0, 0, 0);
        let ret = unsafe { bind::SDL_GetTextureColorMod(self.as_ptr(), &mut r, &mut g, &mut b) };
        if ret != 0 {
            Sdl::error_then_panic("Getting texture color mod");
        }
        Rgb { r, g, b }
    }

    /// Sets the color mod of the texture.
    pub fn set_color_mod(&self, Rgb { r, g, b }: Rgb) {
        let ret = unsafe { bind::SDL_SetTextureColorMod(self.as_ptr(), r, g, b) };
        if ret != 0 {
            Sdl::error_then_panic("Getting texture color mod");
        }
    }

    /// Obtains the lock for the texture in area, or whole if `None`.
    pub fn lock(&'renderer mut self, area: Option<Rect>) -> Lock<'renderer> {
        Lock::new(self, area)
    }

    /// Return the clip area of the texture if available.
    #[must_use]
    pub fn clip(&self) -> &Option<Rect> {
        &self.clip
    }

    /// Sets the clip area.
    pub fn set_clip(&mut self, clip: Option<Rect>) {
        self.clip = clip;
    }

    /// Binds the texture to the current OpenGL context. And returns the size in the context.
    ///
    /// # Errors
    ///
    /// Returns `Err` if failed to bind the texture.
    pub fn bind_to_current_gl_context(&self) -> Result<(f32, f32)> {
        let mut width = 0f32;
        let mut height = 0f32;
        let ret = unsafe { bind::SDL_GL_BindTexture(self.as_ptr(), &mut width, &mut height) };
        if ret == 0 {
            Ok((width, height))
        } else {
            Err(SdlError::Others { msg: Sdl::error() })
        }
    }

    /// Unbinds the texture to the current OpenGL context.
    ///
    /// # Errors
    ///
    /// Returns `Err` if failed to unbind the texture.
    pub fn unbind_from_current_gl_context(&self) -> Result<()> {
        let ret = unsafe { bind::SDL_GL_UnbindTexture(self.as_ptr()) };
        if ret == 0 {
            Ok(())
        } else {
            Err(SdlError::UnsupportedFeature)
        }
    }
}

impl Drop for Texture<'_> {
    fn drop(&mut self) {
        unsafe { bind::SDL_DestroyTexture(self.as_ptr()) }
    }
}