From 2491fd4ccf6070848c515aab35170c4013b5a23e Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 16 Jul 2023 03:48:01 +0200 Subject: image loading --- demo.cpp | 2 +- res/smile.bmp | Bin 0 -> 12426 bytes src/FLD.hpp | 6 +++++- src/destructor.cpp | 3 +++ src/image.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- src/sdlError.cpp | 4 ++-- 6 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 res/smile.bmp diff --git a/demo.cpp b/demo.cpp index 536d055..f335402 100644 --- a/demo.cpp +++ b/demo.cpp @@ -12,7 +12,7 @@ main_loop(FLD& fld, [[maybe_unused]] void *udata) fld.drawSetColor(FLD::WHITE); fld.drawRectangle({ 16, 32, 64, 32 }); - fld.image("coucou.png").draw({ + fld.image("res/smile.bmp").draw({ .p = {}, .sx = 1.5f, .sy = 0.5f, diff --git a/res/smile.bmp b/res/smile.bmp new file mode 100644 index 0000000..733a0a3 Binary files /dev/null and b/res/smile.bmp differ diff --git a/src/FLD.hpp b/src/FLD.hpp index 5cfd0cb..9abe34a 100644 --- a/src/FLD.hpp +++ b/src/FLD.hpp @@ -49,6 +49,10 @@ public: }; struct Image { bool valid = true; + SDL_Texture *ptr = nullptr; + int width = 0; + int height = 0; + Error invalid() const; Error draw(const DrawImageOpts& opts) const; }; @@ -107,7 +111,7 @@ private: } transform; std::map images = {}; - Error sdlError() const; + Error sdlError(const std::string& msg="") const; [[nodiscard]] Error initBasepath() const; [[nodiscard]] Error initWindow(); [[nodiscard]] Error initRenderer(); diff --git a/src/destructor.cpp b/src/destructor.cpp index 7f1c906..1adcb65 100644 --- a/src/destructor.cpp +++ b/src/destructor.cpp @@ -3,6 +3,9 @@ FLD::~FLD() { + for (auto [_, e] : images) + if (e.ptr != nullptr) + SDL_DestroyTexture(e.ptr); if (_target != nullptr) { SDL_DestroyTexture(_target); _target = nullptr; 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]; } diff --git a/src/sdlError.cpp b/src/sdlError.cpp index e77edcc..6d06b0a 100644 --- a/src/sdlError.cpp +++ b/src/sdlError.cpp @@ -2,8 +2,8 @@ #include FLD::Error -FLD::sdlError() const +FLD::sdlError(const std::string& msg) const { - SDL_Log("SDL runtime error: %s", SDL_GetError()); + SDL_Log("SDL runtime error (%s): %s", msg.c_str(), SDL_GetError()); return -1; } -- cgit v1.2.3