summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-16 03:48:01 +0200
committerkdx <kikoodx@paranoici.org>2023-07-16 03:48:01 +0200
commit2491fd4ccf6070848c515aab35170c4013b5a23e (patch)
treefb587c16ef60ba8fd6f193d5a635c317f8754df4
parentf1e3e2f0a272cbfb19fc9dc1ad65aa920ab7b9f1 (diff)
downloadfld-2491fd4ccf6070848c515aab35170c4013b5a23e.tar.gz
image loading
-rw-r--r--demo.cpp2
-rw-r--r--res/smile.bmpbin0 -> 12426 bytes
-rw-r--r--src/FLD.hpp6
-rw-r--r--src/destructor.cpp3
-rw-r--r--src/image.cpp42
-rw-r--r--src/sdlError.cpp4
6 files changed, 51 insertions, 6 deletions
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
--- /dev/null
+++ b/res/smile.bmp
Binary files 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<const std::string, Image> 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 <SDL2/SDL_log.h>
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;
}