Skip to content

Commit 8bf9ba8

Browse files
committed
fix: sprite horiz/vert flipping works now
1 parent 1827ae4 commit 8bf9ba8

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

TODO.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
- [ ] Bug: Why are some sprites flipped around?
2-
- afaict it's a bug with drawing or tile lookup, not with flip horiz / flip vertical
31
- [ ] Add scrolling support
42
- [..] Add tests cases for PPU registers
53
- [ ] Add support for 2 gamepads

src/render.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ use crate::{
33
ppu::{Sprite, PATTERN_TABLE_SIZE},
44
};
55

6-
pub struct Frame {
7-
pub data: Vec<u8>,
8-
}
6+
/// Debug flags
7+
const DEBUG_DRAW_SPRITE_BOUNDING_BOX: bool = false;
98

109
/// Size of a single 8x8 Tile (in bytes).
1110
/// 2 bytes for each row of 8 pixels.
@@ -39,6 +38,10 @@ pub fn get_tile(pattern_table: &[u8], tile_n: usize) -> [[u8; 8]; 8] {
3938
out
4039
}
4140

41+
pub struct Frame {
42+
pub data: Vec<u8>,
43+
}
44+
4245
impl Frame {
4346
const WIDTH: usize = 256;
4447
const HEIGHT: usize = 240;
@@ -96,23 +99,43 @@ impl Frame {
9699
// Draw the tile
97100
let x = sprite.x as usize;
98101
let y = sprite.y as usize;
99-
let v_range = if sprite.flip_vertical {
100-
(0..TILE_SIZE_PIXELS).rev().collect::<Vec<usize>>()
101-
} else {
102-
(0..TILE_SIZE_PIXELS).collect::<Vec<usize>>()
103-
};
104-
for row in v_range {
105-
let h_range = if sprite.flip_horizontal {
106-
(0..TILE_SIZE_PIXELS).rev().collect::<Vec<usize>>()
107-
} else {
108-
(0..TILE_SIZE_PIXELS).collect::<Vec<usize>>()
109-
};
110-
for col in h_range {
102+
#[allow(clippy::needless_range_loop)]
103+
for row in 0..TILE_SIZE_PIXELS {
104+
for col in 0..TILE_SIZE_PIXELS {
111105
// 0 means transparent, for sprites
112106
let palette_idx = tile[row][col];
113107
if palette_idx > 0 {
114108
let color = SYSTEM_PALETTE[palette[palette_idx as usize] as usize];
115-
self.set_pixel(x + col, y + row, color);
109+
110+
let y_offset = match sprite.flip_vertical {
111+
true => TILE_SIZE_PIXELS - row - 1,
112+
false => row,
113+
};
114+
let x_offset = match sprite.flip_horizontal {
115+
true => TILE_SIZE_PIXELS - col - 1,
116+
false => col,
117+
};
118+
self.set_pixel(x + x_offset, y + y_offset, color);
119+
}
120+
121+
if DEBUG_DRAW_SPRITE_BOUNDING_BOX {
122+
// Debug sprite by drawing a bounding box
123+
if col == 0
124+
|| row == 0
125+
|| col == TILE_SIZE_PIXELS - 1
126+
|| row == TILE_SIZE_PIXELS - 1
127+
{
128+
// default = red
129+
// flip v = yellow
130+
// flip h = purple
131+
// flip v+h = white
132+
let color = (
133+
255,
134+
if sprite.flip_vertical { 255 } else { 0 },
135+
if sprite.flip_horizontal { 255 } else { 0 },
136+
);
137+
self.set_pixel(x + col, y + row, color);
138+
}
116139
}
117140
}
118141
}

0 commit comments

Comments
 (0)