yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
texture_atlas.cc
Go to the documentation of this file.
1#include "texture_atlas.h"
2
3#include "util/log.h"
4
5namespace yaze {
6namespace gfx {
7
8TextureAtlas::TextureAtlas(int width, int height)
9 : width_(width), height_(height) {
10 // Create atlas bitmap with initial empty data
11 std::vector<uint8_t> empty_data(width * height, 0);
12 atlas_bitmap_ = Bitmap(width, height, 8, empty_data);
13 LOG_DEBUG("[TextureAtlas]", "Created %dx%d atlas", width, height);
14}
15
17 int width, int height) {
18 // Simple linear packing algorithm
19 // TODO: Implement more efficient rect packing (shelf, guillotine, etc.)
20
21 int pack_x, pack_y;
22 if (!TryPackRect(width, height, pack_x, pack_y)) {
23 LOG_DEBUG("[TextureAtlas]",
24 "Failed to allocate %dx%d region for source %d (atlas full)",
25 width, height, source_id);
26 return nullptr;
27 }
28
29 AtlasRegion region;
30 region.x = pack_x;
31 region.y = pack_y;
32 region.width = width;
33 region.height = height;
34 region.source_id = source_id;
35 region.in_use = true;
36
37 regions_[source_id] = region;
38
39 LOG_DEBUG("[TextureAtlas]", "Allocated region (%d,%d,%dx%d) for source %d",
40 pack_x, pack_y, width, height, source_id);
41
42 return &regions_[source_id];
43}
44
45absl::Status TextureAtlas::PackBitmap(const Bitmap& src,
46 const AtlasRegion& region) {
47 if (!region.in_use) {
48 return absl::FailedPreconditionError("Region not allocated");
49 }
50
51 if (!src.is_active() || src.width() == 0 || src.height() == 0) {
52 return absl::InvalidArgumentError("Source bitmap not active");
53 }
54
55 if (region.width < src.width() || region.height < src.height()) {
56 return absl::InvalidArgumentError("Region too small for bitmap");
57 }
58
59 // TODO: Implement pixel copying from src to atlas_bitmap_ at region
60 // coordinates For now, just return OK (stub implementation)
61
62 LOG_DEBUG("[TextureAtlas]",
63 "Packed %dx%d bitmap into region at (%d,%d) for source %d",
64 src.width(), src.height(), region.x, region.y, region.source_id);
65
66 return absl::OkStatus();
67}
68
69absl::Status TextureAtlas::DrawRegion(int source_id, int /*dest_x*/,
70 int /*dest_y*/) {
71 auto it = regions_.find(source_id);
72 if (it == regions_.end() || !it->second.in_use) {
73 return absl::NotFoundError("Region not found or not in use");
74 }
75
76 // TODO: Integrate with renderer to draw atlas region at (dest_x, dest_y)
77 // For now, just return OK (stub implementation)
78
79 return absl::OkStatus();
80}
81
82void TextureAtlas::FreeRegion(int source_id) {
83 auto it = regions_.find(source_id);
84 if (it != regions_.end()) {
85 it->second.in_use = false;
86 LOG_DEBUG("[TextureAtlas]", "Freed region for source %d", source_id);
87 }
88}
89
91 regions_.clear();
92 next_x_ = 0;
93 next_y_ = 0;
94 row_height_ = 0;
95 LOG_DEBUG("[TextureAtlas]", "Cleared all regions");
96}
97
99 auto it = regions_.find(source_id);
100 if (it != regions_.end() && it->second.in_use) {
101 return &it->second;
102 }
103 return nullptr;
104}
105
107 AtlasStats stats;
108 stats.total_pixels = width_ * height_;
109 stats.total_regions = regions_.size();
110
111 for (const auto& [id, region] : regions_) {
112 if (region.in_use) {
113 stats.used_regions++;
114 stats.used_pixels += region.width * region.height;
115 }
116 }
117
118 if (stats.total_pixels > 0) {
119 stats.utilization =
120 static_cast<float>(stats.used_pixels) / stats.total_pixels * 100.0f;
121 }
122
123 return stats;
124}
125
126bool TextureAtlas::TryPackRect(int width, int height, int& out_x, int& out_y) {
127 // Simple shelf packing algorithm
128 // Try to pack in current row
129 if (next_x_ + width <= width_) {
130 // Fits in current row
131 out_x = next_x_;
132 out_y = next_y_;
133 next_x_ += width;
134 row_height_ = std::max(row_height_, height);
135 return true;
136 }
137
138 // Move to next row
139 next_x_ = 0;
141 row_height_ = 0;
142
143 // Check if fits in new row
144 if (next_y_ + height <= height_ && width <= width_) {
145 out_x = next_x_;
146 out_y = next_y_;
147 next_x_ += width;
149 return true;
150 }
151
152 // Atlas is full
153 return false;
154}
155
156} // namespace gfx
157} // namespace yaze
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
bool is_active() const
Definition bitmap.h:384
int height() const
Definition bitmap.h:374
int width() const
Definition bitmap.h:373
AtlasStats GetStats() const
const AtlasRegion * GetRegion(int source_id) const
Get region for a specific source.
absl::Status PackBitmap(const Bitmap &src, const AtlasRegion &region)
Pack a bitmap into an allocated region.
void FreeRegion(int source_id)
Free a region and mark it as available.
int width() const
Get atlas dimensions.
TextureAtlas(int width=2048, int height=2048)
Construct texture atlas with specified dimensions.
void Clear()
Clear all regions and reset atlas.
absl::Status DrawRegion(int source_id, int dest_x, int dest_y)
Draw a region from the atlas to screen coordinates.
AtlasRegion * AllocateRegion(int source_id, int width, int height)
Allocate a region in the atlas for a source texture.
bool TryPackRect(int width, int height, int &out_x, int &out_y)
std::map< int, AtlasRegion > regions_
#define LOG_DEBUG(category, format,...)
Definition log.h:103
Region within the atlas texture.
Get atlas utilization statistics.