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
|
use std::collections::HashMap;
use log::{info, warn};
use skia_safe::{
canvas::SrcRectConstraint, Canvas, Color4f, ColorSpace, ISize, Image, Paint, Rect,
};
use twclient::world::{
map::{format, TILE_NUM},
World,
};
pub struct MapRenderer {
tileset: HashMap<Option<usize>, Image>,
}
const TILE_SIZE: f32 = 32.0;
impl MapRenderer {
pub fn new() -> Self {
Self {
tileset: HashMap::new(),
}
}
pub fn tick(&mut self) {}
pub fn map_changed(&mut self, world: &World) {
self.tileset.clear();
for (key, t) in &world.map.tilesets {
let mut bytes: Vec<u8> = Vec::with_capacity(t.dim().0 * t.dim().1 * 4);
info!("loading tileset: {:?} => {:?}", key, t.dim());
for ((_x, _y), c) in t.indexed_iter() {
bytes.push(c.red);
bytes.push(c.green);
bytes.push(c.blue);
bytes.push(c.alpha);
}
let d = skia_safe::Data::new_copy(&bytes);
let v = skia_safe::Image::from_raster_data(
&skia_safe::ImageInfo::new(
ISize::new(t.dim().0 as i32, t.dim().1 as i32),
skia_safe::ColorType::RGBA8888,
skia_safe::AlphaType::Opaque,
None,
),
d,
t.dim().0 * 4,
)
.unwrap();
self.tileset.insert(*key, v);
}
}
pub fn draw(&mut self, world: &World, canvas: &mut Canvas) {
let mut grid_paint = Paint::new(
Color4f {
a: 1.0,
r: 1.0,
g: 1.0,
b: 1.0,
},
&ColorSpace::new_srgb(),
);
grid_paint.set_style(skia_safe::PaintStyle::Stroke);
grid_paint.set_anti_alias(true);
let center = world
.local_tee()
.map(|t| (t.x / 32, t.y / 32))
.unwrap_or((0, 0));
let tile_rect = Rect {
top: center.1 as f32 * TILE_SIZE,
left: center.0 as f32 * TILE_SIZE,
bottom: center.1 as f32 * TILE_SIZE + TILE_SIZE,
right: center.0 as f32 * TILE_SIZE + TILE_SIZE,
};
canvas.draw_rect(tile_rect, &grid_paint);
for l in &world.map.layers {
let tileset = match world.map.tilesets.get(&l.image) {
Some(t) => t,
None => {
warn!("missing tileset for {:?}, skipping layer", l.image);
continue;
}
};
if tileset.dim() == (1, 1) {
continue;
}
// let magic = (186, 50);
// println!("{:?} {:?}", center, l.tiles.dim());
for layer_y in (center.1 - 15)..(center.1 + 15) {
for layer_x in (center.0 - 15)..(center.0 + 15) {
let layer_x = layer_x.try_into().unwrap_or(0);
let layer_y = layer_y.try_into().unwrap_or(0);
let tile_rect = Rect {
top: layer_y as f32 * TILE_SIZE,
left: layer_x as f32 * TILE_SIZE,
bottom: layer_y as f32 * TILE_SIZE + TILE_SIZE,
right: layer_x as f32 * TILE_SIZE + TILE_SIZE,
};
canvas.draw_rect(tile_rect, &grid_paint);
let tile = match l.tiles.get((layer_y, layer_x)) {
Some(t) => t,
None => continue,
};
let _rotate = tile.flags & format::TILEFLAG_ROTATE != 0;
let _vflip = tile.flags & format::TILEFLAG_VFLIP != 0;
let _hflip = tile.flags & format::TILEFLAG_HFLIP != 0;
let tile_x = tile.index as u32 % TILE_NUM;
let tile_y = tile.index as u32 / TILE_NUM;
if tile_x == 0 && tile_y == 0 {
continue;
}
const TL: u32 = 64;
canvas.draw_image_rect(
self.tileset.get(&l.image).unwrap(),
Some((
&Rect {
top: (tile_y * TL) as f32,
left: (tile_x * TL) as f32,
bottom: ((tile_y + 1) * TL) as f32,
right: ((tile_x + 1) * TL) as f32,
},
SrcRectConstraint::Strict,
)),
tile_rect,
&Paint::default(),
);
}
}
}
}
}
|