From 19c09901fbe23c3753c79dace84ba78534690aa3 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Tue, 19 Sep 2023 23:21:15 +0200 Subject: [PATCH] lcd - A few steps towards handling tilemaps Bit by bit. --- lcd/lcd.cpp | 15 +++++++++------ lcd/lcd.h | 1 + lcd/lcdtest.cpp | 5 ++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lcd/lcd.cpp b/lcd/lcd.cpp index c8a144d..09991b4 100644 --- a/lcd/lcd.cpp +++ b/lcd/lcd.cpp @@ -18,11 +18,13 @@ LCD::LCD(Cpu& cpu) { tiles[i].create(8,8,sf::Color::Red); } + + for(int l = 0; l < 2; l++) + std::memset(&tilemap[l], 0, 32*32); } void LCD::generate_tile(int idx) { - std::cerr << "Updating tile " << idx << std::endl; for(int y = 0, addr = idx << 4; y < 8; y++, addr+=2) { u8 a = vram_raw[addr]; @@ -30,7 +32,6 @@ void LCD::generate_tile(int idx) for(int x = 7; x >= 0; x--, a>>=1, b>>=1) { tiles[idx].setPixel(x,y,bgp.getColorByIdx( ((a&0x1) << 1) | (b&0x1))); - //tiles[idx].setPixel(x,y,sf::Color::Red); } } } @@ -146,14 +147,16 @@ u8 LCD::read8(u16 addr) { void LCD::render(sf::RenderTarget& target) { cpu.signalInterrupt(INT_VBlank); - for(int row = 0; row < 16; row++) - for(int col = 0; col < 12; col++) + for(int row = 0; row < 32; row++) + for(int col = 0; col < 32; col++) { - unsigned int idx = row*12+col; + unsigned int map_idx = row*32+col; sf::Texture txt; sf::Sprite spr; - txt.loadFromImage(tiles[idx]); + unsigned int tile_idx = tilemap[0][map_idx]; + + txt.loadFromImage(tiles[tile_idx]); spr.setTexture(txt,true); spr.setPosition(col*8,row*8); target.draw(spr); diff --git a/lcd/lcd.h b/lcd/lcd.h index 539f76c..c417a3a 100644 --- a/lcd/lcd.h +++ b/lcd/lcd.h @@ -28,6 +28,7 @@ private: // Graphics sf::RenderTexture screenbuffer; sf::Image tiles[384]; + unsigned int tilemap[2][32*32]; // Emulated device u8 regLY; diff --git a/lcd/lcdtest.cpp b/lcd/lcdtest.cpp index 4e0da18..4e7507f 100644 --- a/lcd/lcdtest.cpp +++ b/lcd/lcdtest.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -26,7 +27,7 @@ int main(int argc, char** argv) return 2; } - sf::RenderWindow window(sf::VideoMode(12*8,16*8), "VGBC tiles view"); + sf::RenderWindow window(sf::VideoMode(256,256), "VGBC BG view"); RAM wram1(0x1000); RAM wram2(0x1000); @@ -39,6 +40,7 @@ int main(int argc, char** argv) Cpu cpu(&b); LCD lcd(cpu); + TimerDiv timer; b.map_device(0x0000, 0x7FFF, &mbc1); b.map_device(0x8000, 0x9FFF, &lcd, 0x8000); @@ -47,6 +49,7 @@ int main(int argc, char** argv) b.map_device(0xD000, 0xDFFF, &wram2); b.map_device(0xE000, 0xFDFF, &wram1); b.map_device(0xFE00, 0xFE9F, &oam); + b.map_device(0xFF04, 0xFF07, &timer); b.map_device(0xFF40, 0xFF4F, &lcd, 0xFF40); b.map_device(0xFF80, 0xFFFE, &hram);