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

View File

@ -59,6 +59,9 @@ public class Joystick extends Device {
public Joystick(int port, Computer computer) {
super(computer);
if (LawlessLegends.getApplication() == null) {
return;
}
Stage stage = LawlessLegends.getApplication().primaryStage;
// Register a mouse handler on the primary stage that tracks the
// 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.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
@ -21,21 +23,20 @@ public class Media {
File tempFile;
public Media(String resourcePath) throws IOException {
// Copy resource to temp file because STBVorbis can't read from jar
tempFile = File.createTempFile("temp", ".ogg");
tempFile.deleteOnExit();
getClass().getResource(resourcePath).openStream().transferTo(new java.io.FileOutputStream(tempFile));
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);
System.out.println("Loading media: " + resourcePath);
byte[] oggFile;
try (InputStream oggStream = getClass().getResourceAsStream(resourcePath)) {
oggFile = oggStream.readAllBytes();
}
try (MemoryStack stack = MemoryStack.stackPush()) {
ByteBuffer oggBuffer = MemoryUtil.memAlloc(oggFile.length);
oggBuffer.put(oggFile);
oggBuffer.flip();
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) {
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);
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.Media;
public class SoundTest {
public class SoundTest extends AbstractFXTest {
@Test
public void musicDecodeTest() {
// 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() {
try {
System.out.println("Performing sound test...");