From 44e987c8521a99519350a42292bcfcd28451dcbd Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 20 Dec 2021 10:26:12 +0100 Subject: Async load images --- src/mgui/image.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'src/mgui/image.c') diff --git a/src/mgui/image.c b/src/mgui/image.c index 1a0871f..9f50b9c 100644 --- a/src/mgui/image.c +++ b/src/mgui/image.c @@ -2,12 +2,15 @@ #include "../../include/resource_loader.h" #include "../../include/common.h" #include "../../include/alloc.h" +#include "../../include/async_image.h" #include #include -#include +#include +#include #include -/* TODO: Load image asynchronously and support network files */ +/* TODO: Support network files */ +/* TODO: Set a target size and use that for calculating size and resize image to that */ static mgl_vec2i wrap_to_size_x(mgl_vec2i size, int clamp_size) { mgl_vec2i new_size; @@ -52,13 +55,24 @@ static mgl_vec2i clamp_to_size(mgl_vec2i size, mgl_vec2i clamp_size) { mgui_image* mgui_image_create(const char *filepath) { mgui_image *image = mgui_alloc(sizeof(mgui_image)); mgui_widget_init(&image->widget, MGUI_WIDGET_IMAGE); - (void)filepath; - /* TODO: Use |filepath| */ mgl_sprite_init(&image->sprite, NULL); + image->max_size = (mgl_vec2i){ 0, 0 }; + + if(filepath) { + image->async_image = mgui_async_image_get_by_path(filepath); + if(image->async_image->state == MGUI_ASYNC_IMAGE_APPLIED) + mgl_sprite_set_texture(&image->sprite, &image->async_image->texture); + } else { + image->async_image = NULL; + } + return image; } void mgui_image_destroy(mgui_image *image) { + if(image->async_image) + mgui_async_image_unref(image->async_image); + image->sprite.texture = NULL; mgui_free(image); } @@ -76,6 +90,7 @@ void mgui_image_set_position(mgui_image *self, mgl_vec2i position) { } void mgui_image_calculate_size(mgui_image *self, mgl_vec2i max_size) { + self->max_size = max_size; if(self->sprite.texture) { const mgl_vec2i texture_size = (mgl_vec2i){ self->sprite.texture->width, self->sprite.texture->height }; const mgl_vec2i new_size = clamp_to_size(texture_size, max_size); @@ -85,7 +100,7 @@ void mgui_image_calculate_size(mgui_image *self, mgl_vec2i max_size) { self->widget.size = new_size; } else { - self->widget.size = (mgl_vec2i){ 0, 0 }; + self->widget.size = (mgl_vec2i){ 1, 1 }; } } @@ -97,9 +112,14 @@ void mgui_image_on_event(mgui_image *self, mgl_window *window, mgl_event *event) } void mgui_image_draw(mgui_image *self, mgl_window *window) { - if(self->sprite.texture) { - const mgl_vec2i texture_size = (mgl_vec2i){ self->sprite.texture->width, self->sprite.texture->height }; - if(mgui_rectangle_intersects_with_scissor((mgl_vec2i){ self->sprite.position.x, self->sprite.position.y }, texture_size, window)) - mgl_sprite_draw(mgl_get_context(), &self->sprite); + if(self->async_image && mgui_rectangle_intersects_with_scissor((mgl_vec2i){ self->sprite.position.x, self->sprite.position.y }, self->widget.size, window)) { + mgui_async_image_update(self->async_image); + if(self->async_image->state == MGUI_ASYNC_IMAGE_APPLIED) { + mgl_sprite_set_texture(&self->sprite, &self->async_image->texture); + /* TODO: Check if this is correct when taking margin into consideration */ + mgui_image_calculate_size(self, self->max_size); + if(self->sprite.texture && self->sprite.texture->id) + mgl_sprite_draw(mgl_get_context(), &self->sprite); + } } } -- cgit v1.2.3