diff --git a/src/cinnabar-engine/cinnabar-render/texture.cpp b/src/cinnabar-engine/cinnabar-render/texture.cpp index 1768e199..8e2bd2ad 100644 --- a/src/cinnabar-engine/cinnabar-render/texture.cpp +++ b/src/cinnabar-engine/cinnabar-render/texture.cpp @@ -18,17 +18,24 @@ ce::Texture::Texture(std::string filename, GLenum type) glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_REPEAT); - if (textureFile.data) { - glTexImage2D(type, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, - GL_UNSIGNED_BYTE, textureFile.data); - glGenerateMipmap(type); + + if (this->loadData(textureFile.data, textureFile.width, textureFile.height, GL_RGBA, type)) { LOG_SUCCESS("Loaded texture: %s", filename.c_str()); } else LOG_ERROR("TEXTURE_LOADING_FAILED: %s", filename.c_str()); - unbind(); ce::assetManager::freeTextureFile(textureFile); } +ce::Texture::Texture(const void* data, GLsizei width, GLsizei height, GLenum color_space, GLenum type) + : m_width(0), m_height(0), m_channelCount(0), m_type(type) { + + if (this->loadData(data, width, height, color_space, type)) { + LOG_SUCCESS("Loaded texture"); + } else { + LOG_ERROR("TEXTURE_LOADING_FAILED"); + } +} + ce::Texture::~Texture() { glDeleteTextures(1, &m_texture); } @@ -48,3 +55,20 @@ void ce::Texture::unbind() { glActiveTexture(0); glBindTexture(m_type, 0); } + +bool ce::Texture::loadData(const void* data, GLsizei width, GLsizei height, GLenum color_space, GLenum type) { + m_width = width; + m_height = height; + + bool out = false; + bind(); + + if (data) { + glTexImage2D(type, 0, color_space, m_width, m_height, 0, color_space, + GL_UNSIGNED_BYTE, data); + glGenerateMipmap(type); + out = true; + } + unbind(); + return out; +} diff --git a/src/cinnabar-engine/cinnabar-render/texture.hpp b/src/cinnabar-engine/cinnabar-render/texture.hpp index 15974a8a..c9ff1d49 100644 --- a/src/cinnabar-engine/cinnabar-render/texture.hpp +++ b/src/cinnabar-engine/cinnabar-render/texture.hpp @@ -1,12 +1,13 @@ #pragma once -#include #include "asset_manager.hpp" +#include namespace ce { class Texture { public: Texture(std::string filename, GLenum type = GL_TEXTURE_2D); + Texture(const void* data, GLsizei width, GLsizei height, GLenum color_space = GL_RGBA, GLenum type = GL_TEXTURE_2D); ~Texture(); void bind(), unbind(), activate(int slot); @@ -15,5 +16,6 @@ namespace ce { GLuint m_texture; int m_width, m_height, m_channelCount; unsigned int m_type; + bool loadData(const void* data, GLsizei width, GLsizei height, GLenum color_space = GL_RGBA, GLenum type = GL_TEXTURE_2D); }; }