Skip to content

Commit e66f86c

Browse files
committed
Fixed Window not closing, and changed game loop
1 parent 2eeb9ba commit e66f86c

File tree

2 files changed

+118
-39
lines changed

2 files changed

+118
-39
lines changed

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

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.awt.event.WindowListener;
55
import java.io.IOException;
66

7+
import javax.swing.JFrame;
8+
79
import com.googlecode.lanterna.TerminalSize;
810
import com.googlecode.lanterna.TextColor;
911
import com.googlecode.lanterna.graphics.TextGraphics;
@@ -26,30 +28,37 @@ public abstract class ConsoleJavaEngine {
2628
protected Terminal terminal;
2729
protected TextGraphics termGraphics;
2830
protected KeyStroke termKey;
31+
protected SwingTerminalFrame frame;
2932
protected Screen screen;
3033
protected boolean isRunning = false;
3134

3235
private int termWidth, termHeight;
3336

34-
private int targetFPS = 60;
37+
private final int targetFPS = 60;
38+
private final long optimalTime = (long) (1E9f / targetFPS);
3539

3640
public static final String CJE_VERSION = "0.1.5-Talos";
3741

3842
public ConsoleJavaEngine(String title, int width, int height) {
3943
DefaultTerminalFactory defaultTermFactory = new DefaultTerminalFactory();
40-
defaultTermFactory.setTerminalEmulatorTitle(title);
41-
defaultTermFactory.setInitialTerminalSize(new TerminalSize(width, height));
44+
defaultTermFactory.setTerminalEmulatorTitle(title).setInitialTerminalSize(new TerminalSize(width, height))
45+
.setForceAWTOverSwing(false).setPreferTerminalEmulator(true);
4246

4347
try {
44-
terminal = defaultTermFactory.createTerminal();
48+
terminal = defaultTermFactory.createSwingTerminal();
49+
frame = (SwingTerminalFrame) terminal;
50+
frame.setVisible(true);
51+
frame.pack();
52+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
53+
4554
screen = new TerminalScreen(terminal);
4655

47-
this.termWidth = terminal.getTerminalSize().getColumns();
48-
this.termHeight = terminal.getTerminalSize().getRows();
56+
this.termWidth = frame.getTerminalSize().getColumns();
57+
this.termHeight = frame.getTerminalSize().getRows();
4958

5059
this.termGraphics = screen.newTextGraphics();
5160

52-
terminal.addResizeListener(new TerminalResizeListener() {
61+
frame.addResizeListener(new TerminalResizeListener() {
5362

5463
@Override
5564
public void onResized(Terminal terminal, TerminalSize newSize) {
@@ -59,14 +68,15 @@ public void onResized(Terminal terminal, TerminalSize newSize) {
5968

6069
});
6170

62-
((SwingTerminalFrame) terminal).addWindowListener(new WindowListener() {
71+
frame.addWindowListener(new WindowListener() {
6372

6473
@Override
6574
public void windowOpened(WindowEvent e) {
6675
}
6776

6877
@Override
6978
public void windowClosing(WindowEvent e) {
79+
7080
isRunning = false;
7181
try {
7282
screen.close();
@@ -110,63 +120,96 @@ public void windowDeactivated(WindowEvent e) {
110120

111121
protected abstract void onGameInput(float deltaTime, KeyStroke keyStroke);
112122

113-
protected abstract void onGameRender(float alpha);
123+
protected abstract void onGameRender(float deltaTime);
114124

115125
public void run() {
116126

117127
isRunning = true;
118-
try {
119-
loop();
120-
screen.close();
121-
terminal.close();
122-
} catch (IOException e) {
123-
e.printStackTrace();
124-
}
128+
129+
Thread gameLoop = new Thread(() -> {
130+
try {
131+
loop();
132+
} catch (IOException | InterruptedException e) {
133+
e.printStackTrace();
134+
}
135+
});
136+
137+
gameLoop.start();
125138
}
126139

127-
private void loop() throws IOException {
140+
private void loop() throws IOException, InterruptedException {
141+
long lastLoopTime = System.nanoTime();
128142

129143
screen.startScreen();
130144
screen.setCursorPosition(null);
131145

132-
final float targetDelta = 1f / targetFPS;
133-
float accumulator = 0f;
134-
long lastTime = System.nanoTime();
135-
136146
while (isRunning) {
137147
long now = System.nanoTime();
138-
float deltaTime = (now - lastTime) / 1E9f;
139-
lastTime = now;
148+
long updateLength = now - lastLoopTime;
149+
lastLoopTime = now;
150+
151+
float delta = updateLength / ((float) optimalTime);
140152

141153
TerminalSize newSize = screen.doResizeIfNecessary();
142154
if (newSize != null) {
143155
this.termWidth = newSize.getColumns();
144156
this.termHeight = newSize.getRows();
145157
}
146158

147-
accumulator += deltaTime;
159+
termKey = screen.pollInput();
148160

149-
while (accumulator >= targetDelta) {
150-
termKey = screen.pollInput();
161+
if (termKey != null && termKey.getKeyType().equals(KeyType.Escape))
162+
isRunning = false;
151163

152-
if (termKey != null && termKey.getKeyType().equals(KeyType.Escape))
153-
isRunning = false;
164+
if (termKey != null)
165+
onGameInput(delta, termKey);
154166

155-
if (termKey != null)
156-
onGameInput(targetDelta, termKey);
167+
onGameUpdate(delta);
157168

158-
onGameUpdate(targetDelta);
169+
onGameRender(delta);
170+
screen.refresh();
159171

160-
accumulator -= targetDelta;
172+
long sleepNanos = (lastLoopTime - System.nanoTime() + optimalTime);
173+
if (sleepNanos > 0) {
174+
long millis = sleepNanos / 1_000_000;
175+
int nanos = (int) (sleepNanos % 1_000_000);
176+
Thread.sleep(millis, nanos);
161177
}
162-
163-
onGameRender(accumulator / targetDelta);
164-
screen.refresh();
165178
}
166179

167180
screen.stopScreen();
168181
}
169182

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+
// }
212+
170213
// -----------------------------------
171214
// Getters and Setters
172215

@@ -178,10 +221,6 @@ public int getTerminalHeight() {
178221
return termHeight;
179222
}
180223

181-
public void setTargetFps(int fps) {
182-
this.targetFPS = fps;
183-
}
184-
185224
// -----------------------------------
186225
// Drawing Methods
187226

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.spireprod.cje;
2+
3+
import com.googlecode.lanterna.input.KeyStroke;
4+
5+
public class Test extends ConsoleJavaEngine {
6+
7+
public Test() {
8+
super("Test", 80, 24);
9+
// TODO Auto-generated constructor stub
10+
}
11+
12+
@Override
13+
protected void onGameCreate() {
14+
// TODO Auto-generated method stub
15+
16+
}
17+
18+
@Override
19+
protected void onGameUpdate(float deltaTime) {
20+
// TODO Auto-generated method stub
21+
22+
}
23+
24+
@Override
25+
protected void onGameInput(float deltaTime, KeyStroke keyStroke) {
26+
// TODO Auto-generated method stub
27+
28+
}
29+
30+
@Override
31+
protected void onGameRender(float alpha) {
32+
writeString("Quack bitch", 5, 10);
33+
}
34+
35+
public static void main(String[] args) {
36+
Test t = new Test();
37+
t.run();
38+
}
39+
40+
}

0 commit comments

Comments
 (0)