86 lines
2.2 KiB
C
86 lines
2.2 KiB
C
/**
|
|
* 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"
|
|
|
|
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);
|
|
glBindTexture(GL_TEXTURE_2D, texture->id);
|
|
errorChain(errorGLCheck());
|
|
|
|
switch(format) {
|
|
case TEXTURE_FORMAT_RGBA:
|
|
glTexImage2D(
|
|
GL_TEXTURE_2D, 0, format, width, height, 0,
|
|
format, GL_UNSIGNED_BYTE, (void*)data.rgbaColors
|
|
);
|
|
break;
|
|
|
|
case TEXTURE_FORMAT_PALETTE:
|
|
assertNotNull(data.paletteData, "Palette texture data cannot be NULL");
|
|
glTexImage2D(
|
|
GL_TEXTURE_2D,
|
|
0, GL_COLOR_INDEX8_EXT,
|
|
width, height,
|
|
0, GL_COLOR_INDEX8_EXT,
|
|
GL_UNSIGNED_BYTE, (void*)data.paletteData
|
|
);
|
|
errorChain(errorGLCheck());
|
|
|
|
// glColorTableEXT(
|
|
// GL_TEXTURE_2D, GL_RGBA, data.palette.palette->colorCount, GL_RGBA,
|
|
// GL_UNSIGNED_BYTE, (const void*)data.palette.palette->colors
|
|
// );
|
|
break;
|
|
|
|
default:
|
|
assertUnreachable("Unknown texture format");
|
|
break;
|
|
}
|
|
errorChain(errorGLCheck());
|
|
|
|
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_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
errorChain(errorGLCheck());
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
errorChain(errorGLCheck());
|
|
|
|
errorOk();
|
|
}
|
|
|
|
errorret_t textureBindGL(texturegl_t *texture) {
|
|
if(texture == NULL) {
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
errorChain(errorGLCheck());
|
|
errorOk();
|
|
}
|
|
|
|
assertTrue(texture->id != 0, "Texture ID must be valid");
|
|
glBindTexture(GL_TEXTURE_2D, texture->id);
|
|
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();
|
|
} |