Skip to content

Commit deb5f07

Browse files
committed
Adding Scene system, currently prototyping
1 parent e1fc893 commit deb5f07

File tree

5 files changed

+127
-78
lines changed

5 files changed

+127
-78
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ The Console Java Engine is based off of JavidX9's PixelGameEngine.
44

55
Uses Lanterna to emulate a terminal, also comes with a Pair class that is a basic copy of C++s pair class.
66

7+
Requires Java 17+
78

89
## How to use
910

src/main/java/com/spireprod/cje/ConsoleJavaEngine.java

Lines changed: 33 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import javax.swing.JFrame;
88

99
import com.googlecode.lanterna.TerminalSize;
10-
import com.googlecode.lanterna.TextColor;
11-
import com.googlecode.lanterna.graphics.TextGraphics;
1210
import com.googlecode.lanterna.input.KeyStroke;
1311
import com.googlecode.lanterna.input.KeyType;
1412
import com.googlecode.lanterna.screen.Screen;
@@ -17,6 +15,9 @@
1715
import com.googlecode.lanterna.terminal.Terminal;
1816
import com.googlecode.lanterna.terminal.TerminalResizeListener;
1917
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame;
18+
import com.spireprod.cje.core.ConsoleRenderer;
19+
import com.spireprod.cje.core.scenes.Scene;
20+
import com.spireprod.cje.core.scenes.SceneManager;
2021

