Manages multiple textures packed into a single large texture for performance. More...
#include <texture_atlas.h>
Classes | |
struct | AtlasRegion |
Region within the atlas texture. More... | |
struct | AtlasStats |
Get atlas utilization statistics. More... | |
Public Member Functions | |
TextureAtlas (int width=2048, int height=2048) | |
Construct texture atlas with specified dimensions. | |
AtlasRegion * | AllocateRegion (int source_id, int width, int height) |
Allocate a region in the atlas for a source texture. | |
absl::Status | PackBitmap (const Bitmap &src, const AtlasRegion ®ion) |
Pack a bitmap into an allocated region. | |
absl::Status | DrawRegion (int source_id, int dest_x, int dest_y) |
Draw a region from the atlas to screen coordinates. | |
void | FreeRegion (int source_id) |
Free a region and mark it as available. | |
void | Clear () |
Clear all regions and reset atlas. | |
Bitmap & | GetAtlasBitmap () |
Get the atlas bitmap (contains all packed textures) | |
const Bitmap & | GetAtlasBitmap () const |
const AtlasRegion * | GetRegion (int source_id) const |
Get region for a specific source. | |
int | width () const |
Get atlas dimensions. | |
int | height () const |
AtlasStats | GetStats () const |
Private Member Functions | |
bool | TryPackRect (int width, int height, int &out_x, int &out_y) |
Private Attributes | |
int | width_ |
int | height_ |
Bitmap | atlas_bitmap_ |
int | next_x_ = 0 |
int | next_y_ = 0 |
int | row_height_ = 0 |
std::map< int, AtlasRegion > | regions_ |
Manages multiple textures packed into a single large texture for performance.
Future-proof infrastructure for combining multiple room textures into one atlas. This reduces GPU state changes and improves rendering performance when many rooms are open.
Benefits:
Usage (Future): TextureAtlas atlas(2048, 2048); auto region = atlas.AllocateRegion(room_id, 512, 512); atlas.PackBitmap(room.bg1_buffer().bitmap(), *region); atlas.DrawRegion(room_id, x, y);
Definition at line 33 of file texture_atlas.h.
|
explicit |
Construct texture atlas with specified dimensions.
width | Atlas width in pixels (typically 2048 or 4096) |
height | Atlas height in pixels (typically 2048 or 4096) |
Definition at line 8 of file texture_atlas.cc.
References atlas_bitmap_, height(), LOG_DEBUG, and width().
TextureAtlas::AtlasRegion * yaze::gfx::TextureAtlas::AllocateRegion | ( | int | source_id, |
int | width, | ||
int | height | ||
) |
Allocate a region in the atlas for a source texture.
source_id | Identifier for the source (e.g., room_id) |
width | Required width in pixels |
height | Required height in pixels |
Uses simple rect packing algorithm. Future: implement more efficient packing.
Definition at line 16 of file texture_atlas.cc.
References yaze::gfx::TextureAtlas::AtlasRegion::height, height(), yaze::gfx::TextureAtlas::AtlasRegion::in_use, LOG_DEBUG, regions_, yaze::gfx::TextureAtlas::AtlasRegion::source_id, TryPackRect(), yaze::gfx::TextureAtlas::AtlasRegion::width, width(), yaze::gfx::TextureAtlas::AtlasRegion::x, and yaze::gfx::TextureAtlas::AtlasRegion::y.
absl::Status yaze::gfx::TextureAtlas::PackBitmap | ( | const Bitmap & | src, |
const AtlasRegion & | region | ||
) |
Pack a bitmap into an allocated region.
src | Source bitmap to pack |
region | Region to pack into (must be pre-allocated) |
Copies pixel data from source bitmap into atlas at region coordinates.
Definition at line 43 of file texture_atlas.cc.
References yaze::gfx::Bitmap::height(), yaze::gfx::TextureAtlas::AtlasRegion::height, yaze::gfx::TextureAtlas::AtlasRegion::in_use, yaze::gfx::Bitmap::is_active(), LOG_DEBUG, yaze::gfx::TextureAtlas::AtlasRegion::source_id, yaze::gfx::Bitmap::width(), yaze::gfx::TextureAtlas::AtlasRegion::width, yaze::gfx::TextureAtlas::AtlasRegion::x, and yaze::gfx::TextureAtlas::AtlasRegion::y.
absl::Status yaze::gfx::TextureAtlas::DrawRegion | ( | int | source_id, |
int | dest_x, | ||
int | dest_y | ||
) |
Draw a region from the atlas to screen coordinates.
source_id | Source identifier (e.g., room_id) |
dest_x | Destination X coordinate |
dest_y | Destination Y coordinate |
Future: Integrate with renderer to draw atlas regions.
Definition at line 65 of file texture_atlas.cc.
References regions_.
void yaze::gfx::TextureAtlas::FreeRegion | ( | int | source_id | ) |
Free a region and mark it as available.
source_id | Source identifier to free |
Definition at line 77 of file texture_atlas.cc.
void yaze::gfx::TextureAtlas::Clear | ( | ) |
Clear all regions and reset atlas.
Definition at line 85 of file texture_atlas.cc.
References LOG_DEBUG, next_x_, next_y_, regions_, and row_height_.
|
inline |
Get the atlas bitmap (contains all packed textures)
Definition at line 101 of file texture_atlas.h.
References atlas_bitmap_.
|
inline |
Definition at line 102 of file texture_atlas.h.
References atlas_bitmap_.
const TextureAtlas::AtlasRegion * yaze::gfx::TextureAtlas::GetRegion | ( | int | source_id | ) | const |
Get region for a specific source.
source_id | Source identifier |
Definition at line 93 of file texture_atlas.cc.
References regions_.
|
inline |
Get atlas dimensions.
Definition at line 114 of file texture_atlas.h.
References width_.
Referenced by AllocateRegion(), TextureAtlas(), and TryPackRect().
|
inline |
Definition at line 115 of file texture_atlas.h.
References height_.
Referenced by AllocateRegion(), TextureAtlas(), and TryPackRect().
TextureAtlas::AtlasStats yaze::gfx::TextureAtlas::GetStats | ( | ) | const |
Definition at line 101 of file texture_atlas.cc.
References height_, regions_, yaze::gfx::TextureAtlas::AtlasStats::total_pixels, yaze::gfx::TextureAtlas::AtlasStats::total_regions, yaze::gfx::TextureAtlas::AtlasStats::used_pixels, yaze::gfx::TextureAtlas::AtlasStats::used_regions, yaze::gfx::TextureAtlas::AtlasStats::utilization, and width_.
|
private |
Definition at line 120 of file texture_atlas.cc.
References height(), height_, next_x_, next_y_, row_height_, width(), and width_.
Referenced by AllocateRegion().
|
private |
Definition at line 130 of file texture_atlas.h.
Referenced by GetStats(), TryPackRect(), and width().
|
private |
Definition at line 131 of file texture_atlas.h.
Referenced by GetStats(), height(), and TryPackRect().
|
private |
Definition at line 132 of file texture_atlas.h.
Referenced by GetAtlasBitmap(), GetAtlasBitmap(), and TextureAtlas().
|
private |
Definition at line 135 of file texture_atlas.h.
Referenced by Clear(), and TryPackRect().
|
private |
Definition at line 136 of file texture_atlas.h.
Referenced by Clear(), and TryPackRect().
|
private |
Definition at line 137 of file texture_atlas.h.
Referenced by Clear(), and TryPackRect().
|
private |
Definition at line 140 of file texture_atlas.h.
Referenced by AllocateRegion(), Clear(), DrawRegion(), FreeRegion(), GetRegion(), and GetStats().