Fix loading issues for OGG on windows

This commit is contained in:
Brendan Robert 2024-01-23 21:44:49 -06:00
parent e435aa3a7a
commit 191066f65c
6 changed files with 36 additions and 16 deletions

View File

@ -1,3 +1,4 @@
{ {
"java.configuration.updateBuildConfiguration": "automatic" "java.configuration.updateBuildConfiguration": "automatic",
"java.debug.settings.onBuildFailureProceed": true
} }

View File

@ -158,7 +158,10 @@ public class Utility {
if (isHeadless) { if (isHeadless) {
return Optional.empty(); return Optional.empty();
} }
Image img = loadIcon(filename).get(); Optional<Image> img = loadIcon(filename);
if (img.isEmpty()) {
return Optional.empty();
}
Label label = new Label() { Label label = new Label() {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
@ -174,7 +177,7 @@ public class Utility {
return getText().hashCode(); return getText().hashCode();
} }
}; };
label.setGraphic(new ImageView(img)); label.setGraphic(new ImageView(img.get()));
label.setAlignment(Pos.CENTER); label.setAlignment(Pos.CENTER);
label.setContentDisplay(ContentDisplay.TOP); label.setContentDisplay(ContentDisplay.TOP);
label.setTextFill(Color.WHITE); label.setTextFill(Color.WHITE);

View File

@ -59,6 +59,9 @@ public class Joystick extends Device {
public Joystick(int port, Computer computer) { public Joystick(int port, Computer computer) {
super(computer); super(computer);
if (LawlessLegends.getApplication() == null) {
return;
}
Stage stage = LawlessLegends.getApplication().primaryStage; Stage stage = LawlessLegends.getApplication().primaryStage;
// Register a mouse handler on the primary stage that tracks the // Register a mouse handler on the primary stage that tracks the
// mouse x/y position as a percentage of window width and height // mouse x/y position as a percentage of window width and height

View File

@ -2,6 +2,8 @@ package jace.lawless;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
@ -21,21 +23,20 @@ public class Media {
File tempFile; File tempFile;
public Media(String resourcePath) throws IOException { public Media(String resourcePath) throws IOException {
// Copy resource to temp file because STBVorbis can't read from jar System.out.println("Loading media: " + resourcePath);
tempFile = File.createTempFile("temp", ".ogg"); byte[] oggFile;
tempFile.deleteOnExit(); try (InputStream oggStream = getClass().getResourceAsStream(resourcePath)) {
getClass().getResource(resourcePath).openStream().transferTo(new java.io.FileOutputStream(tempFile)); oggFile = oggStream.readAllBytes();
String canonicalPath = tempFile.getAbsolutePath(); }
// Get caononical file path from relative resource path
// String canonicalPath = URLDecoder.decode(getClass().getResource(resourcePath).getPath(), "UTF-8");
System.out.println("Loading media: " + canonicalPath);
try (MemoryStack stack = MemoryStack.stackPush()) { try (MemoryStack stack = MemoryStack.stackPush()) {
ByteBuffer oggBuffer = MemoryUtil.memAlloc(oggFile.length);
oggBuffer.put(oggFile);
oggBuffer.flip();
IntBuffer error = stack.callocInt(1); IntBuffer error = stack.callocInt(1);
Long decoder = STBVorbis.stb_vorbis_open_filename(canonicalPath, error, null); Long decoder = STBVorbis.stb_vorbis_open_memory(oggBuffer, error, null);
if (decoder == null || decoder <= 0) { if (decoder == null || decoder <= 0) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + getError(error.get(0))); throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + getError(error.get(0)) + " -- file is located at " + resourcePath);
} }
STBVorbisInfo info = STBVorbisInfo.malloc(stack); STBVorbisInfo info = STBVorbisInfo.malloc(stack);
STBVorbis.stb_vorbis_get_info(decoder, info); STBVorbis.stb_vorbis_get_info(decoder, info);

View File

@ -0,0 +1,12 @@
package jace;
import org.junit.BeforeClass;
import javafx.application.Platform;
public abstract class AbstractFXTest {
@BeforeClass
public static void initJfxRuntime() {
Platform.startup(() -> {});
}
}

View File

@ -9,7 +9,7 @@ import jace.core.SoundMixer.SoundBuffer;
import jace.lawless.LawlessHacks; import jace.lawless.LawlessHacks;
import jace.lawless.Media; import jace.lawless.Media;
public class SoundTest { public class SoundTest extends AbstractFXTest {
@Test @Test
public void musicDecodeTest() { public void musicDecodeTest() {
// For every song in the music folder, decode it and print out the duration // For every song in the music folder, decode it and print out the duration
@ -25,7 +25,7 @@ public class SoundTest {
} }
} }
// @Test //@Test (Only use this to ensure the sound engine produces audible output, it's otherwise annoying to hear all the time)
public void soundGenerationTest() { public void soundGenerationTest() {
try { try {
System.out.println("Performing sound test..."); System.out.println("Performing sound test...");