adding unit Tests and documentation for flipping function

This commit is contained in:
Clément Le Bihan
2021-07-25 16:41:22 +02:00
parent 3ed7e2677d
commit 8270973d98
7 changed files with 849 additions and 720 deletions
+14 -8
View File
@@ -2,10 +2,10 @@
// Created by cbihan on 5/14/20.
//
#include "PPUUtils.hpp"
#include "PPU/PPUUtils.hpp"
#include "PPU.hpp"
#include "Background.hpp"
#include "Tile.hpp"
#include "PPU/Background.hpp"
#include "PPU/Tile.hpp"
#include "Models/Vector2.hpp"
namespace ComSquare::PPU
@@ -87,11 +87,12 @@ namespace ComSquare::PPU
// todo check why i need to invert vertical and horizontal flips
if (tileData.verticalFlip)
Utils::HFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y});
Utils::hFlip2DBuffer(this->_tileBuffer, this->_characterNbPixels);
if (tileData.horizontalFlip)
Utils::VFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y});
Utils::vFlip2DBuffer(this->_tileBuffer, this->_characterNbPixels);
Vector2<int> pixelPosition{indexOffset.x * this->_characterNbPixels.x, indexOffset.y * this->_characterNbPixels.y};
Vector2<int> pixelPosition{indexOffset.x * this->_characterNbPixels.x,
indexOffset.y * this->_characterNbPixels.y};
std::for_each(this->_tileBuffer.begin(), this->_tileBuffer.begin() + this->_characterNbPixels.y,
[this, &pixelPosition](const auto &row) {
std::move(row.begin(), row.begin() + this->_characterNbPixels.x,
@@ -114,8 +115,7 @@ namespace ComSquare::PPU
if (pos.x % 31 == 0 && pos.x) {
pos.y++;
pos.x = 0;
}
else {
} else {
pos.x++;
}
}
@@ -133,6 +133,12 @@ namespace ComSquare::PPU
void Background::setCharacterSize(Vector2<int> size)
{
if ((size.x != Tile::NbPixelsWidth && size.x != Tile::NbPixelsWidth * 2)
|| (size.y != Tile::NbPixelsHeight && size.y != Tile::NbPixelsHeight * 2)) {
throw std::runtime_error(
"Tile wrong character size (x: " + std::to_string(size.x) + ", y: " + std::to_string(size.y) + ")"
);
}
this->_characterNbPixels = size;
}
+1
View File
@@ -36,6 +36,7 @@ namespace ComSquare::PPU
//! @note members are set to true if the tilemap is expended in their direction
Vector2<bool> _tileMapMirroring;
//! @brief The number of pixels of a character (x: width, y: height)
//! @note A character can be 8x8, 16x16, 8x16 or 16x8
Vector2<int> _characterNbPixels;
//! @brief The number of bits per pixels to currently look for each pixel
int _bpp;
+21 -19
View File
@@ -7,6 +7,7 @@
#include <stdint-gcc.h>
#include <cstddef>
#include <memory>
#include <vector>
#include <array>
#include "Models/Vector2.hpp"
@@ -74,40 +75,41 @@ namespace ComSquare::PPU::Utils
}
}
//! @brief Flips Vertically an 2D array
//! @param array The array to be flipped
//! @brief Flips Vertically an 2D buffer
//! @param buffer The buffer to be flipped
//! @param size The maximum size you want to be split
//! @param offset The starting position of the flip
//! @tparam HORIZONTAL_SIZE The horizontal size of the array
//! @tparam VERTICAL_SIZE The vertical size of the array
//! @tparam HORIZONTAL_SIZE The horizontal size of the buffer
//! @tparam VERTICAL_SIZE The vertical size of the buffer
template <std::size_t VERTICAL_SIZE, std::size_t HORIZONTAL_SIZE>
void VFlipArray(std::array<std::array<uint32_t, HORIZONTAL_SIZE>, VERTICAL_SIZE> &array,
const Vector2<int> &size = {HORIZONTAL_SIZE, VERTICAL_SIZE},
const Vector2<int> &offset = {0, 0})
void vFlip2DBuffer(std::array<std::array<uint32_t, HORIZONTAL_SIZE>, VERTICAL_SIZE> &buffer,
const Vector2<int> &size = {HORIZONTAL_SIZE, VERTICAL_SIZE},
const Vector2<int> &offset = {0, 0})
{
for (int i = offset.y; i < offset.y + size.y; i++) {
std::reverse(array[i].begin() + offset.x, array[i].begin() + offset.x + size.x);
std::reverse(buffer[i].begin() + offset.x, buffer[i].begin() + offset.x + size.x);
}
}
//! @brief Flips Horizontally an 2D array
//! @param array The array to be flipped
//! @brief Flips Horizontally an 2D buffer
//! @param buffer The buffer to be flipped
//! @param size The maximum size you want to be split
//! @param offset The starting position of the flip
//! @tparam HORIZONTAL_SIZE The horizontal size of the array
//! @tparam VERTICAL_SIZE The vertical size of the array
template <std::size_t VERTICAL_SIZE, std::size_t HORIZONTAL_SIZE>
void HFlipArray(std::array<std::array<uint32_t, HORIZONTAL_SIZE>, VERTICAL_SIZE> &array,
const Vector2<int> &size = {HORIZONTAL_SIZE, VERTICAL_SIZE},
const Vector2<int> &offset = {0, 0})
//! @tparam HORIZONTAL_SIZE The horizontal size of the buffer
//! @tparam VERTICAL_SIZE The vertical size of the buffer
//! @warning This function might not behave like you think, it doesn't even look for the size.y but it's only meant to be use in the tile context
template<std::size_t VERTICAL_SIZE, std::size_t HORIZONTAL_SIZE>
void hFlip2DBuffer(std::array<std::array<uint32_t, HORIZONTAL_SIZE>, VERTICAL_SIZE> &buffer,
const Vector2<int> &size = {HORIZONTAL_SIZE, VERTICAL_SIZE},
const Vector2<int> &offset = {0, 0})
{
std::reverse(array.begin() + offset.x, array.begin() + offset.x + size.x);
std::reverse(buffer.begin() + offset.x, buffer.begin() + offset.x + size.x);
}
//! @brief Add a bg buffer to another buffer
template <std::size_t DEST_SIZE_X, std::size_t DEST_SIZE_Y, std::size_t SRC_SIZE_X, std::size_t SRC_SIZE_Y>
template<std::size_t DEST_SIZE_X, std::size_t DEST_SIZE_Y, std::size_t SRC_SIZE_X, std::size_t SRC_SIZE_Y>
static void addBuffer(std::array<std::array<uint32_t, DEST_SIZE_Y>, DEST_SIZE_X> &bufferDest,
const std::array<std::array<uint32_t, SRC_SIZE_Y>, SRC_SIZE_X> &bufferSrc)
const std::array<std::array<uint32_t, SRC_SIZE_Y>, SRC_SIZE_X> &bufferSrc)
{
int i = 0;
int j = 0;