diff options
Diffstat (limited to 'src/image.cpp')
-rw-r--r-- | src/image.cpp | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/image.cpp b/src/image.cpp index ec0b37d..f9fb1e2 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,11 +1,49 @@ #include "FLD.hpp" +static FLD::Image& +invalid_image() +{ + static FLD::Image image = { .valid = false }; + return image; +} + FLD::Image& FLD::image(const std::string &path) { if (images.contains(path)) return images[path]; - static Image invalid_image = { .valid = false }; - return invalid_image; + const auto surf = SDL_LoadBMP(path.c_str()); + if (surf == nullptr) { + sdlError(path); + return invalid_image(); + } + + const auto tex = SDL_CreateTextureFromSurface(_renderer, surf); + SDL_FreeSurface(surf); + if (tex == nullptr) { + sdlError(path); + return invalid_image(); + } + + if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) { + SDL_DestroyTexture(tex); + sdlError(path); + return invalid_image(); + } + + int width, height; + if (SDL_QueryTexture(tex, nullptr, nullptr, &width, &height) < 0) { + SDL_DestroyTexture(tex); + sdlError(path); + return invalid_image(); + } + + images[path] = { + .ptr = tex, + .width = width, + .height = height + }; + + return images[path]; } |