From 4aba5afade20a8bc6bfd4fea8c797605dc618c7f Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 26 Aug 2016 18:32:45 -0400 Subject: [PATCH] v0.4.2: updated fog of war to respect concurrency --- .../shatteredpixeldungeon/FogOfWar.java | 40 +++++++++++++------ .../scenes/GameScene.java | 6 +-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/FogOfWar.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/FogOfWar.java index 98f7e1941..a677c3ab7 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/FogOfWar.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/FogOfWar.java @@ -45,7 +45,8 @@ public class FogOfWar extends Image { private int width2; private int height2; - public Rect updated; + private volatile Rect updated; + private Rect updating; public FogOfWar( int mapWidth, int mapHeight ) { @@ -78,18 +79,31 @@ public class FogOfWar extends Image { updated = new Rect(0, 0, pWidth, pHeight); } - - public void updateVisibility( boolean[] visible, boolean[] visited, boolean[] mapped ) { - if (updated.isEmpty()) - return; + public synchronized void updateFog(){ + updated.set( 0, 0, pWidth, pWidth ); + } + + public synchronized void updateFogArea(int x, int y, int w, int h){ + updated.union(x, y); + updated.union(x + w, y + h); + } + + public synchronized void moveToUpdating(){ + updating = new Rect(updated); + updated.setEmpty(); + } + + private void updateTexture( boolean[] visible, boolean[] visited, boolean[] mapped ) { + + moveToUpdating(); FogTexture fog = (FogTexture)texture; - for (int i=updated.top; i < updated.bottom; i++) { - int cell = (pWidth - 1) * i + updated.left; - fog.pixels.position((width2) * i + updated.left); - for (int j=updated.left; j < updated.right; j++) { + for (int i=updating.top; i < updating.bottom; i++) { + int cell = (pWidth - 1) * i + updating.left; + fog.pixels.position((width2) * i + updating.left); + for (int j=updating.left; j < updating.right; j++) { if (cell < pWidth || cell >= Dungeon.level.length()) { fog.pixels.put(INVISIBLE); } else @@ -112,11 +126,10 @@ public class FogOfWar extends Image { } } - if (updated.width() == pWidth && updated.height() == pHeight) + if (updating.width() == pWidth && updating.height() == pHeight) fog.update(); else - fog.update(updated.top, updated.bottom); - updated.setEmpty(); + fog.update(updating.top, updating.bottom); } @@ -193,7 +206,8 @@ public class FogOfWar extends Image { public void draw() { if (!updated.isEmpty()){ - updateVisibility(Dungeon.visible, Dungeon.level.visited, Dungeon.level.mapped); + updateTexture(Dungeon.visible, Dungeon.level.visited, Dungeon.level.mapped); + updating.setEmpty(); } super.draw(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java index 4b8281918..cfc124a9b 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java @@ -229,7 +229,6 @@ public class GameScene extends PixelScene { } fog = new FogOfWar( Dungeon.level.width(), Dungeon.level.height() ); - fog.updateVisibility( Dungeon.visible, Dungeon.level.visited, Dungeon.level.mapped ); add( fog ); brightness( ShatteredPixelDungeon.brightness() ); @@ -707,13 +706,12 @@ public class GameScene extends PixelScene { public static void updateFog(){ if (scene != null) - scene.fog.updated.set(0, 0, Dungeon.level.width()+1, Dungeon.level.height()+1); + scene.fog.updateFog(); } public static void updateFog(int x, int y, int w, int h){ if (scene != null) { - scene.fog.updated.union(x, y); - scene.fog.updated.union(x + w, y + h); + scene.fog.updateFogArea(x, y, w, h); } }