Skip to content

Commit

Permalink
Merge remote-tracking branch 'BenjaminAmos/dispose-textures-properly'…
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
Cervator committed Oct 31, 2021
2 parents eca02c8 + cccb3ce commit 60b63e9
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 12 deletions.
7 changes: 7 additions & 0 deletions engine/src/main/java/org/destinationsol/SolApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.destinationsol.game.SolNames;
import org.destinationsol.game.WorldConfig;
import org.destinationsol.game.drawables.DrawableLevel;
import org.destinationsol.game.drawables.SpriteManager;
import org.destinationsol.health.components.Health;
import org.destinationsol.location.components.Angle;
import org.destinationsol.location.components.Velocity;
Expand Down Expand Up @@ -377,6 +378,12 @@ public void dispose() {
}

inputManager.dispose();

SpriteManager.clearCache();

Assets.getAssetHelper().dispose();

moduleManager.dispose();
}

public SolGame getGame() {
Expand Down
13 changes: 13 additions & 0 deletions engine/src/main/java/org/destinationsol/assets/AssetHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.destinationsol.assets.sound.OggSound;
import org.destinationsol.assets.ui.UIFormat;
import org.destinationsol.assets.ui.UISkinFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.Asset;
import org.terasology.gestalt.assets.AssetData;
import org.terasology.gestalt.assets.AssetType;
Expand All @@ -47,10 +49,12 @@
import org.terasology.reflection.reflect.ReflectFactory;
import org.terasology.reflection.reflect.ReflectionReflectFactory;

import java.io.IOException;
import java.util.Optional;
import java.util.Set;

public class AssetHelper {
private static final Logger logger = LoggerFactory.getLogger(AssetHelper.class);
private ModuleAwareAssetTypeManager assetTypeManager;

public AssetHelper() {
Expand Down Expand Up @@ -115,4 +119,13 @@ public Set<ResourceUrn> listAssets(Class<? extends Asset<?>> type, String asset,
});
return list;
}

public void dispose() {
try {
assetTypeManager.unloadEnvironment();
assetTypeManager.close();
} catch (IOException e) {
logger.error("Error closing assetTypeManager", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,30 @@
import com.badlogic.gdx.graphics.Texture;
import org.terasology.gestalt.assets.Asset;
import org.terasology.gestalt.assets.AssetType;
import org.terasology.gestalt.assets.DisposableResource;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.module.annotations.RegisterAssetType;
import org.terasology.nui.UITextureRegion;

@RegisterAssetType(folderName = {"textures", "ships", "items", "grounds", "mazes", "asteroids", "fonts"}, factoryClass = DSTextureFactory.class)
public class DSTexture extends Asset<DSTextureData> {
private final TextureResources resources;
private DSTextureData dsTextureData;

public DSTexture(ResourceUrn urn, AssetType<?, DSTextureData> assetType, DSTextureData data) {
super(urn, assetType);
public DSTexture(ResourceUrn urn, AssetType<?, DSTextureData> assetType, DSTextureData data, TextureResources resources) {
super(urn, assetType, resources);
this.resources = resources;
reload(data);
}

public static DSTexture create(ResourceUrn urn, AssetType<?, DSTextureData> assetType, DSTextureData data) {
return new DSTexture(urn, assetType, data, new TextureResources());
}

@Override
protected void doReload(DSTextureData data) {
this.dsTextureData = data;
resources.texture = data.getTexture();
}

public Texture getTexture() {
Expand All @@ -42,9 +50,21 @@ public Texture getTexture() {

/**
* Obtains the NUI {@link UITextureRegion} associated with this texture.
* @return the assoicated UI texture
* @return the associated UI texture
*/
public UITextureRegion getUiTexture() {
return dsTextureData;
}

private static class TextureResources implements DisposableResource {
private Texture texture;

/**
* Closes the asset. It is expected this should only happen once.
*/
@Override
public void close() {
texture.dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class DSTextureFactory implements AssetFactory<DSTexture, DSTextureData>

@Override
public DSTexture build(ResourceUrn urn, AssetType<DSTexture, DSTextureData> type, DSTextureData data) {
return new DSTexture(urn, type, data);
return DSTexture.create(urn, type, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,8 @@ public static TextureAtlas.AtlasRegion[] getSequentialRegions(TextureAtlas.Atlas

return regions;
}

public static void clearCache() {
sprites.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package org.destinationsol.game.screens;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import org.destinationsol.GameOptions;
import org.destinationsol.SolApplication;
import org.destinationsol.assets.Assets;
import org.destinationsol.common.SolColor;
import org.destinationsol.menu.MenuLayout;
import org.destinationsol.ui.SolInputManager;
Expand Down Expand Up @@ -58,9 +60,12 @@ public WaypointCreationScreen(MenuLayout menuLayout, GameOptions gameOptions, Ma
previewRect = menuLayout.buttonRect(-1, -1);
outcomeColor = Color.BLACK.cpy();

sliderRed = new SolUiSlider(menuLayout.buttonRect(-1, 0), "Red: ", 1, 2);
sliderGreen = new SolUiSlider(menuLayout.buttonRect(-1, 1), "Green: ", 1, 2);
sliderBlue = new SolUiSlider(menuLayout.buttonRect(-1, 2), "Blue:", 1, 2);
TextureAtlas.AtlasRegion sliderTexture = Assets.getAtlasRegion("engine:ui/slider");
TextureAtlas.AtlasRegion sliderMarkerTexture = Assets.getAtlasRegion("engine:ui/sliderMarker");

sliderRed = new SolUiSlider(menuLayout.buttonRect(-1, 0), "Red: ", 1, 2,sliderTexture, sliderMarkerTexture);
sliderGreen = new SolUiSlider(menuLayout.buttonRect(-1, 1), "Green: ", 1, 2, sliderTexture, sliderMarkerTexture);
sliderBlue = new SolUiSlider(menuLayout.buttonRect(-1, 2), "Blue:", 1, 2, sliderTexture, sliderMarkerTexture);

background = menuLayout.background(-1, -1, 6);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,8 @@ public void printAvailableModules() {
logger.info("Module Discovered: {}", module);
}
}

public void dispose() {
environment.close();
}
}
10 changes: 6 additions & 4 deletions engine/src/main/java/org/destinationsol/ui/SolUiSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import org.destinationsol.assets.Assets;
import org.destinationsol.common.SolColor;
import org.destinationsol.common.SolMath;

public class SolUiSlider {
private static final float HANDLE_SCALE = 0.0125f;

private static final TextureAtlas.AtlasRegion sliderTexture = Assets.getAtlasRegion("engine:ui/slider");
private static final TextureAtlas.AtlasRegion sliderMarkerTexture = Assets.getAtlasRegion("engine:ui/sliderMarker");
private final TextureAtlas.AtlasRegion sliderTexture;
private final TextureAtlas.AtlasRegion sliderMarkerTexture;

private final Rectangle rectangle;
private float value;
private String text;
private final int trimAt;

public SolUiSlider(Rectangle sliderRectangle, String text, float startingValue, int trimAt) {
public SolUiSlider(Rectangle sliderRectangle, String text, float startingValue, int trimAt,
TextureAtlas.AtlasRegion sliderTexture, TextureAtlas.AtlasRegion sliderMarkerTexture) {
rectangle = sliderRectangle;
value = SolMath.clamp(value);
this.text = text;
this.trimAt = trimAt;
this.sliderTexture = sliderTexture;
this.sliderMarkerTexture = sliderMarkerTexture;
}

public void draw(UiDrawer uiDrawer) {
Expand Down
3 changes: 2 additions & 1 deletion engine/src/main/java/org/destinationsol/ui/UiDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum TextAlignment {
private Matrix4 straightMtx;
private final float uiLineWidth;

public static final TextureRegion whiteTexture = Assets.getAtlasRegion("engine:uiWhiteTex");
public final TextureRegion whiteTexture;
public final Rectangle filler;
private final CommonDrawer drawer;
//TODO WTF is `isTextMode` for? discuss and potentially (=probably) remove
Expand All @@ -52,6 +52,7 @@ public enum TextAlignment {
public UiDrawer(CommonDrawer commonDrawer) {
displayDimensions = SolApplication.displayDimensions;
drawer = commonDrawer;
whiteTexture = Assets.getAtlasRegion("engine:uiWhiteTex");

uiLineWidth = 1.0f / displayDimensions.getHeight();

Expand Down

0 comments on commit 60b63e9

Please sign in to comment.