destroy even more images when closing

This commit is contained in:
yugecin 2017-01-18 19:40:31 +01:00
parent 156026bd9b
commit 0eac32505b
5 changed files with 74 additions and 27 deletions

View File

@ -42,7 +42,7 @@ import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import yugecin.opsudance.Dancer;
import yugecin.opsudance.utils.SlickUtil;
/**
* Holds game data and renders all related elements.
@ -101,8 +101,16 @@ public class GameData {
* This does NOT destroy images, so be careful of memory leaks!
*/
public static void clearReferences() {
for (Grade grade : Grade.values())
for (Grade grade : Grade.values()) {
grade.menuImage = null;
}
}
public static void destroyImages() {
for (Grade grade : Grade.values()) {
SlickUtil.destroyImage(grade.menuImage);
grade.menuImage = null;
}
}
/**

View File

@ -28,6 +28,7 @@ import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;
import yugecin.opsudance.utils.SlickUtil;
/**
* Game images.
@ -461,37 +462,18 @@ public enum GameImage {
}
}
public static void destroyAll() {
public static void destroyImages() {
for (GameImage img : GameImage.values()) {
destroyAll(img.defaultImages);
destroyImage(img.defaultImage);
destroyAll(img.skinImages);
destroyImage(img.skinImage);
SlickUtil.destroyImages(img.defaultImages);
SlickUtil.destroyImage(img.defaultImage);
SlickUtil.destroyImages(img.skinImages);
SlickUtil.destroyImage(img.skinImage);
img.isSkinned = false;
img.defaultImages = img.skinImages = null;
img.defaultImage = img.skinImage = null;
}
}
public static void destroyAll(Image[] imgs) {
if (imgs == null) {
return;
}
for (Image i : imgs) {
destroyImage(i);
}
}
public static void destroyImage(Image image) {
if (image == null) {
return;
}
try {
image.destroy();
} catch (SlickException ignored) {
}
}
/**
* Returns the bitmask image type from a type string.
* @param type the type string

View File

@ -22,6 +22,7 @@ import itdelatrisu.opsu.Options;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
@ -59,6 +60,14 @@ public class Beatmap implements Comparable<Beatmap> {
*/
public static void clearBackgroundImageCache() { bgImageCache.clear(); }
public static void destroyBackgroundImageCache() {
Collection<ImageLoader> values = bgImageCache.values();
for (ImageLoader value : values) {
value.destroy();
}
bgImageCache.clear();
}
/** The OSU File object associated with this beatmap. */
private File file;

View File

@ -17,9 +17,11 @@
*/
package yugecin.opsudance.core;
import itdelatrisu.opsu.GameData;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.Options;
import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.beatmap.Beatmap;
import itdelatrisu.opsu.ui.Fonts;
import org.lwjgl.Sys;
import org.lwjgl.openal.AL;
@ -213,7 +215,9 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
public void teardown() {
InternalTextureLoader.get().clear();
GameImage.destroyAll();
GameImage.destroyImages();
GameData.Grade.destroyImages();
Beatmap.destroyBackgroundImageCache();
Display.destroy();
}

View File

@ -0,0 +1,44 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.utils;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class SlickUtil {
public static void destroyImages(Image[] imgs) {
if (imgs == null) {
return;
}
for (Image i : imgs) {
destroyImage(i);
}
}
public static void destroyImage(Image image) {
if (image == null) {
return;
}
try {
image.destroy();
} catch (SlickException ignored) {
}
}
}