1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-05-15 05:31:34 +02:00

unhooked rendering from logic

This commit is contained in:
Enrico Fasoli 2015-07-03 18:34:33 +02:00
parent 4638bdab3e
commit ace1c3434a
3 changed files with 40 additions and 22 deletions

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.ConcurrentModificationException;
import logic.Element; import logic.Element;
import logic.World; import logic.World;
@ -25,8 +26,8 @@ public class Game extends ApplicationAdapter {
shaper = new ShapeRenderer(); shaper = new ShapeRenderer();
shaper.setAutoShapeType(true); shaper.setAutoShapeType(true);
font = new BitmapFont(); font = new BitmapFont();
new Thread(world).start();
} }
@Override @Override
public void render() { public void render() {
@ -55,22 +56,21 @@ public class Game extends ApplicationAdapter {
if (Gdx.input.isKeyJustPressed(Input.Keys.P)) { if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
paused = !paused; paused = !paused;
} }
// Update
if (!paused && world.isBusy()) {
world.update();
}
// Draw // Draw
Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shaper.setColor(1, 1, 1, 1); shaper.setColor(1, 1, 1, 1);
shaper.begin(ShapeRenderer.ShapeType.Line); shaper.begin(ShapeRenderer.ShapeType.Line);
for (Element e : world.getElements()) { try {
try { for (Element e : world.getElements()) {
e.render(shaper); try {
} catch (ArrayIndexOutOfBoundsException ex) { e.render(shaper);
} catch (ArrayIndexOutOfBoundsException ex) {
// No idea why it happens, but it's rendering so meh // 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); shaper.setColor(0.3f, 0.3f, 0.3f, 1);
// draw borders // draw borders
@ -85,4 +85,8 @@ public class Game extends ApplicationAdapter {
public static Game get() { public static Game get() {
return game; return game;
} }
public boolean isPaused() {
return paused;
}
} }

View File

@ -5,6 +5,7 @@
*/ */
package logic; package logic;
import com.mygdx.game.Game;
import com.mygdx.game.Log; import com.mygdx.game.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
@ -15,7 +16,7 @@ import java.util.logging.Logger;
* *
* @author fazo * @author fazo
*/ */
public class World { public class World implements Runnable {
private final int width, height, nPlants, creatPerGen; private final int width, height, nPlants, creatPerGen;
private int generation = 1; private int generation = 1;
@ -63,9 +64,25 @@ public class World {
newGen(true); 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() { public void update() {
busy = true; busy = true;
Log.log(Log.DEBUG,"Started."); Log.log(Log.DEBUG, "Started.");
for (Element e : toAdd) { for (Element e : toAdd) {
elements.add(e); elements.add(e);
if (e instanceof Creature) { if (e instanceof Creature) {
@ -82,7 +99,7 @@ public class World {
creatures.removeAll(graveyard); creatures.removeAll(graveyard);
if (creatures.isEmpty()) { if (creatures.isEmpty()) {
// All dead, next gen // All dead, next gen
Log.log(Log.DEBUG,"Newgen."); Log.log(Log.DEBUG, "Newgen.");
newGen(false); newGen(false);
} }
while (plants.size() < nPlants) { while (plants.size() < nPlants) {
@ -93,7 +110,7 @@ public class World {
} }
workerDone = false; workerDone = false;
busy = false; busy = false;
Log.log(Log.DEBUG,"Launching Creature workers"); Log.log(Log.DEBUG, "Launching Creature workers");
int readyCount = 0; int readyCount = 0;
Thread.yield(); Thread.yield();
do { do {
@ -103,9 +120,9 @@ public class World {
readyCount++; 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()); } while (readyCount < creatures.size());
Log.log(Log.DEBUG,"Done creatures, awaiting veg worker..."); Log.log(Log.DEBUG, "Done creatures, awaiting veg worker...");
while (!workerDone) { while (!workerDone) {
Thread.yield(); Thread.yield();
/*try { /*try {
@ -115,11 +132,11 @@ public class World {
}*/ }*/
} }
busy = true; 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) { for (Creature c : creatures) {
c.applyToWorld(); c.applyToWorld();
} }
Log.log(Log.DEBUG,"Done."); Log.log(Log.DEBUG, "Done.");
} }
public void newGen(boolean restart) { public void newGen(boolean restart) {

View File

@ -11,10 +11,7 @@ public class DesktopLauncher {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.height = 600; config.height = 600;
config.width = 800; config.width = 800;
config.resizable = false; config.resizable = false; // add resizeability to game
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); new LwjglApplication(new Game(), config);
} }
} }