More structured graphic engine

This commit is contained in:
Andrea Cavalli 2018-03-17 00:09:40 +01:00
parent f410a9181b
commit ef948579a4
17 changed files with 289 additions and 131 deletions

View File

@ -1,14 +1,3 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter

View File

@ -1,6 +1,8 @@
package org.warp.picalculator;
import org.warp.picalculator.device.Keyboard;
import org.warp.picalculator.device.PIHardwareDisplay;
import org.warp.picalculator.gui.CalculatorHUD;
import org.warp.picalculator.gui.DisplayManager;
import org.warp.picalculator.gui.screens.LoadingScreen;
import org.warp.picalculator.gui.screens.Screen;
@ -19,11 +21,12 @@ public class Main {
public Main(Screen screen, String[] args) {
System.out.println("WarpPI Calculator");
instance = this;
Main.args = args;
StaticVars.classLoader = this.getClass();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Thread.currentThread().setName("Main thread");
Main.args = args;
beforeStart();
new DisplayManager(screen);
new DisplayManager(new PIHardwareDisplay(), new CalculatorHUD(), screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
afterStart();
if (screen instanceof LoadingScreen) {
((LoadingScreen) screen).loaded = true;

View File

@ -12,7 +12,7 @@ public class StaticVars {
public static boolean debugOn;
public static int outputLevel = 0;
public static boolean debugWindow2x = false;
public static final Class<?> classLoader = Main.instance.getClass();
public static Class<?> classLoader;
private StaticVars() {

View File

@ -0,0 +1,28 @@
package org.warp.picalculator.device;
import org.warp.picalculator.StaticVars;
import org.warp.picalculator.Utils;
import org.warp.picalculator.gui.HardwareDisplay;
import org.warp.picalculator.gui.screens.Screen;
import com.pi4j.wiringpi.Gpio;
public class PIHardwareDisplay implements HardwareDisplay {
@Override
public void initialize() {}
@Override
public void shutdown() {}
@Override
public void setBrightness(double value) {
if (StaticVars.debugOn == false) {
Gpio.pwmWrite(12, (int) Math.ceil(value * 1024f));
// SoftPwm.softPwmWrite(12, (int)(Math.ceil(brightness*10)));
} else {
Utils.out.println(1, "Brightness: " + value);
}
}
}

View File

@ -0,0 +1,138 @@
package org.warp.picalculator.gui;
import org.warp.picalculator.StaticVars;
import org.warp.picalculator.Utils;
import org.warp.picalculator.device.Keyboard;
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
import org.warp.picalculator.gui.graphicengine.Renderer;
import org.warp.picalculator.gui.graphicengine.Skin;
public class CalculatorHUD extends HUD {
@Override
public void created() throws InterruptedException {
// TODO Auto-generated method stub
}
@Override
public void initialized() throws InterruptedException {
// TODO Auto-generated method stub
}
@Override
public void render() {
// TODO Auto-generated method stub
}
@Override
public void renderTopmostBackground() {
Renderer renderer = d.renderer;
GraphicEngine engine = d.engine;
Skin guiSkin = d.guiSkin;
renderer.glColor(0xFFc5c2af);
renderer.glFillColor(0, 0, engine.getWidth(), 20);
}
@Override
public void renderTopmost() {
Renderer renderer = d.renderer;
GraphicEngine engine = d.engine;
Skin guiSkin = d.guiSkin;
//DRAW TOP
renderer.glColor3i(0, 0, 0);
renderer.glDrawLine(0, 20, engine.getWidth() - 1, 20);
renderer.glColor3i(255, 255, 255);
guiSkin.use(engine);
if (Keyboard.shift) {
renderer.glFillRect(2 + 18 * 0, 2, 16, 16, 16 * 2, 16 * 0, 16, 16);
} else {
renderer.glFillRect(2 + 18 * 0, 2, 16, 16, 16 * 3, 16 * 0, 16, 16);
}
if (Keyboard.alpha) {
renderer.glFillRect(2 + 18 * 1, 2, 16, 16, 16 * 0, 16 * 0, 16, 16);
} else {
renderer.glFillRect(2 + 18 * 1, 2, 16, 16, 16 * 1, 16 * 0, 16, 16);
}
/*
if (Calculator.angleMode == AngleMode.DEG) {
drawSkinPart(8 + 18 * 2, 2, 16 * 4, 16 * 0, 16 + 16 * 4, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0);
} else if (Calculator.angleMode == AngleMode.RAD) {
drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 6, 16 * 0, 16 + 16 * 6, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0);
} else if (Calculator.angleMode == AngleMode.GRA) {
drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 8, 16 * 0, 16 + 16 * 8, 16 + 16 * 0);
} else {
drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0);
}*/
int padding = 2;
final int brightness = (int) (Math.ceil(DisplayManager.INSTANCE.getBrightness() * 9));
if (brightness <= 10) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * brightness, 16 * 1, 16, 16);
} else {
Utils.out.println(1, "Brightness error");
}
padding += 18 + 6;
final boolean canGoBack = DisplayManager.INSTANCE.canGoBack();
final boolean canGoForward = DisplayManager.INSTANCE.canGoForward();
if (StaticVars.haxMode) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 18, 16 * 0, 16, 16);
padding += 18 + 6;
}
if (canGoBack && canGoForward) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 14, 16 * 0, 16, 16);
} else if (canGoBack) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 15, 16 * 0, 16, 16);
} else if (canGoForward) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 16, 16 * 0, 16, 16);
} else {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 17, 16 * 0, 16, 16);
}
padding += 18;
//DRAW BOTTOM
this.d.renderer.glDrawStringLeft(2, 90, this.d.displayDebugString);
Utils.getFont(true, false).use(DisplayManager.INSTANCE.engine);
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 40);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1 + 1, StaticVars.screenSize[1] - 7 - 7 + 1, "WORK IN");
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 80);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1, StaticVars.screenSize[1] - 7 - 7, "WORK IN");
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 40);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1 + 1, StaticVars.screenSize[1] - 7 + 1, "PROGRESS.");
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 80);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1, StaticVars.screenSize[1] - 7, "PROGRESS.");
}
@Override
public void beforeRender(float dt) {
// TODO Auto-generated method stub
}
@Override
public void renderBackground() {
// TODO Auto-generated method stub
}
}

