44 std::shared_ptr<SDL_Texture>
LoadTexture(SDL_Renderer* renderer, std::string filepath) {
45 if (!mTextureResources.contains(filepath)) {
47 auto extension = filepath.substr(filepath.find_last_of(
".") + 1);
51 if (extension ==
"bmp") {
52 pixels = SDL_LoadBMP(filepath.c_str());
54 if (extension ==
"png") {
55 imgFlags = IMG_INIT_PNG;
56 }
else if (extension ==
"jpg" || extension ==
"jpeg") {
57 imgFlags = IMG_INIT_JPG;
58 }
else if (extension ==
"avif") {
59 imgFlags = IMG_INIT_AVIF;
60 }
else if (extension ==
"jxl") {
61 imgFlags = IMG_INIT_JXL;
62 }
else if (extension ==
"tif") {
63 imgFlags = IMG_INIT_TIF;
64 }
else if (extension ==
"webp") {
65 imgFlags = IMG_INIT_WEBP;
67 SDL_Log(
"Unsupported image format %s", extension.c_str());
70 if( !(IMG_Init(imgFlags) & imgFlags)) {
71 SDL_Log(
"SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
74 pixels = IMG_Load(filepath.c_str());
78 SDL_Log(
"Failed to load image %s", filepath.c_str());
81 SDL_SetColorKey(pixels, SDL_TRUE, SDL_MapRGB(pixels->format,0xFF,0,0xFF));
82 std::shared_ptr<SDL_Texture> texture = make_shared_texture(renderer, pixels);
83 mTextureResources.insert({filepath, texture});
84 SDL_FreeSurface(pixels);
85 SDL_Log(
"Created new resource %s", filepath.c_str());
87 SDL_Log(
"Reused resource %s", filepath.c_str());
90 return mTextureResources[filepath];