fixed crash occuring on double tick + unlimited framerate

This commit is contained in:
MitchWeaver
2017-05-01 10:33:57 -05:00
parent 329f11cec0
commit febe7b2e37
3 changed files with 39 additions and 29 deletions

View File

@@ -9,6 +9,7 @@ import sjgs.base_objects.BaseTile;
import sjgs.base_objects.Bullet;
import sjgs.base_objects.HardObject;
import sjgs.base_objects.PlayerBase;
import sjgs.core.Camera;
import sjgs.core.DeveloperConsole;
import sjgs.core.Engine;
import sjgs.core.Handler;
@@ -31,11 +32,13 @@ class __PhysicsDemonstration extends Engine {
public static __PhysicsDemonstration engine;
public static ExamplePlayer player;
public static Camera camera;
@Override
protected void init() {
// disableFpsCap();
disableFpsCap();
setDoubleTickRate();
camera = new Camera(this);
new ExampleDevConsole(this);
new ExampleMouseInput(this);
generateWorld();
@@ -72,9 +75,11 @@ class __PhysicsDemonstration extends Engine {
try {
if(DeveloperConsole.CONSOLE_OPEN) return;
ExampleKeyInput.tick(player);
camera.tick(player.getCenter(), getScaleFactor());
Handler.tick(camera, getScaleFactor());
} catch (final NullPointerException npe) { }
camera.tick(player.getCenter(), getScaleFactor());
} catch (final NullPointerException npe) {
System.out.println("null pointer caught in tick()");
}
}
@Override
protected void render(Graphics2D g2d) {

View File

@@ -26,6 +26,7 @@ public class Camera { //TODO: camera acceleartion / delay and shake
}
public void tick(final Point2f target, final double scaleFactor) {
final float w = (float)(getWidth() / scaleFactor);
final float h = (float)(getHeight() / scaleFactor);
final float hw = w * 0.5f;
@@ -53,10 +54,7 @@ public class Camera { //TODO: camera acceleartion / delay and shake
if(getY() != tY) speedY = cinch(speedY + acceleration, maxSpeed);
else speedY = 0;
}
// ELSE IF WE *ARE* SHAKING
} // ELSE IF WE *ARE* SHAKING
} else {
if(shakeIndex >= shakePoints.length - 1) shakePoints = null;
proposedX = shakePoints[shakeIndex].x;

View File

@@ -21,7 +21,7 @@ import sjgs.utils.multithreading.ThreadPool;
public abstract class Engine extends Canvas implements Runnable, Serializable {
private static final String engine_version = "SJGS v0.0.12";
private static final String engine_version = "SJGS v0.0.13 - Jan 17, 2017";
/** @TICK_INTERVAL && @FPS_CAP: default no arg constructor gives 60 tps / fps */
private double TICK_INTERVAL;
@@ -36,35 +36,41 @@ public abstract class Engine extends Canvas implements Runnable, Serializable {
public final ThreadPool pool;
public final JFrame frame;
public final JPanel panel;
public final Camera camera;
public final PyObject self;
public Engine(final int WIDTH, final int HEIGHT, final String title) {
error(getVersion() + "\n" + Utils.OS + "\n" + "-----------------------------");
pool = new ThreadPool();
frame = new JFrame();
panel = new JPanel();
self = java2py(this);
Utils.print("Objects initialized.");
new Runner(() -> {
new Runner(() -> { new Physics().init(); }).run();
new Runner(() -> { new Handler().init(); }).run();
setFPS_CAP(60.0d);
TICK_INTERVAL = Utils.second / 60.0d;
error(getVersion() + "\n" + Utils.OS);
toggleDrawFPS();
}).run();
createWindow(WIDTH, HEIGHT, title);
Utils.print("Game window created.");
new Physics().init();
Utils.print("Physics initialized.");
new Handler().init();
Utils.print("Handler initialized.");
new Jython().__init__();
createWindow(WIDTH, HEIGHT, title);
camera = new Camera(this);
Utils.print("Jython scripting initialized.");
init();
self = java2py(this);
pool.runTask(this);
Utils.print("Game engine starting...");
error("-----------------------------");
}
private void createWindow(final int WIDTH, final int HEIGHT, final String title) {
@@ -89,6 +95,7 @@ public abstract class Engine extends Canvas implements Runnable, Serializable {
private boolean drawFPS, rendering;
private BufferStrategy bs; private Graphics g; private Graphics2D g2d;
public void engine_render() {
if(rendering) return;
begin();
drawBaseLayer();
scale();
@@ -111,6 +118,7 @@ public abstract class Engine extends Canvas implements Runnable, Serializable {
DeveloperConsole.render(consoleRenderer, getWidth(), getHeight());
}
private void begin() {
rendering = true;
if (bs == null) createBufferStrategy(3);
bs = getBufferStrategy();
if (bs.getDrawGraphics() != null) g = bs.getDrawGraphics();
@@ -173,7 +181,6 @@ public abstract class Engine extends Canvas implements Runnable, Serializable {
if(multithreadedRendering) pool.runTask(renderer);
else { engine_render(); frames++; }
frameThen = now;
rendering = true;
}
// UPDATE FRAMES PER SECOND