Refactored BlockRenderer into two classes

This commit is contained in:
Anuken
2018-04-17 22:20:57 -04:00
parent 434256b0a5
commit 622f0d9355
7 changed files with 174 additions and 136 deletions
@@ -1,22 +1,13 @@
package io.anuke.mindustry.graphics;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.types.StaticBlock;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.CacheBatch;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.Lines;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Mathf;
import java.util.Arrays;
@@ -27,9 +18,8 @@ import static io.anuke.ucore.core.Core.camera;
public class BlockRenderer{
private final static int chunksize = 32;
private final static int initialRequests = 32*32;
private int[][][] cache;
private CacheBatch cbatch;
private FloorRenderer floorRenderer;
private Array<BlockRequest> requests = new Array<BlockRequest>(initialRequests);
private Layer lastLayer;
@@ -37,6 +27,8 @@ public class BlockRenderer{
private int iterateidx = 0;
public BlockRenderer(){
floorRenderer = new FloorRenderer();
for(int i = 0; i < requests.size; i ++){
requests.set(i, new BlockRequest());
}
@@ -113,7 +105,7 @@ public class BlockRenderer{
Draw.color();
Graphics.end();
drawCache(DrawLayer.walls, crangex, crangey);
floorRenderer.drawCache(DrawLayer.walls, crangex, crangey);
Graphics.begin();
Arrays.sort(requests.items, 0, requestidx);
@@ -182,6 +174,14 @@ public class BlockRenderer{
}
}
public void clearTiles(){
floorRenderer.clearTiles();
}
public void drawFloor(){
floorRenderer.drawFloor();
}
private void layerBegins(Layer layer){}
private void layerEnds(Layer layer){}
@@ -198,118 +198,4 @@ public class BlockRenderer{
r.layer = layer;
requestidx ++;
}
public void drawFloor(){
int chunksx = world.width() / chunksize, chunksy = world.height() / chunksize;
//render the entire map
if(cache == null || cache.length != chunksx || cache[0].length != chunksy){
cache = new int[chunksx][chunksy][DrawLayer.values().length];
Timers.markNs();
for(DrawLayer layer : DrawLayer.values()){
for(int x = 0; x < chunksx; x++){
for(int y = 0; y < chunksy; y++){
cacheChunk(x, y, layer);
}
}
}
Log.info("CACHING ELAPSED: {0}", Timers.elapsedNs());
}
OrthographicCamera camera = Core.camera;
if(Graphics.drawing()) Graphics.end();
int crangex = (int)(camera.viewportWidth * camera.zoom / (chunksize * tilesize))+1;
int crangey = (int)(camera.viewportHeight * camera.zoom / (chunksize * tilesize))+1;
DrawLayer[] layers = DrawLayer.values();
for(int i = 0; i < layers.length - 1; i ++) {
drawCache(layers[i], crangex, crangey);
}
Graphics.begin();
Draw.reset();
if(debug && debugChunks){
Draw.color(Color.YELLOW);
Lines.stroke(1f);
for(int x = -crangex; x <= crangex; x++){
for(int y = -crangey; y <= crangey; y++){
int worldx = Mathf.scl(camera.position.x, chunksize * tilesize) + x;
int worldy = Mathf.scl(camera.position.y, chunksize * tilesize) + y;
if(!Mathf.inBounds(worldx, worldy, cache))
continue;
Lines.rect(worldx * chunksize * tilesize, worldy * chunksize * tilesize, chunksize * tilesize, chunksize * tilesize);
}
}
Draw.reset();
}
}
void drawCache(DrawLayer layer, int crangex, int crangey){
Gdx.gl.glEnable(GL20.GL_BLEND);
layer.begin(cbatch);
for(int x = -crangex; x <= crangex; x++){
for(int y = -crangey; y <= crangey; y++){
int worldx = Mathf.scl(camera.position.x, chunksize * tilesize) + x;
int worldy = Mathf.scl(camera.position.y, chunksize * tilesize) + y;
if(!Mathf.inBounds(worldx, worldy, cache))
continue;
cbatch.drawCache(cache[worldx][worldy][layer.ordinal()]);
}
}
layer.end(cbatch);
}
void cacheChunk(int cx, int cy, DrawLayer layer){
if(cbatch == null){
createBatch();
}
cbatch.begin();
Graphics.useBatch(cbatch);
for(int tilex = cx * chunksize; tilex < (cx + 1) * chunksize; tilex++){
for(int tiley = cy * chunksize; tiley < (cy + 1) * chunksize; tiley++){
Tile tile = world.tile(tilex, tiley);
if(tile == null) continue;
if(tile.floor().drawLayer == layer && tile.block().drawLayer != DrawLayer.walls){
tile.floor().draw(tile);
}else if(tile.floor().drawLayer.ordinal() < layer.ordinal() && tile.block().drawLayer != DrawLayer.walls){
tile.floor().drawNonLayer(tile);
}
if(tile.block().drawLayer == layer && layer == DrawLayer.walls){
tile.block().draw(tile);
}
}
}
Graphics.popBatch();
cbatch.end();
cache[cx][cy][layer.ordinal()] = cbatch.getLastCache();
}
public void clearTiles(){
cache = null;
createBatch();
}
private void createBatch(){
if(cbatch != null)
cbatch.dispose();
cbatch = new CacheBatch(world.width() * world.height() * 4);
}
}
@@ -0,0 +1,144 @@
package io.anuke.mindustry.graphics;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.CacheBatch;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.Lines;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.core.Core.camera;
public class FloorRenderer {
private final static int chunksize = 32;
private int[][][] cache;
private CacheBatch cbatch;
public FloorRenderer(){
}
public void drawFloor(){
int chunksx = world.width() / chunksize, chunksy = world.height() / chunksize;
//render the entire map
if(cache == null || cache.length != chunksx || cache[0].length != chunksy){
cache = new int[chunksx][chunksy][DrawLayer.values().length];
Timers.markNs();
for(DrawLayer layer : DrawLayer.values()){
for(int x = 0; x < chunksx; x++){
for(int y = 0; y < chunksy; y++){
cacheChunk(x, y, layer);
}
}
}
Log.info("CACHING ELAPSED: {0}", Timers.elapsedNs());
}
OrthographicCamera camera = Core.camera;
if(Graphics.drawing()) Graphics.end();
int crangex = (int)(camera.viewportWidth * camera.zoom / (chunksize * tilesize))+1;
int crangey = (int)(camera.viewportHeight * camera.zoom / (chunksize * tilesize))+1;
DrawLayer[] layers = DrawLayer.values();
for(int i = 0; i < layers.length - 1; i ++) {
drawCache(layers[i], crangex, crangey);
}
Graphics.begin();
Draw.reset();
if(debug && debugChunks){
Draw.color(Color.YELLOW);
Lines.stroke(1f);
for(int x = -crangex; x <= crangex; x++){
for(int y = -crangey; y <= crangey; y++){
int worldx = Mathf.scl(camera.position.x, chunksize * tilesize) + x;
int worldy = Mathf.scl(camera.position.y, chunksize * tilesize) + y;
if(!Mathf.inBounds(worldx, worldy, cache))
continue;
Lines.rect(worldx * chunksize * tilesize, worldy * chunksize * tilesize, chunksize * tilesize, chunksize * tilesize);
}
}
Draw.reset();
}
}
void drawCache(DrawLayer layer, int crangex, int crangey){
Gdx.gl.glEnable(GL20.GL_BLEND);
layer.begin(cbatch);
for(int x = -crangex; x <= crangex; x++){
for(int y = -crangey; y <= crangey; y++){
int worldx = Mathf.scl(camera.position.x, chunksize * tilesize) + x;
int worldy = Mathf.scl(camera.position.y, chunksize * tilesize) + y;
if(!Mathf.inBounds(worldx, worldy, cache))
continue;
cbatch.drawCache(cache[worldx][worldy][layer.ordinal()]);
}
}
layer.end(cbatch);
}
void cacheChunk(int cx, int cy, DrawLayer layer){
if(cbatch == null){
createBatch();
}
cbatch.begin();
Graphics.useBatch(cbatch);
for(int tilex = cx * chunksize; tilex < (cx + 1) * chunksize; tilex++){
for(int tiley = cy * chunksize; tiley < (cy + 1) * chunksize; tiley++){
Tile tile = world.tile(tilex, tiley);
if(tile == null) continue;
if(tile.floor().drawLayer == layer && tile.block().drawLayer != DrawLayer.walls){
tile.floor().draw(tile);
}else if(tile.floor().drawLayer.ordinal() < layer.ordinal() && tile.block().drawLayer != DrawLayer.walls){
tile.floor().drawNonLayer(tile);
}
if(tile.block().drawLayer == layer && layer == DrawLayer.walls){
tile.block().draw(tile);
}
}
}
Graphics.popBatch();
cbatch.end();
cache[cx][cy][layer.ordinal()] = cbatch.getLastCache();
}
public void clearTiles(){
cache = null;
createBatch();
}
private void createBatch(){
if(cbatch != null)
cbatch.dispose();
cbatch = new CacheBatch(world.width() * world.height() * 4);
}
}