Skip to content

Commit ad43b9c

Browse files
committed
Move map draw function into map module
1 parent 5181084 commit ad43b9c

File tree

3 files changed

+143
-201
lines changed

3 files changed

+143
-201
lines changed

src/isomap.rs

Lines changed: 30 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
1-
use std::error::Error;
21
use std::ops::Range;
3-
use std::path::Path;
42

53
use super::blocktypes::BlockType;
64
use super::color;
75
use super::color::RGBA;
8-
use super::image;
96
use super::region;
107
use super::sizes::*;
118
use super::types::*;
129
use super::world::World;
1310

14-
fn draw_chunk(pixels: &mut [u8], blocktypes: &[BlockType], chunk: &region::Chunk, co: &isize,
11+
pub fn get_size(world: &World) -> Pair<usize> {
12+
Pair {
13+
x: (world.bsize.x + world.bsize.z) * ISO_BLOCK_X_MARGIN,
14+
z: (world.bsize.x + world.bsize.z) * ISO_BLOCK_Y_MARGIN + ISO_CHUNK_SIDE_HEIGHT,
15+
}
16+
}
17+
18+
pub fn get_crop(world: &World, size: &Pair<usize>) -> usize {
19+
let cbcrop = Edges {
20+
n: block_pos_in_chunk(world.bedges.n, None),
21+
e: MAX_BLOCK_IN_CHUNK - block_pos_in_chunk(world.bedges.e, None),
22+
s: MAX_BLOCK_IN_CHUNK - block_pos_in_chunk(world.bedges.s, None),
23+
w: block_pos_in_chunk(world.bedges.w, None),
24+
};
25+
(cbcrop.w + cbcrop.n) * ISO_BLOCK_Y_MARGIN * size.x +
26+
(cbcrop.w + cbcrop.s) * ISO_BLOCK_X_MARGIN
27+
}
28+
29+
pub fn get_chunk_pixel(world: &World, arc: &Pair<isize>, c: &Pair<usize>) -> Pair<usize> {
30+
let ac = Pair {
31+
x: (arc.x + c.x as isize) as usize,
32+
z: (arc.z + c.z as isize) as usize,
33+
};
34+
Pair {
35+
x: (ac.x + world.csize.z - ac.z - 1) * ISO_CHUNK_X_MARGIN,
36+
z: (ac.x + ac.z) * ISO_CHUNK_Y_MARGIN,
37+
}
38+
}
39+
40+
pub fn draw_chunk(pixels: &mut [u8], blocktypes: &[BlockType], chunk: &region::Chunk, co: &isize,
1541
width: &usize, cblimits: &Edges<usize>, ylimits: &Range<usize>) {
1642
let blank_color = RGBA::default();
1743

@@ -100,95 +126,3 @@ fn draw_chunk(pixels: &mut [u8], blocktypes: &[BlockType], chunk: &region::Chunk
100126
}
101127
}
102128
}
103-
104-
fn get_size(world: &World) -> Pair<usize> {
105-
Pair {
106-
x: (world.bsize.x + world.bsize.z) * ISO_BLOCK_X_MARGIN,
107-
z: (world.bsize.x + world.bsize.z) * ISO_BLOCK_Y_MARGIN + ISO_CHUNK_SIDE_HEIGHT,
108-
}
109-
}
110-
111-
fn get_crop(world: &World, size: &Pair<usize>) -> usize {
112-
let cbcrop = Edges {
113-
n: block_pos_in_chunk(world.bedges.n, None),
114-
e: MAX_BLOCK_IN_CHUNK - block_pos_in_chunk(world.bedges.e, None),
115-
s: MAX_BLOCK_IN_CHUNK - block_pos_in_chunk(world.bedges.s, None),
116-
w: block_pos_in_chunk(world.bedges.w, None),
117-
};
118-
(cbcrop.w + cbcrop.n) * ISO_BLOCK_Y_MARGIN * size.x +
119-
(cbcrop.w + cbcrop.s) * ISO_BLOCK_X_MARGIN
120-
}
121-
122-
fn get_chunk_pixel(world: &World, arc: &Pair<isize>, c: &Pair<usize>) -> Pair<usize> {
123-
let ac = Pair {
124-
x: (arc.x + c.x as isize) as usize,
125-
z: (arc.z + c.z as isize) as usize,
126-
};
127-
Pair {
128-
x: (ac.x + world.csize.z - ac.z - 1) * ISO_CHUNK_X_MARGIN,
129-
z: (ac.x + ac.z) * ISO_CHUNK_Y_MARGIN,
130-
}
131-
}
132-
133-
pub fn draw_iso_map(world: &World, outpath: &Path, blocktypes: &[BlockType])
134-
-> Result<(), Box<Error>> {
135-
let size = get_size(world);
136-
let crop = get_crop(world, &size);
137-
let mut pixels = vec![0u8; size.x * size.z * 4];
138-
139-
let mut i = 0;
140-
let len = world.regions.len();
141-
142-
for rz in (world.redges.n..world.redges.s + 1).rev() {
143-
for rx in (world.redges.w..world.redges.e + 1).rev() {
144-
let r = &Pair { x: rx, z: rz };
145-
if !world.regions.contains_key(&r) {
146-
continue;
147-
}
148-
149-
i += 1;
150-
println!("Reading block data for region {}, {} ({}/{})", r.x, r.z, i, len);
151-
if let Some(reg) = region::read_region_data(&world, r, blocktypes)? {
152-
let chunk_count = reg.chunks.len();
153-
println!("Drawing block map for region {}, {} ({} chunk{})", r.x, r.z,
154-
chunk_count, if chunk_count == 1 { "" } else { "s" });
155-
156-
let arc = &Pair {
157-
x: r.x * CHUNKS_IN_REGION as isize - world.cedges.w,
158-
z: r.z * CHUNKS_IN_REGION as isize - world.cedges.n,
159-
};
160-
161-
for cz in (0..CHUNKS_IN_REGION).rev() {
162-
for cx in (0..CHUNKS_IN_REGION).rev() {
163-
let c = &Pair { x: cx, z: cz };
164-
if let Some(chunk) = reg.get_chunk(c) {
165-
// println!("Drawing chunk {}, {}", c.x, c.z);
166-
let wc = Pair {
167-
x: r.x * CHUNKS_IN_REGION as isize + c.x as isize,
168-
z: r.z * CHUNKS_IN_REGION as isize + c.z as isize,
169-
};
170-
let cblimits = Edges {
171-
n: block_pos_in_chunk(world.bedges.n, Some(wc.z)),
172-
e: block_pos_in_chunk(world.bedges.e, Some(wc.x)),
173-
s: block_pos_in_chunk(world.bedges.s, Some(wc.z)),
174-
w: block_pos_in_chunk(world.bedges.w, Some(wc.x)),
175-
};
176-
177-
let cp = get_chunk_pixel(world, arc, c);
178-
let co = (cp.z * size.x + cp.x) as isize - crop as isize;
179-
180-
draw_chunk(&mut pixels, blocktypes, &chunk, &co, &size.x, &cblimits,
181-
world.ylimits);
182-
}
183-
}
184-
}
185-
} else {
186-
println!("No data in region.");
187-
}
188-
}
189-
}
190-
191-
image::draw_block_map(&pixels, size, outpath, true)?;
192-
193-
Ok(())
194-
}

