11package com .spireprod .cje ;
22
3+ import java .awt .Font ;
4+ import java .awt .FontFormatException ;
5+ import java .awt .GraphicsEnvironment ;
36import java .awt .event .WindowEvent ;
47import java .awt .event .WindowListener ;
8+ import java .io .File ;
59import java .io .IOException ;
10+ import java .io .InputStream ;
611
712import javax .swing .JFrame ;
813
1318import com .googlecode .lanterna .terminal .DefaultTerminalFactory ;
1419import com .googlecode .lanterna .terminal .Terminal ;
1520import com .googlecode .lanterna .terminal .TerminalResizeListener ;
21+ import com .googlecode .lanterna .terminal .swing .SwingTerminalFontConfiguration ;
1622import com .googlecode .lanterna .terminal .swing .SwingTerminalFrame ;
1723import com .spireprod .cje .core .ConsoleRenderer ;
1824import com .spireprod .cje .core .input .Input ;
1925import com .spireprod .cje .core .scenes .AbstractScene ;
2026import com .spireprod .cje .core .scenes .SceneManager ;
21- import com .spireprod .cje .core .ui .TextUI ;
27+ import com .spireprod .cje .core .ui .UIContext ;
2228
2329public abstract class ConsoleJavaEngine {
2430
25- protected String PIXEL_BLOCK = "\u2588 " ;
26- protected String PIXEL_SHADE = "\u2591 " ;
27- protected String PIXEL_SHADE_FULL = "\u2593 " ;
28- protected String PIXEL_SHADE_HALF = "\u2592 " ;
31+ public static final String PIXEL_BLOCK = "\u2588 " ;
32+ public static final String PIXEL_SHADE = "\u2591 " ;
33+ public static final String PIXEL_SHADE_FULL = "\u2593 " ;
34+ public static final String PIXEL_SHADE_HALF = "\u2592 " ;
35+ public static int termWidth , termHeight ;
2936
3037 protected Terminal terminal ;
3138 protected SceneManager sceneManager ;
32- protected TextUI ctx ;
39+ protected UIContext ctx ;
3340 protected ConsoleRenderer renderer ;
3441 protected Input input ;
3542 protected SwingTerminalFrame frame ;
3643 protected Screen screen ;
3744 protected boolean isRunning = false ;
3845
39- private int termWidth , termHeight ;
40-
4146 private final int targetFPS = 60 ;
4247 private final long optimalTime = (long ) (1E9f / targetFPS );
4348
44- public static final String CJE_VERSION = "0.1.15-Jyggalag" ;
49+ public static final String CJE_VERSION = "0.1.20-Jyggalag" ;
50+
51+ public static String DATA_FOLDER ;
52+ public static final String [] DATA_FLDRS = { "Scripts/" , "Saves/" , "Maps/" , "Config/" };
4553
4654 public ConsoleJavaEngine (String title , int width , int height ) {
55+ DATA_FOLDER = title .replace (" " , "_" ).trim () + File .separator ;
56+
57+ // Make Sure Data Folder Exists, if not, create it.
58+ File dataFolder = new File (DATA_FOLDER );
59+ if (!dataFolder .exists ()) {
60+ dataFolder .mkdir ();
61+
62+ for (String folder : DATA_FLDRS )
63+ new File (DATA_FOLDER + folder ).mkdirs ();
64+
65+ }
66+
4767 DefaultTerminalFactory defaultTermFactory = new DefaultTerminalFactory ();
68+
69+ // Setup Default Font 'Ubuntu Mono'
70+ Font font = null ;
71+ try {
72+ InputStream is = ConsoleJavaEngine .class .getResourceAsStream ("/ascii-sector-16x16-tileset.ttf" );
73+ font = Font .createFont (Font .TRUETYPE_FONT , is );
74+ is .close ();
75+ } catch (FontFormatException | IOException e ) {
76+ e .printStackTrace ();
77+ }
78+
79+ if (font == null )
80+ font = new Font (Font .MONOSPACED , Font .PLAIN , 16 );
81+
82+ GraphicsEnvironment .getLocalGraphicsEnvironment ().registerFont (font );
83+
4884 defaultTermFactory .setTerminalEmulatorTitle (title + " -" + CJE_VERSION )
85+ .setTerminalEmulatorFontConfiguration (
86+ SwingTerminalFontConfiguration .newInstance (font .deriveFont (Font .PLAIN , 16 )))
4987 .setInitialTerminalSize (new TerminalSize (width , height )).setForceAWTOverSwing (false )
5088 .setPreferTerminalEmulator (true );
5189
5290 try {
5391 terminal = defaultTermFactory .createSwingTerminal ();
5492 frame = (SwingTerminalFrame ) terminal ;
93+ frame .setResizable (false );
5594 frame .pack ();
5695 frame .setLocationRelativeTo (null );
5796 frame .setDefaultCloseOperation (JFrame .DISPOSE_ON_CLOSE );
5897 frame .setVisible (true );
5998
60- ctx = new TextUI ();
99+ ctx = new UIContext ();
61100 screen = new TerminalScreen (terminal );
62101 termWidth = frame .getTerminalSize ().getColumns ();
63102 termHeight = frame .getTerminalSize ().getRows ();
@@ -66,6 +105,9 @@ public ConsoleJavaEngine(String title, int width, int height) {
66105 sceneManager = new SceneManager ();
67106 renderer = new ConsoleRenderer (screen .newTextGraphics ());
68107
108+ ctx .setConsoleRenderer (renderer );
109+ ctx .setInput (input );
110+
69111 frame .addResizeListener (new TerminalResizeListener () {
70112
71113 @ Override
@@ -150,7 +192,7 @@ private void onGameInput(float deltaTime, Input input) {
150192 sceneManager .sceneInput (deltaTime , input );
151193 }
152194
153- private void onGameUIRender (TextUI ctx , ConsoleRenderer renderer , Input input ) {
195+ private void onGameUIRender (UIContext ctx , ConsoleRenderer renderer , Input input ) {
154196 sceneManager .sceneUIRender (ctx , renderer , input );
155197 }
156198
@@ -165,6 +207,7 @@ private void loop() throws IOException, InterruptedException {
165207 screen .setCursorPosition (null );
166208
167209 while (isRunning ) {
210+ input .beginFrame ();
168211 long now = System .nanoTime ();
169212 long updateLength = now - lastLoopTime ;
170213 lastLoopTime = now ;
@@ -173,8 +216,8 @@ private void loop() throws IOException, InterruptedException {
173216
174217 TerminalSize newSize = screen .doResizeIfNecessary ();
175218 if (newSize != null ) {
176- this . termWidth = newSize .getColumns ();
177- this . termHeight = newSize .getRows ();
219+ termWidth = newSize .getColumns ();
220+ termHeight = newSize .getRows ();
178221 }
179222
180223 KeyStroke termKey ;
0 commit comments