Skip to content

Commit a157eca

Browse files
committed
feat: game engine basica feita
1 parent ae72095 commit a157eca

File tree

11 files changed

+272
-12
lines changed

11 files changed

+272
-12
lines changed

res/teste.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package com.gustavolr;
22

3-
import com.gustavolr.os.OsUtils;
3+
import com.gustavolr.engine.GameEngine;
44

5-
/**
6-
* Hello world!
7-
*
8-
*/
9-
public class Main
5+
public final class Main
106
{
117
public static void main( String[] args ) {
12-
System.out.println(OsUtils.getResource(OsUtils.join("fonts","Roboto-Regular.ttf")));
8+
new GameEngine();
139
}
1410
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.gustavolr.engine;
2+
3+
import java.awt.Graphics;
4+
import java.awt.event.KeyEvent;
5+
import java.awt.image.BufferStrategy;
6+
7+
import com.gustavolr.engine.game_loop.GameLoop;
8+
import com.gustavolr.engine.game_loop.GameLoopListener;
9+
import com.gustavolr.engine.input.GameInput;
10+
import com.gustavolr.engine.window.GameWindow;
11+
12+
public final class GameEngine implements GameLoopListener{
13+
14+
private final GameWindow window;
15+
private final GameInput input;
16+
private final GameLoop gameLoop;
17+
18+
public GameEngine() {
19+
20+
this.input = new GameInput();
21+
22+
this.window = new GameWindow("Pong");
23+
this.window.addKeyListener(input);
24+
this.gameLoop = new GameLoop(this);
25+
}
26+
27+
@Override
28+
public void update() {
29+
30+
if (GameInput.isKeyPressed(KeyEvent.VK_LEFT)) {
31+
System.out.println("Ola");
32+
}
33+
}
34+
35+
@Override
36+
public void render() {
37+
38+
BufferStrategy bs = this.window.getBufferStrategy();
39+
if ( bs == null ) {
40+
this.window.createBufferStrategy(3);
41+
return;
42+
}
43+
44+
Graphics layer = this.window.getGraphics();
45+
46+
layer.dispose();
47+
layer = bs.getDrawGraphics();
48+
49+
layer.fillRect(50, 50, 100, 100);
50+
51+
bs.show();
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.gustavolr.engine.game_loop;
2+
3+
public final class GameLoop implements Runnable{
4+
5+
private static Thread thread;
6+
7+
private boolean isRunning;
8+
9+
private GameLoopListener listener;
10+
11+
public GameLoop(GameLoopListener gameLoopListener) {
12+
this.listener = gameLoopListener;
13+
this.start();
14+
}
15+
16+
public synchronized void start() {
17+
18+
thread = new Thread(this);
19+
20+
isRunning = true;
21+
22+
thread.start();
23+
}
24+
25+
@Override
26+
public void run() {
27+
28+
//Fazendo com que o jogo rode a 60fps
29+
long lastTime = System.nanoTime();
30+
double amountOfTicks = 60.0;
31+
double ns = 1000000000 / amountOfTicks;
32+
double delta = 0.0;
33+
34+
35+
while(isRunning) {
36+
37+
//Calculo do tempo até então decorrido
38+
long now = System.nanoTime();
39+
delta += (now - lastTime) / ns;
40+
lastTime = now;
41+
42+
//Ao passar um segundo ou mais
43+
if ( delta >= 1) {
44+
45+
this.listener.update();
46+
this.listener.render();
47+
48+
delta--;
49+
}
50+
51+
}
52+
//Parar o programa
53+
try {
54+
stop();
55+
} catch (InterruptedException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
60+
public void stop() throws InterruptedException {
61+
62+
isRunning = false;
63+
thread.join();
64+
}
65+
66+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.gustavolr.engine.game_loop;
2+
3+
public interface GameLoopListener {
4+
void update();
5+
void render();
6+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.gustavolr.engine.input;
2+
3+
import java.awt.event.KeyEvent;
4+
import java.awt.event.KeyListener;
5+
import java.util.HashMap;
6+
7+
public final class GameInput implements KeyListener {
8+
9+
private static final HashMap<Integer, Boolean> inputMap = new HashMap<>();
10+
11+
public static final Boolean isKeyPressed(int key) {
12+
Boolean resp = false;
13+
14+
if (!inputMap.containsKey(key))
15+
return resp;
16+
17+
return inputMap.get(key);
18+
}
19+
20+
@Override
21+
public void keyPressed(KeyEvent k) {
22+
23+
if (inputMap.containsKey(k.getKeyCode()))
24+
inputMap.replace(k.getKeyCode(), GameInputConstants.KEY_PRESSED_VALUE);
25+
26+
else
27+
inputMap.put(k.getKeyCode(), GameInputConstants.KEY_PRESSED_VALUE);
28+
}
29+
30+
@Override
31+
public void keyReleased(KeyEvent k) {
32+
33+
if (inputMap.containsKey(k.getKeyCode()))
34+
inputMap.replace(k.getKeyCode(), GameInputConstants.KEY_RELEASED_VALUE);
35+
36+
else
37+
inputMap.put(k.getKeyCode(), GameInputConstants.KEY_RELEASED_VALUE);
38+
}
39+
40+
@Override
41+
public void keyTyped(KeyEvent arg0) {
42+
// TODO Auto-generated method stub
43+
44+
}
45+
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.gustavolr.engine.input;
2+
3+
public final class GameInputConstants {
4+
5+
public static final Boolean KEY_PRESSED_VALUE = true;
6+
public static final Boolean KEY_RELEASED_VALUE = false;
7+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.gustavolr.engine.window;
2+
3+
import java.awt.Canvas;
4+
import java.awt.Graphics;
5+
import java.awt.Dimension;
6+
import java.awt.image.BufferedImage;
7+
8+
import javax.swing.JFrame;
9+
10+
public final class GameWindow extends Canvas{
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
private static JFrame windowFrame;
15+
16+
private static short width;
17+
private static short height;
18+
private static byte scale;
19+
20+
public static BufferedImage bufferLayer;
21+
22+
public static short getWindowWidth() {
23+
return width;
24+
}
25+
26+
public static short getWindowHeight() {
27+
return height;
28+
}
29+
30+
public static byte getWindowScale() {
31+
return scale;
32+
}
33+
34+
public static Graphics getGraphicsLayer() {
35+
return bufferLayer.getGraphics();
36+
}
37+
38+
public GameWindow(String frameName) {
39+
this(GameWindowConstants.DEFAULT_WINDOW_WIDTH,
40+
GameWindowConstants.DEFAULT_WINDOW_HEIGHT,
41+
GameWindowConstants.DEFAULT_WINDOW_SCALE,
42+
frameName);
43+
}
44+
45+
public GameWindow(int width, int height, int scale, String frameName) {
46+
GameWindow.width = (short)width;
47+
GameWindow.height = (short)height;
48+
GameWindow.scale = (byte)scale;
49+
50+
this.initWindowFrame(frameName);
51+
}
52+
53+
public void initWindowFrame(String frameName) {
54+
55+
windowFrame = new JFrame(frameName);
56+
57+
windowFrame.setResizable(false);
58+
59+
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
60+
61+
this.setPreferredSize(new Dimension(height*scale,width*scale));
62+
windowFrame.add(this);
63+
windowFrame.pack(); // The pack method sizes the frame so that all its contents are at or above their preferred sizes
64+
65+
// Centralize the window to the center of the user screen
66+
windowFrame.setLocationRelativeTo(null);
67+
68+
windowFrame.setVisible(true);
69+
70+
// Set the icon for the frame
71+
//Image icon = ImageIO.read(getClass().getResource("/icon.png"));
72+
//windowFrame.setIconImage(icon);
73+
}
74+
75+
76+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.gustavolr.engine.window;
2+
3+
public final class GameWindowConstants {
4+
5+
public static final int DEFAULT_WINDOW_HEIGHT = 240;
6+
public static final int DEFAULT_WINDOW_WIDTH = 160;
7+
public static final int DEFAULT_WINDOW_SCALE = 4;
8+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
package com.gustavolr.os;
1+
package com.gustavolr.utils;
22

3-
public class OsUtils
3+
public final class OsUtils
44
{
5-
65
public static String join(String... s) {
76
return java.nio.file.Paths.get("",s).toString();
87
}
98

109
public static java.net.URL getResource(String path) {
1110
return OsUtils.class.getClassLoader().getResource(path);
1211
}
12+
13+
public static java.io.InputStream getResourceAsInputStream(String path) {
14+
return OsUtils.class.getClassLoader().getResourceAsStream(path);
15+
}
1316
}

0 commit comments

Comments
 (0)