aboutsummaryrefslogtreecommitdiff
path: root/src/client/render/texture.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/render/texture.hh')
-rw-r--r--src/client/render/texture.hh37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/client/render/texture.hh b/src/client/render/texture.hh
new file mode 100644
index 0000000..bc3d008
--- /dev/null
+++ b/src/client/render/texture.hh
@@ -0,0 +1,37 @@
+#ifndef CLIENT_RENDER_TEXTURE_HH_
+#define CLIENT_RENDER_TEXTURE_HH_
+
+#include <stdexcept>
+#include <string>
+
+#include "client/render/lib/stb_image/stb_image.hh"
+
+namespace client {
+namespace render {
+
+// Automatic freeing of a texture from stb image.
+struct texture {
+ unsigned char* image;
+ int width;
+ int height;
+ int channels;
+
+ texture(const std::string& dir, const bool flip = false) {
+ stbi_set_flip_vertically_on_load(flip);
+ this->image = stbi_load(dir.c_str(), &this->width, &this->height,
+ &this->channels, 0);
+
+ if (this->image == nullptr) {
+ throw std::runtime_error("stbi failed to load texture \"" + dir +
+ '\"');
+ }
+ }
+ texture(const texture&) = delete;
+ texture(texture&&) = delete;
+ ~texture() noexcept { stbi_image_free(image); }
+};
+
+} // namespace render
+} // namespace client
+
+#endif