Skip to content

Commit

Permalink
Merge pull request EasyRPG#1018 from Ghabry/cache_image
Browse files Browse the repository at this point in the history
Improve Bitmap cache
  • Loading branch information
fdelapena authored Sep 15, 2016
2 parents 75b1a9f + 47a26d4 commit 1235fce
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
66 changes: 48 additions & 18 deletions src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,47 @@

namespace {
typedef std::pair<std::string,std::string> string_pair;

struct CacheItem {
BitmapRef bitmap;
uint32_t last_access;
};

typedef std::pair<std::string, int> tile_pair;

typedef std::map<string_pair, std::weak_ptr<Bitmap> > cache_type;
typedef std::map<string_pair, CacheItem> cache_type;
cache_type cache;

typedef std::map<tile_pair, std::weak_ptr<Bitmap> > cache_tiles_type;
cache_tiles_type cache_tiles;

static std::string system_name;
std::string system_name;

void FreeBitmapMemory() {
int32_t cur_ticks = DisplayUi->GetTicks();

for (auto& i : cache) {
if (i.second.bitmap.use_count() != 1) { continue; }

if (cur_ticks - i.second.last_access < 5000) {
// Last access < 5s
continue;
}

//Output::Debug("Freeing memory of %s/%s %d %d",
// i.first.first.c_str(), i.first.second.c_str(), i.second.last_access, cur_ticks);

i.second.bitmap.reset();
}
}

BitmapRef LoadBitmap(std::string const& folder_name, const std::string& filename,
bool transparent, uint32_t const flags) {
string_pair const key(folder_name, filename);

cache_type::const_iterator const it = cache.find(key);
cache_type::iterator const it = cache.find(key);

if (it == cache.end() || it->second.expired()) {
if (it == cache.end() || !it->second.bitmap) {
std::string const path = FileFinder::FindImage(folder_name, filename);

BitmapRef bmp = BitmapRef();
Expand All @@ -64,8 +88,13 @@ namespace {
}
}

return (cache[key] = bmp).lock();
} else { return it->second.lock(); }
FreeBitmapMemory();

return (cache[key] = {bmp, DisplayUi->GetTicks()}).bitmap;
} else {
it->second.last_access = DisplayUi->GetTicks();
return it->second.bitmap;
}
}

struct Material {
Expand Down Expand Up @@ -171,7 +200,7 @@ namespace {

BitmapRef bitmap = s.dummy_renderer();

return (cache[key] = bitmap).lock();
return (cache[key] = {bitmap, DisplayUi->GetTicks()}).bitmap;
}

template<Material::Type T>
Expand All @@ -198,6 +227,8 @@ namespace {
0));

if (!ret) {
Output::Warning("Image not found: %s/%s", s.directory, f.c_str());

return LoadDummyBitmap<T>(s.directory, f);
}

Expand Down Expand Up @@ -244,11 +275,14 @@ BitmapRef Cache::Picture(const std::string& f, bool trans) {
BitmapRef Cache::Exfont() {
string_pair const hash("ExFont","ExFont");

cache_type::const_iterator const it = cache.find(hash);
cache_type::iterator const it = cache.find(hash);

if (it == cache.end() || it->second.expired()) {
return(cache[hash] = Bitmap::Create(exfont_h, sizeof(exfont_h), true)).lock();
} else { return it->second.lock(); }
if (it == cache.end() || !it->second.bitmap) {
return(cache[hash] = {Bitmap::Create(exfont_h, sizeof(exfont_h), true), DisplayUi->GetTicks()}).bitmap;
} else {
it->second.last_access = DisplayUi->GetTicks();
return it->second.bitmap;
}
}

BitmapRef Cache::Tile(const std::string& filename, int tile_id) {
Expand Down Expand Up @@ -285,18 +319,14 @@ BitmapRef Cache::Tile(const std::string& filename, int tile_id) {
}

void Cache::Clear() {
for(cache_type::const_iterator i = cache.begin(); i != cache.end(); ++i) {
if(i->second.expired()) { continue; }
Output::Debug("possible leak in cached bitmap %s/%s",
i->first.first.c_str(), i->first.second.c_str());
}
cache.clear();

for(cache_tiles_type::const_iterator i = cache_tiles.begin(); i != cache_tiles.end(); ++i) {
if(i->second.expired()) { continue; }
for (cache_tiles_type::const_iterator i = cache_tiles.begin(); i != cache_tiles.end(); ++i) {
if (i->second.expired()) { continue; }
Output::Debug("possible leak in cached tilemap %s/%d",
i->first.first.c_str(), i->first.second);
}

cache_tiles.clear();
}

Expand Down
5 changes: 3 additions & 2 deletions src/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ void Plane::Draw() {
if (needs_refresh) {
needs_refresh = false;

if (!tone_bitmap) {
if (!tone_bitmap ||
bitmap->GetWidth() != tone_bitmap->GetWidth() ||
bitmap->GetHeight() != tone_bitmap->GetHeight()) {
tone_bitmap = Bitmap::Create(bitmap->GetWidth(), bitmap->GetHeight());
}

tone_bitmap->Clear();
tone_bitmap->ToneBlit(0, 0, *bitmap, bitmap->GetRect(), tone_effect, Opacity::opaque);
}
Expand Down
7 changes: 3 additions & 4 deletions src/sprite_battler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ Sprite_Battler::Sprite_Battler(Game_Battler* battler) :
flash_counter(0),
old_hidden(false),
idling(true) {

CreateSprite();
}

Sprite_Battler::~Sprite_Battler() {
Expand All @@ -51,8 +49,9 @@ void Sprite_Battler::SetBattler(Game_Battler* new_battler) {
}

void Sprite_Battler::Update() {
if (sprite_name != battler->GetSpriteName() ||
hue != battler->GetHue()) {
if (GetVisible() &&
(sprite_name != battler->GetSpriteName() ||
hue != battler->GetHue())) {

CreateSprite();
}
Expand Down

0 comments on commit 1235fce

Please sign in to comment.