diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index 419db6158..8eef97e29 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -769,6 +769,12 @@ static void OnContextLost(void* obj) { DeleteVbs(); Gfx_DeleteTexture(&sides_tex); Gfx_DeleteTexture(&edges_tex); + + if (Gfx.ManagedTextures) return; + Gfx_DeleteTexture(&clouds_tex); + Gfx_DeleteTexture(&skybox_tex); + Gfx_DeleteTexture(&rain_tex); + Gfx_DeleteTexture(&snow_tex); } static void UpdateAll(void) { @@ -894,11 +900,6 @@ static void EnvRenderer_Free(void) { OnContextLost(NULL); Mem_Free(Weather_Heightmap); Weather_Heightmap = NULL; - - Gfx_DeleteTexture(&clouds_tex); - Gfx_DeleteTexture(&skybox_tex); - Gfx_DeleteTexture(&rain_tex); - Gfx_DeleteTexture(&snow_tex); } static void EnvRenderer_Reset(void) { diff --git a/src/Game.c b/src/Game.c index 334847bad..6b3675f72 100644 --- a/src/Game.c +++ b/src/Game.c @@ -602,6 +602,7 @@ static void Game_RenderFrame(double delta) { void Game_Free(void* obj) { struct IGameComponent* comp; Atlas_Free(); + Gfx.ManagedTextures = false; Event_UnregisterVoid(&WorldEvents.NewMap, NULL, HandleOnNewMap); Event_UnregisterVoid(&WorldEvents.MapLoaded, NULL, HandleOnNewMapLoaded); diff --git a/src/Graphics.c b/src/Graphics.c index e626fb6f8..9ac4f8b2e 100644 --- a/src/Graphics.c +++ b/src/Graphics.c @@ -406,6 +406,7 @@ void Gfx_Init(void) { Gfx.MinZNear = 0.05f; customMipmapsLevels = true; + Gfx.ManagedTextures = true; CommonInit(); TryCreateDevice(); } diff --git a/src/Graphics.h b/src/Graphics.h index 29e1be73f..3082ceaed 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -39,6 +39,7 @@ CC_VAR extern struct _GfxData { /* Whether some textures are created with mipmaps. */ cc_bool Mipmaps; /* Whether managed textures are actually supported. */ + /* If not, you must free/create them just like normal textures */ cc_bool ManagedTextures; /* Whether Gfx_Init has been called to initialise state. */ cc_bool Initialised; diff --git a/src/Gui.c b/src/Gui.c index 1d4fd64e4..5ba554238 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -129,7 +129,7 @@ static void Gui_LoadOptions(void) { Gui_RawChatScale = Options_GetFloat(OPT_CHAT_SCALE, 0.35f, 5.0f, 1.0f); } -static void OnContextLost(void* obj) { +static void LoseAllScreens(void) { struct Screen* s; int i; @@ -150,7 +150,7 @@ static void OnContextRecreated(void* obj) { } void Gui_RefreshAll(void) { - OnContextLost(NULL); + LoseAllScreens(); OnContextRecreated(NULL); } @@ -393,6 +393,15 @@ static void OnTextChanged(void* obj, const String* str) { } #endif +static void OnContextLost(void* obj) { + LoseAllScreens(); + if (Gfx.ManagedTextures) return; + + Gfx_DeleteTexture(&Gui_GuiTex); + Gfx_DeleteTexture(&Gui_GuiClassicTex); + Gfx_DeleteTexture(&Gui_IconsTex); +} + static void Gui_Init(void) { Event_RegisterVoid(&ChatEvents.FontChanged, NULL, OnFontChanged); Event_RegisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged); @@ -422,9 +431,7 @@ static void Gui_Free(void) { while (Gui_ScreensCount) Gui_Remove(Gui_Screens[0]); - Gfx_DeleteTexture(&Gui_GuiTex); - Gfx_DeleteTexture(&Gui_GuiClassicTex); - Gfx_DeleteTexture(&Gui_IconsTex); + OnContextLost(NULL); Gui_Reset(); } diff --git a/src/Model.c b/src/Model.c index 8a0e3ddf2..4d8a8c4bd 100644 --- a/src/Model.c +++ b/src/Model.c @@ -419,7 +419,13 @@ static struct ModelTex* textures_tail; #define Model_RetAABB(x1,y1,z1, x2,y2,z2) static struct AABB BB = { (x1)/16.0f,(y1)/16.0f,(z1)/16.0f, (x2)/16.0f,(y2)/16.0f,(z2)/16.0f }; e->ModelAABB = BB; static void Models_ContextLost(void* obj) { + struct ModelTex* tex; Gfx_DeleteDynamicVb(&Models.Vb); + if (Gfx.ManagedTextures) return; + + for (tex = textures_head; tex; tex = tex->next) { + Gfx_DeleteTexture(&tex->texID); + } } static void Models_ContextRecreated(void* obj) { @@ -2024,18 +2030,12 @@ static void Models_Init(void) { } static void Models_Free(void) { - struct ModelTex* tex; - - for (tex = textures_head; tex; tex = tex->next) { - Gfx_DeleteTexture(&tex->texID); - } Models_ContextLost(NULL); + CustomModel_FreeAll(); Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, Models_TextureChanged); Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, Models_ContextLost); Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, Models_ContextRecreated); - - CustomModel_FreeAll(); } static void Models_Reset(void) { CustomModel_FreeAll(); } diff --git a/src/Particle.c b/src/Particle.c index 346835e31..7f04bb95a 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -573,6 +573,9 @@ void Particles_CustomEffect(int effectID, float x, float y, float z, float origi *#########################################################################################################################*/ static void OnContextLost(void* obj) { Gfx_DeleteDynamicVb(&Particles_VB); + + if (Gfx.ManagedTextures) return; + Gfx_DeleteTexture(&Particles_TexId); } static void OnContextRecreated(void* obj) { Particles_VB = Gfx_CreateDynamicVb(VERTEX_FORMAT_TEXTURED, PARTICLES_MAX * 4); @@ -599,7 +602,6 @@ static void Particles_Init(void) { } static void Particles_Free(void) { - Gfx_DeleteTexture(&Particles_TexId); OnContextLost(NULL); Event_UnregisterBlock(&UserEvents.BlockChanged, NULL, OnBreakBlockEffect_Handler);