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
use static_assertions::assert_not_impl_all;
use std::ffi::{CStr, CString};
use std::ptr::NonNull;
use crate::{bind, Result, Sdl, SdlError};
use super::{RawSurface, Surface};
pub struct Bmp {
ptr: NonNull<RawSurface>,
}
impl std::fmt::Debug for Bmp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Bmp").finish_non_exhaustive()
}
}
assert_not_impl_all!(Bmp: Send, Sync);
impl Bmp {
pub fn new(file_name: &str) -> Result<Self> {
let c_str = CString::new(file_name).expect("must be a valid string");
let read_binary_mode = CStr::from_bytes_with_nul(b"rb\0").unwrap();
let ptr = NonNull::new(unsafe {
bind::SDL_LoadBMP_RW(
bind::SDL_RWFromFile(c_str.as_ptr(), read_binary_mode.as_ptr()),
1,
)
})
.ok_or_else(|| SdlError::Others { msg: Sdl::error() })?;
Ok(Self { ptr })
}
}
impl Surface for Bmp {
fn as_ptr(&self) -> NonNull<RawSurface> {
self.ptr
}
}
impl Drop for Bmp {
fn drop(&mut self) {
unsafe { bind::SDL_FreeSurface(self.ptr.as_ptr()) }
}
}
#[derive(Debug, Clone)]
pub struct BmpSaveError(pub String);
pub trait BmpSaveExt {
fn save_bmp(&self, file_name: &str) -> std::result::Result<(), BmpSaveError>;
}
impl<T: Surface> BmpSaveExt for T {
fn save_bmp(&self, file_name: &str) -> std::result::Result<(), BmpSaveError> {
let write_binary_mode = CStr::from_bytes_with_nul(b"wb\0").unwrap();
let c_str = CString::new(file_name).expect("must be a valid string");
let ret = unsafe {
bind::SDL_SaveBMP_RW(
self.as_ptr().as_ptr(),
bind::SDL_RWFromFile(c_str.as_ptr(), write_binary_mode.as_ptr()),
1,
)
};
if ret != 0 {
return Err(BmpSaveError(Sdl::error()));
}
Ok(())
}
}