MCPcopy Create free account
hub / github.com/EasyRPG/Player / LoadBitmap

Function LoadBitmap

src/cache.cpp:239–328  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

237
238 template<Material::Type T>
239 BitmapRef LoadBitmap(std::string_view filename, bool transparent) {
240 static_assert(Material::REND < T && T < Material::END, "Invalid material.");
241 const Spec& s = spec[T];
242
243 // This assert is triggered by the request cache clear when switching languages
244 // Remove comment to test if all assets are requested correctly
245 //auto* req = AsyncHandler::RequestFile(s.directory, filename);
246 //assert(req != nullptr && req->IsReady());
247
248 BitmapRef bmp;
249
250 const auto key = MakeHashKey(s.directory, filename, transparent);
251 auto it = cache.find(key);
252 if (it == cache.end()) {
253 if (filename == CACHE_DEFAULT_BITMAP) {
254 bmp = LoadDummyBitmap<T>(s.directory, filename, true);
255 }
256
257 if (!bmp) {
258 auto is = FileFinder::OpenImage(s.directory, filename);
259
260 FreeBitmapMemory();
261
262 if (!is) {
263 if (s.warn_missing) {
264 Output::Warning("Image not found: {}/{}", s.directory, filename);
265 } else {
266 Output::Debug("Image not found: {}/{}", s.directory, filename);
267 bmp = CreateEmpty<T>();
268 }
269 } else {
270 auto flags = Bitmap::Flag_ReadOnly | (
271 T == Material::Chipset ? Bitmap::Flag_Chipset :
272 T == Material::System ? Bitmap::Flag_System : 0);
273 bmp = Bitmap::Create(std::move(is), transparent, flags);
274 if (!bmp) {
275 Output::Warning("Invalid image: {}/{}", s.directory, filename);
276 } else {
277 if (bmp->GetOriginalBpp() > 8) {
278 // FIXME: This HasActiveTranslation check will also load 32 bit images in the game directory when
279 // a translation is active and our API does not expose whether the asset was redirected or not.
280 if (!Player::HasEasyRpgExtensions() && !Player::IsPatchManiac() && !Tr::HasActiveTranslation()) {
281 Output::Warning("Image {}/{} has a bit depth of {} that is not supported by RPG_RT. Enable EasyRPG Extensions or Maniac Patch to load such images.", s.directory, filename, bmp->GetOriginalBpp());
282 bmp.reset();
283 }
284 }
285 }
286 }
287 }
288
289 if (!bmp) {
290 // Even for images without "warn_missing" this still creates a checkboard for invalid images
291 bmp = LoadDummyBitmap<T>(s.directory, filename, transparent);
292 }
293
294 bmp = AddToCache(key, bmp);
295 } else {
296 it->second.last_access = Game_Clock::GetFrameTime();

Callers

nothing calls this directly

Calls 9

MakeHashKeyFunction · 0.85
FreeBitmapMemoryFunction · 0.85
AddToCacheFunction · 0.85
GetOriginalBppMethod · 0.80
endMethod · 0.45
resetMethod · 0.45
GetWidthMethod · 0.45
GetHeightMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected