Skip to content

Commit e65273b

Browse files
committed
Version 1.5.1: Music update
added current song to HUD started ground work for menu optimized rendering: limited frames 60fps
1 parent 9051a6b commit e65273b

File tree

6 files changed

+153
-16
lines changed

6 files changed

+153
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
JavaGame Alpha v1.5
1+
JavaGame Alpha v1.5.1
22
===================
33

44
#####What is JavaGame?

src/com/redomar/game/Game.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class Game extends Canvas implements Runnable {
5858
private Player player;
5959
private Music music = new Music();
6060
private Thread musicThread = new Thread(music);
61-
private String nowPlaying = "Playing Music";
61+
private String nowPlaying;
6262

6363
private boolean notActive = true;
6464

@@ -147,7 +147,7 @@ public void run() {
147147
long now = System.nanoTime();
148148
delta += (now - lastTime) / nsPerTick;
149149
lastTime = now;
150-
boolean shouldRender = true;
150+
boolean shouldRender = false;
151151

152152
while (delta >= 1) {
153153
ticks++;
@@ -235,6 +235,7 @@ public void render() {
235235
g.drawString("MUSIC is OFF | press 'M' to start", 0, getHeight()-8);
236236
} else{
237237
g.drawString("MUSIC is ON | You cannot turn off the music", 0, getHeight()-8);
238+
setNowPlaying(WordUtils.capitalize(music.getSongName()[music.getSongNumber()].substring(7, (music.getSongName()[music.getSongNumber()].length() - 4))));
238239
g.drawString(nowPlaying, (getWidth() - nowPlaying.length()) - (120 + nowPlaying.length()), getHeight() - 20 );
239240
}
240241
g.dispose();
@@ -243,25 +244,25 @@ public void render() {
243244

244245
public static void main(String[] args) {
245246
try {
246-
JSplash splash = new JSplash(Game.class.getResource("/splash/splash.png"), true, true, false, "v1.5 Alpha", null, Color.RED, Color.ORANGE);
247+
JSplash splash = new JSplash(Game.class.getResource("/splash/splash.png"), true, true, false, "v1.5.1 Alpha", null, Color.RED, Color.ORANGE);
247248
splash.toFront();
248249
splash.splashOn();
249250
splash.setProgress(10, "Initializing Game");
250-
Thread.sleep(750);
251-
splash.setProgress(25, "Loading Classes");
252251
Thread.sleep(500);
252+
splash.setProgress(25, "Loading Classes");
253+
Thread.sleep(250);
253254
splash.setProgress(35, "Applying Configurations");
254-
Thread.sleep(500);
255+
Thread.sleep(250);
255256
splash.setProgress(40, "Loading Sprites");
256-
Thread.sleep(750);
257-
splash.setProgress(50, "Loading Textures");
258257
Thread.sleep(500);
258+
splash.setProgress(50, "Loading Textures");
259+
Thread.sleep(250);
259260
splash.setProgress(60, "Loading Map");
260-
Thread.sleep(1500);
261+
Thread.sleep(750);
261262
splash.setProgress(80, "Configuring Map");
262-
Thread.sleep(500);
263+
Thread.sleep(250);
263264
splash.setProgress(90, "Pulling InputPanes");
264-
Thread.sleep(750);
265+
Thread.sleep(500);
265266
splash.setProgress(92, "Aquring data: Multiplayer");
266267
Thread.sleep(200);
267268
Jdata_Host = JOptionPane.showConfirmDialog(getGame(), "Do you want to be the HOST?");
@@ -277,6 +278,7 @@ public static void main(String[] args) {
277278
Thread.sleep(500);
278279
splash.splashOff();
279280
new Game().start();
281+
// new Menu().start();
280282
} catch (Exception e) {
281283
e.printStackTrace();
282284
}

src/com/redomar/game/lib/Music.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class Music implements Runnable{
1212

1313
private InputStream file;
1414
private Player musicPlayer;
15-
private String songName[] = {"/music/yoshi.mp3", "/music/Towards The End.mp3"};
16-
private int songNumber;
15+
private static String songName[] = {"/music/yoshi song.mp3", "/music/Towards The End.mp3", "/music/Towards The End.mp3"};
16+
private static int songNumber;
1717

1818
private static Random rand = new Random();
1919

@@ -22,7 +22,7 @@ public Music(InputStream url){
2222
}
2323

2424
public Music() {
25-
25+
Music.songNumber = rand.nextInt(3);
2626
}
2727

2828
public void Play(){
@@ -35,6 +35,10 @@ public void Play(){
3535
System.out.println(e);
3636
}
3737
}
38+
39+
public synchronized void start(){
40+
this.run();
41+
}
3842

3943
@Override
4044
public void run() {
@@ -57,6 +61,14 @@ public void stop() {
5761
}
5862

5963
private void initSongNumber() {
60-
this.songNumber = rand.nextInt(2);
64+
Music.songNumber = rand.nextInt(3);
65+
}
66+
67+
public String[] getSongName() {
68+
return songName;
69+
}
70+
71+
public int getSongNumber() {
72+
return songNumber;
6173
}
6274
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.redomar.game.menu;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Dimension;
5+
6+
import javax.swing.JFrame;
7+
8+
public class DedicatedJFrame {
9+
10+
private static JFrame frame;
11+
12+
public DedicatedJFrame(int WIDTH, int HEIGHT, int SCALE, String NAME){
13+
setFrame(new JFrame(NAME));
14+
getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15+
getFrame().setLayout(new BorderLayout());
16+
getFrame().pack();
17+
getFrame().setResizable(false);
18+
getFrame().setLocationRelativeTo(null);
19+
getFrame().setVisible(true);
20+
21+
getFrame().setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
22+
getFrame().setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
23+
getFrame().setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
24+
}
25+
26+
public JFrame getFrame() {
27+
return frame;
28+
}
29+
30+
public void setFrame(JFrame frame) {
31+
DedicatedJFrame.frame = frame;
32+
}
33+
34+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.redomar.game.menu;
2+
3+
import java.awt.Color;
4+
import java.util.Random;
5+
6+
7+
8+
public class Menu implements Runnable{
9+
10+
private static final int WIDTH = 160;
11+
private static final int HEIGHT = (WIDTH / 3 * 2);
12+
private static final int SCALE = 3;
13+
private static final String NAME = "Menu";
14+
15+
private DedicatedJFrame frame = new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME);
16+
17+
private boolean running = false;
18+
19+
public synchronized void start() {
20+
running = true;
21+
new Thread(this).start();
22+
}
23+
24+
public synchronized void stop() {
25+
running = false;
26+
}
27+
28+
public void run() {
29+
long lastTime = System.nanoTime();
30+
double nsPerTick = 1000000000D / 3D;
31+
32+
int ticks = 0;
33+
int frames = 0;
34+
35+
long lastTimer = System.currentTimeMillis();
36+
double delta = 0;
37+
38+
while (running) {
39+
long now = System.nanoTime();
40+
delta += (now - lastTime) / nsPerTick;
41+
lastTime = now;
42+
boolean shouldRender = false;
43+
44+
while (delta >= 1) {
45+
ticks++;
46+
delta -= 1;
47+
shouldRender = true;
48+
}
49+
50+
try {
51+
Thread.sleep(2);
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
56+
if (shouldRender) {
57+
frames++;
58+
render();
59+
}
60+
61+
if (System.currentTimeMillis() - lastTimer >= 1000) {
62+
lastTimer += 1000;
63+
frame.getFrame().setTitle("Frames: " + frames + " Ticks: " + ticks);
64+
frames = 0;
65+
ticks = 0;
66+
}
67+
}
68+
}
69+
70+
private void render() {
71+
frame.getFrame().getContentPane().setBackground(Color.BLACK);
72+
73+
Random rand = new Random();
74+
75+
float r = rand.nextFloat();
76+
float g = rand.nextFloat();
77+
float b = rand.nextFloat();
78+
79+
Color randomColor = new Color(r, g, b);
80+
81+
frame.getFrame().getContentPane().setBackground(randomColor);
82+
83+
}
84+
85+
// public static void main(String[] args) {
86+
// new Menu().start();
87+
// }
88+
89+
}

0 commit comments

Comments
 (0)