aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-12-20 10:26:12 +0100
committerdec05eba <dec05eba@protonmail.com>2021-12-21 20:22:33 +0100
commit44e987c8521a99519350a42292bcfcd28451dcbd (patch)
tree699015a5dd459e96e0b19f4836f7dcffc1e347de /include
parent6bb40bf0c5cd8ee8fb87640fd04b2c595f84c1d3 (diff)
Async load images
Diffstat (limited to 'include')
-rw-r--r--include/async_image.h45
-rw-r--r--include/hashmap.h4
-rw-r--r--include/mgui/image.h5
-rw-r--r--include/mgui/list.h2
-rw-r--r--include/mgui/mgui.h4
-rw-r--r--include/mgui/richtext.h1
-rw-r--r--include/mgui/widget.h9
7 files changed, 66 insertions, 4 deletions
diff --git a/include/async_image.h b/include/async_image.h
new file mode 100644
index 0000000..a108d5e
--- /dev/null
+++ b/include/async_image.h
@@ -0,0 +1,45 @@
+#ifndef MGUI_ASYNC_IMAGE_H
+#define MGUI_ASYNC_IMAGE_H
+
+#include <mgl/graphics/image.h>
+#include <mgl/graphics/texture.h>
+#include <stdint.h>
+
+typedef struct mgui_async_image mgui_async_image;
+
+typedef enum {
+ MGUI_ASYNC_IMAGE_NOT_LOADED,
+ MGUI_ASYNC_IMAGE_LOADING,
+ MGUI_ASYNC_IMAGE_LOADED,
+ MGUI_ASYNC_IMAGE_APPLIED,
+ MGUI_ASYNC_IMAGE_FAILED_TO_LOAD,
+ MGUI_ASYNC_IMAGE_UNLOADED
+} mgui_async_image_state;
+
+struct mgui_async_image {
+ mgl_image image;
+ mgl_texture texture;
+ uint64_t hash;
+ mgui_async_image_state state;
+ uint32_t updated;
+ uint32_t ref_count;
+ char *filepath;
+};
+
+/* Do not call this manually */
+void mgui_async_image_init();
+/* Do not call this manually */
+void mgui_async_image_deinit();
+/* Do not call this manually */
+void mgui_async_image_unload_unreferenced();
+
+/*
+ Increases reference count by 1 every time this is called.
+ Call |mgui_async_image_unref| when done with the image so it can be unloaded when all references are gone.
+*/
+mgui_async_image* mgui_async_image_get_by_path(const char *filepath);
+/* Needs to be called at least once a frame to keep the image from being unloaded */
+void mgui_async_image_update(mgui_async_image *self);
+void mgui_async_image_unref(mgui_async_image *self);
+
+#endif /* MGUI_ASYNC_IMAGE_H */
diff --git a/include/hashmap.h b/include/hashmap.h
index 0928081..f027d25 100644
--- a/include/hashmap.h
+++ b/include/hashmap.h
@@ -34,5 +34,9 @@ bool mgui_hashmap_get(mgui_hashmap *self, const char *key, size_t key_size, void
/* Note: |hash| has to be the hash for |key|, which you can get by using calling |mgui_hashmap_hash| with |key| or as a result of |mgui_hashmap_insert| */
bool mgui_hashmap_get_by_hash(mgui_hashmap *self, const char *key, size_t key_size, uint64_t hash, void **value_out);
uint64_t mgui_hashmap_hash(const char *key, size_t key_size);
+/* Return false from |callback| to stop the iteration */
+void mgui_hashmap_for_each(mgui_hashmap *self, bool(*callback)(void *value, void *userdata), void *userdata);
+/* Return true from |callback| to erase the item */
+void mgui_hashmap_for_each_erase(mgui_hashmap *self, bool(*callback)(void *value, void *userdata), void *userdata);
#endif /* MGUI_HASHMAP_H */
diff --git a/include/mgui/image.h b/include/mgui/image.h
index e6787f2..d9ed025 100644
--- a/include/mgui/image.h
+++ b/include/mgui/image.h
@@ -4,11 +4,16 @@
#include "widget.h"
#include <mgl/graphics/sprite.h>
+typedef struct mgui_async_image mgui_async_image;
+
typedef struct {
mgui_widget widget;
mgl_sprite sprite;
+ mgui_async_image *async_image;
+ mgl_vec2i max_size;
} mgui_image;
+/* If |filepath| is not a valid image then the image will be a broken-icon type */
mgui_image* mgui_image_create(const char *filepath);
void mgui_image_destroy(mgui_image *image);
mgui_widget* mgui_image_to_widget(mgui_image *list);
diff --git a/include/mgui/list.h b/include/mgui/list.h
index 7769ab6..92dce0a 100644
--- a/include/mgui/list.h
+++ b/include/mgui/list.h
@@ -21,6 +21,7 @@ typedef struct {
typedef struct {
mgui_widget widget;
mgui_list_direction direction;
+ int spacing;
mgl_vec2i position;
mgui_list_item *items;
size_t items_capacity;
@@ -32,6 +33,7 @@ void mgui_list_destroy(mgui_list *list);
mgui_widget* mgui_list_to_widget(mgui_list *list);
mgui_list* mgui_widget_to_list(mgui_widget *widget);
+void mgui_list_set_spacing(mgui_list *self, int spacing);
void mgui_list_set_position(mgui_list *self, mgl_vec2i position);
void mgui_list_calculate_size(mgui_list *self, mgl_vec2i max_size);
void mgui_list_append(mgui_list *self, mgui_widget *widget);
diff --git a/include/mgui/mgui.h b/include/mgui/mgui.h
index 15a2f73..cb018d5 100644
--- a/include/mgui/mgui.h
+++ b/include/mgui/mgui.h
@@ -6,9 +6,13 @@ typedef struct mgl_event mgl_event;
typedef struct mgl_window mgl_window;
void mgui_init();
+void mgui_deinit();
void mgui_on_event(mgui_widget *root_widget, mgl_window *window, mgl_event *event);
+/* This should only be called once every frame */
void mgui_draw(mgui_widget *root_widget, mgl_window *window);
/* Clamped to 1.0 second */
double mgui_get_seconds_since_last_update();
+/* Clamped to 1.0 second */
+double mgui_get_frame_time_seconds();
#endif /* MGUI_H */
diff --git a/include/mgui/richtext.h b/include/mgui/richtext.h
index ff57271..f3b07a5 100644
--- a/include/mgui/richtext.h
+++ b/include/mgui/richtext.h
@@ -23,6 +23,7 @@ typedef struct {
int width;
mgui_richtext_vertex_data vertex_data[2];
bool dirty;
+ bool vertices_dirty;
} mgui_richtext;
mgui_richtext* mgui_richtext_create(const char *str, size_t size, unsigned char character_size);
diff --git a/include/mgui/widget.h b/include/mgui/widget.h
index d425c11..0e77fd2 100644
--- a/include/mgui/widget.h
+++ b/include/mgui/widget.h
@@ -40,10 +40,10 @@ typedef enum {
} mgui_widget_flags;
typedef struct {
- int left;
- int right;
- int top;
- int bottom;
+ uint16_t left;
+ uint16_t right;
+ uint16_t top;
+ uint16_t bottom;
} mgui_margin;
struct mgui_widget {
@@ -52,6 +52,7 @@ struct mgui_widget {
uint8_t alignment; /* mgui_alignment, MGUI_WIDGET_ALIGN_TOP_LEFT by default */
mgui_margin margin;
mgl_vec2i size;
+ void *userdata;
};
void mgui_widget_init(mgui_widget *self, mgui_widget_type type);