| 93 | size_t cache_size = 0; |
| 94 | |
| 95 | void FreeBitmapMemory() { |
| 96 | auto cur_ticks = Game_Clock::GetFrameTime(); |
| 97 | |
| 98 | for (auto it = cache.begin(); it != cache.end();) { |
| 99 | if (it->second.bitmap.use_count() != 1) { |
| 100 | // Bitmap is referenced |
| 101 | ++it; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | auto last_access = cur_ticks - it->second.last_access; |
| 106 | bool cache_exhausted = cache_size > cache_limit; |
| 107 | if (cache_exhausted) { |
| 108 | if (last_access <= 50ms) { |
| 109 | // Used during the last 3 frames, must be important, keep it. |
| 110 | ++it; |
| 111 | continue; |
| 112 | } |
| 113 | } else if (last_access <= 3s) { |
| 114 | ++it; |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | #ifdef CACHE_DEBUG |
| 119 | Output::Debug("Freeing memory of {}/{}", std::get<0>(i.first), std::get<1>(i.first)); |
| 120 | #endif |
| 121 | |
| 122 | cache_size -= it->second.bitmap->GetSize(); |
| 123 | |
| 124 | it = cache.erase(it); |
| 125 | } |
| 126 | |
| 127 | #ifdef CACHE_DEBUG |
| 128 | Output::Debug("Bitmap cache size: {}", cache_size / 1024.0 / 1024); |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | BitmapRef AddToCache(const std::string& key, BitmapRef bmp) { |
| 133 | if (bmp) { |
no test coverage detected