305. Sprite render flip support

Add horizontal and vertical flip support to one-sprite rendering.

Lesson 305 of 356 · tests/chapter_09_sprite_rendering/test_305_sprite_render_flip_support.py

File to update

emulator/rendering/sprite_renderer.py

Why this step exists

Sprite attributes already decode horizontal and vertical flip bits. The one-sprite renderer should now use those bits when choosing which CHR pixel to draw.

Sprite attribute flip bits

bit 6: horizontal flip
bit 7: vertical flip

Important model

Flipping mirrors the image inside the sprite's 8x8 box. It does not move the sprite's screen position.

Suggested implementation example inside render_sprite_8x8_to_framebuffer():

for tile_y in range(8):
    for tile_x in range(8):
        source_x = 7 - tile_x if attributes.flip_horizontal else tile_x
        source_y = 7 - tile_y if attributes.flip_vertical else tile_y

        color_index = color_indexes[source_y][source_x]

        if color_index == 0:
            continue

        screen_x = sprite.x + tile_x
        screen_y = sprite.y + tile_y

        ...

Minimal example

original row:          1 2 3 . . . . .
horizontal flip row:   . . . . . 3 2 1

Common misconception

"Horizontal flip should change sprite.x."

No. The destination box stays at sprite.x/sprite.y. Only the source pixel lookup is mirrored.

Out of scope

  • rendering all 64 sprites
  • sprite priority behind background
  • sprite 0 hit
  • sprite overflow
  • pygame

Run this lesson

uv run pytest tests/chapter_09_sprite_rendering/test_305_sprite_render_flip_support.py -v