77import javax .swing .JFrame ;
88
99import com .googlecode .lanterna .TerminalSize ;
10- import com .googlecode .lanterna .TextColor ;
11- import com .googlecode .lanterna .graphics .TextGraphics ;
1210import com .googlecode .lanterna .input .KeyStroke ;
1311import com .googlecode .lanterna .input .KeyType ;
1412import com .googlecode .lanterna .screen .Screen ;
1715import com .googlecode .lanterna .terminal .Terminal ;
1816import com .googlecode .lanterna .terminal .TerminalResizeListener ;
1917import 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
2122public 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}
0 commit comments