Multi-threaded generation previews

This commit is contained in:
Anuken
2018-07-20 09:26:41 -04:00
parent 3a63fa5475
commit 3c83b88127
3 changed files with 38 additions and 17 deletions
@@ -1,8 +1,10 @@
package io.anuke.mindustry.ui.dialogs;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.async.AsyncExecutor;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.maps.generation.WorldGenerator.GenResult;
import io.anuke.mindustry.world.ColorMapper;
@@ -12,6 +14,7 @@ import io.anuke.ucore.scene.event.InputEvent;
import io.anuke.ucore.scene.event.InputListener;
import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.GridMap;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.sectorSize;
import static io.anuke.mindustry.Vars.world;
@@ -26,9 +29,11 @@ public class GenViewDialog extends FloatingDialog{
public class GenView extends Element{
GridMap<Texture> map = new GridMap<>();
GridMap<Boolean> processing = new GridMap<>();
float panX, panY;
float lastX, lastY;
int viewsize = 2;
int viewsize = 3;
AsyncExecutor async = new AsyncExecutor(Mathf.sqr(viewsize*2));
{
addListener(new InputListener(){
@@ -57,9 +62,9 @@ public class GenViewDialog extends FloatingDialog{
}
public void draw(){
int tx = (int)(panX / sectorSize);
int ty = (int)(panY / sectorSize);
float padSectorSize = 200f;
int tx = (int)(panX / padSectorSize);
int ty = (int)(panY / padSectorSize);
Draw.color();
@@ -67,14 +72,24 @@ public class GenViewDialog extends FloatingDialog{
for(int y = -viewsize; y <= viewsize; y++){
int wx = tx + x, wy = ty + y;
if(map.get(wx, wy) == null){
Pixmap pixmap = new Pixmap(sectorSize, sectorSize, Format.RGBA8888);
for(int i = 0; i < sectorSize; i++){
for(int j = 0; j < sectorSize; j++){
GenResult result = world.generator().generateTile(wx, wy, i, j);
pixmap.drawPixel(i, sectorSize - 1 - j, ColorMapper.colorFor(result.floor, result.wall, Team.none, result.elevation));
}
if(processing.get(wx, wy) == Boolean.TRUE){
continue;
}
map.put(wx, wy, new Texture(pixmap));
processing.put(wx, wy, true);
async.submit(() -> {
GenResult result = new GenResult();
Pixmap pixmap = new Pixmap(sectorSize, sectorSize, Format.RGBA8888);
for(int i = 0; i < sectorSize; i++){
for(int j = 0; j < sectorSize; j++){
world.generator().generateTile(result, wx, wy, i, j, true);
pixmap.drawPixel(i, sectorSize - 1 - j, ColorMapper.colorFor(result.floor, result.wall, Team.none, result.elevation));
}
}
Gdx.app.postRunnable(() -> map.put(wx, wy, new Texture(pixmap)));
return pixmap;
});
continue;
}
float drawX = x + width/2f+ wx * padSectorSize - tx * padSectorSize - panX % padSectorSize;