Skip to content

Commit

Permalink
libgdx - fix few memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhael-Danilov committed Dec 8, 2024
1 parent c970bba commit c840e1d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, SmartTexture> all = new HashMap<>();
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 1 addition & 3 deletions RemixedDungeon/src/main/java/com/watabou/noosa/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions RemixedDungeonDesktop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,6 +133,7 @@ public void pause() {

MusicManager.INSTANCE.pause();
Sample.INSTANCE.pause();
Sample.INSTANCE.reset();

Script.reset();
}
Expand All @@ -158,7 +158,6 @@ public void dispose() {
}

MusicManager.INSTANCE.mute();
Sample.INSTANCE.reset();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;

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;
Expand Down Expand Up @@ -110,6 +110,7 @@ public void save(String path) {
}

public void dispose() {

bmp.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, BitmapFont> fontCache = new HashMap<>();

private final FreeTypeFontParameter fontParameters;
private final BitmapFont font;
private final GlyphLayout glyphLayout;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c840e1d

Please sign in to comment.