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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::internal::*;
use crate::*;
use bytes::*;
use std::ffi::*;
use std::marker::PhantomData;
#[derive(Debug, PartialEq, Eq)]
pub struct Texture<'a> {
pub id: u32,
pub width: u32,
pub height: u32,
pub(crate) raw: *mut BLZ_Texture,
pub(crate) _marker: PhantomData<&'a ()>,
pub(crate) no_free: bool,
}
enum_from_primitive! {
#[cfg_attr(tarpaulin, skip)]
#[derive(Debug, PartialEq)]
pub enum ImageChannels
{
Auto = BLZ_ImageChannels_AUTO as isize,
Grayscale = BLZ_ImageChannels_GRAYSCALE as isize,
GrayscaleAlpha = BLZ_ImageChannels_GRAYSCALE_ALPHA as isize,
RGB = BLZ_ImageChannels_RGB as isize,
RGBA = BLZ_ImageChannels_RGBA as isize
}
}
bitflags! {
#[cfg_attr(tarpaulin, skip)]
pub struct ImageFlags: u32 {
const None = 0;
const PowerOfTwo = BLZ_ImageFlags_POWER_OF_TWO;
const Mipmaps = BLZ_ImageFlags_MIPMAPS;
const Repeats = BLZ_ImageFlags_TEXTURE_REPEATS;
const MultiplyAlpha = BLZ_ImageFlags_MULTIPLY_ALPHA;
const InvertY = BLZ_ImageFlags_INVERT_Y;
const CompressToDXT = BLZ_ImageFlags_COMPRESS_TO_DXT;
const DDSLoadDirect = BLZ_ImageFlags_DDS_LOAD_DIRECT;
const NTSCSafeRGB = BLZ_ImageFlags_NTSC_SAFE_RGB;
const CoCgY = BLZ_ImageFlags_CoCg_Y;
const TextureRectangle = BLZ_ImageFlags_TEXTURE_RECTANGLE;
}
}
enum_from_primitive! {
#[cfg_attr(tarpaulin, skip)]
#[derive(Debug, PartialEq)]
pub enum SaveImageFormat
{
TGA = BLZ_SaveImageFormat_TGA as isize,
BMP = BLZ_SaveImageFormat_BMP as isize,
DDS = BLZ_SaveImageFormat_DDS as isize
}
}
enum_from_primitive! {
#[cfg_attr(tarpaulin, skip)]
#[derive(Debug, PartialEq)]
pub enum TextureFilter {
Nearest = BLZ_TextureFilter_NEAREST as isize,
Linear = BLZ_TextureFilter_LINEAR as isize,
}
}
enum_from_primitive! {
#[cfg_attr(tarpaulin, skip)]
#[derive(Debug, PartialEq)]
pub enum TextureWrap {
ClampToEdge = BLZ_TextureWrap_CLAMP_TO_EDGE as isize,
Repeat = BLZ_TextureWrap_REPEAT as isize,
MirroredRepeat = BLZ_TextureWrap_MIRRORED_REPEAT as isize,
}
}
impl<'a> Drop for Texture<'a> {
fn drop(&mut self) {
unsafe {
if !self.no_free {
BLZ_FreeTexture(self.raw);
}
}
}
}
unsafe fn from_ptr<'a>(ptr: *mut BLZ_Texture) -> Result<Texture<'a>, String> {
if ptr.is_null() {
Err(try_get_err())
} else {
let tex = *ptr;
Ok(Texture {
raw: ptr,
_marker: PhantomData,
id: tex.id,
width: tex.width as u32,
height: tex.height as u32,
no_free: false,
})
}
}
fn path_to_ptr(path: &str) -> Result<CString, String> {
CString::new(path.to_owned()).map_err(|_| "Path cannot be null".to_owned())
}
impl<'a> Texture<'a> {
pub fn set_filtering(
&self,
minification: TextureFilter,
magnification: TextureFilter,
) -> CallResult {
unsafe {
wrap_result(BLZ_SetTextureFiltering(
self.raw,
minification as u32,
magnification as u32,
))
}
}
pub fn set_wrap(&self, x: TextureWrap, y: TextureWrap) -> CallResult {
unsafe { wrap_result(BLZ_SetTextureWrap(self.raw, x as u32, y as u32)) }
}
pub fn from_memory(
bytes: &Bytes,
channels: ImageChannels,
texture_id: Option<u32>,
flags: ImageFlags,
) -> Result<Texture<'a>, String> {
unsafe {
if let Some(i) = texture_id {
if i <= 0 {
return Err("Invalid texture ID, must be greater than zero".to_owned());
}
}
let buf_ptr = bytes.as_ptr();
let ptr = BLZ_LoadTextureFromMemory(
buf_ptr,
bytes.len() as i32,
channels as u32,
match texture_id {
Some(i) => i,
None => 0,
},
flags.bits,
);
from_ptr(ptr)
}
}
pub fn from_file(
path: &str,
channels: ImageChannels,
texture_id: Option<u32>,
flags: ImageFlags,
) -> Result<Texture<'a>, String> {
unsafe {
if let Some(i) = texture_id {
if i <= 0 {
return Err("Invalid texture ID, must be greater than zero".to_owned());
}
}
let path_ptr = path_to_ptr(path);
if let Ok(p) = path_ptr {
return from_ptr(BLZ_LoadTextureFromFile(
p.as_ptr(),
channels as u32,
match texture_id {
Some(i) => i,
None => 0,
},
flags.bits,
));
} else {
return Err("Invalid path".to_owned());
}
}
}
pub fn get_max_slots() -> u32 {
unsafe { BLZ_GetMaxTextureSlots() as u32 }
}
pub fn bind(texture: Option<&Texture>, slot: u32) -> CallResult {
unsafe {
if let Some(tex) = texture {
wrap_result(BLZ_BindTexture(tex.raw, slot as i32))
} else {
wrap_result(BLZ_BindTexture(std::ptr::null_mut(), slot as i32))
}
}
}
}
pub fn save_screenshot(
path: &str,
format: SaveImageFormat,
x_start: u32,
y_start: u32,
width: u32,
height: u32,
) -> CallResult {
let path_ptr = path_to_ptr(path);
unsafe {
match path_ptr {
Ok(p) => wrap_result(BLZ_SaveScreenshot(
p.as_ptr(),
format as u32,
x_start as i32,
y_start as i32,
width as i32,
height as i32,
)),
Err(s) => Err(s),
}
}
}