chrisledet / gbc

Game Boy Color emulator (WIP)
0 stars 0 forks source link

Feature: LCD Controller #4

Open chrisledet opened 1 year ago

chrisledet commented 1 year ago

1. Setting up SDL2:

Ensure you have SDL2 set up and integrated with your development environment. You'll also want the SDL2_image extension if you decide to use image assets in the future.

2. Creating the Display Window:

Use SDL2 to create a window that represents the Game Boy's screen. The Game Boy has a resolution of 160x144 pixels, but you'll probably want to scale this up for modern displays.

3. Memory Map Integration:

Ensure your emulator correctly handles reads/writes to the following important memory locations:

4. Scanline Rendering:

Implement scanline rendering. For each scanline:

  1. Check if the LCD is enabled.
  2. Render the background.
  3. Render the sprites (taking priority and flags into account).
  4. Render the window if it's enabled.

5. Palettes:

Handle color palettes correctly, including translating Game Boy color values to values that SDL2 can use. The Game Boy is monochrome, but games can assign shades based on various palettes.

6. Buffering and Refresh:

Keep an offscreen buffer where you render the Game Boy's graphics. Once a frame is done (all scanlines rendered), you can present this buffer to your SDL2 window. This ensures smooth graphics and minimizes tearing.

7. Interrupts and STAT Register:

Update the STAT register and fire LCD STAT interrupts when appropriate. The Game Boy fires interrupts based on specific LCD conditions, like when a scanline matches the LYC.

8. Sprite Attributes:

Parse OAM (Object Attribute Memory) to get sprite attributes like position, tile number, priority, and flip flags. Handle 8x8 and 8x16 sprites.

9. Input Handling:

While not directly tied to the LCD, using SDL2 to handle input and translating SDL2 key events to Game Boy button presses would be a logical step during this phase.

10. Debugging Tools (Optional):

Building debugging tools, like a tile viewer or a palette viewer, can be immensely helpful in diagnosing rendering issues. SDL2 can be used to render these tools as separate windows or overlays.

11. Optimizations:

Once your rendering works, consider optimization strategies. SDL2 offers hardware-accelerated rendering, so you might consider using that. On the emulator side, there might be ways to avoid unnecessary recalculations or redundant renderings.

12. Testing:

Use test ROMs and known games to verify the accuracy and performance of your rendering. "Blargg's" test ROMs are especially helpful in this regard.

Remember, the Game Boy's LCD hardware and the associated PPU (Picture Processing Unit) have a lot of nuances. Many games exploit specific behaviors of the hardware, so always be prepared to revisit and refine your implementation.

It might seem overwhelming at first, but breaking the task down into these steps and tackling each one at a time can make the process more manageable. Good luck with your emulator development!

chrisledet commented 1 year ago

Tools to help visual a tile: https://www.huderlem.com/demos/gameboy2bpp.html