Skip to content

Commit de62477

Browse files
authored
Merge pull request #1 from Programando-o-Mundo/awt-refactor
Awt refactor
2 parents 65721c6 + 1da5325 commit de62477

File tree

5 files changed

+83
-36
lines changed

5 files changed

+83
-36
lines changed

src/main/java/com/gustavolr/engine/GameEngine.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public final class GameEngine implements GameLoopListener{
1515

1616
private final GameWindow window;
1717
private final GameInput input;
18+
private GameLoop gameLoop;
1819

1920
private final List<Scene> scene_manager;
2021

@@ -37,15 +38,15 @@ public void addScene(Scene scene) {
3738
public void start() {
3839
this.window.toggleFrameVisibility(true);
3940
this.window.requestFocus();
40-
new GameLoop(this);
41+
gameLoop = new GameLoop(this);
4142
}
4243

4344
@Override
4445
public void update() {
4546

46-
for(Scene scene : scene_manager) {
47+
for(Scene scene : scene_manager)
4748
scene.update();
48-
}
49+
4950
}
5051

5152
@Override
@@ -55,6 +56,7 @@ public void render() {
5556
if ( g == null )
5657
return;
5758

59+
5860
this.window.drawBackground(g);
5961

6062
// Game Rendering goes here

src/main/java/com/gustavolr/engine/sound/SoundPlayer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.gustavolr.engine.sound;
22

3+
import java.io.BufferedInputStream;
34
import java.io.File;
5+
import java.io.InputStream;
46

57
import javax.sound.sampled.AudioInputStream;
68
import javax.sound.sampled.AudioSystem;
@@ -9,26 +11,24 @@
911
public class SoundPlayer {
1012

1113
Clip soundClip;
12-
public String fileName;
1314

1415
boolean soundLoaded;
1516

1617
public SoundPlayer() {
1718
soundLoaded = false;
1819
}
1920

20-
public SoundPlayer(String fileName) {
21-
this.fileName = fileName;
21+
public SoundPlayer(InputStream fileName) {
2222
this.loadSFXfromFile(fileName);
2323
}
2424

25-
public void loadSFXfromFile(String fileName) {
25+
public void loadSFXfromFile(InputStream file) {
2626

27-
File sfxFile = new File(fileName.strip());
27+
InputStream bufferedInputStream = new BufferedInputStream(file);
2828

2929
try {
3030

31-
AudioInputStream audio = AudioSystem.getAudioInputStream(sfxFile);
31+
AudioInputStream audio = AudioSystem.getAudioInputStream(bufferedInputStream);
3232
this.soundClip = AudioSystem.getClip();
3333
soundClip.open(audio);
3434

src/main/java/com/gustavolr/engine/window/GameWindow.java

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package com.gustavolr.engine.window;
22

3-
import java.awt.Canvas;
4-
import java.awt.Graphics;
53
import java.awt.Dimension;
4+
import java.awt.Frame;
65
import java.awt.image.BufferedImage;
76
import java.awt.image.BufferStrategy;
7+
import java.awt.event.WindowEvent;
8+
import java.awt.event.WindowListener;
9+
import java.awt.Graphics;
810

9-
import javax.swing.JFrame;
10-
11-
public final class GameWindow extends Canvas{
12-
13-
private static final long serialVersionUID = 1L;
14-
15-
private static JFrame windowFrame;
11+
public final class GameWindow extends Frame implements WindowListener{
1612

1713
private static short width;
1814
private static short height;
1915
private static byte scale;
2016

2117
public static BufferedImage bufferLayer;
2218

19+
public boolean playerPressXtoQuit;
20+
2321
public GameWindow(String frameName) {
2422
this(GameWindowConstants.DEFAULT_WINDOW_WIDTH,
2523
GameWindowConstants.DEFAULT_WINDOW_HEIGHT,
@@ -28,10 +26,13 @@ public GameWindow(String frameName) {
2826
}
2927

3028
public GameWindow(int width, int height, int scale, String frameName) {
29+
super(frameName);
3130
GameWindow.width = (short)width;
3231
GameWindow.height = (short)height;
3332
GameWindow.scale = (byte)scale;
3433

34+
playerPressXtoQuit = false;
35+
3536
this.initWindowFrame(frameName);
3637
}
3738

@@ -61,18 +62,15 @@ public BufferedImage getBufferedImage() {
6162

6263
public void initWindowFrame(String frameName) {
6364

64-
windowFrame = new JFrame(frameName);
65-
66-
windowFrame.setResizable(false);
65+
setResizable(false);
6766

68-
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
69-
70-
this.setPreferredSize(new Dimension(width*scale,height*scale));
71-
windowFrame.add(this);
72-
windowFrame.pack(); // The pack method sizes the frame so that all its contents are at or above their preferred sizes
67+
setPreferredSize(new Dimension(width*scale,height*scale));
7368

69+
pack(); // The pack method sizes the frame so that all its contents are at or above their preferred sizes
70+
addWindowListener(this);
7471
// Centralize the window to the center of the user screen
75-
windowFrame.setLocationRelativeTo(null);
72+
setLocationRelativeTo(null);
73+
setLayout(null);
7674

7775
bufferLayer = new BufferedImage((int)width,(int)height, BufferedImage.TYPE_INT_RGB);
7876

@@ -82,7 +80,7 @@ public void initWindowFrame(String frameName) {
8280
}
8381

8482
public void toggleFrameVisibility(boolean visibility) {
85-
windowFrame.setVisible(visibility);
83+
this.setVisible(visibility);
8684
}
8785

8886
public Graphics getWindowLayer() {
@@ -106,11 +104,58 @@ public void drawFinalScene(Graphics g) {
106104
BufferStrategy bs = this.getBufferStrategy();
107105

108106
g.dispose();
109-
g = bs.getDrawGraphics();
110107

111-
g.drawImage(bufferLayer, 0, 0, width * scale, height * scale, null);
108+
try {
109+
g = bs.getDrawGraphics();
110+
111+
g.drawImage(bufferLayer, 5, 29, width * scale, height * scale, null);
112+
113+
bs.show();
114+
} catch(Exception e) {
115+
e.printStackTrace();
116+
}
117+
}
118+
119+
@Override
120+
public void windowActivated(WindowEvent arg0) {
121+
// TODO Auto-generated method stub
122+
123+
}
124+
125+
@Override
126+
public void windowClosed(WindowEvent arg0) {
127+
// TODO Auto-generated method stub
128+
129+
}
130+
131+
@Override
132+
public void windowClosing(WindowEvent arg0) {
133+
setVisible(false);
134+
dispose();
135+
System.exit(0);
136+
}
137+
138+
@Override
139+
public void windowDeactivated(WindowEvent arg0) {
140+
// TODO Auto-generated method stub
141+
142+
}
143+
144+
@Override
145+
public void windowDeiconified(WindowEvent arg0) {
146+
// TODO Auto-generated method stub
147+
148+
}
149+
150+
@Override
151+
public void windowIconified(WindowEvent arg0) {
152+
// TODO Auto-generated method stub
153+
154+
}
155+
156+
@Override
157+
public void windowOpened(WindowEvent arg0) {
158+
// TODO Auto-generated method stub
112159

113-
bs.show();
114160
}
115-
116161
}

src/main/java/com/gustavolr/game_entities/Ball.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class Ball extends Entity {
1616

1717
public double dx,dy,speed;
1818

19-
private final static SoundPlayer wallSFX = new SoundPlayer(OsUtils.getResource(OsUtils.join("sfx","wall.wav")).getFile());
20-
private final static SoundPlayer paddleSFX = new SoundPlayer(OsUtils.getResource(OsUtils.join("sfx","paddle.wav")).getFile());
19+
private final static SoundPlayer wallSFX = new SoundPlayer(OsUtils.getResourceAsInputStream(OsUtils.join("sfx","wall.wav")));
20+
private final static SoundPlayer paddleSFX = new SoundPlayer(OsUtils.getResourceAsInputStream(OsUtils.join("sfx","paddle.wav")));
2121

2222
public Ball(Vector position, int width, int height) {
2323
super(position, width, height);

src/main/java/com/gustavolr/game_scenes/MainScene.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public final class MainScene implements Scene {
3434

3535
private final List<Entity> entities;
3636

37-
private final static SoundPlayer scoreSFX = new SoundPlayer(OsUtils.getResource(OsUtils.join("sfx","score.wav")).getFile());
37+
private final static SoundPlayer scoreSFX = new SoundPlayer(OsUtils.getResourceAsInputStream(OsUtils.join("sfx","score.wav")));
3838

39-
private final static int MAX_SCORE = 1;
39+
private final static int MAX_SCORE = 3;
4040

4141
public MainScene() {
4242

0 commit comments

Comments
 (0)