From 7b268efa6a4a893e64c2cad18a05aa633b5aebe1 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Sun, 9 Jan 2022 08:54:37 +0100 Subject: [PATCH] i dont even know --- include/lol/Image.hpp | 21 ++++----------------- src/Image.cpp | 41 +++++++++++++++++++---------------------- src/Texture.cpp | 3 ++- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/include/lol/Image.hpp b/include/lol/Image.hpp index 96e7d06..6171b2e 100644 --- a/include/lol/Image.hpp +++ b/include/lol/Image.hpp @@ -4,6 +4,7 @@ #include #include +#include namespace lol { @@ -13,7 +14,7 @@ namespace lol * This class is used to store image pixel- and metadata as well as load * image files from disk. It has no OpenGL equivalent */ - class Image + class Image : public NonCopyable { public: /** @@ -42,24 +43,10 @@ namespace lol */ Image(const std::string& filepath); + Image(unsigned char* buffer, size_t len); + ~Image(); - /** - * @brief Copy data of another Image - * - * @param other The Image to copy from - */ - Image(const Image& other); - - /** - * @brief Assign this Image data of another Image - * - * @param other The Image to assign from - * @return The modified Image - */ - Image& operator=(const Image& other); - - /** * @brief Get the dimensions of the image * diff --git a/src/Image.cpp b/src/Image.cpp index 1cf96e1..63ffb49 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -35,31 +35,28 @@ namespace lol } } + Image::Image(unsigned char* buffer, size_t len) + { + int width, height, channels; + pixels = stbi_load_from_memory(buffer, len, &width, &height, &channels, 0); + + size = glm::uvec2(width, height); + type = PixelType::UByte; + switch (channels) + { + case 1: format = PixelFormat::R; break; + case 2: format = PixelFormat::RG; break; + case 3: format = PixelFormat::RGB; break; + case 4: format = PixelFormat::RGBA; break; + default: + format = PixelFormat::RGB; + break; + } + } + Image::~Image() { if(pixels != nullptr) stbi_image_free(pixels); } - - Image::Image(const Image& other) - { - size = other.size; - format = other.format; - type = other.type; - - pixels = new uint8_t[size.x * size.y * SizeOf(type)]; - *pixels = *other.pixels; - } - - Image& Image::operator=(const Image& other) - { - size = other.size; - format = other.format; - type = other.type; - - pixels = new uint8_t[size.x * size.y * SizeOf(type)]; - *pixels = *other.pixels; - - return *this; - } } \ No newline at end of file diff --git a/src/Texture.cpp b/src/Texture.cpp index 6b19345..50a7b49 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -52,7 +52,8 @@ namespace lol Texture(TargetTexture::Texture2D) { glm::uvec2 imageSize = image.GetDimensions(); - glTexImage2D(NATIVE(target), 0, NATIVE(texFormat), imageSize.x, imageSize.y, 0, NATIVE(image.GetPixelFormat()), NATIVE(image.GetPixelType()), image.GetPixels()); + uint8_t* pixels = image.GetPixels(); + glTexImage2D(NATIVE(target), 0, NATIVE(texFormat), imageSize.x, imageSize.y, 0, NATIVE(image.GetPixelFormat()), NATIVE(image.GetPixelType()), pixels); glGenerateMipmap(NATIVE(target)); }