diff --git a/CMakeLists.txt b/CMakeLists.txt index 36966d6..9726007 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,7 +212,7 @@ add_executable(ComSquare sources/Models/Components.hpp sources/Debugger/CGramDebug.cpp sources/Debugger/CGramDebug.hpp -) + sources/Models/Vector2.hpp) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp new file mode 100644 index 0000000..cfe8d01 --- /dev/null +++ b/sources/Models/Vector2.hpp @@ -0,0 +1,112 @@ +// +// Created by cbihan on 5/13/20. +// + +#ifndef COMSQUARE_VECTOR2_HPP +#define COMSQUARE_VECTOR2_HPP + +#include +#include + +namespace ComSquare +{ + template + class Vector2 + { + public: + T x; + T y; + + Vector2() + : x(0), y(0) {} + + Vector2(T x, T y) + : x(x), y(y) {} + + Vector2(sf::Vector2 v) + : x(v.x), y(v.y) {} + + template + Vector2 &operator+=(const Vector2 &vec) + { + this->x += vec.x; + this->y += vec.y; + return *this; + } + + template + Vector2 operator+(const Vector2 &vec) const + { + return Vector2(this->x + vec.x, this->y + vec.y); + } + + template + Vector2 &operator-=(const Vector2 &vec) + { + this->x -= vec.x; + this->y -= vec.y; + return *this; + } + + template + Vector2 &operator*=(T2 d) + { + this->x *= d; + this->y *= d; + return *this; + } + + template + Vector2 operator*(T2 d) const + { + return Vector2(this->x * d, this->y * d); + } + + template + T operator*(Vector2 &b) const + { + return this->x * b.x + this->y * b.y; + } + + template + Vector2 operator/=(Vector2 &b) + { + this->x /= b.x; + this->y /= b.y; + return this; + } + + template + Vector2 operator/(Vector2 &b) const + { + return Vector2(this->x / b.x, this->y / b.y); + } + + template + Vector2 operator/=(T2 b) + { + this->x /= b; + this->y /= b; + return this; + } + + template + Vector2 operator/(T2 b) const + { + return Vector2(this->x / b, this->y / b); + } + }; + + typedef Vector2 Vector2f; + typedef Vector2 Vector2u; + typedef Vector2 Vector2i; +} + +template +std::ostream &operator<<(std::ostream &s, const ComSquare::Vector2 &v) +{ + s << v.x << " " << v.y; + return s; +} + +#endif //COMSQUARE_VECTOR2_HPP diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 1106959..ff33d45 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -5,9 +5,11 @@ #include #include #include "PPU.hpp" +#include "PPUUtils.hpp" #include "../Exceptions/NotImplementedException.hpp" #include "../Exceptions/InvalidAddress.hpp" #include "../Ram/Ram.hpp" +#include "../Models/Vector2.hpp" namespace ComSquare::PPU { @@ -421,15 +423,36 @@ namespace ComSquare::PPU { int nbCharactersHeight = (this->_registers._bgsc[bgNumber].tilemapVerticalMirroring) ? 64 : 32; int nbCharactersWidth = (this->_registers._bgsc[bgNumber].tilemapHorizontalMirroring) ? 64 : 32; - uint16_t vramAddress = this->_registers._bgsc[bgNumber].tilemapAddress >> 8U; + uint16_t vramAddress = this->_registers._bgsc[bgNumber].tilemapAddress << 1U; uint16_t tilemapValue; for (int i = 0; i < nbCharactersHeight * nbCharactersWidth; i++) { for (int j = 0; j < 0x800; j++) { - tilemapValue = this->vram->read(vramAddress); - vramAddress++; + tilemapValue = this->vram->read_internal(vramAddress); + tilemapValue += this->vram->read_internal(vramAddress + 1) << 8U; + vramAddress += 2; } } } + + uint16_t PPU::getGraphicVramAddress(int x, int y, int bg, int bpp) + { + uint16_t baseAddress = this->_registers._bgnba[bg > 2].raw; + int step = bpp * 8; + + baseAddress = (bg % 2) ? baseAddress & 0xFU : (baseAddress & 0xFU) >> 4U; + baseAddress = baseAddress << 12U; + return baseAddress + (x * 16 * step) + (y * step); + } + + void PPU::drawBgTile(uint16_t data, Vector2 pos, int bg, int bpp) + { + uint16_t graphicAddress; + union TileMapData tileData; + + tileData.raw = data; + graphicAddress = this->getGraphicVramAddress(tileData.posX, tileData.posY, bg, bpp); + // loop on all pixels of the tile 8x8 6x16 16x8 8x16 + } } \ No newline at end of file diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 1e52f33..9cb04e3 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -589,6 +589,10 @@ namespace ComSquare::PPU uint16_t cgramRead(uint16_t addr); //! @brief Render a background on the screen void renderBackground(int bgNumber, std::vector characterSize, int bpp, bool priority); + //! @brief Get the correct Vram address for a gien x and y + uint16_t getGraphicVramAddress(int x, int y, int bg, int bpp); + //! @brief Draw a tile on the screen at x y pos + void drawBgTile(uint16_t data, std::vector pos, int bg, int bpp); }; } #endif //COMSQUARE_PPU_HPP diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp new file mode 100644 index 0000000..94db8d3 --- /dev/null +++ b/sources/PPU/PPUUtils.hpp @@ -0,0 +1,24 @@ +// +// Created by cbihan on 1/27/20. +// + +#ifndef COMSQUARE_PPU_UTILS_HPP +#define COMSQUARE_PPU_UTILS_HPP + + + +namespace ComSquare::PPU +{ + union TileMapData { + struct { + uint8_t posY: 4; + uint8_t posX: 6; + uint8_t palette: 3; + bool tilePriority: 1; + bool horizontalFlip: 1; + bool verticalFlip: 1; + }; + uint16_t raw; + }; +} +#endif //COMSQUARE_PPU_UTILS_HPP