diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-26 09:00:38 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-26 09:03:21 +0100 |
commit | aaf0b6edbbda67c2414b4736b82a00e6fe4025f5 (patch) | |
tree | e1fb563f5fcc17209dedc9cc11b2dc328a77e6b4 | |
parent | 1d4cc1d1c2c4f6a574775914078c80cf496fc7de (diff) |
Allow reloading images, textures and memory mapped files
-rw-r--r-- | src/graphics/Font.cpp | 6 | ||||
-rw-r--r-- | src/graphics/Image.cpp | 12 | ||||
-rw-r--r-- | src/graphics/Shader.cpp | 6 | ||||
-rw-r--r-- | src/graphics/Texture.cpp | 4 | ||||
-rw-r--r-- | 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, |