// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "Texture.hpp" using namespace Dawn; void Texture::bind(textureslot_t slot) { assertTrue(this->id != -1); glActiveTexture(GL_TEXTURE0 + slot); glBindTexture(GL_TEXTURE_2D, this->id); } int32_t Texture::getWidth() { return this->width; } int32_t Texture::getHeight() { return this->height; } void Texture::setSize(int32_t width, int32_t height) { if(this->id != -1) glDeleteTextures(1, &this->id); this->width = width; this->height = height; glGenTextures(1, &this->id); if(this->id <= 0) throw "Texture generation failed!"; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, this->id); // Setup our preferred texture params, later this will be configurable. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Initialize the texture to blank glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL ); } void Texture::fill(struct Color color) { struct Color *pixels = (struct Color *)memoryAllocate( sizeof(struct Color) * this->width * this->height ); this->buffer(pixels); memoryFree(pixels); } bool_t Texture::isReady() { return this->id != -1; } void Texture::buffer(struct Color pixels[]) { glBindTexture(GL_TEXTURE_2D, this->id); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)pixels ); } Texture::~Texture() { if(this->id != -1) glDeleteTextures(1, &this->id); }