yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
atlas_renderer.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_ATLAS_RENDERER_H
2#define YAZE_APP_GFX_ATLAS_RENDERER_H
3
5
6#include <memory>
7#include <unordered_map>
8#include <vector>
9
10#include "app/gfx/core/bitmap.h"
13
14namespace yaze {
15namespace gfx {
16
22 float x, y;
24 float rotation;
25 SDL_Color tint;
26
27 RenderCommand(int id, float x_pos, float y_pos, float sx = 1.0f,
28 float sy = 1.0f, float rot = 0.0f,
29 SDL_Color color = {255, 255, 255, 255})
30 : atlas_id(id),
31 x(x_pos),
32 y(y_pos),
33 scale_x(sx),
34 scale_y(sy),
35 rotation(rot),
36 tint(color) {}
37};
38
58
86 public:
87 static AtlasRenderer& Get();
88
94 void Initialize(IRenderer* renderer, int initial_size = 1024);
95
101 int AddBitmap(const Bitmap& bitmap);
102
109 int AddBitmapWithBppOptimization(const Bitmap& bitmap, BppFormat target_bpp);
110
115 void RemoveBitmap(int atlas_id);
116
122 void UpdateBitmap(int atlas_id, const Bitmap& bitmap);
123
128 void RenderBatch(const std::vector<RenderCommand>& render_commands);
129
136 const std::vector<RenderCommand>& render_commands,
137 const std::unordered_map<BppFormat, std::vector<int>>& bpp_groups);
138
143 AtlasStats GetStats() const;
144
148 void Defragment();
149
153 void Clear();
154
163 void RenderBitmap(int atlas_id, float x, float y, float scale_x = 1.0f,
164 float scale_y = 1.0f);
165
171 SDL_Rect GetUVCoordinates(int atlas_id) const;
172
173 private:
174 AtlasRenderer() = default;
176
177 struct AtlasEntry {
179 SDL_Rect uv_rect; // UV coordinates in atlas
181 bool in_use;
182 BppFormat bpp_format; // BPP format of this entry
185
186 AtlasEntry(int id, const SDL_Rect& rect, TextureHandle tex,
187 BppFormat bpp = BppFormat::kBpp8, int width = 0, int height = 0)
188 : atlas_id(id),
189 uv_rect(rect),
190 texture(tex),
191 in_use(true),
192 bpp_format(bpp),
193 original_width(width),
194 original_height(height) {}
195 };
196
197 struct Atlas {
199 int size;
200 std::vector<AtlasEntry> entries;
201 std::vector<bool> used_regions; // Track used regions for packing
202
203 Atlas(int s) : size(s), used_regions(s * s, false) {}
204 };
205
207 std::vector<std::unique_ptr<Atlas>> atlases_;
208 std::unordered_map<int, AtlasEntry*> atlas_lookup_;
211
212 // Helper methods
213 bool PackBitmap(Atlas& atlas, const Bitmap& bitmap, SDL_Rect& uv_rect);
214 void CreateNewAtlas();
215 void RebuildAtlas(Atlas& atlas);
216 SDL_Rect FindFreeRegion(Atlas& atlas, int width, int height);
217 void MarkRegionUsed(Atlas& atlas, const SDL_Rect& rect, bool used);
218};
219
220} // namespace gfx
221} // namespace yaze
222
223#endif // YAZE_APP_GFX_ATLAS_RENDERER_H
Atlas-based rendering system for efficient graphics operations.
SDL_Rect FindFreeRegion(Atlas &atlas, int width, int height)
AtlasStats GetStats() const
Get atlas statistics.
bool PackBitmap(Atlas &atlas, const Bitmap &bitmap, SDL_Rect &uv_rect)
void UpdateBitmap(int atlas_id, const Bitmap &bitmap)
Update a bitmap in the atlas.
std::vector< std::unique_ptr< Atlas > > atlases_
void RebuildAtlas(Atlas &atlas)
void Clear()
Clear all atlases.
int AddBitmapWithBppOptimization(const Bitmap &bitmap, BppFormat target_bpp)
Add a bitmap to the atlas with BPP format optimization.
void RenderBitmap(int atlas_id, float x, float y, float scale_x=1.0f, float scale_y=1.0f)
Render a single bitmap using atlas (convenience method)
void MarkRegionUsed(Atlas &atlas, const SDL_Rect &rect, bool used)
std::unordered_map< int, AtlasEntry * > atlas_lookup_
void RenderBatch(const std::vector< RenderCommand > &render_commands)
Render multiple bitmaps in a single draw call.
void RemoveBitmap(int atlas_id)
Remove a bitmap from the atlas.
void Defragment()
Defragment the atlas to reclaim space.
SDL_Rect GetUVCoordinates(int atlas_id) const
Get UV coordinates for a bitmap in the atlas.
int AddBitmap(const Bitmap &bitmap)
Add a bitmap to the atlas.
static AtlasRenderer & Get()
void Initialize(IRenderer *renderer, int initial_size=1024)
Initialize the atlas renderer.
void RenderBatchWithBppOptimization(const std::vector< RenderCommand > &render_commands, const std::unordered_map< BppFormat, std::vector< int > > &bpp_groups)
Render multiple bitmaps with BPP-aware batching.
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
BppFormat
BPP format enumeration for SNES graphics.
@ kBpp8
8 bits per pixel (256 colors)
SDL2/SDL3 compatibility layer.
AtlasEntry(int id, const SDL_Rect &rect, TextureHandle tex, BppFormat bpp=BppFormat::kBpp8, int width=0, int height=0)
std::vector< AtlasEntry > entries
Atlas usage statistics.
Render command for batch rendering.
float rotation
Rotation angle in degrees.
SDL_Color tint
Color tint.
int atlas_id
Atlas ID of bitmap to render.
float y
Screen coordinates.
RenderCommand(int id, float x_pos, float y_pos, float sx=1.0f, float sy=1.0f, float rot=0.0f, SDL_Color color={255, 255, 255, 255})
float scale_y
Scale factors.