78 lines
1.9 KiB
C
78 lines
1.9 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "texture.h"
|
|
#include "assert/assert.h"
|
|
#include "util/memory.h"
|
|
#include "util/math.h"
|
|
|
|
void textureInit(
|
|
texture_t *texture,
|
|
const int32_t width,
|
|
const int32_t height,
|
|
const GLenum format,
|
|
const uint8_t *data
|
|
) {
|
|
assertNotNull(texture, "Texture cannot be NULL");
|
|
assertTrue(width > 0 && height > 0, "Width and height must be greater than 0");
|
|
|
|
#if PSP
|
|
assertTrue(
|
|
width == mathNextPowTwo(width),
|
|
"Width must be powers of 2 for PSP"
|
|
);
|
|
assertTrue(
|
|
height == mathNextPowTwo(height),
|
|
"Height must be powers of 2 for PSP"
|
|
);
|
|
#endif
|
|
|
|
memoryZero(texture, sizeof(texture_t));
|
|
texture->width = width;
|
|
texture->height = height;
|
|
|
|
glGenTextures(1, &texture->id);
|
|
glBindTexture(GL_TEXTURE_2D, texture->id);
|
|
glTexImage2D(
|
|
GL_TEXTURE_2D, 0, format, width, height, 0,
|
|
format, GL_UNSIGNED_BYTE, data
|
|
);
|
|
|
|
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_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|
|
void textureBind(const texture_t *texture) {
|
|
if(texture == NULL) {
|
|
glDisable(GL_TEXTURE_2D);
|
|
// glBindTexture(GL_TEXTURE_2D, 0);
|
|
return;
|
|
}
|
|
|
|
assertTrue(
|
|
texture->id != 0,
|
|
"Texture ID must not be 0"
|
|
);
|
|
assertTrue(
|
|
texture->width > 0 && texture->height > 0,
|
|
"Texture width and height must be greater than 0"
|
|
);
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
glBindTexture(GL_TEXTURE_2D, texture->id);
|
|
}
|
|
|
|
void textureDispose(texture_t *texture) {
|
|
assertNotNull(texture, "Texture cannot be NULL");
|
|
assertTrue(texture->id != 0, "Texture ID must not be 0");
|
|
|
|
glDeleteTextures(1, &texture->id);
|
|
} |