adding Vector2 in Models and starting drawBgTile loop

This commit is contained in:
Clément Le Bihan
2020-05-13 13:07:20 +02:00
parent 3e811db9cd
commit ffe1b8fc1b
5 changed files with 167 additions and 4 deletions
+1 -1
View File
@@ -212,7 +212,7 @@ add_executable(ComSquare
sources/Models/Components.hpp sources/Models/Components.hpp
sources/Debugger/CGramDebug.cpp sources/Debugger/CGramDebug.cpp
sources/Debugger/CGramDebug.hpp sources/Debugger/CGramDebug.hpp
) sources/Models/Vector2.hpp)
target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED)
+112
View File
@@ -0,0 +1,112 @@
//
// Created by cbihan on 5/13/20.
//
#ifndef COMSQUARE_VECTOR2_HPP
#define COMSQUARE_VECTOR2_HPP
#include <ostream>
#include <cmath>
namespace ComSquare
{
template<typename T>
class Vector2
{
public:
T x;
T y;
Vector2<T>()
: x(0), y(0) {}
Vector2<T>(T x, T y)
: x(x), y(y) {}
Vector2<T>(sf::Vector2<T> v)
: x(v.x), y(v.y) {}
template<typename T2>
Vector2<T> &operator+=(const Vector2<T2> &vec)
{
this->x += vec.x;
this->y += vec.y;
return *this;
}
template<typename T2>
Vector2<T> operator+(const Vector2<T2> &vec) const
{
return Vector2<T>(this->x + vec.x, this->y + vec.y);
}
template<typename T2>
Vector2<T> &operator-=(const Vector2<T2> &vec)
{
this->x -= vec.x;
this->y -= vec.y;
return *this;
}
template<typename T2>
Vector2<T> &operator*=(T2 d)
{
this->x *= d;
this->y *= d;
return *this;
}
template<typename T2>
Vector2<T> operator*(T2 d) const
{
return Vector2<T>(this->x * d, this->y * d);
}
template<typename T2>
T operator*(Vector2<T2> &b) const
{
return this->x * b.x + this->y * b.y;
}
template<typename T2>
Vector2<T> operator/=(Vector2<T2> &b)
{
this->x /= b.x;
this->y /= b.y;
return this;
}
template<typename T2>
Vector2<T> operator/(Vector2<T2> &b) const
{
return Vector2<T>(this->x / b.x, this->y / b.y);
}
template<typename T2>
Vector2<T> operator/=(T2 b)
{
this->x /= b;
this->y /= b;
return this;
}
template<typename T2>
Vector2<T> operator/(T2 b) const
{
return Vector2<T>(this->x / b, this->y / b);
}
};
typedef Vector2<float> Vector2f;
typedef Vector2<unsigned> Vector2u;
typedef Vector2<int> Vector2i;
}
template<typename T>
std::ostream &operator<<(std::ostream &s, const ComSquare::Vector2<T> &v)
{
s << v.x << " " << v.y;
return s;
}
#endif //COMSQUARE_VECTOR2_HPP
+26 -3
View File
@@ -5,9 +5,11 @@
#include <iostream> #include <iostream>
#include <bitset> #include <bitset>
#include "PPU.hpp" #include "PPU.hpp"
#include "PPUUtils.hpp"
#include "../Exceptions/NotImplementedException.hpp" #include "../Exceptions/NotImplementedException.hpp"
#include "../Exceptions/InvalidAddress.hpp" #include "../Exceptions/InvalidAddress.hpp"
#include "../Ram/Ram.hpp" #include "../Ram/Ram.hpp"
#include "../Models/Vector2.hpp"
namespace ComSquare::PPU namespace ComSquare::PPU
{ {
@@ -421,15 +423,36 @@ namespace ComSquare::PPU
{ {
int nbCharactersHeight = (this->_registers._bgsc[bgNumber].tilemapVerticalMirroring) ? 64 : 32; int nbCharactersHeight = (this->_registers._bgsc[bgNumber].tilemapVerticalMirroring) ? 64 : 32;
int nbCharactersWidth = (this->_registers._bgsc[bgNumber].tilemapHorizontalMirroring) ? 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; uint16_t tilemapValue;
for (int i = 0; i < nbCharactersHeight * nbCharactersWidth; i++) { for (int i = 0; i < nbCharactersHeight * nbCharactersWidth; i++) {
for (int j = 0; j < 0x800; j++) { for (int j = 0; j < 0x800; j++) {
tilemapValue = this->vram->read(vramAddress); tilemapValue = this->vram->read_internal(vramAddress);
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<int> 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
}
} }
+4
View File
@@ -589,6 +589,10 @@ namespace ComSquare::PPU
uint16_t cgramRead(uint16_t addr); uint16_t cgramRead(uint16_t addr);
//! @brief Render a background on the screen //! @brief Render a background on the screen
void renderBackground(int bgNumber, std::vector<int> characterSize, int bpp, bool priority); void renderBackground(int bgNumber, std::vector<int> 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<int> pos, int bg, int bpp);
}; };
} }
#endif //COMSQUARE_PPU_HPP #endif //COMSQUARE_PPU_HPP
+24
View File
@@ -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