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