From 1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 18:05:18 +1100 Subject: initial commit --- src/client/render/texture.hh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/client/render/texture.hh (limited to 'src/client/render/texture.hh') 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 +#include + +#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 -- cgit v1.2.3