diff --git a/RemixedDungeon/src/main/java/com/watabou/gltextures/SmartTexture.java b/RemixedDungeon/src/main/java/com/watabou/gltextures/SmartTexture.java index 6ffc253a0..1fe8eb0b7 100644 --- a/RemixedDungeon/src/main/java/com/watabou/gltextures/SmartTexture.java +++ b/RemixedDungeon/src/main/java/com/watabou/gltextures/SmartTexture.java @@ -20,7 +20,6 @@ import com.nyrds.platform.compatibility.RectF; import com.nyrds.platform.gfx.BitmapData; import com.nyrds.platform.gl.Texture; - import org.jetbrains.annotations.NotNull; public class SmartTexture extends Texture { diff --git a/RemixedDungeon/src/main/java/com/watabou/gltextures/TextureCache.java b/RemixedDungeon/src/main/java/com/watabou/gltextures/TextureCache.java index 8cf538a73..db18f7d18 100644 --- a/RemixedDungeon/src/main/java/com/watabou/gltextures/TextureCache.java +++ b/RemixedDungeon/src/main/java/com/watabou/gltextures/TextureCache.java @@ -4,16 +4,15 @@ import com.nyrds.platform.gl.Texture; import com.nyrds.util.ModdingMode; import com.watabou.noosa.TextureFilm; - +import com.watabou.pixeldungeon.Assets; +import lombok.Synchronized; +import lombok.val; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; -import lombok.Synchronized; -import lombok.val; - public class TextureCache { private static final Map all = new HashMap<>(); @@ -67,8 +66,12 @@ public static SmartTexture get(@NotNull Object src) { } else if (src instanceof SmartTexture) { return (SmartTexture) src; } else { - SmartTexture tx = new SmartTexture(ModdingMode.getBitmapData(src)); + BitmapData bmp = ModdingMode.getBitmapData(src); + SmartTexture tx = new SmartTexture(bmp); all.put(src, tx); + if (src != Assets.ITEMS && src != Assets.FONTS1X) { + bmp.dispose(); + } return tx; } } diff --git a/RemixedDungeon/src/main/java/com/watabou/noosa/Font.java b/RemixedDungeon/src/main/java/com/watabou/noosa/Font.java index 6b4192673..6b2deef55 100644 --- a/RemixedDungeon/src/main/java/com/watabou/noosa/Font.java +++ b/RemixedDungeon/src/main/java/com/watabou/noosa/Font.java @@ -3,9 +3,7 @@ import com.nyrds.platform.compatibility.RectF; import com.nyrds.platform.gfx.BitmapData; import com.nyrds.platform.gl.Texture; -import com.nyrds.platform.util.PUtil; import com.watabou.gltextures.SmartTexture; -import com.watabou.pixeldungeon.utils.GLog; import com.watabou.pixeldungeon.utils.Utils; import com.watabou.utils.PointF; @@ -47,7 +45,7 @@ protected Font( SmartTexture tx ) { texture = tx; texture.filter(Texture.LINEAR,Texture.NEAREST); - texture.reload(); + //texture.reload(); } private int findNextEmptyLine(BitmapData bitmap, int startFrom){ diff --git a/RemixedDungeon/src/main/java/com/watabou/pixeldungeon/sprites/ItemSprite.java b/RemixedDungeon/src/main/java/com/watabou/pixeldungeon/sprites/ItemSprite.java index cc46c48b2..d79848a23 100644 --- a/RemixedDungeon/src/main/java/com/watabou/pixeldungeon/sprites/ItemSprite.java +++ b/RemixedDungeon/src/main/java/com/watabou/pixeldungeon/sprites/ItemSprite.java @@ -20,12 +20,10 @@ import com.watabou.pixeldungeon.scenes.GameScene; import com.watabou.utils.PointF; import com.watabou.utils.Random; - +import lombok.val; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import lombok.val; - public class ItemSprite extends MovieClip { public static final int SIZE = 16; diff --git a/RemixedDungeonDesktop/build.gradle b/RemixedDungeonDesktop/build.gradle index cab69c4f9..15005ea84 100644 --- a/RemixedDungeonDesktop/build.gradle +++ b/RemixedDungeonDesktop/build.gradle @@ -36,6 +36,8 @@ application { applicationName = 'RemixedDungeon-desktop' applicationDefaultJvmArgs = ['-Dassets.dir=assets'] mainClass = 'com.nyrds.pixeldungeon.desktop.DesktopLauncher' + applicationDefaultJvmArgs = ["--add-opens", "java.base/java.util=ALL-UNNAMED"] + sourceSets { main { java { @@ -48,6 +50,11 @@ application { } } + +tasks.withType(JavaExec) { + jvmArgs += ["--add-opens", "java.base/java.util=ALL-UNNAMED"] +} + // Custom task to run the Python script tasks.register('generateJavaClasses', Exec) { commandLine 'python3', 'make_r.py' diff --git a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java index 9170d6d55..c0d384afd 100644 --- a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java +++ b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java @@ -16,7 +16,6 @@ import com.watabou.gltextures.TextureCache; import com.watabou.noosa.InterstitialPoint; import com.watabou.noosa.Scene; - import org.jetbrains.annotations.NotNull; import java.io.InputStream; @@ -134,6 +133,7 @@ public void pause() { MusicManager.INSTANCE.pause(); Sample.INSTANCE.pause(); + Sample.INSTANCE.reset(); Script.reset(); } @@ -158,7 +158,6 @@ public void dispose() { } MusicManager.INSTANCE.mute(); - Sample.INSTANCE.reset(); } @Override diff --git a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/BitmapData.java b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/BitmapData.java index b50404bdc..3ab79b19f 100644 --- a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/BitmapData.java +++ b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/BitmapData.java @@ -1,8 +1,5 @@ package com.nyrds.platform.gfx; -import static com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.GDX2D_BLEND_NONE; -import static com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.GDX2D_FORMAT_RGBA8888; - import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.PixmapIO; @@ -10,6 +7,9 @@ import java.io.InputStream; +import static com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.GDX2D_BLEND_NONE; +import static com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.GDX2D_FORMAT_RGBA8888; + public class BitmapData { public Gdx2DPixmap bmp; @@ -110,6 +110,7 @@ public void save(String path) { } public void dispose() { + bmp.dispose(); } } diff --git a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/SystemText.java b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/SystemText.java index 6ad337b6b..8f6e43b8c 100644 --- a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/SystemText.java +++ b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/gfx/SystemText.java @@ -11,13 +11,16 @@ import com.nyrds.platform.util.StringsManager; import com.watabou.glwrap.Matrix; import com.watabou.noosa.Text; - import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; public class SystemText extends Text { private static FreeTypeFontGenerator generator; + private static Map fontCache = new HashMap<>(); + private final FreeTypeFontParameter fontParameters; private final BitmapFont font; private final GlyphLayout glyphLayout; @@ -39,37 +42,29 @@ public SystemText(float baseLine) { super(0, 0, 0, 0); fontParameters = getFontParameters(baseLine); - font = generator.generateFont(fontParameters); + String fontKey = getFontKey(fontParameters); + synchronized (fontCache) { + if (!fontCache.containsKey(fontKey)) { + fontCache.put(fontKey, generator.generateFont(fontParameters)); + } + font = fontCache.get(fontKey); + } glyphLayout = new GlyphLayout(); spaceLayout = new GlyphLayout(); // Initialize spaceLayout spaceLayout.setText(font, " "); // Measure the width of a space } - private FreeTypeFontParameter getFontParameters(float baseLine) { - if (fontScale != fontScale) { - updateFontScale(); - } - - final FreeTypeFontParameter fontParameters; - fontParameters = new FreeTypeFontParameter(); - fontParameters.characters = FreeTypeFontGenerator.DEFAULT_CHARS + StringsManager.getAllCharsAsString(); - fontParameters.size = (int) (baseLine * oversample * fontScale); - fontParameters.borderColor = Color.BLACK; - fontParameters.borderWidth = oversample * fontScale; - fontParameters.flip = true; - fontParameters.genMipMaps = true; - fontParameters.magFilter = Texture.TextureFilter.Linear; - fontParameters.minFilter = Texture.TextureFilter.MipMapLinearLinear; - fontParameters.spaceX = 0; - fontParameters.spaceY = 0; - return fontParameters; - } - public SystemText(final String text, float size, boolean multiline) { super(0, 0, 0, 0); fontParameters = getFontParameters(size); - font = generator.generateFont(fontParameters); + String fontKey = getFontKey(fontParameters); + synchronized (fontCache) { + if (!fontCache.containsKey(fontKey)) { + fontCache.put(fontKey, generator.generateFont(fontParameters)); + } + font = fontCache.get(fontKey); + } glyphLayout = new GlyphLayout(); spaceLayout = new GlyphLayout(); // Initialize spaceLayout spaceLayout.setText(font, " "); // Measure the width of a space @@ -95,6 +90,30 @@ public static void updateFontScale() { fontScale = scale; } + private FreeTypeFontParameter getFontParameters(float baseLine) { + if (fontScale != fontScale) { + updateFontScale(); + } + + final FreeTypeFontParameter fontParameters; + fontParameters = new FreeTypeFontParameter(); + fontParameters.characters = FreeTypeFontGenerator.DEFAULT_CHARS + StringsManager.getAllCharsAsString(); + fontParameters.size = (int) (baseLine * oversample * fontScale); + fontParameters.borderColor = Color.BLACK; + fontParameters.borderWidth = oversample * fontScale; + fontParameters.flip = true; + fontParameters.genMipMaps = true; + fontParameters.magFilter = Texture.TextureFilter.Linear; + fontParameters.minFilter = Texture.TextureFilter.MipMapLinearLinear; + fontParameters.spaceX = 0; + fontParameters.spaceY = 0; + return fontParameters; + } + + private String getFontKey(FreeTypeFontParameter params) { + return params.size + "_" + params.characters + "_" + params.borderColor + "_" + params.borderWidth + "_" + params.flip + "_" + params.genMipMaps + "_" + params.magFilter + "_" + params.minFilter + "_" + params.spaceX + "_" + params.spaceY; + } + private void wrapText() { if (multiline && maxWidth == Integer.MAX_VALUE) { return; @@ -141,7 +160,7 @@ private void wrapText() { @Override public void destroy() { super.destroy(); - font.dispose(); + // No need to dispose font here as it's managed by the cache } @Override @@ -239,6 +258,12 @@ public static void invalidate() { generator.dispose(); generator = new FreeTypeFontGenerator(FileSystem.getInternalStorageFileHandle("fonts/pixel_font.ttf")); } + synchronized (fontCache) { + for (BitmapFont font : fontCache.values()) { + font.dispose(); + } + fontCache.clear(); + } } @Override