Skip to content

Commit 54f7ce3

Browse files
committed
Invalidate the Picture cache when a picture is written to the disk
Otherwise an old version of the Picture will be loaded next time ShowPicture is used
1 parent 9e7fe47 commit 54f7ce3

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/cache.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ namespace {
248248
BitmapRef bmp;
249249

250250
const auto key = MakeHashKey(s.directory, filename, transparent, extra_flags);
251+
251252
auto it = cache.find(key);
252253
if (it == cache.end()) {
253254
if (filename == CACHE_DEFAULT_BITMAP) {
@@ -529,6 +530,16 @@ BitmapRef Cache::SpriteEffect(const BitmapRef& src_bitmap, const Rect& rect, boo
529530
} else { return it->second.lock(); }
530531
}
531532

533+
void Cache::Invalidate(std::string_view section) {
534+
for (auto it = cache.begin(); it != cache.end(); ) {
535+
if (StartsWith(it->first, section)) {
536+
it = cache.erase(it);
537+
} else {
538+
++it;
539+
}
540+
}
541+
}
542+
532543
void Cache::Clear() {
533544
cache_effects.clear();
534545
cache.clear();

src/cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ namespace Cache {
5858
BitmapRef Tile(std::string_view filename, int tile_id);
5959
BitmapRef SpriteEffect(const BitmapRef& src_bitmap, const Rect& rect, bool flip_x, bool flip_y, const Tone& tone, const Color& blend);
6060

61+
/**
62+
* Removes all cached entries of the given section
63+
*
64+
* @param section Cache section to remove
65+
*/
66+
void Invalidate(std::string_view section);
6167
void Clear();
6268
void ClearAll();
6369

src/game_interpreter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5543,6 +5543,8 @@ bool Game_Interpreter::CommandManiacWritePicture(lcf::rpg::EventCommand const& c
55435543
auto img_out = FileFinder::OpenWrite(filename);
55445544
if (img_out) {
55455545
bitmap->WritePNG(img_out);
5546+
// Not ideal but figuring out the exact cache entry is complicated
5547+
Cache::Invalidate("Picture");
55465548
} else {
55475549
Output::Warning("ManiacSaveImage: Failed to open file for writing: {}", filename);
55485550
}

0 commit comments

Comments
 (0)