2122
public abstract class ConsoleJavaEngine {
2223

@@ -26,7 +27,8 @@ public abstract class ConsoleJavaEngine {
2627
protected String PIXEL_SHADE_HALF = "\u2592";
2728

2829
protected Terminal terminal;
29-
protected TextGraphics termGraphics;
30+
protected SceneManager sceneManager;
31+
protected ConsoleRenderer renderer;
3032
protected KeyStroke termKey;
3133
protected SwingTerminalFrame frame;
3234
protected Screen screen;
@@ -37,26 +39,28 @@ public abstract class ConsoleJavaEngine {
3739
private final int targetFPS = 60;
3840
private final long optimalTime = (long) (1E9f / targetFPS);
3941

40-
public static final String CJE_VERSION = "0.1.10-Talos";
42+
public static final String CJE_VERSION = "0.1.15-Talos";
4143

4244
public ConsoleJavaEngine(String title, int width, int height) {
4345
DefaultTerminalFactory defaultTermFactory = new DefaultTerminalFactory();
44-
defaultTermFactory.setTerminalEmulatorTitle(title).setInitialTerminalSize(new TerminalSize(width, height))
45-
.setForceAWTOverSwing(false).setPreferTerminalEmulator(true);
46+
defaultTermFactory.setTerminalEmulatorTitle(title + " -" + CJE_VERSION)
47+
.setInitialTerminalSize(new TerminalSize(width, height)).setForceAWTOverSwing(false)
48+
.setPreferTerminalEmulator(true);
4649

4750
try {
4851
terminal = defaultTermFactory.createSwingTerminal();
4952
frame = (SwingTerminalFrame) terminal;
50-
frame.setVisible(true);
5153
frame.pack();
54+
frame.setLocationRelativeTo(null);
5255
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
56+
frame.setVisible(true);
5357

5458
screen = new TerminalScreen(terminal);
59+
termWidth = frame.getTerminalSize().getColumns();
60+
termHeight = frame.getTerminalSize().getRows();
5561

56-
this.termWidth = frame.getTerminalSize().getColumns();
57-
this.termHeight = frame.getTerminalSize().getRows();
58-
59-
this.termGraphics = screen.newTextGraphics();
62+
sceneManager = new SceneManager();
63+
renderer = new ConsoleRenderer(screen.newTextGraphics());
6064

6165
frame.addResizeListener(new TerminalResizeListener() {
6266

@@ -116,12 +120,6 @@ public void windowDeactivated(WindowEvent e) {
116120

117121
protected abstract void onGameCreate();
118122

119-
protected abstract void onGameUpdate(float deltaTime);
120-
121-
protected abstract void onGameInput(float deltaTime, KeyStroke keyStroke);
122-
123-
protected abstract void onGameRender(float deltaTime);
124-
125123
public void run() {
126124

127125
isRunning = true;
@@ -137,6 +135,18 @@ public void run() {
137135
gameLoop.start();
138136
}
139137

138+
private void onGameUpdate(float deltaTime) {
139+
sceneManager.sceneUpdate(deltaTime);
140+
}
141+
142+
private void onGameInput(float deltaTime, KeyStroke key) {
143+
sceneManager.sceneInput(deltaTime, key);
144+
}
145+
146+
private void onGameRender(ConsoleRenderer renderer) {
147+
sceneManager.sceneRender(renderer);
148+
}
149+
140150
private void loop() throws IOException, InterruptedException {
141151
long lastLoopTime = System.nanoTime();
142152

@@ -166,7 +176,9 @@ private void loop() throws IOException, InterruptedException {
166176

167177
onGameUpdate(delta);
168178

169-
onGameRender(delta);
179+
renderer.clearScreen();
180+
181+
onGameRender(renderer);
170182
screen.refresh();
171183

172184
long sleepNanos = (lastLoopTime - System.nanoTime() + optimalTime);
@@ -180,35 +192,9 @@ private void loop() throws IOException, InterruptedException {
180192
screen.stopScreen();
181193
}
182194

183-
// private void loop() throws IOException {
184-
//
185-
// screen.startScreen();
186-
// screen.setCursorPosition(null);
187-
//
188-
// final float targetDelta = 1f / targetFPS;
189-
// float accumulator = 0f;
190-
// long lastTime = System.nanoTime();
191-
//
192-
// while (isRunning) {
193-
// long now = System.nanoTime();
194-
// float deltaTime = (now - lastTime) / 1E9f;
195-
// lastTime = now;
196-
//
197-
198-
//
199-
// accumulator += deltaTime;
200-
//
201-
// while (accumulator >= targetDelta) {
202-
203-
//
204-
// accumulator -= targetDelta;
205-
// }
206-
//
207-
208-
// }
209-
//
210-
// screen.stopScreen();
211-
// }
195+
protected void setScene(Scene scene) {
196+
sceneManager.setScene(scene);
197+
}
212198

213199
// -----------------------------------
214200
// Getters and Setters
@@ -221,35 +207,4 @@ public int getTerminalHeight() {
221207
return termHeight;
222208
}
223209

224-
// -----------------------------------
225-
// Drawing Methods
226-
227-
public void writeString(char str, int x, int y, TextColor backgroundColor, TextColor textColor) {
228-
termGraphics.setBackgroundColor(backgroundColor);
229-
termGraphics.setForegroundColor(textColor);
230-
termGraphics.setCharacter(x, y, str);
231-
}
232-
233-
public void writeString(char str, int x, int y, TextColor backgroundColor) {
234-
writeString(str, x, y, backgroundColor, TextColor.ANSI.WHITE);
235-
}
236-
237-
public void writeString(char str, int x, int y) {
238-
writeString(str, x, y, TextColor.ANSI.BLACK);
239-
}
240-
241-
public void writeString(String str, int x, int y, TextColor backgroundColor, TextColor textColor) {
242-
termGraphics.setBackgroundColor(backgroundColor);
243-
termGraphics.setForegroundColor(textColor);
244-
termGraphics.putString(x, y, str);
245-
}
246-
247-
public void writeString(String str, int x, int y, TextColor backgroundColor) {
248-
writeString(str, x, y, backgroundColor, TextColor.ANSI.WHITE);
249-
}
250-
251-
public void writeString(String str, int x, int y) {
252-
writeString(str, x, y, TextColor.ANSI.BLACK, TextColor.ANSI.WHITE);
253-
}
254-
255210
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.spireprod.cje.core;
2+
3+
import com.googlecode.lanterna.TextColor;
4+
import com.googlecode.lanterna.graphics.TextGraphics;
5+
6+
public class ConsoleRenderer {
7+
8+
private TextGraphics graphics;
9+
10+
public ConsoleRenderer(TextGraphics graphics) {
11+
this.graphics = graphics;
12+
}
13+
14+
public void clearScreen() {
15+
graphics.setBackgroundColor(TextColor.ANSI.BLACK);
16+
graphics.fill(' ');
17+
}
18+
19+
public void writeString(char str, int x, int y, TextColor backgroundColor, TextColor textColor) {
20+
graphics.setBackgroundColor(backgroundColor);
21+
graphics.setForegroundColor(textColor);
22+
graphics.setCharacter(x, y, str);
23+
}
24+
25+
public void writeString(char str, int x, int y, TextColor backgroundColor) {
26+
writeString(str, x, y, backgroundColor, TextColor.ANSI.WHITE);
27+
}
28+
29+
public void writeString(char str, int x, int y) {
30+
writeString(str, x, y, TextColor.ANSI.BLACK);
31+
}
32+
33+
public void writeString(String str, int x, int y, TextColor backgroundColor, TextColor textColor) {
34+
graphics.setBackgroundColor(backgroundColor);
35+
graphics.setForegroundColor(textColor);
36+
graphics.putString(x, y, str);
37+
}
38+
39+
public void writeString(String str, int x, int y, TextColor backgroundColor) {
40+
writeString(str, x, y, backgroundColor, TextColor.ANSI.WHITE);
41+
}
42+
43+
public void writeString(String str, int x, int y) {
44+
writeString(str, x, y, TextColor.ANSI.BLACK, TextColor.ANSI.WHITE);
45+
}
46+
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.spireprod.cje.core.scenes;
2+
3+
import com.googlecode.lanterna.input.KeyStroke;
4+
import com.spireprod.cje.core.ConsoleRenderer;
5+
6+
public interface Scene {
7+
8+
void onSceneUpdate(float delta);
9+
10+
void onSceneRender(ConsoleRenderer renderer);
11+
12+
void onSceneInput(float delta, KeyStroke key);
13+
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.spireprod.cje.core.scenes;
2+
3+
import com.googlecode.lanterna.input.KeyStroke;
4+
import com.spireprod.cje.core.ConsoleRenderer;
5+
6+
public class SceneManager {
7+
8+
private Scene currentScene;
9+
10+
public SceneManager() {
11+
}
12+
13+
public void sceneUpdate(float deltaTime) {
14+
if (currentScene != null)
15+
currentScene.onSceneUpdate(deltaTime);
16+
}
17+
18+
public void sceneInput(float deltaTime, KeyStroke key) {
19+
if (currentScene != null)
20+
currentScene.onSceneInput(deltaTime, key);
21+
}
22+
23+
public void sceneRender(ConsoleRenderer renderer) {
24+
if (currentScene != null)
25+
currentScene.onSceneRender(renderer);
26+
}
27+
28+
public void setScene(Scene scene) {
29+
currentScene = scene;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)