Skip to content

Commit 81f4a5c

Browse files
committed
hfix: corrigido erro com a renderizacao
1 parent a157eca commit 81f4a5c

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.gustavolr.engine;
22

3+
import java.awt.Color;
34
import java.awt.Graphics;
45
import java.awt.event.KeyEvent;
5-
import java.awt.image.BufferStrategy;
66

77
import com.gustavolr.engine.game_loop.GameLoop;
88
import com.gustavolr.engine.game_loop.GameLoopListener;
@@ -13,41 +13,55 @@ public final class GameEngine implements GameLoopListener{
1313

1414
private final GameWindow window;
1515
private final GameInput input;
16-
private final GameLoop gameLoop;
16+
17+
int x = 0;
18+
int y = 0;
1719

1820
public GameEngine() {
1921

2022
this.input = new GameInput();
2123

2224
this.window = new GameWindow("Pong");
2325
this.window.addKeyListener(input);
24-
this.gameLoop = new GameLoop(this);
26+
new GameLoop(this);
2527
}
2628

2729
@Override
2830
public void update() {
2931

3032
if (GameInput.isKeyPressed(KeyEvent.VK_LEFT)) {
31-
System.out.println("Ola");
33+
x--;
34+
}
35+
else if (GameInput.isKeyPressed(KeyEvent.VK_RIGHT)) {
36+
x++;
3237
}
38+
39+
if (GameInput.isKeyPressed(KeyEvent.VK_UP)) {
40+
y--;
41+
} else if (GameInput.isKeyPressed(KeyEvent.VK_DOWN)) {
42+
y++;
43+
}
44+
45+
46+
System.out.println(x);
3347
}
3448

3549
@Override
3650
public void render() {
3751

38-
BufferStrategy bs = this.window.getBufferStrategy();
39-
if ( bs == null ) {
40-
this.window.createBufferStrategy(3);
52+
Graphics g = this.window.getWindowLayer();
53+
if ( g == null )
4154
return;
42-
}
55+
56+
this.window.drawBackground(g);
57+
58+
// Game Rendering goes here
4359

44-
Graphics layer = this.window.getGraphics();
60+
g.setColor(Color.GREEN);
61+
g.fillRect(x, y, 50, 50);
4562

46-
layer.dispose();
47-
layer = bs.getDrawGraphics();
63+
// Game Rendering stops here
4864

49-
layer.fillRect(50, 50, 100, 100);
50-
51-
bs.show();
65+
this.window.drawFinalScene(g);
5266
}
5367
}

src/main/java/com/gustavolr/engine/input/GameInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void keyReleased(KeyEvent k) {
4040
@Override
4141
public void keyTyped(KeyEvent arg0) {
4242
// TODO Auto-generated method stub
43-
43+
System.out.println(arg0);
4444
}
4545

4646
}

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.awt.Graphics;
55
import java.awt.Dimension;
66
import java.awt.image.BufferedImage;
7+
import java.awt.Color;
8+
import java.awt.image.BufferStrategy;
79

810
import javax.swing.JFrame;
911

@@ -27,12 +29,20 @@ public static short getWindowHeight() {
2729
return height;
2830
}
2931

32+
public static short getRealWindowWidth() {
33+
return (short)(width * (short)scale);
34+
}
35+
36+
public static short getRealWindowHeight() {
37+
return (short)(height * (short)scale);
38+
}
39+
3040
public static byte getWindowScale() {
3141
return scale;
3242
}
3343

34-
public static Graphics getGraphicsLayer() {
35-
return bufferLayer.getGraphics();
44+
public BufferedImage getBufferedImage() {
45+
return bufferLayer;
3646
}
3747

3848
public GameWindow(String frameName) {
@@ -44,6 +54,7 @@ public GameWindow(String frameName) {
4454

4555
public GameWindow(int width, int height, int scale, String frameName) {
4656
GameWindow.width = (short)width;
57+
System.out.println(height);
4758
GameWindow.height = (short)height;
4859
GameWindow.scale = (byte)scale;
4960

@@ -58,7 +69,7 @@ public void initWindowFrame(String frameName) {
5869

5970
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
6071

61-
this.setPreferredSize(new Dimension(height*scale,width*scale));
72+
this.setPreferredSize(new Dimension(width*scale,height*scale));
6273
windowFrame.add(this);
6374
windowFrame.pack(); // The pack method sizes the frame so that all its contents are at or above their preferred sizes
6475

@@ -67,10 +78,39 @@ public void initWindowFrame(String frameName) {
6778

6879
windowFrame.setVisible(true);
6980

81+
bufferLayer = new BufferedImage((int)width,(int)height, BufferedImage.TYPE_INT_RGB);
82+
7083
// Set the icon for the frame
7184
//Image icon = ImageIO.read(getClass().getResource("/icon.png"));
7285
//windowFrame.setIconImage(icon);
7386
}
7487

88+
public Graphics getWindowLayer() {
89+
BufferStrategy bs = this.getBufferStrategy();
90+
if ( bs == null ) {
91+
this.createBufferStrategy(3);
92+
return null;
93+
}
94+
95+
return bufferLayer.getGraphics();
96+
}
97+
98+
public void drawBackground(Graphics g) {
99+
100+
g.setColor(Color.BLACK);
101+
g.fillRect(0, 0, GameWindow.getWindowWidth(), GameWindow.getWindowHeight());
102+
}
103+
104+
public void drawFinalScene(Graphics g) {
105+
106+
BufferStrategy bs = this.getBufferStrategy();
107+
108+
g.dispose();
109+
g = bs.getDrawGraphics();
110+
111+
g.drawImage(bufferLayer, 0, 0, width * scale, height * scale, null);
112+
113+
bs.show();
114+
}
75115

76116
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public final class GameWindowConstants {
44

5-
public static final int DEFAULT_WINDOW_HEIGHT = 240;
6-
public static final int DEFAULT_WINDOW_WIDTH = 160;
5+
public static final int DEFAULT_WINDOW_WIDTH = 260;
6+
public static final int DEFAULT_WINDOW_HEIGHT = 140;
77
public static final int DEFAULT_WINDOW_SCALE = 4;
88
}

0 commit comments

Comments
 (0)