Optimized map renderer to no longer lag out on crappy phones

This commit is contained in:
Anuken 2018-03-20 22:15:44 -04:00
parent 1984921883
commit e330b28e49
3 changed files with 48 additions and 15 deletions

View File

@ -25,7 +25,7 @@ allprojects {
appName = 'Mindustry' appName = 'Mindustry'
gdxVersion = '1.9.8' gdxVersion = '1.9.8'
aiVersion = '1.8.1' aiVersion = '1.8.1'
uCoreVersion = '9503bcb' uCoreVersion = 'df5b262'
getVersionString = { getVersionString = {
String buildVersion = getBuildVersion() String buildVersion = getBuildVersion()

View File

@ -1,7 +1,7 @@
#Autogenerated file. Do not modify. #Autogenerated file. Do not modify.
#Tue Mar 20 21:50:29 EDT 2018 #Tue Mar 20 22:13:13 EDT 2018
version=release version=release
androidBuildCode=572 androidBuildCode=580
name=Mindustry name=Mindustry
code=3.4 code=3.4
build=custom build build=custom build

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.editor;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.IntSet; import com.badlogic.gdx.utils.IntSet;
import com.badlogic.gdx.utils.IntSet.IntSetIterator;
import io.anuke.mindustry.io.MapTileData.TileDataWriter; import io.anuke.mindustry.io.MapTileData.TileDataWriter;
import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.blocks.Blocks; import io.anuke.mindustry.world.blocks.Blocks;
@ -9,15 +10,14 @@ import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics; import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Timers; import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.MeshBatch; import io.anuke.ucore.graphics.IndexedRenderer;
import io.anuke.ucore.util.Log; import io.anuke.ucore.util.Log;
import static io.anuke.mindustry.Vars.tilesize; import static io.anuke.mindustry.Vars.tilesize;
public class MapRenderer { public class MapRenderer {
private static final int chunksize = 32; private static final int chunksize = 64;
private MeshBatch mesh = new MeshBatch(0); private IndexedRenderer[][] chunks;
private int[][] chunks;
private IntSet updates = new IntSet(); private IntSet updates = new IntSet();
private MapEditor editor; private MapEditor editor;
private int width, height; private int width, height;
@ -27,7 +27,22 @@ public class MapRenderer {
} }
public void resize(int width, int height){ public void resize(int width, int height){
mesh.resize(width * height * 3); //TODO just freezes on tablets
if(chunks != null){
for(int x = 0; x < chunks.length; x ++){
for(int y = 0; y < chunks[0].length; y ++){
chunks[x][y].dispose();
}
}
}
chunks = new IndexedRenderer[width/chunksize][height/chunksize];
for(int x = 0; x < width/chunksize; x ++){
for(int y = 0; y < height/chunksize; y ++){
chunks[x][y] = new IndexedRenderer(chunksize*chunksize*2);
}
}
this.width = width; this.width = width;
this.height = height; this.height = height;
updateAll(); updateAll();
@ -39,11 +54,26 @@ public class MapRenderer {
Graphics.end(); Graphics.end();
mesh.getTransformMatrix().setToTranslation(tx, ty, 0).scl(tw / (width * tilesize), IntSetIterator it = updates.iterator();
th / (height * tilesize), 1f); while(it.hasNext){
mesh.setProjectionMatrix(Core.batch.getProjectionMatrix()); int i = it.next();
int x = i % width;
int y = i / height;
render(x, y);
}
updates.clear();
mesh.render(Core.atlas.getTextures().first()); for(int x = 0; x < width/chunksize; x ++){
for(int y = 0; y < height/chunksize; y ++){
IndexedRenderer mesh = chunks[x][y];
mesh.getTransformMatrix().setToTranslation(tx, ty, 0).scl(tw / (width * tilesize),
th / (height * tilesize), 1f);
mesh.setProjectionMatrix(Core.batch.getProjectionMatrix());
mesh.render(Core.atlas.getTextures().first());
}
}
Graphics.begin(); Graphics.begin();
@ -53,7 +83,8 @@ public class MapRenderer {
} }
public void updatePoint(int x, int y){ public void updatePoint(int x, int y){
render(x, y); //TODO spread out over multiple frames?
updates.add(x + y*width);
} }
public void updateAll(){ public void updateAll(){
@ -65,6 +96,8 @@ public class MapRenderer {
} }
private void render(int wx, int wy){ private void render(int wx, int wy){
int x = wx/chunksize, y = wy/chunksize;
IndexedRenderer mesh = chunks[x][y];
TileDataWriter data = editor.getMap().readAt(wx, wy); TileDataWriter data = editor.getMap().readAt(wx, wy);
Block floor = Block.getByID(data.floor); Block floor = Block.getByID(data.floor);
Block wall = Block.getByID(data.wall); Block wall = Block.getByID(data.wall);
@ -73,14 +106,14 @@ public class MapRenderer {
if (floor != Blocks.air && Draw.hasRegion(fregion)) { if (floor != Blocks.air && Draw.hasRegion(fregion)) {
TextureRegion region = Draw.region(fregion); TextureRegion region = Draw.region(fregion);
mesh.draw(wx + wy*width, region, wx * tilesize, wy * tilesize, 8, 8); mesh.draw((wx % chunksize) + (wy % chunksize)*chunksize, region, wx * tilesize, wy * tilesize, 8, 8);
} }
String wregion = Draw.hasRegion(wall.name) ? wall.name : wall.name + "1"; String wregion = Draw.hasRegion(wall.name) ? wall.name : wall.name + "1";
if (wall != Blocks.air && Draw.hasRegion(wregion)) { if (wall != Blocks.air && Draw.hasRegion(wregion)) {
TextureRegion region = Draw.region(wregion); TextureRegion region = Draw.region(wregion);
mesh.draw(wx + wy*width + (width*height), region, wx * tilesize, wy * tilesize, 8, 8); mesh.draw((wx % chunksize) + (wy % chunksize)*chunksize + chunksize*chunksize, region, wx * tilesize, wy * tilesize, 8, 8);
} }
} }
} }