AMay d8f4bf9c4c
Some checks failed
Emscripten Build / Build_and_Deploy_Web_Build (push) Successful in 8m31s
Emscripten Build / UnitTesting (push) Failing after 9m11s
Fix a bug with a rare chance to crash the game whenever transitioning to the overworld map stage. Fix asset references to point to new assets locations. Release Build 13843.
2026-06-03 11:56:15 -05:00

112 lines
5.3 KiB
C++

#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2026 Amy Sigona <sigonasr2@gmail.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#include"AdventuresInLestoria.h"
#include"DEFINES.h"
#include"GraphicsManager.h"
INCLUDE_game
INCLUDE_PACK_KEY
GraphicsManager GraphicsManager::self;
GraphicsManager::ForcedGraphicsManager GraphicsManager::ForcedGraphicsManager::self{};
ImgData::ImgData():lastAccessTime(game->GetRunTime()){}
void GraphicsManager::Initialize(){}
const Renderable&GraphicsManager::operator[](const ImgFilename&filename,const std::source_location&loc){
if(!gfx.contains(filename)){
gfx[filename].renderable.Load(blankImgFilename,&game->gamepack);
imgLoadQueue.emplace(filename,loc);
}
gfx[filename].lastAccessTime=game->GetRunTime();
return gfx[filename].renderable;
}
const Renderable&GraphicsManager::ForcedGraphicsManager::operator[](const ImgFilename&filename){
if(!GFX.gfx.contains(filename)){
if(GFX.LoadResource(GFX.gfx[filename].renderable,filename)!=rcode::OK)ERR("WARNING! Failed to instant load requested resource "<<filename<<"! THIS SHOULD NOT BE HAPPENING!!!")
GFX.currentImgLoadedSize+=GFX.gfx[filename].renderable.Sprite()->Size().area()*4;
LOG("Instant Requested and loaded image "<<filename<<". Total: "<<GFX.currentImgLoadedSize<<"/"<<GFX.imgLoadCapacity<<" bytes");
}
GFX.gfx[filename].lastAccessTime=game->GetRunTime();
return GFX.gfx[filename].renderable;
}
void GraphicsManager::LoadImageInQueue(){
while(imgLoadQueue.size()>0){
const auto&[imgToLoad,sourceLoc]{imgLoadQueue.front()};
LOG("Image load request "<<imgToLoad<<".");
try{
if(const rcode result{LoadResource(gfx[imgToLoad].renderable,imgToLoad)};result!=rcode::OK)throw std::runtime_error{std::format("Failed to load resource with result {}",int(result))};
}catch(std::exception&exception)ERR("WARNING! Failed to load requested resource "<<imgToLoad<<"! Error message:"<<exception.what()<<" THIS SHOULD NOT BE HAPPENING!!! "<<sourceLoc.file_name()<<" ("<<sourceLoc.line()<<":"<<sourceLoc.column()<<"):"<<sourceLoc.function_name());
currentImgLoadedSize+=(gfx[imgToLoad].renderable).Sprite()->Size().area()*4;
LOG("Requested and loaded image "<<imgToLoad<<". Total: "<<currentImgLoadedSize<<"/"<<imgLoadCapacity<<" bytes");
imgLoadQueue.pop();
}
}
void GraphicsManager::FreeUpMemory(){
//Two strategies are performed here. Cleanup until capacity < imgLoadCapacity with priority of clearing out images unused over the longest period of time first.
using LastAccessedTime=double;
static std::vector<std::pair<ImgFilename,LastAccessedTime>>gfxByAccessTime;
for(auto&val:gfx)gfxByAccessTime.emplace_back(val.first,val.second.lastAccessTime);
std::ranges::sort(gfxByAccessTime,[](std::pair<ImgFilename,LastAccessedTime>&val1,std::pair<ImgFilename,LastAccessedTime>&val2){return val1.second<val2.second;});
for(auto&[filename,lastAccessTime]:gfxByAccessTime){
if(currentImgLoadedSize>imgLoadCapacity){
currentImgLoadedSize-=(gfx[filename].renderable).Sprite()->Size().area();
gfx.erase(filename);
LOG("Deleted image "<<filename<<". Total: "<<currentImgLoadedSize<<"/"<<imgLoadCapacity<<" bytes");
}else break;
}
}
void GraphicsManager::Reset(){
currentImgLoadedSize=0;
gfx.clear();
GraphicsManager::Initialize();
}
rcode GraphicsManager::LoadResource(Renderable&renderable,std::string_view imgPath,bool filter,bool clamp){
rcode returnCode;
if(game->gamepack.Loaded()&&game->gamepack.FileExists(std::string{imgPath})){
returnCode=renderable.Load(std::string{imgPath},&game->gamepack,filter,clamp);
}else{
returnCode=renderable.Load(std::string{imgPath},nullptr,filter,clamp);
if("GENERATE_GAMEPACK"_B){
game->gamepack.AddFile(std::string{imgPath});
}
}
return returnCode;
}