yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
map_texture_coordinator.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <new>
5
9#include "util/log.h"
11
12namespace yaze::editor {
13
14namespace {
15
17 return ctx.overworld && ctx.maps_bmp;
18}
19
20} // namespace
21
23 if (!ctx_.maps_bmp) {
24 return;
25 }
26 for (auto& bitmap : *ctx_.maps_bmp) {
27 bitmap = gfx::Bitmap();
28 }
29}
30
32 if (ctx_.renderer) {
34 }
35 if (!HasTextureContext(ctx_) || !ctx_.current_map || !ctx_.current_world ||
37 return;
38 }
39
40 int refresh_count = 0;
41 constexpr int kMaxRefreshesPerFrame = 2;
42
43 const auto try_refresh_map = [&](int map_index) {
44 if (map_index < 0 || map_index >= ctx_.overworld->inspected_map_count() ||
45 refresh_count >= kMaxRefreshesPerFrame) {
46 return;
47 }
48 auto& bitmap = (*ctx_.maps_bmp)[map_index];
49 if (bitmap.modified() && bitmap.is_active()) {
50 ctx_.refresh_map_on_demand(map_index);
51 ++refresh_count;
52 }
53 };
54
55 try_refresh_map(*ctx_.current_map);
56
57 const int current_world = std::clamp(*ctx_.current_world, 0, 2);
58 const int world_start = current_world * 0x40;
59 const int world_end =
60 std::min(world_start + 0x40, ctx_.overworld->inspected_map_count());
61 for (int map_index = world_start;
62 map_index < world_end && refresh_count < kMaxRefreshesPerFrame;
63 ++map_index) {
64 if (map_index == *ctx_.current_map) {
65 continue;
66 }
67 try_refresh_map(map_index);
68 }
69}
70
72 if (!HasTextureContext(ctx_) || map_index < 0 ||
73 map_index >= ctx_.overworld->inspected_map_count()) {
74 return;
75 }
76
77 const auto* existing_map = ctx_.overworld->overworld_map(map_index);
78 const bool was_built = existing_map && existing_map->is_built();
79
80 auto status = ctx_.overworld->EnsureMapBuilt(map_index);
81 if (!status.ok()) {
82 LOG_ERROR("OverworldMapTextureCoordinator", "Failed to build map %d: %s",
83 map_index, status.message());
84 return;
85 }
86
87 auto& bitmap = (*ctx_.maps_bmp)[map_index];
88 const auto* map = ctx_.overworld->overworld_map(map_index);
89 if (!map) {
90 return;
91 }
92 // A modified editor bitmap can be newer than a built map's cached pixels.
93 // Preserve it and its dirty flag until the content refresh rebuilds the map.
94 const bool needs_bitmap_sync = !was_built;
95
96 if (!bitmap.is_active()) {
97 const auto& palette = map->current_palette();
98 try {
99 bitmap.Create(kOverworldMapSize, kOverworldMapSize, 8,
100 map->bitmap_data());
101 bitmap.SetPalette(palette);
102 } catch (const std::bad_alloc& e) {
103 LOG_ERROR("OverworldMapTextureCoordinator",
104 "Error allocating bitmap for map %d: %s", map_index, e.what());
105 return;
106 }
107 } else if (needs_bitmap_sync) {
108 bitmap.set_data(map->bitmap_data());
109 bitmap.SetPalette(map->current_palette());
110 bitmap.set_modified(false);
111 }
112
113 if (!bitmap.texture() && bitmap.is_active()) {
116 } else if (needs_bitmap_sync && bitmap.texture() && bitmap.is_active()) {
119 }
120}
121
123 int world, bool process_texture_queue) {
124 if (!HasTextureContext(ctx_)) {
125 return;
126 }
127
128 const int clamped_world = std::clamp(world, 0, 2);
129 const int world_start = clamped_world * 0x40;
130 const int world_end =
131 std::min(world_start + 0x40, ctx_.overworld->inspected_map_count());
132 if (world_start >= ctx_.overworld->inspected_map_count()) {
133 return;
134 }
135
136 const int current_world = ctx_.current_world ? *ctx_.current_world : 0;
137 const int current_map = ctx_.current_map ? *ctx_.current_map : world_start;
138 const int current_local_map =
139 (current_world == clamped_world) ? (current_map & 0x3F) : 0;
140 const int first_map = world_start + std::clamp(current_local_map, 0,
141 world_end - world_start - 1);
142 constexpr int kMaxMapsToPrime = 8;
143 const int prime_end = std::min(first_map + kMaxMapsToPrime, world_end);
144 for (int map_index = first_map; map_index < prime_end; ++map_index) {
145 EnsureMapTexture(map_index);
146 }
147
148 if (process_texture_queue) {
150 }
151}
152
153} // namespace yaze::editor
void PrimeWorldMaps(int world, bool process_texture_queue=false)
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:39
void ProcessTextureQueue(IRenderer *renderer)
Definition arena.cc:211
static Arena & Get()
Definition arena.cc:24
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
int inspected_map_count() const
Definition overworld.h:433
auto overworld_map(int i) const
Definition overworld.h:737
absl::Status EnsureMapBuilt(int map_index)
Build a map on-demand if it hasn't been built yet.
#define LOG_ERROR(category, format,...)
Definition log.h:140
Editors are the view controllers for the application.
constexpr unsigned int kOverworldMapSize
std::function< void(int map_index)> refresh_map_on_demand
std::array< gfx::Bitmap, zelda3::kExpandedMapCount > * maps_bmp