yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::gfx::TextureAtlas Class Reference

Manages multiple textures packed into a single large texture for performance. More...

#include <texture_atlas.h>

Collaboration diagram for yaze::gfx::TextureAtlas:

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 &region)
 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_
 

Detailed Description

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:

  • Fewer texture binds per frame
  • Better memory locality
  • Reduced VRAM fragmentation
  • Easier batch rendering

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 35 of file texture_atlas.h.

Constructor & Destructor Documentation

◆ TextureAtlas()

yaze::gfx::TextureAtlas::TextureAtlas ( int width = 2048,
int height = 2048 )
explicit

Construct texture atlas with specified dimensions.

Parameters
widthAtlas width in pixels (typically 2048 or 4096)
heightAtlas height in pixels (typically 2048 or 4096)

Definition at line 8 of file texture_atlas.cc.

References atlas_bitmap_, height(), LOG_DEBUG, and width().

Here is the call graph for this function:

Member Function Documentation

◆ AllocateRegion()

TextureAtlas::AtlasRegion * yaze::gfx::TextureAtlas::AllocateRegion ( int source_id,
int width,
int height )

Allocate a region in the atlas for a source texture.

Parameters
source_idIdentifier for the source (e.g., room_id)
widthRequired width in pixels
heightRequired height in pixels
Returns
Pointer to allocated region, or nullptr if no space

Uses simple rect packing algorithm. Future: implement more efficient packing.

Todo
Implement more efficient rect packing (shelf, guillotine, etc.)

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.

Here is the call graph for this function:

◆ PackBitmap()

absl::Status yaze::gfx::TextureAtlas::PackBitmap ( const Bitmap & src,
const AtlasRegion & region )

Pack a bitmap into an allocated region.

Parameters
srcSource bitmap to pack
regionRegion to pack into (must be pre-allocated)
Returns
Status of packing operation

Copies pixel data from source bitmap into atlas at region coordinates.

Todo
Implement pixel copying from src to atlas_bitmap_ at region

Definition at line 45 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.

Here is the call graph for this function:

◆ DrawRegion()

absl::Status yaze::gfx::TextureAtlas::DrawRegion ( int source_id,
int dest_x,
int dest_y )

Draw a region from the atlas to screen coordinates.

Parameters
source_idSource identifier (e.g., room_id)
dest_xDestination X coordinate
dest_yDestination Y coordinate
Returns
Status of drawing operation

Future: Integrate with renderer to draw atlas regions.

Todo
Integrate with renderer to draw atlas region at (dest_x, dest_y)

Definition at line 69 of file texture_atlas.cc.

References regions_.

◆ FreeRegion()

void yaze::gfx::TextureAtlas::FreeRegion ( int source_id)

Free a region and mark it as available.

Parameters
source_idSource identifier to free

Definition at line 82 of file texture_atlas.cc.

References LOG_DEBUG, and regions_.

◆ Clear()

void yaze::gfx::TextureAtlas::Clear ( )

Clear all regions and reset atlas.

Definition at line 90 of file texture_atlas.cc.

References LOG_DEBUG, next_x_, next_y_, regions_, and row_height_.

◆ GetAtlasBitmap() [1/2]

Bitmap & yaze::gfx::TextureAtlas::GetAtlasBitmap ( )
inline

Get the atlas bitmap (contains all packed textures)

Returns
Reference to atlas bitmap

Definition at line 104 of file texture_atlas.h.

References atlas_bitmap_.

◆ GetAtlasBitmap() [2/2]

const Bitmap & yaze::gfx::TextureAtlas::GetAtlasBitmap ( ) const
inline

Definition at line 105 of file texture_atlas.h.

References atlas_bitmap_.

◆ GetRegion()

const TextureAtlas::AtlasRegion * yaze::gfx::TextureAtlas::GetRegion ( int source_id) const

Get region for a specific source.

Parameters
source_idSource identifier
Returns
Pointer to region, or nullptr if not found

Definition at line 98 of file texture_atlas.cc.

References regions_.

◆ width()

int yaze::gfx::TextureAtlas::width ( ) const
inline

Get atlas dimensions.

Definition at line 117 of file texture_atlas.h.

References width_.

Referenced by AllocateRegion(), TextureAtlas(), and TryPackRect().

◆ height()

int yaze::gfx::TextureAtlas::height ( ) const
inline

Definition at line 118 of file texture_atlas.h.

References height_.

Referenced by AllocateRegion(), TextureAtlas(), and TryPackRect().

◆ GetStats()

◆ TryPackRect()

bool yaze::gfx::TextureAtlas::TryPackRect ( int width,
int height,
int & out_x,
int & out_y )
private

Definition at line 126 of file texture_atlas.cc.

References height(), height_, next_x_, next_y_, row_height_, width(), and width_.

Referenced by AllocateRegion().

Here is the call graph for this function:

Member Data Documentation

◆ width_

int yaze::gfx::TextureAtlas::width_
private

Definition at line 133 of file texture_atlas.h.

Referenced by GetStats(), TryPackRect(), and width().

◆ height_

int yaze::gfx::TextureAtlas::height_
private

Definition at line 134 of file texture_atlas.h.

Referenced by GetStats(), height(), and TryPackRect().

◆ atlas_bitmap_

Bitmap yaze::gfx::TextureAtlas::atlas_bitmap_
private

Definition at line 135 of file texture_atlas.h.

Referenced by GetAtlasBitmap(), GetAtlasBitmap(), and TextureAtlas().

◆ next_x_

int yaze::gfx::TextureAtlas::next_x_ = 0
private

Definition at line 138 of file texture_atlas.h.

Referenced by Clear(), and TryPackRect().

◆ next_y_

int yaze::gfx::TextureAtlas::next_y_ = 0
private

Definition at line 139 of file texture_atlas.h.

Referenced by Clear(), and TryPackRect().

◆ row_height_

int yaze::gfx::TextureAtlas::row_height_ = 0
private

Definition at line 140 of file texture_atlas.h.

Referenced by Clear(), and TryPackRect().

◆ regions_

std::map<int, AtlasRegion> yaze::gfx::TextureAtlas::regions_
private

Definition at line 143 of file texture_atlas.h.

Referenced by AllocateRegion(), Clear(), DrawRegion(), FreeRegion(), GetRegion(), and GetStats().


The documentation for this class was generated from the following files: