From a4fb01b87a11865c5e3c81c9994048764602eb45 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Thu, 12 Dec 2019 13:54:17 +0100 Subject: [PATCH] allow bigger tilemaps and custom charblocks in BGs --- README.md | 2 ++ .../background/background.h | 18 ++++++++++-- engine/src/background/background.cpp | 8 ++--- test/CMakeLists.txt | 2 +- test/backgroundtest.cpp | 29 +++++++++++++++++++ 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 test/backgroundtest.cpp diff --git a/README.md b/README.md index 5b418b6..a33b7ce 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,8 @@ Creating a background: Backgrounds work a bit different in VRAM compared to sprites. There are only 4 backgrounds available, and background #4 is taken by the font. Parameter 1 identifies your background used for prioritizing. Remember to use a screen block different than your map data. +If you want to create bigger maps than 32x32, use `MAPLAYOUT_64x64` or similar in the second constructor. + #### Sprites Conjuring sprites on the screen is a matter of exposing them to the sprites vector in your scene. Create them in your load and set them as a `std::unique_ptr` member variable in your scene so they get cleaned up automatically. diff --git a/engine/include/libgba-sprite-engine/background/background.h b/engine/include/libgba-sprite-engine/background/background.h index efcfcf1..19d5c5b 100644 --- a/engine/include/libgba-sprite-engine/background/background.h +++ b/engine/include/libgba-sprite-engine/background/background.h @@ -8,6 +8,11 @@ #include +#define MAPLAYOUT_32X32 0 +#define MAPLAYOUT_32X64 1 +#define MAPLAYOUT_64X32 2 +#define MAPLAYOUT_64X64 3 + class Background { private: void buildRegister(); @@ -17,7 +22,7 @@ protected: const void *data; const void *map; int size, bgIndex; - int mapSize; + int mapSize, mapLayout; int screenBlockIndex, charBlockIndex; public: @@ -27,8 +32,15 @@ public: void scroll(int x, int y); void scrollSpeed(int dx, int dy); - Background(int bgIndex, const void *data, int size, const void* map, int mapSize) : data(data), bgIndex(bgIndex), size(size), map(map), - screenBlockIndex(0), charBlockIndex(0), mapSize(mapSize) {} + Background(int bgIndex, const void *data, int size, const void* map, int mapSize, int screenBlockIndex, int charBlockIndex, int mapLayout) + : Background(bgIndex, data, size, map, mapSize) { + this->screenBlockIndex = screenBlockIndex; + this->charBlockIndex = charBlockIndex; + this->mapLayout = mapLayout; + } + + Background(int bgIndex, const void *data, int size, const void* map, int mapSize) : data(data), bgIndex(bgIndex), size(size), map(map), mapLayout(MAPLAYOUT_32X32), + screenBlockIndex(0), charBlockIndex(bgIndex), mapSize(mapSize) {} virtual void persist(); void updateMap(const void* map); void clearMap(); diff --git a/engine/src/background/background.cpp b/engine/src/background/background.cpp index 3191005..e56cb54 100644 --- a/engine/src/background/background.cpp +++ b/engine/src/background/background.cpp @@ -32,7 +32,7 @@ void Background::updateMap(const void *map) { } void Background::persist() { - dma3_cpy(char_block(bgIndex), this->data, this->size); + dma3_cpy(char_block(charBlockIndex), this->data, this->size); if(this->map) { dma3_cpy(screen_block(screenBlockIndex), this->map, this->mapSize); @@ -44,7 +44,7 @@ void Background::persist() { void Background::clearData() { this->clearMap(); int empty[this->size]; - dma3_cpy(char_block(bgIndex), empty, this->size); + dma3_cpy(char_block(charBlockIndex), empty, this->size); } void Background::clearMap() { @@ -67,12 +67,12 @@ u32 Background::getBgControlRegisterIndex() { void Background::buildRegister() { *(vu16*)(REG_BASE+getBgControlRegisterIndex()) = bgIndex | /* priority, 0 is highest, 3 is lowest */ - (bgIndex << 2) | /* the char block the image data is stored in */ + (charBlockIndex << 2) | /* the char block the image data is stored in */ (0 << 6) | /* the mosaic flag */ (1 << 7) | /* color mode, 0 is 16 colors, 1 is 256 colors */ (screenBlockIndex << 8) | /* the screen block the tile data is stored in */ (1 << 13) | /* wrapping flag */ - (0 << 14); + (mapLayout << 14); } void Background::scroll(int x, int y) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index da283f8..b914ce7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,6 +37,6 @@ add_executable(unittest ../engine/src/palette/combined_palette.cpp ../engine/src/timer.cpp ../engine/src/gbavector.cpp - timertest.cpp gbavectortest.cpp) + timertest.cpp gbavectortest.cpp backgroundtest.cpp) target_link_libraries(unittest ${GTEST_LIBRARY}/build/libgtest.a ${GTEST_LIBRARY}/build/libgtest_main.a) diff --git a/test/backgroundtest.cpp b/test/backgroundtest.cpp new file mode 100644 index 0000000..cd104e0 --- /dev/null +++ b/test/backgroundtest.cpp @@ -0,0 +1,29 @@ +// +// Created by Wouter Groeneveld on 12/12/19. +// + +#include +#include + +class BGTestSuite : public ::testing::Test { +protected: + virtual void TearDown() { + } + + virtual void SetUp() { + } +}; + +TEST_F(BGTestSuite, Create_Background_Can_Set_Custom_ScreenAndMapAndLayout) { + Background bg(1, NULL, 100, NULL, 200, 15, 16, MAPLAYOUT_64X64); + + ASSERT_EQ(16, bg.getCharBlock()); + ASSERT_EQ(15, bg.getScreenBlock()); +} + +TEST_F(BGTestSuite, Create_Background_CharBlockIsBgIndex_ByDefault) { + Background bg(1, NULL, 100, NULL, 200); + + ASSERT_EQ(1, bg.getCharBlock()); + ASSERT_EQ(0, bg.getScreenBlock()); +} \ No newline at end of file