yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
arena.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_ARENA_H
2#define YAZE_APP_GFX_ARENA_H
3
4#include <array>
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <mutex>
9#include <string>
10#include <tuple>
11#include <unordered_map>
12#include <vector>
13
14#include "app/gfx/core/bitmap.h"
16#include "util/sdl_deleter.h"
17
18namespace yaze {
19namespace gfx {
20
46class Arena {
47 public:
48 static Arena& Get();
49
50 void Initialize(IRenderer* renderer);
51 ~Arena();
52
53 // --- New Deferred Command System ---
57 Bitmap* bitmap; // The bitmap that needs a texture operation
58 uint32_t generation; // Generation at queue time for staleness detection
59 };
60
62 void ProcessTextureQueue(IRenderer* renderer);
63
68 bool HasPendingTextures() const { return !texture_command_queue_.empty(); }
69
75 bool ProcessSingleTexture(IRenderer* renderer);
76
77 // --- Surface Management (unchanged) ---
78 SDL_Surface* AllocateSurface(int width, int height, int depth, int format);
79 void FreeSurface(SDL_Surface* surface);
80
81 void Shutdown();
82
83 // Resource tracking for debugging
84 size_t GetTextureCount() const { return textures_.size(); }
85 size_t GetSurfaceCount() const { return surfaces_.size(); }
86 size_t GetPooledTextureCount() const {
88 }
89 size_t GetPooledSurfaceCount() const {
91 }
93 return texture_command_queue_.size();
94 }
95
96 // Graphics sheet access (223 total sheets in YAZE)
101 std::array<gfx::Bitmap, 223>& gfx_sheets() { return gfx_sheets_; }
102
108 auto gfx_sheet(int i) {
109 if (i < 0 || i >= 223) return gfx::Bitmap{};
110 return gfx_sheets_[i];
111 }
112
118 auto mutable_gfx_sheet(int i) {
119 if (i < 0 || i >= 223) return static_cast<gfx::Bitmap*>(nullptr);
120 return &gfx_sheets_[i];
121 }
122
127 auto mutable_gfx_sheets() { return &gfx_sheets_; }
128
134 void NotifySheetModified(int sheet_index);
135
136 // ========== Palette Change Notification System ==========
137
142 std::function<void(const std::string& group_name, int palette_index)>;
143
150 void NotifyPaletteModified(const std::string& group_name,
151 int palette_index = -1);
152
159
164 void UnregisterPaletteListener(int listener_id);
165
166 // Background buffer access for SNES layer rendering
171 auto& bg1() { return bg1_; }
172
177 auto& bg2() { return bg2_; }
178
179 private:
180 Arena();
181
184
185 static constexpr int kTilesPerRow = 64;
186 static constexpr int kTilesPerColumn = 64;
187 static constexpr int kTotalTiles = kTilesPerRow * kTilesPerColumn;
188
189 std::array<uint16_t, kTotalTiles> layer1_buffer_;
190 std::array<uint16_t, kTotalTiles> layer2_buffer_;
191
192 std::array<gfx::Bitmap, 223> gfx_sheets_;
193
194 std::unordered_map<TextureHandle,
195 std::unique_ptr<SDL_Texture, util::SDL_Texture_Deleter>>
197
198 std::unordered_map<SDL_Surface*,
199 std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>>
201
202 // Resource pooling for efficient memory management
203 struct TexturePool {
204 std::vector<TextureHandle> available_textures_;
205 std::unordered_map<TextureHandle, std::pair<int, int>> texture_sizes_;
206 static constexpr size_t MAX_POOL_SIZE = 100;
208
209 struct SurfacePool {
210 std::vector<SDL_Surface*> available_surfaces_;
211 std::unordered_map<SDL_Surface*, std::tuple<int, int, int, int>>
213 static constexpr size_t MAX_POOL_SIZE = 100;
215
216 std::vector<TextureCommand> texture_command_queue_;
218
219 // Palette change notification system
220 std::unordered_map<int, PaletteChangeCallback> palette_listeners_;
222};
223
224} // namespace gfx
225} // namespace yaze
226
227#endif // YAZE_APP_GFX_ARENA_H
Resource management arena for efficient graphics memory handling.
Definition arena.h:46
IRenderer * renderer_
Definition arena.h:217
size_t GetSurfaceCount() const
Definition arena.h:85
std::unordered_map< SDL_Surface *, std::unique_ptr< SDL_Surface, util::SDL_Surface_Deleter > > surfaces_
Definition arena.h:200
void Initialize(IRenderer *renderer)
Definition arena.cc:15
struct yaze::gfx::Arena::TexturePool texture_pool_
SDL_Surface * AllocateSurface(int width, int height, int depth, int format)
Definition arena.cc:250
std::unordered_map< TextureHandle, std::unique_ptr< SDL_Texture, util::SDL_Texture_Deleter > > textures_
Definition arena.h:196
BackgroundBuffer bg2_
Definition arena.h:183
size_t GetPooledSurfaceCount() const
Definition arena.h:89
bool HasPendingTextures() const
Check if there are pending textures to process.
Definition arena.h:68
auto gfx_sheet(int i)
Get a specific graphics sheet by index.
Definition arena.h:108
static constexpr int kTotalTiles
Definition arena.h:187
size_t GetTextureCount() const
Definition arena.h:84
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:34
int RegisterPaletteListener(PaletteChangeCallback callback)
Register a callback for palette change notifications.
Definition arena.cc:360
std::array< uint16_t, kTotalTiles > layer2_buffer_
Definition arena.h:190
static constexpr int kTilesPerColumn
Definition arena.h:186
size_t GetPooledTextureCount() const
Definition arena.h:86
static constexpr int kTilesPerRow
Definition arena.h:185
auto mutable_gfx_sheets()
Get mutable reference to all graphics sheets.
Definition arena.h:127
std::function< void(const std::string &group_name, int palette_index)> PaletteChangeCallback
Definition arena.h:141
void FreeSurface(SDL_Surface *surface)
Definition arena.cc:280
auto mutable_gfx_sheet(int i)
Get mutable reference to a specific graphics sheet.
Definition arena.h:118
BackgroundBuffer bg1_
Definition arena.h:182
void ProcessTextureQueue(IRenderer *renderer)
Definition arena.cc:110
std::array< gfx::Bitmap, 223 > gfx_sheets_
Definition arena.h:192
bool ProcessSingleTexture(IRenderer *renderer)
Process a single texture command for frame-budget-aware loading.
Definition arena.cc:40
auto & bg2()
Get reference to background layer 2 buffer.
Definition arena.h:177
std::array< gfx::Bitmap, 223 > & gfx_sheets()
Get reference to all graphics sheets.
Definition arena.h:101
std::unordered_map< int, PaletteChangeCallback > palette_listeners_
Definition arena.h:220
std::vector< TextureCommand > texture_command_queue_
Definition arena.h:216
std::array< uint16_t, kTotalTiles > layer1_buffer_
Definition arena.h:189
void NotifySheetModified(int sheet_index)
Notify Arena that a graphics sheet has been modified.
Definition arena.cc:313
auto & bg1()
Get reference to background layer 1 buffer.
Definition arena.h:171
struct yaze::gfx::Arena::SurfacePool surface_pool_
void UnregisterPaletteListener(int listener_id)
Unregister a palette change listener.
Definition arena.cc:367
size_t texture_command_queue_size() const
Definition arena.h:92
void Shutdown()
Definition arena.cc:294
static Arena & Get()
Definition arena.cc:19
int next_palette_listener_id_
Definition arena.h:221
void NotifyPaletteModified(const std::string &group_name, int palette_index=-1)
Notify all listeners that a palette has been modified.
Definition arena.cc:343
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
Defines an abstract interface for all rendering operations.
Definition irenderer.h:40
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:27
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:213
std::vector< SDL_Surface * > available_surfaces_
Definition arena.h:210
std::unordered_map< SDL_Surface *, std::tuple< int, int, int, int > > surface_info_
Definition arena.h:212
TextureCommandType type
Definition arena.h:56
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:206
std::vector< TextureHandle > available_textures_
Definition arena.h:204
std::unordered_map< TextureHandle, std::pair< int, int > > texture_sizes_
Definition arena.h:205