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
4#include <SDL.h>
5#include <vector>
6#include <unordered_map>
7#include <memory>
8
9#include "app/gfx/bitmap.h"
12
13namespace yaze {
14namespace gfx {
15
21 float x, y;
23 float rotation;
24 SDL_Color tint;
25
26 RenderCommand(int id, float x_pos, float y_pos,
27 float sx = 1.0f, float sy = 1.0f,
28 float rot = 0.0f, SDL_Color color = {255, 255, 255, 255})
29 : atlas_id(id), x(x_pos), y(y_pos),
30 scale_x(sx), scale_y(sy), rotation(rot), tint(color) {}
31};
32
47
75 public:
76 static AtlasRenderer& Get();
77
83 void Initialize(IRenderer* renderer, int initial_size = 1024);
84
90 int AddBitmap(const Bitmap& bitmap);
91
98 int AddBitmapWithBppOptimization(const Bitmap& bitmap, BppFormat target_bpp);
99
104 void RemoveBitmap(int atlas_id);
105
111 void UpdateBitmap(int atlas_id, const Bitmap& bitmap);
112
117 void RenderBatch(const std::vector<RenderCommand>& render_commands);
118
124 void RenderBatchWithBppOptimization(const std::vector<RenderCommand>& render_commands,
125 const std::unordered_map<BppFormat, std::vector<int>>& bpp_groups);
126
131 AtlasStats GetStats() const;
132
136 void Defragment();
137
141 void Clear();
142
151 void RenderBitmap(int atlas_id, float x, float y, float scale_x = 1.0f, float scale_y = 1.0f);
152
158 SDL_Rect GetUVCoordinates(int atlas_id) const;
159
160 private:
161 AtlasRenderer() = default;
163
164 struct AtlasEntry {
166 SDL_Rect uv_rect; // UV coordinates in atlas
168 bool in_use;
169 BppFormat bpp_format; // BPP format of this entry
172
173 AtlasEntry(int id, const SDL_Rect& rect, TextureHandle tex, BppFormat bpp = BppFormat::kBpp8,
174 int width = 0, int height = 0)
175 : atlas_id(id), uv_rect(rect), texture(tex), in_use(true),
176 bpp_format(bpp), original_width(width), original_height(height) {}
177 };
178
179 struct Atlas {
181 int size;
182 std::vector<AtlasEntry> entries;
183 std::vector<bool> used_regions; // Track used regions for packing
184
185 Atlas(int s) : size(s), used_regions(s * s, false) {}
186 };
187
189 std::vector<std::unique_ptr<Atlas>> atlases_;
190 std::unordered_map<int, AtlasEntry*> atlas_lookup_;
193
194 // Helper methods
195 bool PackBitmap(Atlas& atlas, const Bitmap& bitmap, SDL_Rect& uv_rect);
196 void CreateNewAtlas();
197 void RebuildAtlas(Atlas& atlas);
198 SDL_Rect FindFreeRegion(Atlas& atlas, int width, int height);
199 void MarkRegionUsed(Atlas& atlas, const SDL_Rect& rect, bool used);
200};
201
202
203} // namespace gfx
204} // namespace yaze
205
206#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:66
Defines an abstract interface for all rendering operations.
Definition irenderer.h:35
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:24
BppFormat
BPP format enumeration for SNES graphics.
@ kBpp8
8 bits per pixel (256 colors)
Main namespace for the application.
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.