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 void ClearTextureQueue();
64
69 bool HasPendingTextures() const { return !texture_command_queue_.empty(); }
70
76 bool ProcessSingleTexture(IRenderer* renderer);
77
78 // --- Surface Management (unchanged) ---
79 SDL_Surface* AllocateSurface(int width, int height, int depth, int format);
80 void FreeSurface(SDL_Surface* surface);
81
82 void Shutdown();
83
84 // Resource tracking for debugging
85 size_t GetTextureCount() const { return textures_.size(); }
86 size_t GetSurfaceCount() const { return surfaces_.size(); }
87 size_t GetPooledTextureCount() const {
89 }
90 size_t GetPooledSurfaceCount() const {
92 }
94 return texture_command_queue_.size();
95 }
96
97 // Graphics sheet access (223 total sheets in YAZE)
102 std::array<gfx::Bitmap, 223>& gfx_sheets() { return gfx_sheets_; }
103
109 auto gfx_sheet(int i) {
110 if (i < 0 || i >= 223) return gfx::Bitmap{};
111 return gfx_sheets_[i];
112 }
113
119 auto mutable_gfx_sheet(int i) {
120 if (i < 0 || i >= 223) return static_cast<gfx::Bitmap*>(nullptr);
121 return &gfx_sheets_[i];
122 }
123
128 auto mutable_gfx_sheets() { return &gfx_sheets_; }
129
135 void NotifySheetModified(int sheet_index);
136
137 // ========== Palette Change Notification System ==========
138
143 std::function<void(const std::string& group_name, int palette_index)>;
144
151 void NotifyPaletteModified(const std::string& group_name,
152 int palette_index = -1);
153
160
165 void UnregisterPaletteListener(int listener_id);
166
167 // Background buffer access for SNES layer rendering
172 auto& bg1() { return bg1_; }
173
178 auto& bg2() { return bg2_; }
179
180 private:
181 Arena();
182
185
186 static constexpr int kTilesPerRow = 64;
187 static constexpr int kTilesPerColumn = 64;
188 static constexpr int kTotalTiles = kTilesPerRow * kTilesPerColumn;
189
190 std::array<uint16_t, kTotalTiles> layer1_buffer_;
191 std::array<uint16_t, kTotalTiles> layer2_buffer_;
192
193 std::array<gfx::Bitmap, 223> gfx_sheets_;
194
195 std::unordered_map<TextureHandle,
196 std::unique_ptr<SDL_Texture, util::SDL_Texture_Deleter>>
198
199 std::unordered_map<SDL_Surface*,
200 std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>>
202
203 // Resource pooling for efficient memory management
204 struct TexturePool {
205 std::vector<TextureHandle> available_textures_;
206 std::unordered_map<TextureHandle, std::pair<int, int>> texture_sizes_;
207 static constexpr size_t MAX_POOL_SIZE = 100;
209
210 struct SurfacePool {
211 std::vector<SDL_Surface*> available_surfaces_;
212 std::unordered_map<SDL_Surface*, std::tuple<int, int, int, int>>
214 static constexpr size_t MAX_POOL_SIZE = 100;
216
217 std::vector<TextureCommand> texture_command_queue_;
219
220 // Palette change notification system
221 std::unordered_map<int, PaletteChangeCallback> palette_listeners_;
223};
224
225} // namespace gfx
226} // namespace yaze
227
228#endif // YAZE_APP_GFX_ARENA_H
Resource management arena for efficient graphics memory handling.
Definition arena.h:46
IRenderer * renderer_
Definition arena.h:218
size_t GetSurfaceCount() const
Definition arena.h:86
std::unordered_map< SDL_Surface *, std::unique_ptr< SDL_Surface, util::SDL_Surface_Deleter > > surfaces_
Definition arena.h:201
void Initialize(IRenderer *renderer)
Definition arena.cc:16
struct yaze::gfx::Arena::TexturePool texture_pool_
SDL_Surface * AllocateSurface(int width, int height, int depth, int format)
Definition arena.cc:260
std::unordered_map< TextureHandle, std::unique_ptr< SDL_Texture, util::SDL_Texture_Deleter > > textures_
Definition arena.h:197
BackgroundBuffer bg2_
Definition arena.h:184
size_t GetPooledSurfaceCount() const
Definition arena.h:90
bool HasPendingTextures() const
Check if there are pending textures to process.
Definition arena.h:69
auto gfx_sheet(int i)
Get a specific graphics sheet by index.
Definition arena.h:109
static constexpr int kTotalTiles
Definition arena.h:188
void ClearTextureQueue()
Definition arena.cc:41
size_t GetTextureCount() const
Definition arena.h:85
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:35
int RegisterPaletteListener(PaletteChangeCallback callback)
Register a callback for palette change notifications.
Definition arena.cc:371
std::array< uint16_t, kTotalTiles > layer2_buffer_
Definition arena.h:191
static constexpr int kTilesPerColumn
Definition arena.h:187
size_t GetPooledTextureCount() const
Definition arena.h:87
static constexpr int kTilesPerRow
Definition arena.h:186
auto mutable_gfx_sheets()
Get mutable reference to all graphics sheets.
Definition arena.h:128
std::function< void(const std::string &group_name, int palette_index)> PaletteChangeCallback
Definition arena.h:142
void FreeSurface(SDL_Surface *surface)
Definition arena.cc:290
auto mutable_gfx_sheet(int i)
Get mutable reference to a specific graphics sheet.
Definition arena.h:119
BackgroundBuffer bg1_
Definition arena.h:183
void ProcessTextureQueue(IRenderer *renderer)
Definition arena.cc:115
std::array< gfx::Bitmap, 223 > gfx_sheets_
Definition arena.h:193
bool ProcessSingleTexture(IRenderer *renderer)
Process a single texture command for frame-budget-aware loading.
Definition arena.cc:45
auto & bg2()
Get reference to background layer 2 buffer.
Definition arena.h:178
std::array< gfx::Bitmap, 223 > & gfx_sheets()
Get reference to all graphics sheets.
Definition arena.h:102
std::unordered_map< int, PaletteChangeCallback > palette_listeners_
Definition arena.h:221
std::vector< TextureCommand > texture_command_queue_
Definition arena.h:217
std::array< uint16_t, kTotalTiles > layer1_buffer_
Definition arena.h:190
void NotifySheetModified(int sheet_index)
Notify Arena that a graphics sheet has been modified.
Definition arena.cc:323
auto & bg1()
Get reference to background layer 1 buffer.
Definition arena.h:172
struct yaze::gfx::Arena::SurfacePool surface_pool_
void UnregisterPaletteListener(int listener_id)
Unregister a palette change listener.
Definition arena.cc:378
size_t texture_command_queue_size() const
Definition arena.h:93
void Shutdown()
Definition arena.cc:304
static Arena & Get()
Definition arena.cc:20
int next_palette_listener_id_
Definition arena.h:222
void NotifyPaletteModified(const std::string &group_name, int palette_index=-1)
Notify all listeners that a palette has been modified.
Definition arena.cc:353
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:214
std::vector< SDL_Surface * > available_surfaces_
Definition arena.h:211
std::unordered_map< SDL_Surface *, std::tuple< int, int, int, int > > surface_info_
Definition arena.h:213
TextureCommandType type
Definition arena.h:56
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:207
std::vector< TextureHandle > available_textures_
Definition arena.h:205
std::unordered_map< TextureHandle, std::pair< int, int > > texture_sizes_
Definition arena.h:206