mindustry logic execution, map- and schematic- parsing and rendering
iterate x, y instead of turning i into x, y
bendn 2023-08-14
parent 20bcdff · commit 6eefeac
-rw-r--r--Cargo.toml2
-rw-r--r--src/data/renderer.rs104
2 files changed, 61 insertions, 45 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 3f30c92..6f73755 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "mindus"
-version = "4.0.2"
+version = "4.0.3"
edition = "2021"
description = "A library for working with mindustry data formats (eg schematics and maps) (fork of plandustry)"
authors = [
diff --git a/src/data/renderer.rs b/src/data/renderer.rs
index bee4ef6..71aff18 100644
--- a/src/data/renderer.rs
+++ b/src/data/renderer.rs
@@ -170,59 +170,75 @@ impl Renderable for Map<'_> {
Image::alloc(scale * self.width as u32, scale * self.height as u32);
let mut top: Image<_, 4> =
Image::alloc(scale * self.width as u32, scale * self.height as u32);
- for (x, y, j, tile) in self.tiles.iter().enumerate().map(|(j, t)| {
- (
- (j % self.width),
- // flip y
- (self.height - (j / self.width)) - 1,
- j,
- t,
- )
- }) {
- // draw the floor first.
- // println!("draw {tile:?} ({x}, {y}) + {scale:?}");
- unsafe {
- floor.as_mut().overlay_at(
- &tile.floor(scale).borrow(),
- scale * x as u32,
- scale * y as u32,
- )
- };
- if tile.has_ore() {
+ for y in 0..self.height {
+ for x in 0..self.width {
+ // Map::new() allocates w*h items
+ let j = x + self.width * y;
+ let tile = unsafe { self.tiles.get_unchecked(j) };
+ let y = self.height - y - 1;
+ // draw the floor first.
+ // println!("draw {tile:?} ({x}, {y})");
unsafe {
floor.as_mut().overlay_at(
- &tile.ore(scale).borrow(),
+ &tile.floor(scale).borrow(),
scale * x as u32,
scale * y as u32,
)
};
- }
+ if tile.has_ore() {
+ unsafe {
+ floor.as_mut().overlay_at(
+ &tile.ore(scale).borrow(),
+ scale * x as u32,
+ scale * y as u32,
+ )
+ };
+ }
- if let Some(build) = tile.build() {
- let s = build.block.get_size();
- let x = x - ((s - 1) / 2) as usize;
- let y = y - (s / 2) as usize;
- let ctx = if build.block.wants_context() {
- let pctx = PositionContext {
- position: GridPos(x, y),
- width: self.width,
- height: self.height,
+ if let Some(build) = tile.build() {
+ let s = build.block.get_size();
+ let x = x
+ - (match s {
+ 1 | 2 => 0,
+ 3 | 4 => 1,
+ 5 | 6 => 2,
+ 7 | 8 => 3,
+ 9 => 4,
+ // SAFETY: no block too big
+ _ => unsafe { std::hint::unreachable_unchecked() },
+ }) as usize;
+ let y = y
+ - (match s {
+ 1 => 0,
+ 2 | 3 => 1,
+ 4 | 5 => 2,
+ 6 | 7 => 3,
+ 8 | 9 => 4,
+ // SAFETY: no block too big
+ _ => unsafe { std::hint::unreachable_unchecked() },
+ }) as usize;
+ let ctx = if build.block.wants_context() {
+ let pctx = PositionContext {
+ position: GridPos(x, y),
+ width: self.width,
+ height: self.height,
+ };
+ let rctx = RenderingContext {
+ cross: self.cross(j, &pctx),
+ position: pctx,
+ };
+ Some(rctx)
+ } else {
+ None
};
- let rctx = RenderingContext {
- cross: self.cross(j, &pctx),
- position: pctx,
+ unsafe {
+ top.as_mut().overlay_at(
+ &tile.build_image(ctx.as_ref(), scale).borrow(),
+ scale * x as u32,
+ scale * y as u32,
+ )
};
- Some(rctx)
- } else {
- None
- };
- unsafe {
- top.as_mut().overlay_at(
- &tile.build_image(ctx.as_ref(), scale).borrow(),
- scale * x as u32,
- scale * y as u32,
- )
- };
+ }
}
}
unsafe { floor.as_mut().overlay_at(&top.as_ref(), 0, 0) };