From 9e390d2131e2f1eaa886956b2b657b35d6d1b5f9 Mon Sep 17 00:00:00 2001 From: fredboy Date: Sun, 21 Apr 2024 20:33:53 +0700 Subject: [PATCH] Dispose textures on app close --- .../ru/deadsoftware/cavedroid/CaveGame.java | 4 +++- .../game/model/mapper/BlockMapper.kt | 4 ++-- .../deadsoftware/cavedroid/misc/Assets.java | 23 ++++++++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.java b/core/src/ru/deadsoftware/cavedroid/CaveGame.java index 5bd5c99..6e3e189 100644 --- a/core/src/ru/deadsoftware/cavedroid/CaveGame.java +++ b/core/src/ru/deadsoftware/cavedroid/CaveGame.java @@ -81,9 +81,10 @@ public class CaveGame extends Game { @Override public void create() { - Gdx.app.log(TAG, mGameFolder); Gdx.files.absolute(mGameFolder).mkdirs(); initConfig(); + + Gdx.app.debug(TAG, mGameFolder); Assets.load(mAssetLoader); setScreen(mMainComponent.getMenuScreen()); } @@ -93,5 +94,6 @@ public class CaveGame extends Game { if (screen != null) { screen.dispose(); } + Assets.dispose(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt index cae219f..bc29783 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt @@ -41,7 +41,7 @@ class BlockMapper @Inject constructor() { isTransparent = dto.transparent, requiresBlock = dto.blockRequired, animationInfo = mapBlockAnimationInfo(dto), - texture = loadTexture(dto.texture), + texture = getTexture(dto.texture), spriteMargins = BlockMargins( left = dto.spriteLeft, top = dto.spriteTop, @@ -75,7 +75,7 @@ class BlockMapper @Inject constructor() { ) } - private fun loadTexture(textureName: String): Texture? { + private fun getTexture(textureName: String): Texture? { if (textureName == GameItemsHolder.FALLBACK_BLOCK_KEY) { return null } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index cf87a08..a6318d7 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -13,11 +13,16 @@ import ru.deadsoftware.cavedroid.game.objects.TouchButton; import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; public class Assets { public static final JsonReader jsonReader = new JsonReader(); + + private static final List loadedTextures = new LinkedList<>(); + public static final Sprite[][] playerSprite = new Sprite[2][4]; public static final Sprite[][] pigSprite = new Sprite[2][2]; public static final HashMap textureRegions = new HashMap<>(); @@ -28,6 +33,18 @@ public class Assets { public static Map blockTextures = new HashMap<>(); public static Map itemTextures = new HashMap<>(); + public static void dispose() { + minecraftFont.dispose(); + loadedTextures.forEach(Texture::dispose); + loadedTextures.clear(); + } + + private static Texture loadTexture(FileHandle fileHandle) { + Texture texture = new Texture(fileHandle); + loadedTextures.add(texture); + return texture; + } + private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) { return new TextureRegion(texture, x, y + height, width, -height); } @@ -47,7 +64,7 @@ public class Assets { private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) { for (int i = 0; i < sprite.length; i++) { for (int j = 0; j < sprite[i].length; j++) { - sprite[i][j] = flippedSprite(new Texture( + sprite[i][j] = flippedSprite(loadTexture( assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png"))); sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0); } @@ -67,7 +84,7 @@ public class Assets { private static void loadJSON(AssetLoader assetLoader) { JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json")); for (JsonValue file = json.child(); file != null; file = file.next()) { - Texture texture = new Texture(assetLoader.getAssetHandle(file.name() + ".png")); + Texture texture = loadTexture(assetLoader.getAssetHandle(file.name() + ".png")); if (file.size == 0) { textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight())); @@ -85,7 +102,7 @@ public class Assets { private static void loadAllPngsFromDirInto(FileHandle dir, Map loadInto) { for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) { - loadInto.put(handle.nameWithoutExtension(), new Texture(handle)); + loadInto.put(handle.nameWithoutExtension(), loadTexture(handle)); } } -- 2.29.2