From 6ee54d8d8f9b8aa9c9ee8855622ed0340a29ad1d Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Sun, 5 Jul 2015 17:37:43 +0200 Subject: [PATCH] reimplemented frame limiter --- core/src/com/mygdx/game/Game.java | 31 +++++++++---- core/src/logic/Creature.java | 15 ++++--- core/src/logic/World.java | 45 ++++++++++++++++++- .../mygdx/game/desktop/DesktopLauncher.java | 3 -- 4 files changed, 74 insertions(+), 20 deletions(-) diff --git a/core/src/com/mygdx/game/Game.java b/core/src/com/mygdx/game/Game.java index 5b198ad..2176d64 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,6 +26,10 @@ public class Game extends ApplicationAdapter { shaper = new ShapeRenderer(); shaper.setAutoShapeType(true); font = new BitmapFont(); + Thread worldThread = new Thread(world); + worldThread.setName("Worker"); + worldThread.setPriority(Thread.MAX_PRIORITY); + worldThread.start(); } @Override @@ -54,22 +59,28 @@ public class Game extends ApplicationAdapter { if (Gdx.input.isKeyJustPressed(Input.Keys.P)) { paused = !paused; } - // Update - if (!paused) { - world.update(); + if (Gdx.input.isKeyJustPressed(Input.Keys.L)) { + if (world.getFpsLimit() == 60) { + world.setFpsLimit(0); + } else { + world.setFpsLimit(60); + } } // 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 ex) { } shaper.setColor(0.3f, 0.3f, 0.3f, 1); // draw borders @@ -84,4 +95,8 @@ public class Game extends ApplicationAdapter { public static Game get() { return game; } + + public boolean isPaused() { + return paused; + } } diff --git a/core/src/logic/Creature.java b/core/src/logic/Creature.java index ee510f0..54bc894 100644 --- a/core/src/logic/Creature.java +++ b/core/src/logic/Creature.java @@ -33,6 +33,7 @@ public class Creature extends Element { fov = (float) Math.PI / 2.5f; fitness = 0; brain = new Brain(10, 5, 2, 10); + sights = new Sight[2]; } @Override @@ -146,18 +147,18 @@ public class Creature extends Element { double relX = Math.cos(dir), relY = Math.sin(dir); float c = 0; float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f); - for (int i = 0; i < sights.length; i++) { - if (sights[i] != null) { - c = sights[i].getDistance() / sightRange * 2 + sightRange; + for (Sight sight : sights) { + if (sight != null) { + c = sight.getDistance() / sightRange * 2 + sightRange; } else { } - if (sights[i] != null) { - if (sights[i].getElement() instanceof Creature) { + if (sight != null) { + if (sight.getElement() instanceof Creature) { s.setColor(c, 0, 0, 1); - } else if (sights[i].getElement() instanceof Vegetable) { + } else if (sight.getElement() instanceof Vegetable) { s.setColor(0, c, 0, 1); } - s.line(eyeX + getX(), getY() + eyeY, sights[i].getElement().getX(), sights[i].getElement().getY()); + s.line(eyeX + getX(), getY() + eyeY, sight.getElement().getX(), sight.getElement().getY()); } } if (sights[0] == null && sights[1] == null) { diff --git a/core/src/logic/World.java b/core/src/logic/World.java index 514ee09..cc6021c 100644 --- a/core/src/logic/World.java +++ b/core/src/logic/World.java @@ -5,9 +5,11 @@ */ package logic; +import com.mygdx.game.Game; import com.mygdx.game.Log; import java.util.ArrayList; import java.util.Comparator; +import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; @@ -15,10 +17,11 @@ 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; + private float fpsLimit = 60; public ArrayList elements; public ArrayList toAdd; public ArrayList creatures; @@ -40,6 +43,36 @@ public class World { newGen(true); } + @Override + public void run() { + Date d; + long time; + float target; + for (;;) { + if (!Game.get().isPaused()) { + d = new Date(); + update(); + if (fpsLimit > 0) { + time = new Date().getTime() - d.getTime(); + target = 1000 / fpsLimit; + if (time < target) { + try { + Thread.sleep((long) (target - time)); + } catch (InterruptedException ex) { + Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + } else { + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + } + public void update() { for (Element e : toAdd) { elements.add(e); @@ -81,7 +114,7 @@ public class World { }; if (graveyard.isEmpty() || restart) { // First gen generation = 1; - Log.log(Log.INFO, "Starting from generation 1: spawning "+creatPerGen+" creatures."); + Log.log(Log.INFO, "Starting from generation 1: spawning " + creatPerGen + " creatures."); for (int i = 0; i < creatPerGen; i++) { spawnCreature(); } @@ -206,4 +239,12 @@ public class World { return plants; } + public float getFpsLimit() { + return fpsLimit; + } + + public void setFpsLimit(float fpsLimit) { + this.fpsLimit = fpsLimit; + } + } diff --git a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java index e7de7e6..039df98 100644 --- a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java +++ b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java @@ -12,9 +12,6 @@ public class DesktopLauncher { 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 new LwjglApplication(new Game(), config); } }