yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
background_buffer.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdint>
5#include <vector>
6
7#include "app/gfx/bitmap.h"
8#include "app/gfx/snes_tile.h"
9
10namespace yaze::gfx {
11
13 : width_(width), height_(height) {
14 // Initialize buffer with size for SNES layers
15 const int total_tiles = (width / 8) * (height / 8);
16 buffer_.resize(total_tiles, 0);
17}
18
19void BackgroundBuffer::SetTileAt(int x, int y, uint16_t value) {
20 if (x >= 0 && x < width_ / 8 && y >= 0 && y < height_ / 8) {
21 buffer_[y * (width_ / 8) + x] = value;
22 }
23}
24
25uint16_t BackgroundBuffer::GetTileAt(int x, int y) const {
26 if (x >= 0 && x < width_ / 8 && y >= 0 && y < height_ / 8) {
27 return buffer_[y * (width_ / 8) + x];
28 }
29 return 0;
30}
31
32void BackgroundBuffer::ClearBuffer() { std::ranges::fill(buffer_, 0); }
33
34void BackgroundBuffer::DrawTile(const TileInfo& tile, uint8_t* canvas,
35 const uint8_t* tiledata, int indexoffset) {
36 int tx = (tile.id_ / 16 * 512) + ((tile.id_ & 0xF) * 4);
37 uint8_t palnibble = (uint8_t)(tile.palette_ << 4);
38 uint8_t r = tile.horizontal_mirror_ ? 1 : 0;
39
40 for (int yl = 0; yl < 512; yl += 64) {
41 int my = indexoffset + (tile.vertical_mirror_ ? 448 - yl : yl);
42
43 for (int xl = 0; xl < 4; xl++) {
44 int mx = 2 * (tile.horizontal_mirror_ ? 3 - xl : xl);
45
46 uint8_t pixel = tiledata[tx + yl + xl];
47
48 int index = mx + my;
49 canvas[index + r ^ 1] = (uint8_t)((pixel & 0x0F) | palnibble);
50 canvas[index + r] = (uint8_t)((pixel >> 4) | palnibble);
51 }
52 }
53}
54
55void BackgroundBuffer::DrawBackground(std::span<uint8_t> gfx16_data) {
56 // Initialize bitmap
57 bitmap_.Create(width_, height_, 8, std::vector<uint8_t>(width_ * height_, 0));
58 if (buffer_.size() < width_ * height_) {
59 buffer_.resize(width_ * height_);
60 }
61
62 // For each tile on the tile buffer
63 for (int yy = 0; yy < (height_ / 8) * (width_ / 8); yy += (width_ / 8)) {
64 for (int xx = 0; xx < width_ / 8; xx++) {
65 // Prevent draw if tile == 0xFFFF since it's 0 indexed
66 if (buffer_[xx + yy] != 0xFFFF) {
67 auto tile = gfx::GetTilesInfo(buffer_[xx + yy]);
68 DrawTile(tile, bitmap_.mutable_data().data(), gfx16_data.data(), 0);
69 }
70 }
71 }
72}
73
74void BackgroundBuffer::DrawFloor(const std::vector<uint8_t>& rom_data,
75 int tile_address, int tile_address_floor,
76 uint8_t floor_graphics) {
77 auto f = (uint8_t)(floor_graphics << 4);
78
79 // Create floor tiles from ROM data
80 gfx::TileInfo floorTile1(rom_data[tile_address + f],
81 rom_data[tile_address + f + 1]);
82 gfx::TileInfo floorTile2(rom_data[tile_address + f + 2],
83 rom_data[tile_address + f + 3]);
84 gfx::TileInfo floorTile3(rom_data[tile_address + f + 4],
85 rom_data[tile_address + f + 5]);
86 gfx::TileInfo floorTile4(rom_data[tile_address + f + 6],
87 rom_data[tile_address + f + 7]);
88
89 gfx::TileInfo floorTile5(rom_data[tile_address_floor + f],
90 rom_data[tile_address_floor + f + 1]);
91 gfx::TileInfo floorTile6(rom_data[tile_address_floor + f + 2],
92 rom_data[tile_address_floor + f + 3]);
93 gfx::TileInfo floorTile7(rom_data[tile_address_floor + f + 4],
94 rom_data[tile_address_floor + f + 5]);
95 gfx::TileInfo floorTile8(rom_data[tile_address_floor + f + 6],
96 rom_data[tile_address_floor + f + 7]);
97
98 // Draw the floor tiles in a pattern
99 for (int xx = 0; xx < 16; xx++) {
100 for (int yy = 0; yy < 32; yy++) {
101 SetTileAt((xx * 4), (yy * 2), floorTile1.id_);
102 SetTileAt((xx * 4) + 1, (yy * 2), floorTile2.id_);
103 SetTileAt((xx * 4) + 2, (yy * 2), floorTile3.id_);
104 SetTileAt((xx * 4) + 3, (yy * 2), floorTile4.id_);
105
106 SetTileAt((xx * 4), (yy * 2) + 1, floorTile5.id_);
107 SetTileAt((xx * 4) + 1, (yy * 2) + 1, floorTile6.id_);
108 SetTileAt((xx * 4) + 2, (yy * 2) + 1, floorTile7.id_);
109 SetTileAt((xx * 4) + 3, (yy * 2) + 1, floorTile8.id_);
110 }
111 }
112}
113
114} // namespace yaze::gfx
void DrawTile(const TileInfo &tile_info, uint8_t *canvas, const uint8_t *tiledata, int indexoffset)
void DrawBackground(std::span< uint8_t > gfx16_data)
BackgroundBuffer(int width=512, int height=512)
void SetTileAt(int x, int y, uint16_t value)
void DrawFloor(const std::vector< uint8_t > &rom_data, int tile_address, int tile_address_floor, uint8_t floor_graphics)
uint16_t GetTileAt(int x, int y) const
std::vector< uint16_t > buffer_
SNES 16-bit tile metadata container.
Definition snes_tile.h:50
Contains classes for handling graphical data.
Definition arena.cc:8
TileInfo GetTilesInfo(uint16_t tile)
Definition snes_tile.cc:354