Skip to content

Commit

Permalink
Fix SFXPlaybacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Sep 11, 2023
1 parent cdb1160 commit d30c66d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.gurkenlabs.litiengine;

import static de.gurkenlabs.litiengine.util.io.FileUtilities.humanReadableByteCount;

import de.gurkenlabs.litiengine.configuration.ClientConfiguration;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
Expand All @@ -14,34 +17,32 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import de.gurkenlabs.litiengine.configuration.ClientConfiguration;

import static de.gurkenlabs.litiengine.util.io.FileUtilities.humanReadableByteCount;

/**
* Handles the uncaught exceptions that might occur while running a game or application with the LITIENGINE.
* Handles the uncaught exceptions that might occur while running a game or application with the
* LITIENGINE.
* <p>
* It provides proper logging of the exception in a {@code crash.txt} file in the game's root directory that can be
* further used to report the issue if it's a generic one.
* It provides proper logging of the exception in a {@code crash.txt} file in the game's root
* directory that can be further used to report the issue if it's a generic one.
* </p>
* <p>
* Depending on the configuration, the default behavior might force the game to exit upon an unexpected exception which
* can be useful to detect problems in your game early.
* Depending on the configuration, the default behavior might force the game to exit upon an
* unexpected exception which can be useful to detect problems in your game early.
*
* @see ClientConfiguration#exitOnError()
*/
public class DefaultUncaughtExceptionHandler implements UncaughtExceptionHandler {
private static final Logger log = Logger.getLogger(DefaultUncaughtExceptionHandler.class.getName());

private static final Logger log = Logger.getLogger(
DefaultUncaughtExceptionHandler.class.getName());

private volatile boolean exitOnException;
private volatile boolean dumpThreads;

/**
* Initializes a new instance of the {@code DefaultUncaughtExceptionHandler} class.
*
* @param exitOnException
* A flag indicating whether the game should exit when an unexpected exception occurs. The game will still exit
* if it encounters an Error.
* @param exitOnException A flag indicating whether the game should exit when an unexpected
* exception occurs. The game will still exit if it encounters an Error.
*/
public DefaultUncaughtExceptionHandler(boolean exitOnException) {
this(exitOnException, false);
Expand All @@ -50,11 +51,10 @@ public DefaultUncaughtExceptionHandler(boolean exitOnException) {
/**
* Initializes a new instance of the {@code DefaultUncaughtExceptionHandler} class.
*
* @param exitOnException
* A flag indicating whether the game should exit when an unexpected exception occurs. The game will still exit
* if it encounters an Error
* @param dumpThreads
* A flag indicating whether the crash report should contain an additional thread dump.
* @param exitOnException A flag indicating whether the game should exit when an unexpected
* exception occurs. The game will still exit if it encounters an Error
* @param dumpThreads A flag indicating whether the crash report should contain an additional
* thread dump.
*/
public DefaultUncaughtExceptionHandler(boolean exitOnException, boolean dumpThreads) {
this.exitOnException = exitOnException;
Expand All @@ -63,9 +63,6 @@ public DefaultUncaughtExceptionHandler(boolean exitOnException, boolean dumpThre

@Override
public void uncaughtException(final Thread t, final Throwable e) {
if (e instanceof ThreadDeath)
return;

try (PrintStream stream = new PrintStream("crash.txt")) {
stream.print(new Date() + " ");
stream.println(t.getName() + " threw an exception:");
Expand Down Expand Up @@ -107,8 +104,7 @@ public boolean dumpsThreads() {
/**
* Set whether the game will exit upon an unhandled exception.
*
* @param exit
* The flag that defines whether the game will exit upon an unhandled exception.
* @param exit The flag that defines whether the game will exit upon an unhandled exception.
*/
public void setExitOnException(boolean exit) {
this.exitOnException = exit;
Expand All @@ -117,8 +113,7 @@ public void setExitOnException(boolean exit) {
/**
* Set whether the generated crash report will contain an additional thread dump
*
* @param dumpThreads
* The flag that defines whether crash report will contain a thread dump.
* @param dumpThreads The flag that defines whether crash report will contain a thread dump.
*/
public void dumpThreads(boolean dumpThreads) {
this.dumpThreads = dumpThreads;
Expand Down Expand Up @@ -161,19 +156,23 @@ protected static String getSystemInfo() {
long freeHeapSize = Runtime.getRuntime().freeMemory();
text.append("\tMax heap size: ").append(humanReadableByteCount(maxHeapSize)).append("\n");
text.append("\tCurrent heap size: ").append(humanReadableByteCount(heapSize)).append("\n");
text.append("\tHeap used: ").append(humanReadableByteCount(heapSize - freeHeapSize)).append("\n");
text.append("\tHeap used: ").append(humanReadableByteCount(heapSize - freeHeapSize))
.append("\n");
text.append("\tFree heap: ").append(humanReadableByteCount(freeHeapSize)).append("\n");
text.append("Java Version: ").append(System.getProperty("java.runtime.name")).append(" ").append(System.getProperty("java.runtime.version"))
.append(" \n");
text.append("Java Version: ").append(System.getProperty("java.runtime.name")).append(" ")
.append(System.getProperty("java.runtime.version"))
.append(" \n");
text.append("\tVendor: ").append(System.getProperty("java.vm.vendor")).append("\n");
text.append("Uptime: ").append(Duration.ofMillis(ManagementFactory.getRuntimeMXBean().getUptime())).append("\n");
text.append("Uptime: ")
.append(Duration.ofMillis(ManagementFactory.getRuntimeMXBean().getUptime())).append("\n");
GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = g.getScreenDevices();
text.append("Screens: ").append(screens.length).append("\n");
for (int i = 0; i < screens.length; i++) {
GraphicsDevice screen = screens[i];
DisplayMode displayMode = screen.getDisplayMode();
text.append("\tScreen ").append(i).append(": ").append(displayMode.getWidth()).append("x").append(displayMode.getHeight());
text.append("\tScreen ").append(i).append(": ").append(displayMode.getWidth()).append("x")
.append(displayMode.getHeight());
if (displayMode.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN) {
text.append("@").append(displayMode.getRefreshRate()).append("hz");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

/** A {@code SoundPlayback} implementation for the playback music. */
public class MusicPlayback extends SoundPlayback {
private Track track;
private VolumeControl musicVolume;
private final Track track;
private final VolumeControl musicVolume;

MusicPlayback(Track track) throws LineUnavailableException {
super(track.getFormat());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

/** A {@code SoundPlayback} implementation for the playback of sound effects. */
public class SFXPlayback extends SoundPlayback {
private Sound sound;
private FloatControl panControl;
private Supplier<Point2D> source;
private int range;
private float volumeModifier;
private VolumeControl volume;
private boolean loop;
private final Sound sound;
private final FloatControl panControl;
private final Supplier<Point2D> source;
private final int range;
private final float volumeModifier;
private final VolumeControl volume;
private final boolean loop;

SFXPlayback(Sound sound, Supplier<Point2D> source, boolean loop, int range, float volumeModifier)
throws LineUnavailableException {
Expand All @@ -39,8 +39,8 @@ public void run() {
return;
}
} while (this.loop);
} catch (Throwable t) {
t.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} finally {
this.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public synchronized MusicPlayback playMusic(
return music;
}

try (MusicPlayback playback = new MusicPlayback(track)) {
try {
MusicPlayback playback = new MusicPlayback(track);
if (config != null) {
config.accept(playback);
}
Expand Down Expand Up @@ -601,23 +602,21 @@ public void update() {

Iterator<SFXPlayback> iter = sounds.iterator();
while (iter.hasNext()) {
try (SFXPlayback s = iter.next()) {
if (s.isPlaying()) {
s.updateLocation(listenerLocation);
} else {
iter.remove();
}
SFXPlayback s = iter.next();
if (s.isPlaying()) {
s.updateLocation(listenerLocation);
} else {
iter.remove();
}
}

Iterator<MusicPlayback> iter2 = allMusic.iterator();
while (iter.hasNext()) {
try (MusicPlayback s = iter2.next()) {
if (s.isPlaying()) {
s.setMusicVolume(Game.config().sound().getMusicVolume());
} else {
iter.remove();
}
MusicPlayback s = iter2.next();
if (s.isPlaying()) {
s.setMusicVolume(Game.config().sound().getMusicVolume());
} else {
iter.remove();
}
}

Expand Down Expand Up @@ -651,6 +650,7 @@ private SFXPlayback playSound(
return null;
}
playback.start();
System.out.println("Played sound: " + sound.getName());
return playback;
}

Expand Down
Loading

0 comments on commit d30c66d

Please sign in to comment.