aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-08-19 20:16:34 +0200
committerdec05eba <dec05eba@protonmail.com>2024-08-19 20:16:34 +0200
commit8731a00681a2cf99965203849bd7ba8f6c09df9a (patch)
tree2f7a3db3ec862e50c9e6e9154e575cc3c88bd293 /src
parentf1ede6cfa5cc2af2e7f93e29f2311e9ece7f41c4 (diff)
Add mgl_texture_init_reference_existing_gl_texture to easily create a texture and then a sprite from an existing opengl texture
Diffstat (limited to 'src')
-rw-r--r--src/gl.c1
-rw-r--r--src/graphics/texture.c38
2 files changed, 39 insertions, 0 deletions
diff --git a/src/gl.c b/src/gl.c
index 7c9a6bc..bb2e90b 100644
--- a/src/gl.c
+++ b/src/gl.c
@@ -29,6 +29,7 @@ static int mgl_gl_load_gl(mgl_gl *self) {
{ (void**)&self->glBlendFunc, "glBlendFunc" },
{ (void**)&self->glGenTextures, "glGenTextures" },
{ (void**)&self->glDeleteTextures, "glDeleteTextures" },
+ { (void**)&self->glGetTexLevelParameteriv, "glGetTexLevelParameteriv" },
{ (void**)&self->glTexImage2D, "glTexImage2D" },
{ (void**)&self->glTexSubImage2D, "glTexSubImage2D" },
{ (void**)&self->glBindTexture, "glBindTexture" },
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;