ComSquare
PPUUtils.hpp
Go to the documentation of this file.
1 //
2 // Created by cbihan on 1/27/20.
3 //
4 
5 #pragma once
6 
7 #include <stdint-gcc.h>
8 #include <cstddef>
9 #include <memory>
10 #include <array>
11 #include "Models/Vector2.hpp"
12 
13 namespace ComSquare::PPU
14 {
15  class PPU;
16 }
17 
18 namespace ComSquare::PPU::Utils
19 {
23  inline uint8_t to8Bit(int color)
24  {
25  return static_cast<uint8_t>((color << 3) + (color >> 2));
26  }
30  uint32_t CGRAMColorToRGBA(uint16_t CGRAMColor);
32  union TileData {
33  struct {
35  uint16_t posX: 4;
37  uint16_t posY: 6;
39  uint16_t palette: 3;
41  uint16_t tilePriority: 1;
43  uint16_t horizontalFlip: 1;
45  uint16_t verticalFlip: 1;
46  };
47  uint16_t raw = 0;
48  };
49 
51  struct PpuState {
56  };
57 
58  template <std::size_t DEST_SIZE_Y, std::size_t DEST_SIZE_X, std::size_t SRC_SIZE_Y, std::size_t SRC_SIZE_X>
59  void merge2DArray(std::array<std::array<uint32_t, DEST_SIZE_X>, DEST_SIZE_Y> &bufferDest,
60  const std::array<std::array<uint32_t, SRC_SIZE_X>, SRC_SIZE_Y> &bufferSrc,
61  const Vector2<int> &offset)
62  {
63  int offsetY = offset.y;
64  for (auto &row : bufferSrc) {
65  std::copy(row.begin(), row.end(), bufferDest[offsetY++].begin() + offset.x);
66  }
67  }
68 
69  template <std::size_t SRC_SIZE_Y, std::size_t SRC_SIZE_X>
70  void VFlipArray(std::array<std::array<uint32_t, SRC_SIZE_X>, SRC_SIZE_Y> &array,
71  const Vector2<int> &size,
72  const Vector2<int> &offset = {0, 0})
73  {
74  for (int i = offset.y; i < offset.y + size.y; i++) {
75  std::reverse(array[i].begin() + offset.x, array[i].begin() + offset.x + size.x);
76  }
77  }
78 
79  template <std::size_t SRC_SIZE_Y, std::size_t SRC_SIZE_X>
80  void HFlipArray(std::array<std::array<uint32_t, SRC_SIZE_X>, SRC_SIZE_Y> &array,
81  const Vector2<int> &size,
82  const Vector2<int> &offset = {0, 0})
83  {
84  std::reverse(array.begin() + offset.x, array.begin() + offset.x + size.x);
85  }
86 
88  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>
89  static void addBuffer(std::array<std::array<uint32_t, DEST_SIZE_Y>, DEST_SIZE_X> &bufferDest,
90  const std::array<std::array<uint32_t, SRC_SIZE_Y>, SRC_SIZE_X> &bufferSrc)
91  {
92  int i = 0;
93  int j = 0;
94  for (const auto &sourceRow : bufferSrc) {
95  for (const auto &pixel : sourceRow) {
96  if (pixel > 0xFF)
97  bufferDest[i][j] = pixel;
98  j++;
99  };
100  j = 0;
101  i++;
102  };
103  }
104 }
ComSquare::PPU::Utils::HFlipArray
void HFlipArray(std::array< std::array< uint32_t, SRC_SIZE_X >, SRC_SIZE_Y > &array, const Vector2< int > &size, const Vector2< int > &offset={0, 0})
Definition: PPUUtils.hpp:80
Vector2.hpp
ComSquare::PPU::Utils::TileData::posX
uint16_t posX
Tile X offset.
Definition: PPUUtils.hpp:35
ComSquare::PPU::Utils::TileData
Used to parse easily VRAM Tile information.
Definition: PPUUtils.hpp:32
ComSquare::PPU::Utils::TileData::tilePriority
uint16_t tilePriority
True if the Tile has priority.
Definition: PPUUtils.hpp:41
ComSquare::PPU
Definition: Background.cpp:11
ComSquare::PPU::Utils::PpuState
Struct to save all specific variables needed for the registers (prev values for example)
Definition: PPUUtils.hpp:51
ComSquare::PPU::Utils::TileData::palette
uint16_t palette
Palette number used by the Tile.
Definition: PPUUtils.hpp:39
ComSquare::PPU::Utils::TileData::horizontalFlip
uint16_t horizontalFlip
True if the Tile need to be horizontally flipped.
Definition: PPUUtils.hpp:43
ComSquare::PPU::Utils::TileData::posY
uint16_t posY
Tile Y offset.
Definition: PPUUtils.hpp:37
ComSquare::PPU::Utils::to8Bit
uint8_t to8Bit(int color)
Transform CGRAM color data to 8bits.
Definition: PPUUtils.hpp:23
ComSquare::PPU::Utils::merge2DArray
void merge2DArray(std::array< std::array< uint32_t, DEST_SIZE_X >, DEST_SIZE_Y > &bufferDest, const std::array< std::array< uint32_t, SRC_SIZE_X >, SRC_SIZE_Y > &bufferSrc, const Vector2< int > &offset)
Definition: PPUUtils.hpp:59
ComSquare::PPU::Utils::TileData::raw
uint16_t raw
Definition: PPUUtils.hpp:47
ComSquare::Vector2::x
T x
Definition: Vector2.hpp:16
ComSquare::PPU::Utils::PpuState::hScrollPrevValue
uint8_t hScrollPrevValue
Shared by the four BGnHOFS registers.
Definition: PPUUtils.hpp:55
ComSquare::Vector2< int >
ComSquare::PPU::Utils::VFlipArray
void VFlipArray(std::array< std::array< uint32_t, SRC_SIZE_X >, SRC_SIZE_Y > &array, const Vector2< int > &size, const Vector2< int > &offset={0, 0})
Definition: PPUUtils.hpp:70
ComSquare::PPU::Utils::TileData::verticalFlip
uint16_t verticalFlip
True if the Tile need to be vertically flipped.
Definition: PPUUtils.hpp:45
ComSquare::PPU::Utils::addBuffer
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)
Add a bg buffer to another buffer.
Definition: PPUUtils.hpp:89
ComSquare::PPU::Utils
Definition: PPU.cpp:12
ComSquare::PPU::Utils::PpuState::hvSharedScrollPrevValue
uint8_t hvSharedScrollPrevValue
Used by by all eight BGnxOFS registers (0x210D - 0x2114)
Definition: PPUUtils.hpp:53
ComSquare::Vector2::y
T y
Definition: Vector2.hpp:17
ComSquare::PPU::Utils::CGRAMColorToRGBA
uint32_t CGRAMColorToRGBA(uint16_t CGRAMColor)
Transform SNES color code BGR to uint32_t RGBA.
Definition: PPUUtils.cpp:10