Skip to content

Commit

Permalink
Use OpenAL for sounds
Browse files Browse the repository at this point in the history
Changes:
* The sound code has been rewritten to use JOAL (OpenAL for Java) instead of 'javax.sound'.
* The sound files have been sorted into their respective folders and those that are not used have been removed.
* Now, the sounds that mobs play will only work in a certain radius near the mob to avoid clutter and conflicts with other sounds playing.
  • Loading branch information
TheBigEye committed Mar 23, 2024
1 parent 1f544d7 commit 2b74b29
Show file tree
Hide file tree
Showing 114 changed files with 551 additions and 614 deletions.
12 changes: 7 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'java'
apply plugin: 'application'

project.version = '0.5'
project.version = '1.0'

sourceCompatibility = 8
mainClassName = 'minicraft.core.Game'
Expand All @@ -15,12 +15,14 @@ repositories {
}

dependencies {
implementation 'org.jetbrains:annotations:24.0.0'
implementation 'org.json:json:20230227'
implementation 'org.jetbrains:annotations:24.1.0'
implementation 'org.json:json:20240205'
implementation 'com.konghq:unirest-java:3.14.5'
implementation 'org.tinylog:tinylog-api:2.6.2'
implementation 'org.tinylog:tinylog-impl:2.6.2'
implementation 'org.tinylog:tinylog-api:2.7.0'
implementation 'org.tinylog:tinylog-impl:2.7.0'
implementation 'com.github.JnCrMx:discord-game-sdk4j:v0.5.5'
implementation 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'
implementation 'org.jogamp.joal:joal-main:2.3.2'
}

buildscript {
Expand Down
59 changes: 6 additions & 53 deletions src/main/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.awt.GraphicsEnvironment;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand All @@ -14,10 +12,6 @@
import org.jetbrains.annotations.Nullable;
import org.tinylog.Logger;

import de.jcm.discordgamesdk.Core;
import de.jcm.discordgamesdk.CreateParams;
import de.jcm.discordgamesdk.GameSDKException;
import de.jcm.discordgamesdk.activity.Activity;
import minicraft.core.io.FileHandler;
import minicraft.core.io.InputHandler;
import minicraft.core.io.Settings;
Expand Down Expand Up @@ -45,7 +39,7 @@ protected Game() {} // Can't instantiate the Game class.
public static boolean debug = false; // --debug arg

public static final String NAME = "Aircraft"; // This is the name on the application window
public static final String BUILD = "0.5"; // Aircraft version
public static final String BUILD = "1.0"; // Aircraft version

// TODO: not use anymore Minicraft plus versioning
public static final Version VERSION = new Version("2.2.0-dev2"); // Minicraft plus mod base version
Expand All @@ -71,7 +65,7 @@ public static void exitDisplay() {
Logger.warn("Game tried to exit display, but no menu is open.");
return;
}
Sound.Menu_back.playOnDisplay();
Sound.play("Menu_back");
newDisplay = display.getParent();
}

Expand Down Expand Up @@ -163,9 +157,6 @@ public static void main(String[] args) {
" Java Version: " + Utils.JAVA_VERSION + ", " + Utils.JAVA_VENDOR + "\n" +
" Java VM Version: " + Utils.JVM_NAME + " (" + Utils.JVM_INFO + "), " + Utils.JVM_VENDOR + "\n" +
" Memory: " + Utils.memoryInfo() + "\n\n" +

"Flags: " + "\n" +
" isAudioOutputAvailible: " + Sound.isAudioOutputAvailable() + "\n\n" +

"~~ ERROR ~~ " + "\n" +

Expand All @@ -187,50 +178,12 @@ public static void main(String[] args) {
Initializer.frame.pack();
Initializer.frame.setVisible(true);
});


// START EVENTS

// Clean previously downloaded native files
FileHandler.cleanNativesFiles();

// Discord rich presence
Core discordCore = null;
try {
final long CLIENT_ID = 981764521616093214L;
final String LARGE_TEXT = "Aircraft " + BUILD + ", Nice!";
final String SMALL_TEXT = "Minicraft+ mod";

Core.initDownload(); // download java-discord SDK
CreateParams params = new CreateParams();
params.setClientID(CLIENT_ID); // Discord APP ID
params.setFlags(CreateParams.getDefaultFlags());
params.setFlags(CreateParams.Flags.NO_REQUIRE_DISCORD);
discordCore = new Core(params);

Activity activity = new Activity();
activity.assets().setLargeImage("logo"); // Big image
activity.assets().setLargeText(LARGE_TEXT); // Big image text
activity.assets().setSmallImage("small-logo"); // Small image
activity.assets().setSmallText(SMALL_TEXT); // Small image text
activity.timestamps().setStart(Instant.now()); // Start timer

discordCore.activityManager().updateActivity(activity);
if (debug) Logger.debug("Initializing discord RPC ...");

} catch (GameSDKException exception) {
Logger.error("Failed to initialize Discord SDK, no discord detected!");
exception.printStackTrace();

} catch (UnknownHostException exception) {
Logger.error("Failed to download Discord SDK, no internet connection!");
exception.printStackTrace();

} catch (Exception exception) {
Logger.error("Unknown error");
exception.printStackTrace();
}

