Successful desktop compilation

This commit is contained in:
Anuken
2018-12-26 17:38:40 -05:00
parent 8879593381
commit 3b42b604e1
53 changed files with 560 additions and 585 deletions
@@ -2,19 +2,22 @@ package io.anuke.mindustry.graphics;
import io.anuke.arc.Core;
import io.anuke.arc.Events;
import io.anuke.arc.Graphics;
import io.anuke.arc.collection.IntArray;
import io.anuke.arc.collection.IntSet;
import io.anuke.arc.collection.IntSet.IntSetIterator;
import io.anuke.arc.collection.ObjectSet;
import io.anuke.arc.graphics.Camera;
import io.anuke.arc.graphics.Color;
import io.anuke.arc.graphics.GL20;
import io.anuke.arc.graphics.g2d.CacheBatch;
import io.anuke.arc.graphics.g2d.Draw;
import io.anuke.arc.graphics.g2d.Fill;
import io.anuke.arc.graphics.g2d.SpriteBatch;
import io.anuke.arc.math.Mathf;
import io.anuke.arc.util.Log;
import io.anuke.arc.util.Structs;
import io.anuke.arc.util.Time;
import io.anuke.mindustry.game.EventType.WorldLoadEvent;
import io.anuke.mindustry.maps.Sector;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Floor;
@@ -33,7 +36,7 @@ public class FloorRenderer{
private IntArray drawnLayers = new IntArray();
public FloorRenderer(){
Events.on(WorldLoadGraphicsEvent.class, event -> clearTiles());
Events.on(WorldLoadEvent.class, event -> clearTiles());
}
public void drawFloor(){
@@ -41,13 +44,13 @@ public class FloorRenderer{
return;
}
OrthographicCamera camera = Core.camera;
Camera camera = Core.camera;
int crangex = (int) (camera.width / (chunksize * tilesize)) + 1;
int crangey = (int) (camera.height / (chunksize * tilesize)) + 1;
int camx = Mathf.scl(camera.position.x, chunksize * tilesize);
int camy = Mathf.scl(camera.position.y, chunksize * tilesize);
int camx = (int)(camera.position.x / (chunksize * tilesize));
int camy = (int)(camera.position.y / (chunksize * tilesize));
int layers = CacheLayer.values().length;
@@ -81,7 +84,7 @@ public class FloorRenderer{
drawnLayers.sort();
Graphics.end();
Draw.flush();
beginDraw();
for(int i = 0; i < drawnLayers.size; i++){
@@ -91,7 +94,6 @@ public class FloorRenderer{
}
endDraw();
Graphics.begin();
}
public void beginDraw(){
@@ -99,7 +101,7 @@ public class FloorRenderer{
return;
}
cbatch.setProjectionMatrix(Core.camera.combined);
cbatch.setProjection(Core.camera.projection());
cbatch.beginDraw();
Core.gl.glEnable(GL20.GL_BLEND);
@@ -118,7 +120,7 @@ public class FloorRenderer{
return;
}
OrthographicCamera camera = Core.camera;
Camera camera = Core.camera;
int crangex = (int) (camera.width / (chunksize * tilesize)) + 1;
int crangey = (int) (camera.height / (chunksize * tilesize)) + 1;
@@ -127,8 +129,8 @@ public class FloorRenderer{
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;
int worldx = (int)(camera.position.x / (chunksize * tilesize)) + x;
int worldy = (int)(camera.position.y / (chunksize * tilesize)) + y;
if(!Structs.inBounds(worldx, worldy, cache)){
continue;
@@ -145,7 +147,7 @@ public class FloorRenderer{
private void fillChunk(float x, float y){
Draw.color(Color.BLACK);
Fill.crect(x, y, chunksize * tilesize, chunksize * tilesize);
Fill.rect().set(x, y, chunksize * tilesize, chunksize * tilesize);
Draw.color();
}
@@ -172,11 +174,8 @@ public class FloorRenderer{
}
private void cacheChunkLayer(int cx, int cy, Chunk chunk, CacheLayer layer){
Graphics.useBatch(cbatch);
cbatch.begin();
Sector sector = world.getSector();
SpriteBatch current = Core.graphics.batch();
Core.graphics.useBatch(cbatch);
for(int tilex = cx * chunksize; tilex < (cx + 1) * chunksize; tilex++){
for(int tiley = cy * chunksize; tiley < (cy + 1) * chunksize; tiley++){
@@ -196,10 +195,8 @@ public class FloorRenderer{
}
}
}
cbatch.end();
Graphics.popBatch();
chunk.caches[layer.ordinal()] = cbatch.getLastCache();
Core.graphics.useBatch(current);
chunk.caches[layer.ordinal()] = cbatch.flushCache();
}
public void clearTiles(){
@@ -1,176 +0,0 @@
package io.anuke.mindustry.graphics;
import io.anuke.arc.Core;
import io.anuke.arc.Events;
import io.anuke.arc.Graphics;
import io.anuke.arc.collection.Array;
import io.anuke.arc.entities.EntityDraw;
import io.anuke.arc.graphics.Color;
import io.anuke.arc.graphics.GL20;
import io.anuke.arc.graphics.Pixmap.Format;
import io.anuke.arc.graphics.Texture;
import io.anuke.arc.graphics.g2d.Draw;
import io.anuke.arc.graphics.g2d.Fill;
import io.anuke.arc.graphics.g2d.TextureRegion;
import io.anuke.arc.graphics.glutils.FrameBuffer;
import io.anuke.arc.util.Disposable;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.game.EventType.TileChangeEvent;
import io.anuke.mindustry.game.EventType.WorldLoadEvent;
import io.anuke.mindustry.world.Tile;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.*;
/**Used for rendering fog of war. A framebuffer is used for this.*/
public class FogRenderer implements Disposable{
private TextureRegion region = new TextureRegion();
private FrameBuffer buffer;
private ByteBuffer pixelBuffer;
private Array<Tile> changeQueue = new Array<>();
private int shadowPadding;
private boolean dirty;
public FogRenderer(){
Events.on(WorldLoadEvent.class, event -> {
dispose();
shadowPadding = -1;
buffer = new FrameBuffer(Format.RGBA8888, world.width(), world.height(), false);
changeQueue.clear();
//clear buffer to black
buffer.begin();
Core.graphics.clear(0, 0, 0, 1f);
buffer.end();
for(int x = 0; x < world.width(); x++){
for(int y = 0; y < world.height(); y++){
Tile tile = world.tile(x, y);
if(tile.getTeam() == players[0].getTeam() && tile.block().synthetic() && tile.block().viewRange > 0){
changeQueue.add(tile);
}
}
}
pixelBuffer = ByteBuffer.allocateDirect(world.width() * world.height() * 4);
dirty = true;
});
Events.on(TileChangeEvent.class, event -> {
if(event.tile.getTeam() == players[0].getTeam() && event.tile.block().synthetic() && event.tile.block().viewRange > 0){
changeQueue.add(event.tile);
}
});
}
public void writeFog(){
if(buffer == null) return;
buffer.begin();
pixelBuffer.position(0);
Core.gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
Core.gl.glReadPixels(0, 0, world.width(), world.height(), GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixelBuffer);
pixelBuffer.position(0);
for(int i = 0; i < world.width() * world.height(); i++){
int x = i % world.width();
int y = i / world.width();
byte r = pixelBuffer.get();
if(r != 0){
world.tile(x, y).setVisibility((byte)1);
}
pixelBuffer.position(pixelBuffer.position() + 3);
}
buffer.end();
}
public int getPadding(){
return -shadowPadding;
}
public void draw(){
if(buffer == null) return;
float vw = Core.camera.width ;
float vh = Core.camera.height ;
float px = Core.camera.position.x - vw / 2f;
float py = Core.camera.position.y - vh / 2f;
float u = (px / tilesize) / buffer.getWidth();
float v = (py / tilesize) / buffer.getHeight();
float u2 = ((px + vw) / tilesize) / buffer.getWidth();
float v2 = ((py + vh) / tilesize) / buffer.getHeight();
Core.batch.getProjectionMatrix().setToOrtho2D(0, 0, buffer.getWidth() * tilesize, buffer.getHeight() * tilesize);
Draw.color(Color.WHITE);
buffer.begin();
Graphics.beginClip((-shadowPadding), (-shadowPadding), (world.width() + shadowPadding*2), (world.height() + shadowPadding*2));
Graphics.begin();
EntityDraw.setClip(false);
renderer.drawAndInterpolate(playerGroup, player -> !player.isDead() && player.getTeam() == players[0].getTeam(), Unit::drawView);
renderer.drawAndInterpolate(unitGroups[players[0].getTeam().ordinal()], unit -> !unit.isDead(), Unit::drawView);
for(Tile tile : changeQueue){
float viewRange = tile.block().viewRange;
if(viewRange < 0) continue;
Fill.circle(tile.drawx(), tile.drawy(), tile.block().viewRange);
}
changeQueue.clear();
if(dirty){
for(int x = 0; x < world.width(); x++){
for(int y = 0; y < world.height(); y++){
Tile tile = world.tile(x, y);
if(tile.discovered()){
Fill.rect(tile.worldx(), tile.worldy(), tilesize, tilesize);
}
}
}
dirty = false;
}
EntityDraw.setClip(true);
Graphics.end();
buffer.end();
Graphics.endClip();
region.setTexture(buffer.getTexture());
region.setRegion(u, v2, u2, v);
Core.batch.setProjectionMatrix(Core.camera.combined);
Draw.shader(Shaders.fog);
renderer.pixelSurface.getBuffer().begin();
Graphics.begin();
Core.batch.draw(region, px, py, vw, vh);
Graphics.end();
renderer.pixelSurface.getBuffer().end();
Draw.shader();
Graphics.setScreen();
Core.batch.draw(renderer.pixelSurface.texture(), 0, Core.graphics.getHeight(), Core.graphics.getWidth(), -Core.graphics.getHeight());
Graphics.end();
}
public Texture getTexture(){
return buffer.getTexture();
}
@Override
public void dispose(){
if(buffer != null) buffer.dispose();
}
}
@@ -0,0 +1,248 @@
package io.anuke.mindustry.graphics;
import io.anuke.arc.Core;
import io.anuke.arc.graphics.*;
import io.anuke.arc.graphics.VertexAttributes.Usage;
import io.anuke.arc.graphics.g2d.TextureRegion;
import io.anuke.arc.graphics.glutils.Shader;
import io.anuke.arc.math.Mathf;
import io.anuke.arc.math.Matrix3;
import io.anuke.arc.util.Disposable;
//TODO this class is a trainwreck, remove it
public class IndexedRenderer implements Disposable{
private final static int vsize = 5;
private Shader program = createDefaultShader();
private Mesh mesh;
private float[] tmpVerts = new float[vsize * 6];
private float[] vertices;
private Matrix3 projMatrix = new Matrix3();
private Matrix3 transMatrix = new Matrix3();
private Matrix3 combined = new Matrix3();
private float color = Color.WHITE.toFloatBits();
public IndexedRenderer(int sprites){
resize(sprites);
}
static public Shader createDefaultShader(){
String vertexShader = "attribute vec4 " + Shader.POSITION_ATTRIBUTE + ";\n" //
+ "attribute vec2 " + Shader.TEXCOORD_ATTRIBUTE + "0;\n" //
+ "uniform mat4 u_projTrans;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "\n" //
+ "void main()\n" //
+ "{\n" //
+ " v_texCoords = " + Shader.TEXCOORD_ATTRIBUTE + "0;\n" //
+ " gl_Position = u_projTrans * " + Shader.POSITION_ATTRIBUTE + ";\n" //
+ "}\n";
String fragmentShader = "#ifdef GL_ES\n" //
+ "#define LOWP lowp\n" //
+ "precision mediump float;\n" //
+ "#else\n" //
+ "#define LOWP \n" //
+ "#endif\n" //
+ "varying vec2 v_texCoords;\n" //
+ "uniform sampler2D u_texture;\n" //
+ "void main()\n"//
+ "{\n" //
+ " gl_FragColor = texture2D(u_texture, v_texCoords);\n" //
+ "}";
return new Shader(vertexShader, fragmentShader);
}
public void render(Texture texture){
Core.gl.glEnable(GL20.GL_BLEND);
updateMatrix();
program.begin();
texture.bind();
program.setUniformMatrix("u_projTrans", combined);
program.setUniformi("u_texture", 0);
mesh.render(program, GL20.GL_TRIANGLES, 0, vertices.length / 5);
program.end();
}
public void setColor(Color color){
this.color = color.toFloatBits();
}
public void draw(int index, TextureRegion region, float x, float y, float w, float h){
final float fx2 = x + w;
final float fy2 = y + h;
final float u = region.getU();
final float v = region.getV2();
final float u2 = region.getU2();
final float v2 = region.getV();
float[] vertices = tmpVerts;
int idx = 0;
vertices[idx++] = x;
vertices[idx++] = y;
vertices[idx++] = color;
vertices[idx++] = u;
vertices[idx++] = v;
vertices[idx++] = x;
vertices[idx++] = fy2;
vertices[idx++] = color;
vertices[idx++] = u;
vertices[idx++] = v2;
vertices[idx++] = fx2;
vertices[idx++] = fy2;
vertices[idx++] = color;
vertices[idx++] = u2;
vertices[idx++] = v2;
//tri2
vertices[idx++] = x;
vertices[idx++] = y;
vertices[idx++] = color;
vertices[idx++] = u;
vertices[idx++] = v;
vertices[idx++] = fx2;
vertices[idx++] = y;
vertices[idx++] = color;
vertices[idx++] = u2;
vertices[idx++] = v;
vertices[idx++] = fx2;
vertices[idx++] = fy2;
vertices[idx++] = color;
vertices[idx++] = u2;
vertices[idx++] = v2;
mesh.updateVertices(index * vsize * 6, vertices);
}
public void draw(int index, TextureRegion region, float x, float y, float w, float h, float rotation){
final float u = region.getU();
final float v = region.getV2();
final float u2 = region.getU2();
final float v2 = region.getV();
final float originX = w / 2, originY = h / 2;
final float cos = Mathf.cosDeg(rotation);
final float sin = Mathf.sinDeg(rotation);
float fx = -originX;
float fy = -originY;
float fx2 = w - originX;
float fy2 = h - originY;
final float worldOriginX = x + originX;
final float worldOriginY = y + originY;
float x1 = cos * fx - sin * fy;
float y1 = sin * fx + cos * fy;
float x2 = cos * fx - sin * fy2;
float y2 = sin * fx + cos * fy2;
float x3 = cos * fx2 - sin * fy2;
float y3 = sin * fx2 + cos * fy2;
float x4 = x1 + (x3 - x2);
float y4 = y3 - (y2 - y1);
x1 += worldOriginX;
y1 += worldOriginY;
x2 += worldOriginX;
y2 += worldOriginY;
x3 += worldOriginX;
y3 += worldOriginY;
x4 += worldOriginX;
y4 += worldOriginY;
float[] vertices = tmpVerts;
int idx = 0;
vertices[idx++] = x1;
vertices[idx++] = y1;
vertices[idx++] = color;
vertices[idx++] = u;
vertices[idx++] = v;
vertices[idx++] = x3;
vertices[idx++] = y3;
vertices[idx++] = color;
vertices[idx++] = u2;
vertices[idx++] = v2;
vertices[idx++] = x4;
vertices[idx++] = y4;
vertices[idx++] = color;
vertices[idx++] = u;
vertices[idx++] = v2;
//tri2
vertices[idx++] = x1;
vertices[idx++] = y1;
vertices[idx++] = color;
vertices[idx++] = u;
vertices[idx++] = v;
vertices[idx++] = x2;
vertices[idx++] = y2;
vertices[idx++] = color;
vertices[idx++] = u2;
vertices[idx++] = v;
vertices[idx++] = x3;
vertices[idx++] = y3;
vertices[idx++] = color;
vertices[idx++] = u2;
vertices[idx++] = v2;
mesh.updateVertices(index * vsize * 6, vertices);
}
public Matrix3 getTransformMatrix(){
return transMatrix;
}
public void setTransformMatrix(Matrix3 matrix){
transMatrix = matrix;
}
public Matrix3 getProjectionMatrix(){
return projMatrix;
}
public void setProjectionMatrix(Matrix3 matrix){
projMatrix = matrix;
}
public void resize(int sprites){
if(mesh != null) mesh.dispose();
mesh = new Mesh(true, 6 * sprites, 0,
new VertexAttribute(Usage.Position, 2, "a_position"),
new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));
vertices = new float[6 * sprites * vsize];
mesh.setVertices(vertices);
}
private void updateMatrix(){
combined.set(projMatrix).mul(transMatrix);
}
@Override
public void dispose(){
mesh.dispose();
program.dispose();
}
}
@@ -37,11 +37,11 @@ public class Shaders{
build = new UnitBuild();
mix = new MixShader();
fog = new FogShader();
fullMix = new Shader("fullmix", "default");
fullMix = new LoadShader("fullmix", "default");
menu = new MenuShader();
}
public static class MenuShader extends Shader{
public static class MenuShader extends LoadShader{
float time = 0f;
public MenuShader(){
@@ -60,13 +60,13 @@ public class Shaders{
}
}
public static class FogShader extends Shader{
public static class FogShader extends LoadShader{
public FogShader(){
super("fog", "default");
}
}
public static class MixShader extends Shader{
public static class MixShader extends LoadShader{
public Color color = new Color(Color.WHITE);
public MixShader(){
@@ -93,7 +93,7 @@ public class Shaders{
}
}
public static class UnitBuild extends Shader{
public static class UnitBuild extends LoadShader{
public float progress, time;
public Color color = new Color();
public TextureRegion region;
@@ -113,8 +113,9 @@ public class Shaders{
}
}
public static class Outline extends Shader{
public static class Outline extends LoadShader{
public Color color = new Color();
public TextureRegion region = new TextureRegion();
public Outline(){
super("outline", "default");
@@ -127,9 +128,10 @@ public class Shaders{
}
}
public static class BlockBuild extends Shader{
public static class BlockBuild extends LoadShader{
public Color color = new Color();
public float progress;
public TextureRegion region = new TextureRegion();
public BlockBuild(){
super("blockbuild", "default");
@@ -146,8 +148,9 @@ public class Shaders{
}
}
public static class BlockPreview extends Shader{
public static class BlockPreview extends LoadShader{
public Color color = new Color();
public TextureRegion region = new TextureRegion();
public BlockPreview(){
super("blockpreview", "default");
@@ -162,7 +165,7 @@ public class Shaders{
}
}
public static class Shield extends Shader{
public static class Shield extends LoadShader{
public Shield(){
super("shield", "default");
@@ -180,7 +183,7 @@ public class Shaders{
}
}
public static class SurfaceShader extends Shader{
public static class SurfaceShader extends LoadShader{
public SurfaceShader(String frag){
super(frag, "default");
@@ -196,4 +199,10 @@ public class Shaders{
setUniformf("time", Time.time());
}
}
public static class LoadShader extends Shader{
public LoadShader(String frag, String vert){
super(Core.files.internal("shaders/" + vert + ".vertex"), Core.files.internal("shaders/" + frag + ".fragment"));
}
}
}
@@ -0,0 +1,39 @@
package io.anuke.mindustry.graphics;
import io.anuke.arc.Core;
import io.anuke.arc.graphics.g2d.CapStyle;
import io.anuke.arc.graphics.g2d.Draw;
import io.anuke.arc.graphics.g2d.Lines;
import io.anuke.arc.graphics.g2d.TextureRegion;
import io.anuke.arc.math.Mathf;
//TODO remove
public class Shapes{
public static void laser(String line, String edge, float x, float y, float x2, float y2, float scale){
laser(line, edge, x, y, x2, y2, Mathf.atan2(x2 - x, y2 - y), scale);
}
public static void laser(String line, String edge, float x, float y, float x2, float y2){
laser(line, edge, x, y, x2, y2, Mathf.atan2(x2 - x, y2 - y), 1f);
}
public static void laser(String line, String edge, float x, float y, float x2, float y2, float rotation, float scale){
Lines.stroke(12f * scale);
Lines.line(Core.atlas.find(line), x, y, x2, y2, CapStyle.none, 0f);
Lines.stroke(1f);
TextureRegion region = Core.atlas.find(edge);
Draw.rect(edge, x, y, region.getWidth() * Draw.scl, region.getHeight() * scale * Draw.scl).rot(rotation + 180);
Draw.rect(edge, x2, y2, region.getWidth() * Draw.scl, region.getHeight() * scale * Draw.scl).rot(rotation);
}
public static void tri(float x, float y, float width, float length, float rotation){
float oy = 17f / 63f * length;
Core.graphics.batch().draw().tex(Core.atlas.find("shape-3")).pos(x - width / 2f, y - oy)
.origin(width / 2f, oy).size(width, length).rot(rotation - 90);
}
}