View File

@ -20,8 +20,6 @@ import org.warp.picalculator.gui.graphicengine.headless256.Headless256Engine;
import org.warp.picalculator.gui.graphicengine.headless8.Headless8Engine;
import org.warp.picalculator.gui.screens.Screen;
import com.pi4j.wiringpi.Gpio;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public final class DisplayManager implements RenderingLoop {
@ -29,6 +27,7 @@ public final class DisplayManager implements RenderingLoop {
private float brightness;
public final GraphicEngine engine;
public final HardwareDisplay monitor;
public final boolean supportsPauses;
public Renderer renderer;
@ -40,19 +39,43 @@ public final class DisplayManager implements RenderingLoop {
public final int[] glyphsHeight;
private Screen screen;
private HUD hud;
public Semaphore screenChange = new Semaphore(0);
public String displayDebugString;
public ObjectArrayList<GUIErrorMessage> errorMessages;
public DisplayManager(Screen screen) {
public DisplayManager(HardwareDisplay monitor, HUD hud, Screen screen, String title) {
INSTANCE = this;
engine = chooseGraphicEngine();
supportsPauses = engine.doesRefreshPauses();
this.monitor = monitor;
this.hud = hud;
monitor.initialize();
glyphsHeight = new int[] { 9, 6, 12, 9 };
displayDebugString = "";
errorMessages = new ObjectArrayList<>();
try {
hud.d = this;
hud.create();
if (!hud.initialized) hud.initialize();
} catch (final Exception e) {
e.printStackTrace();
System.exit(0);
}
setScreen(screen);
loop();
try {
engine.create();
renderer = engine.getRenderer();
engine.setTitle(title);
loop();
} catch (Exception ex) {
ex.printStackTrace();
}
monitor.shutdown();
}
/*
* private void load_skin() {
@ -236,7 +259,11 @@ public final class DisplayManager implements RenderingLoop {
}
public Screen getScreen() {
return DisplayManager.INSTANCE.screen;
return screen;
}
public HUD getHUD() {
return hud;
}
private void load_skin() throws IOException {
@ -265,94 +292,6 @@ public final class DisplayManager implements RenderingLoop {
renderer.glClear(engine.getWidth(), engine.getHeight());
}
private void draw_status() {
renderer.glColor(0xFFc5c2af);
renderer.glFillColor(0, 0, engine.getWidth(), 20);
renderer.glColor3i(0, 0, 0);
renderer.glDrawLine(0, 20, engine.getWidth() - 1, 20);
renderer.glColor3i(255, 255, 255);
guiSkin.use(engine);
if (Keyboard.shift) {
renderer.glFillRect(2 + 18 * 0, 2, 16, 16, 16 * 2, 16 * 0, 16, 16);
} else {
renderer.glFillRect(2 + 18 * 0, 2, 16, 16, 16 * 3, 16 * 0, 16, 16);
}
if (Keyboard.alpha) {
renderer.glFillRect(2 + 18 * 1, 2, 16, 16, 16 * 0, 16 * 0, 16, 16);
} else {
renderer.glFillRect(2 + 18 * 1, 2, 16, 16, 16 * 1, 16 * 0, 16, 16);
}
/*
if (Calculator.angleMode == AngleMode.DEG) {
drawSkinPart(8 + 18 * 2, 2, 16 * 4, 16 * 0, 16 + 16 * 4, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0);
} else if (Calculator.angleMode == AngleMode.RAD) {
drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 6, 16 * 0, 16 + 16 * 6, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0);
} else if (Calculator.angleMode == AngleMode.GRA) {
drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 8, 16 * 0, 16 + 16 * 8, 16 + 16 * 0);
} else {
drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0);
drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0);
drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0);
}*/
int padding = 2;
final int brightness = (int) (Math.ceil(DisplayManager.INSTANCE.brightness * 9));
if (brightness <= 10) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * brightness, 16 * 1, 16, 16);
} else {
Utils.out.println(1, "Brightness error");
}
padding += 18 + 6;
final boolean canGoBack = canGoBack();
final boolean canGoForward = canGoForward();
if (StaticVars.haxMode) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 18, 16 * 0, 16, 16);
padding += 18 + 6;
}
if (canGoBack && canGoForward) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 14, 16 * 0, 16, 16);
} else if (canGoBack) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 15, 16 * 0, 16, 16);
} else if (canGoForward) {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 16, 16 * 0, 16, 16);
} else {
renderer.glFillRect(StaticVars.screenSize[0] - (padding + 16), 2, 16, 16, 16 * 17, 16 * 0, 16, 16);
}
padding += 18;
screen.renderStatusbar();
}
private void draw_screen() {
screen.render();
}
private void draw_bottom() {
renderer.glDrawStringLeft(2, 90, displayDebugString);
Utils.getFont(true, false).use(DisplayManager.INSTANCE.engine);
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 40);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1 + 1, StaticVars.screenSize[1] - 7 - 7 + 1, "WORK IN");
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 80);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1, StaticVars.screenSize[1] - 7 - 7, "WORK IN");
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 40);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1 + 1, StaticVars.screenSize[1] - 7 + 1, "PROGRESS.");
DisplayManager.INSTANCE.renderer.glColor4i(255, 0, 0, 80);
DisplayManager.INSTANCE.renderer.glDrawStringLeft(1, StaticVars.screenSize[1] - 7, "PROGRESS.");
}
private void draw_world() {
renderer.glColor3i(255, 255, 255);
@ -374,9 +313,12 @@ public final class DisplayManager implements RenderingLoop {
renderer.glDrawStringCenter((StaticVars.screenSize[0] / 2), 11, "UNEXPECTED EXCEPTION");
} else {
fonts[0].use(engine);
draw_screen();
draw_status();
draw_bottom();
hud.renderBackground();
screen.render();
hud.render();
hud.renderTopmostBackground();
screen.renderTopmost();
hud.renderTopmost();
}
}
@ -404,9 +346,6 @@ public final class DisplayManager implements RenderingLoop {
public void loop() {
try {
engine.create();
renderer = engine.getRenderer();
load_skin();
load_fonts();
@ -508,12 +447,7 @@ public final class DisplayManager implements RenderingLoop {
public void setBrightness(float newval) {
if (newval >= 0 && newval <= 1) {
brightness = newval;
if (StaticVars.debugOn == false) {
Gpio.pwmWrite(12, (int) Math.ceil(brightness * 1024f));
// SoftPwm.softPwmWrite(12, (int)(Math.ceil(brightness*10)));
} else {
Utils.out.println(1, "Brightness: " + newval);
}
monitor.setBrightness(brightness);
}
}

View File

@ -0,0 +1,10 @@
package org.warp.picalculator.gui;
public interface GraphicalInterface {
public void create() throws InterruptedException;
public void initialize() throws InterruptedException;
public void render();
public void renderTopmost();
public void beforeRender(float dt);
public boolean mustBeRefreshed();
}

View File

@ -0,0 +1,48 @@
package org.warp.picalculator.gui;
import org.warp.picalculator.gui.screens.Screen;
public abstract class HUD implements GraphicalInterface {
public DisplayManager d;
public boolean created = false;
public boolean initialized = false;
public HUD() {}
public void initialize() throws InterruptedException {
if (!initialized) {
initialized = true;
initialized();
}
}
public void create() throws InterruptedException {
if (!created) {
created = true;
created();
}
}
public abstract void created() throws InterruptedException;
public abstract void initialized() throws InterruptedException;
public abstract void renderBackground();
@Override
public abstract void render();
public abstract void renderTopmostBackground();
@Override
public abstract void renderTopmost();
@Override
public abstract void beforeRender(float dt);
@Override
public boolean mustBeRefreshed() {
return true;
}
}

View File

@ -0,0 +1,7 @@
package org.warp.picalculator.gui;
public interface HardwareDisplay {
public void initialize();
public void shutdown();
public void setBrightness(double value);
}

View File

@ -24,7 +24,7 @@ public class ChooseVariableValueScreen extends Screen {
public void created() throws InterruptedException {}
@Override
public void init() throws InterruptedException {}
public void initialized() throws InterruptedException {}
@Override
public void render() {

View File

@ -17,7 +17,7 @@ public class EmptyScreen extends Screen {
}
@Override
public void init() throws InterruptedException {}
public void initialized() throws InterruptedException {}
@Override
public void render() {

View File

@ -23,7 +23,7 @@ public class KeyboardDebugScreen extends Screen {
public void created() throws InterruptedException {}
@Override
public void init() throws InterruptedException {}
public void initialized() throws InterruptedException {}
@Override
public void render() {

View File

@ -23,7 +23,7 @@ public class LoadingScreen extends Screen {
}
@Override
public void init() throws InterruptedException {}
public void initialized() throws InterruptedException {}
@Override
public void beforeRender(float dt) {

View File

@ -40,7 +40,7 @@ public class MarioScreen extends Screen {
}
@Override
public void init() {
public void initialized() {
try {
if (skin == null)
skin = DisplayManager.INSTANCE.engine.loadSkin("marioskin.png");

View File

@ -79,7 +79,7 @@ public class MathInputScreen extends Screen {
}
@Override
public void init() throws InterruptedException {
public void initialized() throws InterruptedException {
/* Fine caricamento */
}
@ -142,7 +142,7 @@ public class MathInputScreen extends Screen {
}
@Override
public void renderStatusbar() {
public void renderTopmost() {
final Renderer renderer = DisplayManager.INSTANCE.renderer;
renderer.glColor3f(1, 1, 1);
final int pos = 2;

View File

@ -2,8 +2,9 @@ package org.warp.picalculator.gui.screens;
import org.warp.picalculator.device.KeyboardEventListener;
import org.warp.picalculator.gui.DisplayManager;
import org.warp.picalculator.gui.GraphicalInterface;
public abstract class Screen implements KeyboardEventListener {
public abstract class Screen implements KeyboardEventListener, GraphicalInterface {
public DisplayManager d;
public boolean created = false;
public boolean initialized = false;
@ -14,7 +15,7 @@ public abstract class Screen implements KeyboardEventListener {
public void initialize() throws InterruptedException {
if (!initialized) {
initialized = true;
init();
initialized();
}
}
@ -27,11 +28,11 @@ public abstract class Screen implements KeyboardEventListener {
public abstract void created() throws InterruptedException;
public abstract void init() throws InterruptedException;
public abstract void initialized() throws InterruptedException;
public abstract void render();
public void renderStatusbar() {
public void renderTopmost() {
}

View File

@ -20,7 +20,7 @@ public class SolveEquationScreen extends Screen {
public void created() throws InterruptedException {}
@Override
public void init() throws InterruptedException {}
public void initialized() throws InterruptedException {}
@Override
public void render() {