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
use std::cell::Cell;
use std::path::Path;
use std::rc::Rc;
use crate::error::{Result, TetraError};
use crate::fs;
use crate::graphics::{self, DrawParams, Drawable, Rectangle};
use crate::platform::{GraphicsDevice, RawTexture};
use crate::Context;
#[derive(Debug)]
pub(crate) struct TextureSharedData {
pub(crate) handle: RawTexture,
filter_mode: Cell<FilterMode>,
}
impl PartialEq for TextureSharedData {
fn eq(&self, other: &TextureSharedData) -> bool {
self.handle.eq(&other.handle)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Texture {
pub(crate) data: Rc<TextureSharedData>,
}
impl Texture {
pub fn new<P>(ctx: &mut Context, path: P) -> Result<Texture>
where
P: AsRef<Path>,
{
let image = fs::read_to_image(path)?.to_rgba();
let (width, height) = image.dimensions();
Texture::from_rgba(
ctx,
width as i32,
height as i32,
image.into_raw().as_slice(),
)
}
pub fn from_file_data(ctx: &mut Context, data: &[u8]) -> Result<Texture> {
let image = image::load_from_memory(data)
.map_err(TetraError::InvalidTexture)?
.to_rgba();
let (width, height) = image.dimensions();
Texture::from_rgba(
ctx,
width as i32,
height as i32,
image.into_raw().as_slice(),
)
}
pub fn from_rgba(ctx: &mut Context, width: i32, height: i32, data: &[u8]) -> Result<Texture> {
Texture::with_device(
&mut ctx.device,
width,
height,
data,
ctx.graphics.default_filter_mode,
)
}
pub(crate) fn with_device(
device: &mut GraphicsDevice,
width: i32,
height: i32,
data: &[u8],
filter_mode: FilterMode,
) -> Result<Texture> {
let expected = (width * height * 4) as usize;
let actual = data.len();
if expected > actual {
return Err(TetraError::NotEnoughData { expected, actual });
}
let handle = device.new_texture(width, height)?;
device.set_texture_data(&handle, &data, 0, 0, width, height);
device.set_texture_filter_mode(&handle, filter_mode);
Ok(Texture {
data: Rc::new(TextureSharedData {
handle,
filter_mode: Cell::new(FilterMode::Linear),
}),
})
}
pub(crate) fn with_device_empty(
device: &mut GraphicsDevice,
width: i32,
height: i32,
filter_mode: FilterMode,
) -> Result<Texture> {
let handle = device.new_texture(width, height)?;
device.set_texture_filter_mode(&handle, filter_mode);
Ok(Texture {
data: Rc::new(TextureSharedData {
handle,
filter_mode: Cell::new(filter_mode),
}),
})
}
pub fn width(&self) -> i32 {
self.data.handle.width()
}
pub fn height(&self) -> i32 {
self.data.handle.height()
}
pub fn size(&self) -> (i32, i32) {
(self.data.handle.width(), self.data.handle.height())
}
pub fn filter_mode(&self) -> FilterMode {
self.data.filter_mode.get()
}
pub fn set_filter_mode(&mut self, ctx: &mut Context, filter_mode: FilterMode) {
ctx.device
.set_texture_filter_mode(&self.data.handle, filter_mode);
self.data.filter_mode.set(filter_mode);
}
}
impl Drawable for Texture {
fn draw<P>(&self, ctx: &mut Context, params: P)
where
P: Into<DrawParams>,
{
let params = params.into();
let texture_width = self.width() as f32;
let texture_height = self.height() as f32;
let clip = params
.clip
.unwrap_or_else(|| Rectangle::new(0.0, 0.0, texture_width, texture_height));
let x1 = 0.0;
let y1 = 0.0;
let x2 = clip.width;
let y2 = clip.height;
let u1 = clip.x / texture_width;
let v1 = clip.y / texture_height;
let u2 = (clip.x + clip.width) / texture_width;
let v2 = (clip.y + clip.height) / texture_height;
graphics::set_texture(ctx, self);
graphics::push_quad(ctx, x1, y1, x2, y2, u1, v1, u2, v2, ¶ms);
}
}
#[derive(Debug, Clone, Copy)]
pub enum FilterMode {
Nearest,
Linear,
}