diff --git a/Resources/Scripts/Reference/API/Renderer.as b/Resources/Scripts/Reference/API/Renderer.as index 8503ed34..b73496a6 100644 --- a/Resources/Scripts/Reference/API/Renderer.as +++ b/Resources/Scripts/Reference/API/Renderer.as @@ -43,6 +43,13 @@ namespace spades { */ Model @RegisterModel(const string @path) {} + /** + * Clear the cache of models and images loaded via `RegisterModel` + * and `RegisterImage`. This method is merely a hint - the + * implementation may partially or completely ignore the request. + */ + void ClearCache() {} + /** Creates an image from the specified bitmap. */ Image @CreateImage(Bitmap @bitmap) {} diff --git a/Sources/Client/IRenderer.h b/Sources/Client/IRenderer.h index e60c2922..42e02ae0 100644 --- a/Sources/Client/IRenderer.h +++ b/Sources/Client/IRenderer.h @@ -98,6 +98,13 @@ namespace spades { virtual IImage *RegisterImage(const char *filename) = 0; virtual IModel *RegisterModel(const char *filename) = 0; + /** + * Clear the cache of models and images loaded via `RegisterModel` + * and `RegisterImage`. This method is merely a hint - the + * implementation may partially or completely ignore the request. + */ + virtual void ClearCache() {} + virtual IImage *CreateImage(Bitmap *) = 0; virtual IModel *CreateModel(VoxelModel *) = 0; diff --git a/Sources/Draw/GLImageManager.cpp b/Sources/Draw/GLImageManager.cpp index d526e21a..0ae69d61 100644 --- a/Sources/Draw/GLImageManager.cpp +++ b/Sources/Draw/GLImageManager.cpp @@ -19,13 +19,13 @@ */ #include "GLImageManager.h" +#include "GLImage.h" +#include "GLRenderer.h" +#include "IGLDevice.h" #include #include #include #include -#include "GLImage.h" -#include "GLRenderer.h" -#include "IGLDevice.h" namespace spades { namespace draw { @@ -102,5 +102,7 @@ namespace spades { } } } - } -} + + void GLImageManager::ClearCache() { images.clear(); } + } // namespace draw +} // namespace spades diff --git a/Sources/Draw/GLImageManager.h b/Sources/Draw/GLImageManager.h index 1f42dc4a..4156e036 100644 --- a/Sources/Draw/GLImageManager.h +++ b/Sources/Draw/GLImageManager.h @@ -45,6 +45,8 @@ namespace spades { GLImage *GetWhiteImage(); void DrawAllImages(GLRenderer *); + + void ClearCache(); }; - } -} + } // namespace draw +} // namespace spades diff --git a/Sources/Draw/GLModelManager.cpp b/Sources/Draw/GLModelManager.cpp index df57fbd0..dc689112 100644 --- a/Sources/Draw/GLModelManager.cpp +++ b/Sources/Draw/GLModelManager.cpp @@ -66,5 +66,7 @@ namespace spades { return static_cast(renderer->CreateModelOptimized(voxelModel)); } + + void GLModelManager::ClearCache() { models.clear(); } } // namespace draw } // namespace spades diff --git a/Sources/Draw/GLModelManager.h b/Sources/Draw/GLModelManager.h index 00be7812..6eec8146 100644 --- a/Sources/Draw/GLModelManager.h +++ b/Sources/Draw/GLModelManager.h @@ -38,6 +38,8 @@ namespace spades { GLModelManager(GLRenderer *); ~GLModelManager(); GLModel *RegisterModel(const char *); + + void ClearCache(); }; - } -} + } // namespace draw +} // namespace spades diff --git a/Sources/Draw/GLRenderer.cpp b/Sources/Draw/GLRenderer.cpp index 2db3927d..7e38adeb 100644 --- a/Sources/Draw/GLRenderer.cpp +++ b/Sources/Draw/GLRenderer.cpp @@ -251,6 +251,12 @@ namespace spades { return modelManager->RegisterModel(filename); } + void GLRenderer::ClearCache() { + SPADES_MARK_FUNCTION(); + modelManager->ClearCache(); + imageManager->ClearCache(); + } + client::IImage *GLRenderer::CreateImage(spades::Bitmap *bmp) { SPADES_MARK_FUNCTION(); return GLImage::FromBitmap(bmp, device); diff --git a/Sources/Draw/GLRenderer.h b/Sources/Draw/GLRenderer.h index 955ca4ee..65672417 100644 --- a/Sources/Draw/GLRenderer.h +++ b/Sources/Draw/GLRenderer.h @@ -153,6 +153,8 @@ namespace spades { client::IImage *RegisterImage(const char *filename) override; client::IModel *RegisterModel(const char *filename) override; + void ClearCache() override; + client::IImage *CreateImage(Bitmap *) override; client::IModel *CreateModel(VoxelModel *) override; client::IModel *CreateModelOptimized(VoxelModel *); diff --git a/Sources/Draw/SWImage.cpp b/Sources/Draw/SWImage.cpp index 8608e3d0..e67091d6 100644 --- a/Sources/Draw/SWImage.cpp +++ b/Sources/Draw/SWImage.cpp @@ -127,5 +127,9 @@ namespace spades { } SWImage *SWImageManager::CreateImage(Bitmap *vm) { return new SWImage(vm); } + + void SWImageManager::ClearCache() { + images.clear(); + } } } diff --git a/Sources/Draw/SWImage.h b/Sources/Draw/SWImage.h index 5ac23d41..bdca2fb5 100644 --- a/Sources/Draw/SWImage.h +++ b/Sources/Draw/SWImage.h @@ -69,6 +69,8 @@ namespace spades { SWImage *RegisterImage(const std::string &); SWImage *CreateImage(Bitmap *); + + void ClearCache(); }; } } diff --git a/Sources/Draw/SWModel.cpp b/Sources/Draw/SWModel.cpp index a017a54c..16ff451a 100644 --- a/Sources/Draw/SWModel.cpp +++ b/Sources/Draw/SWModel.cpp @@ -147,5 +147,9 @@ namespace spades { } SWModel *SWModelManager::CreateModel(spades::VoxelModel *vm) { return new SWModel(vm); } + + void SWModelManager::ClearCache() { + models.clear(); + } } } diff --git a/Sources/Draw/SWModel.h b/Sources/Draw/SWModel.h index 26857fc5..196e8887 100644 --- a/Sources/Draw/SWModel.h +++ b/Sources/Draw/SWModel.h @@ -63,6 +63,8 @@ namespace spades { SWModel *RegisterModel(const std::string &); SWModel *CreateModel(VoxelModel *); + + void ClearCache(); }; } } diff --git a/Sources/Draw/SWRenderer.cpp b/Sources/Draw/SWRenderer.cpp index 87b8228b..b14f931e 100644 --- a/Sources/Draw/SWRenderer.cpp +++ b/Sources/Draw/SWRenderer.cpp @@ -152,6 +152,13 @@ namespace spades { return modelManager->RegisterModel(filename); } + void SWRenderer::ClearCache() { + SPADES_MARK_FUNCTION(); + EnsureValid(); + imageManager->ClearCache(); + modelManager->ClearCache(); + } + client::IImage *SWRenderer::CreateImage(spades::Bitmap *bmp) { SPADES_MARK_FUNCTION(); EnsureValid(); @@ -1186,5 +1193,5 @@ namespace spades { flatMapRenderer->SetNeedsUpdate(x, y); } - } -} + } // namespace draw +} // namespace spades diff --git a/Sources/Draw/SWRenderer.h b/Sources/Draw/SWRenderer.h index d58a4c91..dffaa438 100644 --- a/Sources/Draw/SWRenderer.h +++ b/Sources/Draw/SWRenderer.h @@ -159,6 +159,8 @@ namespace spades { client::IImage *CreateImage(Bitmap *) override; client::IModel *CreateModel(VoxelModel *) override; + + void ClearCache() override; /* GLProgram *RegisterProgram(const std::string& name) override; GLShader *RegisterShader(const std::string& name) override; diff --git a/Sources/ScriptBindings/IRenderer.cpp b/Sources/ScriptBindings/IRenderer.cpp index 10344dfa..a5a7bd6f 100644 --- a/Sources/ScriptBindings/IRenderer.cpp +++ b/Sources/ScriptBindings/IRenderer.cpp @@ -394,6 +394,11 @@ namespace spades { asFUNCTION(RegisterModel), asCALL_CDECL_OBJLAST); manager->CheckError(r); + r = eng->RegisterObjectMethod("Renderer", + "void ClearCache()", + asMETHOD(IRenderer, ClearCache), + asCALL_THISCALL); + manager->CheckError(r); // OpenSpades' C++ functions increase the reference count of a passed object // when storing it (just like the convention of Objective C), so we must // use "auto handles" (`@+`). Otherwise, a memory leak would occur