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
use std::fmt::Write as _;
use crate::canvas::{Canvas, ToIndex};
use crate::image::SharedImage;
use crate::resource::ResourceItem;
use crate::settings::{RESOURCE_ARCHIVE_DIRNAME, TILEMAP_SIZE};
use crate::types::Tile;
use crate::utils::{as_u32, parse_hex_string, simplify_string};
impl ToIndex for Tile {
fn to_index(&self) -> usize {
0
}
}
pub struct Tilemap {
pub(crate) canvas: Canvas<Tile>,
pub image: SharedImage,
}
pub type SharedTilemap = shared_type!(Tilemap);
impl Tilemap {
pub fn new(width: u32, height: u32, image: SharedImage) -> SharedTilemap {
new_shared_type!(Self {
canvas: Canvas::new(width, height),
image,
})
}
pub const fn width(&self) -> u32 {
self.canvas.width()
}
pub const fn height(&self) -> u32 {
self.canvas.height()
}
pub fn set(&mut self, x: i32, y: i32, data_str: &[&str]) {
let width = simplify_string(data_str[0]).len() as u32 / 4;
let height = data_str.len() as u32;
let tilemap = Self::new(width, height, self.image.clone());
{
let mut tilemap = tilemap.lock();
for y in 0..height {
let src_data = simplify_string(data_str[y as usize]);
for x in 0..width {
let index = x as usize * 4;
let tile = parse_hex_string(&src_data[index..index + 4]).unwrap();
tilemap.canvas.data[y as usize][x as usize] =
(((tile >> 8) & 0xff) as u8, (tile & 0xff) as u8);
}
}
}
self.blt(
x as f64,
y as f64,
tilemap,
0.0,
0.0,
width as f64,
height as f64,
None,
);
}
pub fn clip(&mut self, x: f64, y: f64, width: f64, height: f64) {
self.canvas.clip(x, y, width, height);
}
pub fn clip0(&mut self) {
self.canvas.clip0();
}
pub fn camera(&mut self, x: f64, y: f64) {
self.canvas.camera(x, y);
}
pub fn camera0(&mut self) {
self.canvas.camera0();
}
pub fn cls(&mut self, tile: Tile) {
self.canvas.cls(tile);
}
pub fn pget(&mut self, x: f64, y: f64) -> Tile {
self.canvas.pget(x, y)
}
pub fn pset(&mut self, x: f64, y: f64, tile: Tile) {
self.canvas.pset(x, y, tile);
}
pub fn line(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, tile: Tile) {
self.canvas.line(x1, y1, x2, y2, tile);
}
pub fn rect(&mut self, x: f64, y: f64, width: f64, height: f64, tile: Tile) {
self.canvas.rect(x, y, width, height, tile);
}
pub fn rectb(&mut self, x: f64, y: f64, width: f64, height: f64, tile: Tile) {
self.canvas.rectb(x, y, width, height, tile);
}
pub fn circ(&mut self, x: f64, y: f64, radius: f64, tile: Tile) {
self.canvas.circ(x, y, radius, tile);
}
pub fn circb(&mut self, x: f64, y: f64, radius: f64, tile: Tile) {
self.canvas.circb(x, y, radius, tile);
}
pub fn elli(&mut self, x: f64, y: f64, width: f64, height: f64, tile: Tile) {
self.canvas.elli(x, y, width, height, tile);
}
pub fn ellib(&mut self, x: f64, y: f64, width: f64, height: f64, tile: Tile) {
self.canvas.ellib(x, y, width, height, tile);
}
pub fn tri(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64, tile: Tile) {
self.canvas.tri(x1, y1, x2, y2, x3, y3, tile);
}
pub fn trib(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64, tile: Tile) {
self.canvas.trib(x1, y1, x2, y2, x3, y3, tile);
}
pub fn fill(&mut self, x: f64, y: f64, tile: Tile) {
self.canvas.fill(x, y, tile);
}
pub fn blt(
&mut self,
x: f64,
y: f64,
tilemap: shared_type!(Self),
tilemap_x: f64,
tilemap_y: f64,
width: f64,
height: f64,
transparent: Option<Tile>,
) {
if let Some(tilemap) = tilemap.try_lock() {
self.canvas.blt(
x,
y,
&tilemap.canvas,
tilemap_x,
tilemap_y,
width,
height,
transparent,
None,
);
} else {
let copy_width = as_u32(width.abs());
let copy_height = as_u32(height.abs());
let mut canvas = Canvas::new(copy_width, copy_height);
canvas.blt(
0.0,
0.0,
&self.canvas,
tilemap_x,
tilemap_y,
copy_width as f64,
copy_height as f64,
None,
None,
);
self.canvas
.blt(x, y, &canvas, 0.0, 0.0, width, height, transparent, None);
}
}
}
impl ResourceItem for Tilemap {
fn resource_name(item_no: u32) -> String {
RESOURCE_ARCHIVE_DIRNAME.to_string() + "tilemap" + &item_no.to_string()
}
fn is_modified(&self) -> bool {
for y in 0..self.height() {
for x in 0..self.width() {
if self.canvas.data[y as usize][x as usize] != (0, 0) {
return true;
}
}
}
false
}
fn clear(&mut self) {
self.cls((0, 0));
}
fn serialize(&self) -> String {
let mut output = String::new();
for y in 0..self.height() {
for x in 0..self.width() {
let tile = self.canvas.data[y as usize][x as usize];
let _ = write!(output, "{:02x}{:02x}", tile.0, tile.1);
}
output += "\n";
}
let _ = write!(
output,
"{}",
crate::image_no(self.image.clone()).unwrap_or(0)
);
output
}
fn deserialize(&mut self, version: u32, input: &str) {
for (y, line) in input.lines().enumerate() {
if y < TILEMAP_SIZE as usize {
if version < 15000 {
string_loop!(x, tile, line, 3, {
let tile = parse_hex_string(&tile).unwrap();
self.canvas.data[y][x] = ((tile % 32) as u8, (tile / 32) as u8);
});
} else {
string_loop!(x, tile, line, 4, {
let tile_x = parse_hex_string(&tile[0..2]).unwrap();
let tile_y = parse_hex_string(&tile[2..4]).unwrap();
self.canvas.data[y][x] = (tile_x as u8, tile_y as u8);
});
}
} else {
self.image = crate::image(line.parse().unwrap());
}
}
}
}