nesemu/ppu/pattern_table.c

57 lines
2.1 KiB
C

//
// Created by william on 17/05/24.
//
#include "pattern_table.h"
#include "ppu.h"
#include "../gui/canvas.h"
#include "../gui/gui.h"
void pt_debug() {
Canvas *canvas = gui_get_canvas(WINDOW_ID_PATTERN);
for (int palette = 0; palette < 2; palette++) {
address palette_addr = palette * 0x1000;
int palette_canvas_offset = palette * (PATTERN_TABLE_WIDTH * PATTERN_TILE_SIZE);
for (int y = 0; y < PATTERN_TABLE_WIDTH; y++) {
for (int x = 0; x < PATTERN_TABLE_WIDTH; x++) {
address tile_addr = palette_addr + (x + y * PATTERN_TABLE_WIDTH) * PATTERN_TILE_MEM_SIZE;
for (int tile_y = 0; tile_y < PATTERN_TILE_SIZE; tile_y++) {
byte p1_data = ppu_read(tile_addr + tile_y);
byte p2_data = ppu_read(tile_addr + tile_y + PATTERN_TILE_SIZE);
for (int tile_x = 0; tile_x < PATTERN_TILE_SIZE; tile_x++) {
byte bitmask = 1 << (PATTERN_TILE_SIZE - tile_x - 1);
byte p1_byte = p1_data & bitmask;
byte p2_byte = p2_data & bitmask;
Pixel pixel;
if (p1_byte && p2_byte) {
pixel.r = 255;
pixel.g = 255;
pixel.b = 255;
} else if (p2_byte) {
pixel.r = 255;
pixel.g = 0;
pixel.b = 0;
} else if (p1_byte) {
pixel.r = 0;
pixel.g = 255;
pixel.b = 255;
} else {
pixel.r = 0;
pixel.g = 0;
pixel.b = 0;
}
int canvas_x = x * PATTERN_TILE_SIZE + tile_x;
int canvas_y = y * PATTERN_TILE_SIZE + tile_y + palette_canvas_offset;
canvas_draw_pos(canvas, pixel, canvas_x, canvas_y);
}
}
}
}
}
}