From 115630b520668304af1ccd3eb0b13c06e17ecccc Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 24 Oct 2021 04:52:30 +0200 Subject: Add function to load image from memory, initialize window from an existing window, allow creating text without font/string --- src/graphics/image.c | 19 +++++++++++++++++++ src/graphics/text.c | 9 +++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'src/graphics') diff --git a/src/graphics/image.c b/src/graphics/image.c index 430f03a..593a3d2 100644 --- a/src/graphics/image.c +++ b/src/graphics/image.c @@ -52,6 +52,25 @@ int mgl_image_load_from_file(mgl_image *self, const char *filepath) { return 0; } +/* TODO: Ensure texture is power of 2 if the hardware doesn't support non power of two textures */ +/* TODO: Verify if source format should always be 4 components (RGBA) because apparently if its another format then opengl will internally convert it to RGBA */ +int mgl_image_load_from_memory(mgl_image *self, const unsigned char *data, size_t size) { + self->data = NULL; + self->width = 0; + self->height = 0; + + int format; + self->data = stbi_load_from_memory(data, size, &self->width, &self->height, &format, 0); + if(!self->data) { + fprintf(stderr, "Error: failed to load image from memory, error: %s\n", stbi_failure_reason()); + mgl_image_unload(self); + return -1; + } + self->format = stbi_format_to_mgl_image_format(format); + + return 0; +} + void mgl_image_unload(mgl_image *self) { if(self->data) { stbi_image_free(self->data); diff --git a/src/graphics/text.c b/src/graphics/text.c index e7b31a0..76fd852 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -18,6 +18,10 @@ void mgl_text_set_string(mgl_text *self, const char *str) { self->text = str; } +void mgl_text_set_font(mgl_text *self, mgl_font *font) { + self->font = font; +} + void mgl_text_set_position(mgl_text *self, mgl_vec2f position) { self->position = position; } @@ -43,10 +47,11 @@ static void mgl_text_draw_glyph(mgl_context *context, mgl_font_glyph *glyph, mgl /* TODO: Use opengl buffer object instead */ /* TODO: Cache texture bind to not bind texture if its already bound and do not bind texture 0 */ void mgl_text_draw(mgl_context *context, mgl_text *text) { - const char *str = text->text; - if(!str) + if(!text->text || !text->font) return; + const char *str = text->text; + mgl_font_glyph glyph; mgl_vec2f position = text->position; position.y += text->font->character_size; -- cgit v1.2.3