src/map.rs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ use std::error::Error;
22
use std::time::Instant;
33

44
use super::blocktypes;
5+
use super::image;
56
use super::isomap;
67
use super::options::{Options, View};
78
use super::orthomap;
9+
use super::region;
10+
use super::sizes::*;
11+
use super::types::*;
812
use super::world;
913

1014
pub fn create_map(options: &Options) -> Result<(), Box<Error>> {
@@ -27,12 +31,7 @@ pub fn create_map(options: &Options) -> Result<(), Box<Error>> {
2731
let blocktypes = blocktypes::get_block_types(&options.lighting);
2832

2933
println!("Starting block map");
30-
let result = match options.view {
31-
View::Isometric =>
32-
isomap::draw_iso_map(&world, options.outpath, &blocktypes),
33-
View::Orthographic =>
34-
orthomap::draw_ortho_map(&world, options.outpath, &blocktypes),
35-
};
34+
let result = draw_map(&world, &blocktypes, options);
3635

3736
let elapsed = start.elapsed();
3837
let mins = elapsed.as_secs() / 60;
@@ -42,3 +41,78 @@ pub fn create_map(options: &Options) -> Result<(), Box<Error>> {
4241

4342
result
4443
}
44+
45+
pub fn draw_map(world: &world::World, blocktypes: &[blocktypes::BlockType], options: &Options)
46+
-> Result<(), Box<Error>> {
47+
let size = match options.view {
48+
View::Isometric => isomap::get_size(world),
49+
View::Orthographic => orthomap::get_size(world),
50+
};
51+
let crop = match options.view {
52+
View::Isometric => isomap::get_crop(world, &size),
53+
View::Orthographic => orthomap::get_crop(world, &size),
54+
};
55+
let mut pixels = vec![0u8; size.x * size.z * 4];
56+
57+
let mut i = 0;
58+
let len = world.regions.len();
59+
60+
for rz in (world.redges.n..world.redges.s + 1).rev() {
61+
for rx in (world.redges.w..world.redges.e + 1).rev() {
62+
let r = &Pair { x: rx, z: rz };
63+
if !world.regions.contains_key(&r) {
64+
continue;
65+
}
66+
67+
i += 1;
68+
println!("Reading block data for region {}, {} ({}/{})", r.x, r.z, i, len);
69+
if let Some(reg) = region::read_region_data(&world, r, blocktypes)? {
70+
let chunk_count = reg.chunks.len();
71+
println!("Drawing block map for region {}, {} ({} chunk{})", r.x, r.z,
72+
chunk_count, if chunk_count == 1 { "" } else { "s" });
73+
74+
let arc = &Pair {
75+
x: r.x * CHUNKS_IN_REGION as isize - world.cedges.w,
76+
z: r.z * CHUNKS_IN_REGION as isize - world.cedges.n,
77+
};
78+
79+
for cz in (0..CHUNKS_IN_REGION).rev() {
80+
for cx in (0..CHUNKS_IN_REGION).rev() {
81+
let c = &Pair { x: cx, z: cz };
82+
if let Some(chunk) = reg.get_chunk(c) {
83+
// println!("Drawing chunk {}, {}", c.x, c.z);
84+
let wc = Pair {
85+
x: r.x * CHUNKS_IN_REGION as isize + c.x as isize,
86+
z: r.z * CHUNKS_IN_REGION as isize + c.z as isize,
87+
};
88+
let cblimits = Edges {
89+
n: block_pos_in_chunk(world.bedges.n, Some(wc.z)),
90+
e: block_pos_in_chunk(world.bedges.e, Some(wc.x)),
91+
s: block_pos_in_chunk(world.bedges.s, Some(wc.z)),
92+
w: block_pos_in_chunk(world.bedges.w, Some(wc.x)),
93+
};
94+
95+
let cp = match options.view {
96+
View::Isometric => isomap::get_chunk_pixel(world, arc, c),
97+
View::Orthographic => orthomap::get_chunk_pixel(arc, c),
98+
};
99+
let co = (cp.z * size.x + cp.x) as isize - crop as isize;
100+
101+
(match options.view {
102+
View::Isometric => isomap::draw_chunk,
103+
View::Orthographic => orthomap::draw_chunk,
104+
})(&mut pixels, blocktypes, &chunk, &co, &size.x, &cblimits,
105+
world.ylimits);
106+
}
107+
}
108+
}
109+
} else {
110+
println!("No data in region.");
111+
}
112+
}
113+
}
114+
115+
image::draw_block_map(&pixels, size, options.outpath, true)?;
116+
117+
Ok(())
118+
}

0 commit comments

Comments
 (0)