/** * Copyright (c) 2026 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "display/texture/texture.h" #include "assert/assert.h" #include "error/errorgl.h" #include "util/memory.h" errorret_t textureInitGL( texturegl_t *texture, const int32_t width, const int32_t height, const textureformatgl_t format, const texturedata_t data ) { glGenTextures(1, &texture->id); errorChain(errorGLCheck()); glBindTexture(GL_TEXTURE_2D, texture->id); errorChain(errorGLCheck()); switch(format) { case TEXTURE_FORMAT_RGBA: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)data.rgbaColors ); errorChain(errorGLCheck()); break; case TEXTURE_FORMAT_PALETTE: texture->palette = data.paletted.palette; assertTrue( texture->palette == &PALETTES[0], "Only the first palette is supported in legacy opengl." ); #ifdef DUSK_OPENGL_LEGACY glColorTableEXT( GL_TEXTURE_2D, GL_RGBA, texture->palette->count, GL_RGBA, GL_UNSIGNED_BYTE, (const void*)texture->palette->colors ); errorChain(errorGLCheck()); glTexImage2D( GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, width, height, 0, GL_COLOR_INDEX8_EXT, GL_UNSIGNED_BYTE, (void*)data.paletted.indices ); errorChain(errorGLCheck()); #else // For modern systems we send to only the R channel and the shader does // the rest. glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, (void*)data.paletted.indices ); errorChain(errorGLCheck()); #endif break; default: assertUnreachable("Unknown texture format"); break; } errorChain(errorGLCheck()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); errorChain(errorGLCheck()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); errorChain(errorGLCheck()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); errorChain(errorGLCheck()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); errorChain(errorGLCheck()); glBindTexture(GL_TEXTURE_2D, 0); errorChain(errorGLCheck()); errorOk(); } errorret_t textureDisposeGL(texturegl_t *texture) { assertNotNull(texture, "Texture cannot be NULL"); assertTrue(texture->id != 0, "Texture ID must be valid"); glDeleteTextures(1, &texture->id); errorChain(errorGLCheck()); errorOk(); }