From 52f8505f5c665280b48b2e80beefb254e6ac1bd0 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 27 Nov 2018 00:24:43 +0100 Subject: Add unscii-16 as default font in Scene There is also CamingoCode-Regular.ttf that can be used in the future for dev stuff in the UI. --- src/subconscious/graphics/Scene.java | 22 +++++++++++++++++++++ src/subconscious/graphics/widget/PerfView.java | 20 +++++++++++-------- src/subconscious/res/fonts/CamingoCode-Regular.ttf | Bin 0 -> 93500 bytes src/subconscious/res/fonts/unscii-16.ttf | Bin 0 -> 188204 bytes 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 src/subconscious/res/fonts/CamingoCode-Regular.ttf create mode 100644 src/subconscious/res/fonts/unscii-16.ttf (limited to 'src') diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 74330ff..3849c24 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -10,12 +10,17 @@ import subconscious.graphics.widget.PerfView; import java.util.ArrayList; import java.util.Map; +import java.io.InputStream; +import java.io.IOException; + import java.awt.Canvas; import java.awt.Graphics2D; import java.awt.Dimension; import java.awt.BorderLayout; import java.awt.Panel; import java.awt.RenderingHints; +import java.awt.Font; +import java.awt.FontFormatException; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; @@ -77,6 +82,9 @@ public abstract class Scene extends Panel protected Canvas canvas = new Canvas(); protected BufferStrategy buffer; + protected Font font; + protected final float defaultFontSize = 10.0f; + // TODO: make accessible from user settings protected int guiSize = 2; @@ -88,6 +96,19 @@ public abstract class Scene extends Panel this.UNIQUE_NAME = uniqueName + " (" + label + ")"; this.game = game; + // set default scene font unscii + InputStream fontStream = Scene.class.getResourceAsStream("/subconscious/res/fonts/unscii-16.ttf"); + + assert fontStream != null; + + try { + this.font = Font.createFont(Font.TRUETYPE_FONT, fontStream); + } catch (FontFormatException ex) { + ex.printStackTrace(); + } catch (IOException ioex) { + ioex.printStackTrace(); + } + // watch for resize this.addComponentListener(this); @@ -211,6 +232,7 @@ public abstract class Scene extends Panel do { Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics(); g.addRenderingHints(this.RENDERING_HINTS); + g.setFont(this.font.deriveFont(this.defaultFontSize)); this.render(g); this.renderWidgets(g); g.dispose(); diff --git a/src/subconscious/graphics/widget/PerfView.java b/src/subconscious/graphics/widget/PerfView.java index d5de3cb..c7c9356 100644 --- a/src/subconscious/graphics/widget/PerfView.java +++ b/src/subconscious/graphics/widget/PerfView.java @@ -11,22 +11,26 @@ import java.awt.Graphics2D; * final game. */ public class PerfView extends Widget implements Dynamic { - - public static final int WIDTH = 120; - public static final int HEIGHT = 30; - protected long lastDeltaNanoTime = 0; - public PerfView(String uniqueName, int x, int y) { - super(uniqueName, x, y, PerfView.WIDTH, PerfView.HEIGHT); + // the size depends on the font + super(uniqueName, x, y, 0, 0); } @Override public void render(Graphics2D g) { + String text = "DeltaTime (us): " + + Long.toString(this.lastDeltaNanoTime/1000); + + this.width = g.getFontMetrics().stringWidth(text); + this.height = g.getFontMetrics().getHeight() + 10; + g.setColor(Palette.BLACK); - g.drawString("DeltaTime (us): ", 0, HEIGHT/2); - g.drawString(Long.toString(this.lastDeltaNanoTime/1000), 0, HEIGHT); + g.fillRect(0, 0, this.width, this.height); + + g.setColor(Palette.WHITE); + g.drawString(text, 0, this.height - 10); } @Override diff --git a/src/subconscious/res/fonts/CamingoCode-Regular.ttf b/src/subconscious/res/fonts/CamingoCode-Regular.ttf new file mode 100644 index 0000000..04b8860 Binary files /dev/null and b/src/subconscious/res/fonts/CamingoCode-Regular.ttf differ diff --git a/src/subconscious/res/fonts/unscii-16.ttf b/src/subconscious/res/fonts/unscii-16.ttf new file mode 100644 index 0000000..050ce76 Binary files /dev/null and b/src/subconscious/res/fonts/unscii-16.ttf differ -- cgit v1.2.1