From aaf0b6edbbda67c2414b4736b82a00e6fe4025f5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 26 Nov 2021 09:00:38 +0100 Subject: Allow reloading images, textures and memory mapped files --- src/graphics/Font.cpp | 6 ++++-- src/graphics/Image.cpp | 12 ++++++++---- src/graphics/Shader.cpp | 6 ++++-- src/graphics/Texture.cpp | 4 ++-- src/system/MemoryMappedFile.cpp | 8 ++++++-- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 535daae..2efae40 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -13,8 +13,10 @@ namespace mgl { } bool Font::load_from_file(const MemoryMappedFile &mapped_file, unsigned int character_size) { - if(font.texture.id) - return false; + if(font.texture.id) { + mgl_font_unload(&font); + memset(&font, 0, sizeof(font)); + } return mgl_font_load_from_file(&font, mapped_file.internal_mapped_file(), character_size) == 0; } diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp index 74a9866..30cdb93 100644 --- a/src/graphics/Image.cpp +++ b/src/graphics/Image.cpp @@ -11,14 +11,18 @@ namespace mgl { } bool Image::load_from_file(const char *filepath) { - if(image.data) - return false; + if(image.data) { + mgl_image_unload(&image); + memset(&image, 0, sizeof(image)); + } return mgl_image_load_from_file(&image, filepath) == 0; } bool Image::load_from_memory(const unsigned char *data, size_t size) { - if(image.data) - return false; + if(image.data) { + mgl_image_unload(&image); + memset(&image, 0, sizeof(image)); + } return mgl_image_load_from_memory(&image, data, size) == 0; } diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp index 0d0e651..2bdc9b2 100644 --- a/src/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp @@ -10,8 +10,10 @@ namespace mgl { } bool Shader::load_from_file(const char *filepath, Type type) { - if(shader_program.id) - return false; + if(shader_program.id) { + mgl_shader_program_deinit(&shader_program); + shader_program.id = 0; + } int res = mgl_shader_program_init(&shader_program); if(res != 0) diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp index 29c2eea..794c563 100644 --- a/src/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp @@ -27,7 +27,7 @@ namespace mgl { bool Texture::load_from_file(const char *filepath, const LoadOptions load_options) { if(texture.id) - return false; + clear(); if(mgl_texture_init(&texture) != 0) return false; @@ -41,7 +41,7 @@ namespace mgl { bool Texture::load_from_image(Image &image, const LoadOptions load_options) { if(texture.id) - return false; + clear(); if(mgl_texture_init(&texture) != 0) return false; diff --git a/src/system/MemoryMappedFile.cpp b/src/system/MemoryMappedFile.cpp index 54f15c1..c823cb5 100644 --- a/src/system/MemoryMappedFile.cpp +++ b/src/system/MemoryMappedFile.cpp @@ -12,8 +12,12 @@ namespace mgl { } bool MemoryMappedFile::load(const char *filepath, LoadOptions load_options) { - if(memory_mapped_file.fd != -1) - return false; + if(memory_mapped_file.fd != -1) { + mgl_mapped_file_unload(&memory_mapped_file); + memory_mapped_file.data = nullptr; + memory_mapped_file.size = 0; + memory_mapped_file.fd = -1; + } mgl_memory_mapped_file_load_options mapped_file_load_options = { load_options.readable, -- cgit v1.2.3