Skip to content

Commit

Permalink
Source Code Changes for Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
OmkarDev authored Jan 19, 2025
1 parent 90faaf9 commit f547a92
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
Binary file modified res/data/data.bin
Binary file not shown.
4 changes: 3 additions & 1 deletion src/a.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;

import utils.Utils;

//import javax.microedition.rms.RecordComparator;
//import javax.microedition.rms.RecordFilter;
//import javax.microedition.rms.RecordStore;
Expand All @@ -11,7 +13,7 @@ public final class a {

public final void loadLevelNo() {
try {
byte[] data = Files.readAllBytes(Paths.get(getClass().getResource("/data/data.bin").toURI()));
byte[] data = Files.readAllBytes(Paths.get(Utils.res_folder+"/data/data.bin"));
this.levels_reached = data[1];
} catch (Exception e) {
e.printStackTrace();
Expand Down
52 changes: 30 additions & 22 deletions src/b.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

Expand All @@ -35,6 +35,8 @@
import javax.sound.sampled.Clip;
import javax.swing.JFrame;

import utils.Utils;

public final class b implements Runnable, KeyListener {
private Random a;
private static int scrn_width = 176;
Expand Down Expand Up @@ -187,8 +189,8 @@ private void a() {
InputStream bg_music;
InputStream musicDie;
try {
bg_music = new FileInputStream(new File(getClass().getResource("music.mid").toURI()));
musicDie = new FileInputStream(new File(getClass().getResource("musicDie.mid").toURI()));
bg_music = Utils.deepCopy(new FileInputStream(Utils.res_folder + "/music.mid"));
musicDie = Utils.deepCopy(new FileInputStream(Utils.res_folder + "/musicDie.mid"));
musicBGStream = AudioSystem.getAudioInputStream(new BufferedInputStream(bg_music));
musicDieStream = AudioSystem.getAudioInputStream(new BufferedInputStream(musicDie));
bg_clip = AudioSystem.getClip();
Expand Down Expand Up @@ -524,7 +526,7 @@ private void b_void() {
try {
byte[] data = getBytesFromSave();
data[0] = ak;
FileOutputStream write_data = new FileOutputStream(new File("res/data/data.bin"));
FileOutputStream write_data = new FileOutputStream(new File(Utils.res_folder + "/data/data.bin"));
write_data.write(data);
write_data.close();
} catch (Exception e) {
Expand All @@ -545,7 +547,8 @@ private void c() {
private static boolean d() {
DataInputStream data;
try {
data = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("res/data/data.bin"))));
InputStream stream = new FileInputStream(new File(Utils.res_folder + "/data/data.bin"));
data = new DataInputStream(stream);
byte b = getBytesFromSave()[0];
if (b == (byte) 0xFF) {
return false;
Expand Down Expand Up @@ -671,11 +674,11 @@ private static void b_void_short_array2d(final short[][] array) {
}
}
}

public static byte[] getBytesFromSave() {
byte[] data = null;
try {
data = Files.readAllBytes(Paths.get(b.class.getResource("/data/data.bin").toURI()));
data = Files.readAllBytes(Paths.get(Utils.res_folder+"/data/data.bin"));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -1140,21 +1143,26 @@ public final void paint(final Graphics2D graphics) {
}

public final void run() {
while (b.canRun) {
try {
this.render();
this.bw = 60L - (System.currentTimeMillis() - this.bv);
if (this.bw <= 0L) {
this.bw = 15L;
try {
while (b.canRun) {
try {
this.render();
this.bw = 60L - (System.currentTimeMillis() - this.bv);
if (this.bw <= 0L) {
this.bw = 15L;
}
Thread.sleep(this.bw);
this.bv = System.currentTimeMillis();
} catch (Exception ex) {
ex.printStackTrace();
}
Thread.sleep(this.bw);
this.bv = System.currentTimeMillis();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (rocket.a != null) {
rocket.a.destroyApp(true);
if (rocket.a != null) {
rocket.a.destroyApp(true);
System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
Expand Down Expand Up @@ -1422,7 +1430,7 @@ private void g() {

private void saveNewScore(final int newScore) {
try {
File file = new File("res/data/data.bin");
File file = new File(Utils.res_folder+"/data/data.bin");
byte[] data = getBytesFromSave();
for (int index = 0; index < 5; index++) {
int offset = 2 + 4 * index;
Expand Down Expand Up @@ -1895,7 +1903,7 @@ private void a(final int n) {
if (level_no > a.levels_reached) {
a.levels_reached = level_no;
try {
File file = new File("res/data/data.bin");
File file = new File(Utils.res_folder+"/data/data.bin");
byte[] data = getBytesFromSave();
data[1] = (byte) a.levels_reached;
FileOutputStream writeFile = new FileOutputStream(file);
Expand Down
21 changes: 20 additions & 1 deletion src/rocket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.JFrame;

import utils.Utils;
import static javax.swing.JOptionPane.showMessageDialog;
//import javax.microedition.lcdui.Display;
//import javax.microedition.lcdui.Displayable;
//import javax.microedition.midlet.MIDlet;
Expand All @@ -26,9 +33,21 @@ public rocket() {
d.thread.start();
}

public static void main(String[] args) {
public static void main(String[] args) throws URISyntaxException {
Utils.jar_folder = (getJarDirectory().toString());
Utils.res_folder = Utils.jar_folder + "/" + "res";
File directory = new File(Utils.res_folder);
if (directory.exists() == false) {
showMessageDialog(null, "resource folder not found at jar location: "+Utils.jar_folder);
System.exit(0);
}
new rocket();
}
private static Path getJarDirectory() throws URISyntaxException {
Path jarPath = Paths.get(rocket.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.toAbsolutePath();
return jarPath.getParent();
}

public void startApp() {
a = this;
Expand Down
6 changes: 2 additions & 4 deletions src/utils/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;

import javax.imageio.ImageIO;

Expand All @@ -12,7 +11,7 @@ public class Image {
public static java.awt.Image createImage(String string) {
BufferedImage img = null;
try {
img = ImageIO.read(utils.Image.class.getResource("../"+string));
img = ImageIO.read(Utils.deepCopy(new FileInputStream(Utils.res_folder+string)));
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -23,7 +22,6 @@ public static void drawImage(Graphics g, java.awt.Image img, int x, int y, int a
if (anchor == 20 || anchor == 0) {
g.drawImage(img, x, y, null);
} else if (anchor == 17) {
// top | horizontal center
g.drawImage(img, x - (img.getWidth(null) / 2), y, null);
} else {
System.err.println("Anchor: " + anchor);
Expand Down
24 changes: 24 additions & 0 deletions src/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Utils {


public static String jar_folder = null;
public static String res_folder = null;

public static InputStream deepCopy(InputStream inputStream) throws IOException {
long past = System.currentTimeMillis();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
}

0 comments on commit f547a92

Please sign in to comment.