From 8731a00681a2cf99965203849bd7ba8f6c09df9a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 19 Aug 2024 20:16:34 +0200 Subject: Add mgl_texture_init_reference_existing_gl_texture to easily create a texture and then a sprite from an existing opengl texture --- src/graphics/texture.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/graphics/texture.c') diff --git a/src/graphics/texture.c b/src/graphics/texture.c index 5de3d2b..9a13314 100644 --- a/src/graphics/texture.c +++ b/src/graphics/texture.c @@ -51,6 +51,17 @@ static mgl_texture_format mgl_image_format_to_mgl_texture_format(mgl_image_forma return 0; } +static void gl_get_texture_size(unsigned int texture_id, int *width, int *height) { + *width = 0; + *height = 0; + + mgl_context *context = mgl_get_context(); + context->gl.glBindTexture(GL_TEXTURE_2D, texture_id); + context->gl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, width); + context->gl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, height); + context->gl.glBindTexture(GL_TEXTURE_2D, 0); +} + int mgl_texture_init(mgl_texture *self) { self->id = 0; self->width = 0; @@ -59,6 +70,8 @@ int mgl_texture_init(mgl_texture *self) { self->max_width = 0; self->max_height = 0; self->pixel_coordinates = false; + self->mipmap = false; + self->owned = true; mgl_context *context = mgl_get_context(); context->gl.glGenTextures(1, &self->id); @@ -75,6 +88,28 @@ int mgl_texture_init(mgl_texture *self) { return 0; } +int mgl_texture_init_reference_existing_gl_texture(mgl_texture *self, unsigned int texture_id, mgl_texture_format format, const mgl_texture_reference_options *reference_options) { + self->id = texture_id; + self->width = 0; + self->height = 0; + self->format = format; + self->max_width = 0; + self->max_height = 0; + self->pixel_coordinates = reference_options && reference_options->pixel_coordinates; + self->mipmap = false; + self->owned = false; + + gl_get_texture_size(self->id, &self->width, &self->height); + + int max_texture_size = 0; + mgl_context *context = mgl_get_context(); + context->gl.glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); + self->max_width = max_texture_size; + self->max_height = max_texture_size; + + return 0; +} + int mgl_texture_load_from_file(mgl_texture *self, const char *filepath, const mgl_texture_load_options *load_options) { mgl_image image; if(mgl_image_load_from_file(&image, filepath) != 0) @@ -199,6 +234,9 @@ const mgl_texture* mgl_texture_current_texture(void) { void mgl_texture_unload(mgl_texture *self) { mgl_context *context = mgl_get_context(); + if(!self->owned) + return; + if(self->id) { context->gl.glDeleteTextures(1, &self->id); self->id = 0; -- cgit v1.2.3