v0.4.2: updated fog of war to respect concurrency

This commit is contained in:
Evan Debenham 2016-08-26 18:32:45 -04:00 committed by Evan Debenham
parent 6bcccba062
commit 4aba5afade
2 changed files with 29 additions and 17 deletions

View File

@ -45,7 +45,8 @@ public class FogOfWar extends Image {
private int width2; private int width2;
private int height2; private int height2;
public Rect updated; private volatile Rect updated;
private Rect updating;
public FogOfWar( int mapWidth, int mapHeight ) { public FogOfWar( int mapWidth, int mapHeight ) {
@ -78,18 +79,31 @@ public class FogOfWar extends Image {
updated = new Rect(0, 0, pWidth, pHeight); updated = new Rect(0, 0, pWidth, pHeight);
} }
public void updateVisibility( boolean[] visible, boolean[] visited, boolean[] mapped ) {
if (updated.isEmpty()) public synchronized void updateFog(){
return; 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; FogTexture fog = (FogTexture)texture;
for (int i=updated.top; i < updated.bottom; i++) { for (int i=updating.top; i < updating.bottom; i++) {
int cell = (pWidth - 1) * i + updated.left; int cell = (pWidth - 1) * i + updating.left;
fog.pixels.position((width2) * i + updated.left); fog.pixels.position((width2) * i + updating.left);
for (int j=updated.left; j < updated.right; j++) { for (int j=updating.left; j < updating.right; j++) {
if (cell < pWidth || cell >= Dungeon.level.length()) { if (cell < pWidth || cell >= Dungeon.level.length()) {
fog.pixels.put(INVISIBLE); fog.pixels.put(INVISIBLE);
} else } 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(); fog.update();
else else
fog.update(updated.top, updated.bottom); fog.update(updating.top, updating.bottom);
updated.setEmpty();
} }
@ -193,7 +206,8 @@ public class FogOfWar extends Image {
public void draw() { public void draw() {
if (!updated.isEmpty()){ 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(); super.draw();

View File

@ -229,7 +229,6 @@ public class GameScene extends PixelScene {
} }
fog = new FogOfWar( Dungeon.level.width(), Dungeon.level.height() ); fog = new FogOfWar( Dungeon.level.width(), Dungeon.level.height() );
fog.updateVisibility( Dungeon.visible, Dungeon.level.visited, Dungeon.level.mapped );
add( fog ); add( fog );
brightness( ShatteredPixelDungeon.brightness() ); brightness( ShatteredPixelDungeon.brightness() );
@ -707,13 +706,12 @@ public class GameScene extends PixelScene {
public static void updateFog(){ public static void updateFog(){
if (scene != null) 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){ public static void updateFog(int x, int y, int w, int h){
if (scene != null) { if (scene != null) {
scene.fog.updated.union(x, y); scene.fog.updateFogArea(x, y, w, h);
scene.fog.updated.union(x + w, y + h);
} }
} }