From 95a582b91ecb11899dec65e51f898bd2e5714ae7 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Sat, 6 Jun 2020 16:19:36 +0200 Subject: [PATCH] fixed memory leaks --- core/src/netwerkprog/game/client/MainGame.java | 9 +++++++-- .../game/client/game/map/MapRenderer.java | 13 ++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/core/src/netwerkprog/game/client/MainGame.java b/core/src/netwerkprog/game/client/MainGame.java index ffead10..e7124eb 100644 --- a/core/src/netwerkprog/game/client/MainGame.java +++ b/core/src/netwerkprog/game/client/MainGame.java @@ -3,6 +3,7 @@ package netwerkprog.game.client; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Files; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; @@ -45,9 +46,9 @@ public class MainGame extends ApplicationAdapter { private GAMESTATE gamestate; private Faction chosenFaction; - private Map map; public MapRenderer mapRenderer; + public AssetManager assets; private static MainGame INSTANCE; @@ -72,6 +73,7 @@ public class MainGame extends ApplicationAdapter { textRenderer = new TextRenderer(); font = new BitmapFont(); layout = new GlyphLayout(); + assets = new AssetManager(); String[] strings = new String[]{ "#########################", @@ -107,7 +109,9 @@ public class MainGame extends ApplicationAdapter { } public void initCharacters() { - Texture texture = new Texture(Gdx.files.internal("core/assets/characters.png")); + assets.load("core/assets/characters.png",Texture.class); + assets.finishLoading(); + Texture texture = assets.get("core/assets/characters.png"); TextureRegion[][] characters = TextureRegion.split(texture, 32, 32); this.team = new Team(); @@ -216,6 +220,7 @@ public class MainGame extends ApplicationAdapter { public void dispose() { batch.dispose(); textRenderer.dispose(); + assets.dispose(); } public float getScreenWidth() { diff --git a/core/src/netwerkprog/game/client/game/map/MapRenderer.java b/core/src/netwerkprog/game/client/game/map/MapRenderer.java index 9c84a8e..f9b6bbf 100644 --- a/core/src/netwerkprog/game/client/game/map/MapRenderer.java +++ b/core/src/netwerkprog/game/client/game/map/MapRenderer.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.utils.Disposable; import netwerkprog.game.client.MainGame; import netwerkprog.game.util.game.GameCharacter; import netwerkprog.game.util.graphics.Renderable; @@ -57,8 +58,6 @@ public class MapRenderer implements Renderable { this.camera = camera; this.mainGame = MainGame.getInstance(); font = new BitmapFont(); - square = new Texture(Gdx.files.internal("square.png")); - square2 = new Texture(Gdx.files.internal("square2.png")); makeTiles(); } @@ -66,7 +65,14 @@ public class MapRenderer implements Renderable { * loads all the images for the tiles and adds all the tiles to the array */ private void makeTiles() { - Texture texture = new Texture(Gdx.files.internal(tilePath)); + mainGame.assets.load("square.png", Texture.class); + mainGame.assets.load("square2.png", Texture.class); + mainGame.assets.load(tilePath, Texture.class); + mainGame.assets.finishLoading(); + square = mainGame.assets.get("square.png"); + square2 = mainGame.assets.get("square2.png"); + + Texture texture = mainGame.assets.get(tilePath); TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32); FLOOR_TILE = tileTextures[1][6]; @@ -196,4 +202,5 @@ public class MapRenderer implements Renderable { public List getSurroundedTilesOfCurrentCharacter() { return surroundedTilesOfCurrentCharacter; } + }