WarpPI/desktop/src/main/java/it/cavallium/warppi/gui/graphicengine/impl/swing/SwingEngine.java

248 lines
5.9 KiB
Java
Raw Normal View History

2018-09-22 10:29:40 +02:00
package it.cavallium.warppi.gui.graphicengine.impl.swing;
2016-09-02 20:32:37 +02:00
2019-10-25 13:58:49 +02:00
import java.awt.*;
import java.awt.event.*;
2016-09-02 20:32:37 +02:00
import java.awt.image.BufferedImage;
import java.io.IOException;
2017-09-24 13:09:30 +02:00
import java.util.concurrent.Semaphore;
2019-11-02 23:13:19 +01:00
import java.util.concurrent.atomic.AtomicInteger;
2016-09-02 20:32:37 +02:00
2019-02-27 23:29:03 +01:00
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.StaticVars;
import it.cavallium.warppi.gui.graphicengine.BinaryFont;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.RenderingLoop;
import it.cavallium.warppi.gui.graphicengine.Skin;
2019-11-01 15:23:34 +01:00
import it.cavallium.warppi.util.EventSubmitter;
2016-09-02 20:32:37 +02:00
2018-09-22 10:29:40 +02:00
public class SwingEngine implements GraphicEngine {
2016-09-02 20:32:37 +02:00
2019-10-25 13:58:49 +02:00
private final int defaultWidth;
private final int defaultHeight;
private SwingWindow INSTANCE;
2018-09-22 10:29:40 +02:00
public SwingRenderer r;
2018-09-11 01:12:54 +02:00
public volatile BufferedImage g;
public volatile boolean initialized;
2019-10-25 13:58:49 +02:00
public SwingEngine(int defaultWidth, int defaultHeight) {
this.defaultWidth = defaultWidth;
this.defaultHeight = defaultHeight;
}
2016-09-02 20:32:37 +02:00
@Override
2018-09-22 11:17:30 +02:00
public void setTitle(final String title) {
2016-09-02 20:32:37 +02:00
INSTANCE.setTitle(title);
}
@Override
2018-09-22 11:17:30 +02:00
public void setResizable(final boolean r) {
2016-09-02 20:32:37 +02:00
INSTANCE.setResizable(r);
2018-09-22 11:17:30 +02:00
if (!r)
2016-09-02 20:32:37 +02:00
INSTANCE.setUndecorated(true);
}
@Override
public void setDisplayMode(final int ww, final int wh) {
2016-09-02 20:32:37 +02:00
INSTANCE.setSize(ww, wh);
r.size = new int[] { ww, wh };
2018-09-22 10:29:40 +02:00
SwingRenderer.canvas2d = new int[ww * wh];
2018-08-29 00:07:45 +02:00
g = new BufferedImage(ww, wh, BufferedImage.TYPE_INT_RGB);
2016-09-02 20:32:37 +02:00
}
@Override
public void create() {
2017-09-15 23:24:12 +02:00
create(null);
}
2017-09-15 23:24:12 +02:00
@Override
2018-09-22 11:17:30 +02:00
public void create(final Runnable onInitialized) {
2018-09-22 10:29:40 +02:00
r = new SwingRenderer();
2018-09-11 01:12:54 +02:00
g = new BufferedImage(r.size[0], r.size[1], BufferedImage.TYPE_INT_RGB);
initialized = false;
2019-10-25 13:58:49 +02:00
INSTANCE = new SwingWindow(this, defaultWidth, defaultHeight);
2019-02-27 23:29:03 +01:00
setResizable(WarpPI.getPlatform().getSettings().isDebugEnabled());
2016-09-02 20:32:37 +02:00
INSTANCE.setVisible(true);
initialized = true;
2018-09-22 11:17:30 +02:00
if (onInitialized != null)
2017-09-15 23:24:12 +02:00
onInitialized.run();
2016-09-02 20:32:37 +02:00
}
@Override
2019-11-01 15:23:34 +01:00
public EventSubmitter<Integer[]> onResize() {
2018-08-29 00:07:45 +02:00
return INSTANCE.onResize();
2016-09-02 20:32:37 +02:00
}
@Override
public int getWidth() {
2019-10-25 13:58:49 +02:00
return this.getSize()[0];
2016-09-02 20:32:37 +02:00
}
@Override
public int getHeight() {
2019-10-25 13:58:49 +02:00
return this.getSize()[1];
2016-09-02 20:32:37 +02:00
}
@Override
public void destroy() {
2019-10-25 13:58:49 +02:00
sendPowerOffSignal();
}
protected void destroyEngine() {
2016-09-02 20:32:37 +02:00
initialized = false;
}
@Override
2018-09-22 11:17:30 +02:00
public void start(final RenderingLoop d) {
INSTANCE.setRenderingLoop(d);
final Thread th = new Thread(() -> {
try {
double extratime = 0;
while (initialized) {
2018-08-29 00:07:45 +02:00
final long start = System.nanoTime();
repaint();
2018-08-29 00:07:45 +02:00
final long end = System.nanoTime();
2018-09-22 11:17:30 +02:00
final double delta = end - start;
2018-08-29 00:07:45 +02:00
if (extratime + delta < 50 * 1000d * 1000d) {
Thread.sleep((long) Math.floor(50d - (extratime + delta) / 1000d / 1000d));
extratime = 0;
2018-09-22 11:17:30 +02:00
} else
extratime += delta - 50d;
}
} catch (final InterruptedException e) {
e.printStackTrace();
}
});
th.setName("CPU rendering thread");
th.setDaemon(true);
th.start();
2016-09-02 20:32:37 +02:00
}
@Deprecated()
public void refresh() {
2019-11-02 23:13:19 +01:00
if (WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().getScreen() == null || WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().error != null && WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().error.length() > 0 || WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().getScreen() == null || WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().getScreen().mustBeRefreshed()) {
2018-08-29 00:07:45 +02:00
INSTANCE.c.paintImmediately(0, 0, getWidth(), getHeight());
2019-11-02 23:13:19 +01:00
}
2016-09-02 20:32:37 +02:00
}
@Override
public void repaint() {
INSTANCE.c.repaint();
2016-09-02 20:32:37 +02:00
}
2019-10-25 13:58:49 +02:00
public void subscribeExit(Runnable subscriber) {
INSTANCE.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
subscriber.run();
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
public void sendPowerOffSignal() {
INSTANCE.sendPowerOffSignal();
}
public abstract class Startable {
2016-09-02 20:32:37 +02:00
public Startable() {
force = false;
2016-09-02 20:32:37 +02:00
}
2018-09-22 11:17:30 +02:00
public Startable(final boolean force) {
2016-09-02 20:32:37 +02:00
this.force = force;
}
public boolean force = false;
public abstract void run();
}
@Override
public int[] getSize() {
2019-10-25 13:58:49 +02:00
return r.size.clone();
}
@Override
public boolean isInitialized() {
return initialized;
}
@Override
2018-09-22 10:29:40 +02:00
public SwingRenderer getRenderer() {
return r;
}
@Override
2018-09-22 11:17:30 +02:00
public BinaryFont loadFont(final String fontName) throws IOException {
2018-09-22 10:29:40 +02:00
return new SwingFont(fontName);
2017-11-19 22:58:37 +01:00
}
@Override
2018-09-22 11:17:30 +02:00
public BinaryFont loadFont(final String path, final String fontName) throws IOException {
2018-09-22 10:29:40 +02:00
return new SwingFont(path, fontName);
}
@Override
2018-09-22 11:17:30 +02:00
public Skin loadSkin(final String file) throws IOException {
2018-09-22 10:29:40 +02:00
return new SwingSkin(file);
}
@Override
public boolean isSupported() {
2018-09-22 11:17:30 +02:00
if (StaticVars.startupArguments.isEngineForced() && StaticVars.startupArguments.isCPUEngineForced() == false)
return false;
return (GraphicsEnvironment.isHeadless()) == false;
2016-09-02 20:32:37 +02:00
}
@Override
public boolean doesRefreshPauses() {
return true;
}
2018-09-22 11:17:30 +02:00
public void setAlphaChanged(final boolean val) {
INSTANCE.setAlphaChanged(val);
}
2018-09-22 11:17:30 +02:00
public void setShiftChanged(final boolean val) {
INSTANCE.setShiftChanged(val);
}
2019-10-25 13:58:49 +02:00
public Insets getInsets() {
return INSTANCE.getInsets();
}
public void subscribeTouchDevice(MouseMotionListener mouseMotionListener, MouseListener mouseListener) {
INSTANCE.c.addMouseListener(mouseListener);
INSTANCE.c.addMouseMotionListener(mouseMotionListener);
}
2016-09-02 20:32:37 +02:00
}