// Parses the command line arguments
Initializer.parseArgs(args);

Expand Down Expand Up @@ -265,12 +218,12 @@ public static void main(String[] args) {
setDisplay(new TitleDisplay());

// Start tick() count and start the game
Initializer.run(discordCore);

Initializer.run();

// EXIT EVENTS
Sound.shutdown();

Logger.debug("Main game loop ended; Terminating application...");
// EXIT EVENTS
Logger.debug("Game main loop ended, terminating application ...");

System.exit(0);
}
Expand Down
53 changes: 50 additions & 3 deletions src/main/java/minicraft/core/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.UnknownHostException;
import java.time.Instant;
import java.util.Objects;

import javax.imageio.ImageIO;
Expand All @@ -20,6 +22,9 @@
import org.tinylog.Logger;

import de.jcm.discordgamesdk.Core;
import de.jcm.discordgamesdk.CreateParams;
import de.jcm.discordgamesdk.GameSDKException;
import de.jcm.discordgamesdk.activity.Activity;
import minicraft.core.io.FileHandler;

/*
Expand Down Expand Up @@ -53,7 +58,6 @@ static void parseArgs(String[] args) {
i++;
saveDir = args[i];
} else if (args[i].equals("--fullscreen")) {
// Initializes fullscreen
Updater.FULLSCREEN = true;
}
}
Expand All @@ -70,7 +74,47 @@ static void parseArgs(String[] args) {
* - fires the command to render out the screen.
* - update the discord rpc
*/
static void run(Core discordCore) {
static void run() {

// Discord rich presence
Core discordCore = null;

try {
if (debug) Logger.debug("Initializing discord RPC ...");

final long CLIENT_ID = 981764521616093214L;
final String LARGE_TEXT = "Aircraft " + BUILD + ", Nice!";
final String SMALL_TEXT = "Minicraft+ mod";

Core.initDownload();
CreateParams params = new CreateParams();
params.setClientID(CLIENT_ID);
params.setFlags(CreateParams.getDefaultFlags());
params.setFlags(CreateParams.Flags.NO_REQUIRE_DISCORD);
discordCore = new Core(params);

Activity activity = new Activity();
activity.assets().setLargeImage("logo");
activity.assets().setLargeText(LARGE_TEXT);
activity.assets().setSmallImage("small-logo");
activity.assets().setSmallText(SMALL_TEXT);
activity.timestamps().setStart(Instant.now());

discordCore.activityManager().updateActivity(activity);

} catch (GameSDKException exception) {
Logger.warn("Failed to initialize Discord SDK, no discord running!");
exception.printStackTrace();

} catch (UnknownHostException exception) {
Logger.warn("Failed to download Discord SDK, no internet connection!");
exception.printStackTrace();

} catch (Exception exception) {
Logger.error("Unknown error");
exception.printStackTrace();
}

long lastTick = System.nanoTime();
long lastRender = System.nanoTime();
double unprocessed = 0; // delta?
Expand Down Expand Up @@ -129,9 +173,12 @@ static void run(Core discordCore) {
fra = (int) Math.round(frames * 1000D / interval); // saves total frames in last second
tik = (int) Math.round(ticks * 1000D / interval); // saves total ticks in last second
frames = 0; // resets frames
ticks = 0; // resets ticks; ie, frames and ticks only are per second
ticks = 0; // resets ticks; ie, frames and ticks only are per second
}
}

// Finalize discord rich presence
discordCore.activityManager().clearActivity(Core.DEFAULT_CALLBACK);
}


Expand Down
3 changes: 2 additions & 1 deletion src/main/java/minicraft/core/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public static void tick() {
if (!Renderer.canvas.hasFocus()) {
input.releaseAll();
}

if (Renderer.canvas.hasFocus()) {
gameTime++;

Expand Down Expand Up @@ -302,7 +303,7 @@ public static void changeTimeOfDay(int timeOfDay) {
if (timeOfDay > 0 && timeOfDay < times.length) {
changeTimeOfDay(times[timeOfDay]); // it just references the other one.
} else {
Logger.warn("Time " + timeOfDay + " does not exist.");
Logger.warn("Time {} does not exist", timeOfDay);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/minicraft/core/io/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public FileVisitResult visitFileFailed(Path path, IOException exception) {
* Remove all the natives files downloaded
*/
public static void cleanNativesFiles() {
Logger.info("Looking for old natives & assets to clean up ...");

// Remove these native libraries
String[] folderNames = {
"java-discord"
Expand Down
Loading

0 comments on commit 2b74b29

Please sign in to comment.