/** * Copyright (c) 2021 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "texture.h" texture_t * textureCreate(int32_t width, int32_t height, pixel_t *pixels) { texture_t *texture = malloc(sizeof(texture_t)); if(texture == NULL) return NULL; texture->width = width; texture->height = height; // Generate a texture ID and bind. glGenTextures(1, &texture->id); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture->id); // Setup our preferred texture params 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); // Start by buffering all transparent black pixels. if(pixels == NULL) { pixels = calloc(width * height, sizeof(pixel_t)); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); free(pixels); } else { glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); } glBindTexture(GL_TEXTURE_2D, 0); return texture; } void textureBufferPixels(texture_t *texture, int32_t x, int32_t y, int32_t width, int32_t height, pixel_t *pixels ) { glBindTexture(GL_TEXTURE_2D, texture->id); glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); } void textureDispose(texture_t *texture) { glDeleteTextures(1, &texture->id); free(texture); }