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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

After

Width:  |  Height:  |  Size: 245 B

View File

@ -1,7 +1,7 @@
#Autogenerated file. Do not modify. #Autogenerated file. Do not modify.
#Tue Apr 17 19:48:06 EDT 2018 #Tue Apr 17 22:18:27 EDT 2018
version=release version=release
androidBuildCode=947 androidBuildCode=962
name=Mindustry name=Mindustry
code=3.5 code=3.5
build=custom build build=custom build

View File

@ -27,6 +27,7 @@ import io.anuke.mindustry.graphics.Shaders;
import io.anuke.mindustry.input.InputHandler; import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.input.PlaceMode; import io.anuke.mindustry.input.PlaceMode;
import io.anuke.mindustry.ui.fragments.ToolFragment; import io.anuke.mindustry.ui.fragments.ToolFragment;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.BlockBar; import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.*; import io.anuke.ucore.core.*;
@ -106,6 +107,10 @@ public class Renderer extends RendererModule{
clearColor.a = 1f; clearColor.a = 1f;
background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat); background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
for(Block block : Block.getAllBlocks()){
block.load();
}
} }
@Override @Override
@ -141,7 +146,7 @@ public class Renderer extends RendererModule{
} }
if(state.is(State.menu)){ if(state.is(State.menu)){
clearScreen(); Graphics.clear(Color.BLACK);
}else{ }else{
boolean smoothcam = Settings.getBool("smoothcam"); boolean smoothcam = Settings.getBool("smoothcam");
@ -190,8 +195,8 @@ public class Renderer extends RendererModule{
@Override @Override
public void draw(){ public void draw(){
camera.update(); camera.update();
clearScreen(clearColor); Graphics.clear(clearColor);
batch.setProjectionMatrix(camera.combined); batch.setProjectionMatrix(camera.combined);

View File

@ -1,22 +1,13 @@
package io.anuke.mindustry.graphics; 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 com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.content.blocks.Blocks; import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.game.Team; import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.types.StaticBlock; 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.Graphics;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.CacheBatch;
import io.anuke.ucore.graphics.Draw; 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 io.anuke.ucore.util.Mathf;
import java.util.Arrays; import java.util.Arrays;
@ -27,9 +18,8 @@ import static io.anuke.ucore.core.Core.camera;
public class BlockRenderer{ public class BlockRenderer{
private final static int chunksize = 32; private final static int chunksize = 32;
private final static int initialRequests = 32*32; private final static int initialRequests = 32*32;
private int[][][] cache; private FloorRenderer floorRenderer;
private CacheBatch cbatch;
private Array<BlockRequest> requests = new Array<BlockRequest>(initialRequests); private Array<BlockRequest> requests = new Array<BlockRequest>(initialRequests);
private Layer lastLayer; private Layer lastLayer;
@ -37,6 +27,8 @@ public class BlockRenderer{
private int iterateidx = 0; private int iterateidx = 0;
public BlockRenderer(){ public BlockRenderer(){
floorRenderer = new FloorRenderer();
for(int i = 0; i < requests.size; i ++){ for(int i = 0; i < requests.size; i ++){
requests.set(i, new BlockRequest()); requests.set(i, new BlockRequest());
} }
@ -113,7 +105,7 @@ public class BlockRenderer{
Draw.color(); Draw.color();
Graphics.end(); Graphics.end();
drawCache(DrawLayer.walls, crangex, crangey); floorRenderer.drawCache(DrawLayer.walls, crangex, crangey);
Graphics.begin(); Graphics.begin();
Arrays.sort(requests.items, 0, requestidx); 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 layerBegins(Layer layer){}
private void layerEnds(Layer layer){} private void layerEnds(Layer layer){}
@ -198,118 +198,4 @@ public class BlockRenderer{
r.layer = layer; r.layer = layer;
requestidx ++; 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);
}
} }

View File

@ -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);
}
}

View File

@ -27,9 +27,7 @@ import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.util.Bundles; import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Mathf; import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.state; import static io.anuke.mindustry.Vars.*;
import static io.anuke.mindustry.Vars.tilesize;
import static io.anuke.mindustry.Vars.world;
public class Block extends BaseBlock { public class Block extends BaseBlock {
private static int lastid; private static int lastid;
@ -139,6 +137,11 @@ public class Block extends BaseBlock {
setBars(); setBars();
} }
/**Called after texture atlas is loaded.*/
public void load(){
}
public void tapped(Tile tile){} public void tapped(Tile tile){}
public void buildTable(Tile tile, Table table) {} public void buildTable(Tile tile, Table table) {}
public void configure(Tile tile, byte data){} public void configure(Tile tile, byte data){}

View File

@ -44,7 +44,6 @@ public class Floor extends Block{
} }
} }
@Override @Override
public void drawNonLayer(Tile tile){ public void drawNonLayer(Tile tile){
MathUtils.random.setSeed(tile.id()); MathUtils.random.setSeed(tile.id());
@ -63,6 +62,7 @@ public class Floor extends Block{
private void drawEdges(Tile tile, boolean sameLayer){ private void drawEdges(Tile tile, boolean sameLayer){
if(!blend) return; if(!blend) return;
for(int dx = -1; dx <= 1; dx ++){ for(int dx = -1; dx <= 1; dx ++){
for(int dy = -1; dy <= 1; dy ++){ for(int dy = -1; dy <= 1; dy ++){