From ace1c3434af32a5fa345af63246a75c9a4c4b0f1 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Fri, 3 Jul 2015 18:34:33 +0200 Subject: [PATCH] unhooked rendering from logic --- core/src/com/mygdx/game/Game.java | 24 ++++++++------ core/src/logic/World.java | 33 ++++++++++++++----- .../mygdx/game/desktop/DesktopLauncher.java | 5 +-- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/core/src/com/mygdx/game/Game.java b/core/src/com/mygdx/game/Game.java index 3409aee..594cb54 100644 --- a/core/src/com/mygdx/game/Game.java +++ b/core/src/com/mygdx/game/Game.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import java.util.ConcurrentModificationException; import logic.Element; import logic.World; @@ -25,8 +26,8 @@ public class Game extends ApplicationAdapter { shaper = new ShapeRenderer(); shaper.setAutoShapeType(true); font = new BitmapFont(); + new Thread(world).start(); } - @Override public void render() { @@ -55,22 +56,21 @@ public class Game extends ApplicationAdapter { if (Gdx.input.isKeyJustPressed(Input.Keys.P)) { paused = !paused; } - // Update - if (!paused && world.isBusy()) { - world.update(); - } // Draw Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); shaper.setColor(1, 1, 1, 1); shaper.begin(ShapeRenderer.ShapeType.Line); - for (Element e : world.getElements()) { - try { - e.render(shaper); - } catch (ArrayIndexOutOfBoundsException ex) { + try { + for (Element e : world.getElements()) { + try { + e.render(shaper); + } catch (ArrayIndexOutOfBoundsException ex) { // No idea why it happens, but it's rendering so meh - //Log.log(Log.ERROR, ex+""); + //Log.log(Log.ERROR, ex+""); + } } + } catch (ConcurrentModificationException e) { } shaper.setColor(0.3f, 0.3f, 0.3f, 1); // draw borders @@ -85,4 +85,8 @@ public class Game extends ApplicationAdapter { public static Game get() { return game; } + + public boolean isPaused() { + return paused; + } } diff --git a/core/src/logic/World.java b/core/src/logic/World.java index 2330ff0..7ff41d6 100644 --- a/core/src/logic/World.java +++ b/core/src/logic/World.java @@ -5,6 +5,7 @@ */ package logic; +import com.mygdx.game.Game; import com.mygdx.game.Log; import java.util.ArrayList; import java.util.Comparator; @@ -15,7 +16,7 @@ import java.util.logging.Logger; * * @author fazo */ -public class World { +public class World implements Runnable { private final int width, height, nPlants, creatPerGen; private int generation = 1; @@ -63,9 +64,25 @@ public class World { newGen(true); } + @Override + public void run() { + for (;;) { + // Add speed limiter here + if (!Game.get().isPaused()) { + update(); + } else { + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + } + public void update() { busy = true; - Log.log(Log.DEBUG,"Started."); + Log.log(Log.DEBUG, "Started."); for (Element e : toAdd) { elements.add(e); if (e instanceof Creature) { @@ -82,7 +99,7 @@ public class World { creatures.removeAll(graveyard); if (creatures.isEmpty()) { // All dead, next gen - Log.log(Log.DEBUG,"Newgen."); + Log.log(Log.DEBUG, "Newgen."); newGen(false); } while (plants.size() < nPlants) { @@ -93,7 +110,7 @@ public class World { } workerDone = false; busy = false; - Log.log(Log.DEBUG,"Launching Creature workers"); + Log.log(Log.DEBUG, "Launching Creature workers"); int readyCount = 0; Thread.yield(); do { @@ -103,9 +120,9 @@ public class World { readyCount++; } } - Log.log(Log.DEBUG,"DoneCount: " + readyCount + " out of " + creatures.size()); + Log.log(Log.DEBUG, "DoneCount: " + readyCount + " out of " + creatures.size()); } while (readyCount < creatures.size()); - Log.log(Log.DEBUG,"Done creatures, awaiting veg worker..."); + Log.log(Log.DEBUG, "Done creatures, awaiting veg worker..."); while (!workerDone) { Thread.yield(); /*try { @@ -115,11 +132,11 @@ public class World { }*/ } busy = true; - Log.log(Log.DEBUG,"Paused workers. Applying modifications to world... "); + Log.log(Log.DEBUG, "Paused workers. Applying modifications to world... "); for (Creature c : creatures) { c.applyToWorld(); } - Log.log(Log.DEBUG,"Done."); + Log.log(Log.DEBUG, "Done."); } public void newGen(boolean restart) { diff --git a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java index e7de7e6..bc70426 100644 --- a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java +++ b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java @@ -11,10 +11,7 @@ public class DesktopLauncher { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); config.height = 600; config.width = 800; - config.resizable = false; - config.vSyncEnabled = false; // Setting to false disables vertical sync - config.foregroundFPS = 60; // Setting to 0 disables foreground fps throttling - config.backgroundFPS = 0; // Setting to 0 disables background fps throttling + config.resizable = false; // add resizeability to game new LwjglApplication(new Game(), config); } }