From 0c7f6432d8da4d747fbf13c61a694763585b91cc Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 20 Jun 2018 12:15:56 -0400 Subject: [PATCH] Fixed glitchy minimap unit rendering --- core/src/io/anuke/mindustry/core/Control.java | 7 +++ core/src/io/anuke/mindustry/core/Logic.java | 5 +++ .../mindustry/graphics/MinimapRenderer.java | 45 +++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/Control.java b/core/src/io/anuke/mindustry/core/Control.java index 6dd98745f4..3a8f0bbdf4 100644 --- a/core/src/io/anuke/mindustry/core/Control.java +++ b/core/src/io/anuke/mindustry/core/Control.java @@ -320,6 +320,13 @@ public class Control extends Module{ Platform.instance.updateRPC(); } + /**Called from main logic thread.*/ + public void runUpdateLogic(){ + if(!state.is(State.menu)) { + renderer.minimap().updateUnitArray(); + } + } + @Override public void update(){ diff --git a/core/src/io/anuke/mindustry/core/Logic.java b/core/src/io/anuke/mindustry/core/Logic.java index e36dca5715..2a5baedcf7 100644 --- a/core/src/io/anuke/mindustry/core/Logic.java +++ b/core/src/io/anuke/mindustry/core/Logic.java @@ -1,5 +1,6 @@ package io.anuke.mindustry.core; +import io.anuke.mindustry.Vars; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.entities.TileEntity; import io.anuke.mindustry.game.EventType.GameOverEvent; @@ -104,6 +105,10 @@ public class Logic extends Module { public void update(){ if(threads.isEnabled() && !threads.isOnThread()) return; + if(Vars.control != null){ + control.runUpdateLogic(); + } + if(!state.is(State.menu)){ if(control != null) control.triggerUpdateInput(); diff --git a/core/src/io/anuke/mindustry/graphics/MinimapRenderer.java b/core/src/io/anuke/mindustry/graphics/MinimapRenderer.java index 2c9d0a9377..d438783577 100644 --- a/core/src/io/anuke/mindustry/graphics/MinimapRenderer.java +++ b/core/src/io/anuke/mindustry/graphics/MinimapRenderer.java @@ -6,7 +6,9 @@ import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Disposable; +import io.anuke.mindustry.entities.Unit; import io.anuke.mindustry.entities.Units; import io.anuke.mindustry.game.EventType.TileChangeEvent; import io.anuke.mindustry.game.EventType.WorldLoadGraphicsEvent; @@ -14,19 +16,24 @@ import io.anuke.mindustry.world.ColorMapper; import io.anuke.mindustry.world.Tile; import io.anuke.ucore.core.Core; import io.anuke.ucore.core.Events; +import io.anuke.ucore.core.Graphics; import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.graphics.Pixmaps; +import io.anuke.ucore.scene.utils.ScissorStack; import io.anuke.ucore.util.Mathf; +import io.anuke.ucore.util.ThreadArray; import static io.anuke.mindustry.Vars.tilesize; import static io.anuke.mindustry.Vars.world; public class MinimapRenderer implements Disposable{ private static final int baseSize = 16; + private final Array units = new ThreadArray<>(); private Pixmap pixmap; private Texture texture; private TextureRegion region; private Rectangle rect = new Rectangle(); + private Rectangle clipRect = new Rectangle(); private int zoom = 4; public MinimapRenderer(){ @@ -65,15 +72,23 @@ public class MinimapRenderer implements Disposable{ dx = Mathf.clamp(dx, sz, world.width()-sz); dy = Mathf.clamp(dy, sz, world.height()-sz); - rect.set((dx - sz) * tilesize, (dy - sz) * tilesize, sz * 2 * tilesize, sz * 2 * tilesize); - Units.getNearby(rect, unit -> { - if(!rect.contains(unit.x, unit.y)) return; + synchronized (units){ + rect.set((dx - sz) * tilesize, (dy - sz) * tilesize, sz * 2 * tilesize, sz * 2 * tilesize); + Graphics.flush(); - float rx = (unit.x - rect.x) / rect.width * w, ry = (unit.y - rect.y)/ rect.width * h; - Draw.color(unit.getTeam().color); - Draw.rect("white", x + rx, y + ry, w/(sz*2), h/(sz*2)); - }); - Draw.color(); + boolean clip = ScissorStack.pushScissors(clipRect.set(x, y, w, h)); + + for(Unit unit : units){ + float rx = (unit.x - rect.x) / rect.width * w, ry = (unit.y - rect.y) / rect.width * h; + Draw.color(unit.getTeam().color); + Draw.rect("white", x + rx, y + ry, w / (sz * 2), h / (sz * 2)); + } + + Draw.color(); + + Graphics.flush(); + if(clip) ScissorStack.popScissors(); + } } public TextureRegion getRegion() { @@ -106,6 +121,20 @@ public class MinimapRenderer implements Disposable{ Pixmaps.drawPixel(texture, tile.x, pixmap.getHeight() - 1 - tile.y, color); } + public void updateUnitArray(){ + int sz = baseSize * zoom; + float dx = (Core.camera.position.x / tilesize); + float dy = (Core.camera.position.y / tilesize); + dx = Mathf.clamp(dx, sz, world.width()-sz); + dy = Mathf.clamp(dy, sz, world.height()-sz); + + synchronized (units) { + rect.set((dx - sz) * tilesize, (dy - sz) * tilesize, sz * 2 * tilesize, sz * 2 * tilesize); + units.clear(); + Units.getNearby(rect, units::add); + } + } + private int colorFor(Tile tile){ int color = tile.breakable() ? tile.target().getTeam().intColor : ColorMapper.getColor(tile.block()); if(color == 0) color = ColorMapper.getColor(tile.floor());