diff --git a/core/src/com/riiablo/engine/client/CursorMovementSystem.java b/core/src/com/riiablo/engine/client/CursorMovementSystem.java index c9af4997..2049e4b2 100644 --- a/core/src/com/riiablo/engine/client/CursorMovementSystem.java +++ b/core/src/com/riiablo/engine/client/CursorMovementSystem.java @@ -6,12 +6,14 @@ import com.artemis.ComponentMapper; import com.artemis.EntitySubscription; import com.artemis.annotations.Wire; import com.artemis.utils.IntBag; + import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.utils.UIUtils; + import com.riiablo.Riiablo; import com.riiablo.camera.IsometricCamera; import com.riiablo.engine.Engine; @@ -23,6 +25,7 @@ import com.riiablo.engine.server.component.Target; import com.riiablo.item.Item; import com.riiablo.map.Map; import com.riiablo.map.RenderSystem; +import com.riiablo.profiler.ProfilerSystem; import com.riiablo.save.ItemController; public class CursorMovementSystem extends BaseSystem { @@ -36,6 +39,7 @@ public class CursorMovementSystem extends BaseSystem { protected Pathfinder pathfinder; protected MenuManager menuManager; protected DialogManager dialogManager; + protected ProfilerSystem profiler; @Wire(name = "iso") protected IsometricCamera iso; @@ -64,6 +68,7 @@ public class CursorMovementSystem extends BaseSystem { @Override protected void processSystem() { + if (profiler != null && profiler.hit()) return; stage.screenToStageCoordinates(tmpVec2.set(Gdx.input.getX(), Gdx.input.getY())); Actor hit1 = stage.hit(tmpVec2.x, tmpVec2.y, true); scaledStage.screenToStageCoordinates(tmpVec2.set(Gdx.input.getX(), Gdx.input.getY())); diff --git a/core/src/com/riiablo/engine/client/NetworkProfiler.java b/core/src/com/riiablo/engine/client/NetworkProfiler.java index de085d36..088fffef 100644 --- a/core/src/com/riiablo/engine/client/NetworkProfiler.java +++ b/core/src/com/riiablo/engine/client/NetworkProfiler.java @@ -3,6 +3,7 @@ package com.riiablo.engine.client; import com.riiablo.Riiablo; import com.riiablo.engine.IntervalBaseSystem; import com.riiablo.net.packet.d2gs.Ping; +import com.riiablo.profiler.ProfilerManager; import com.riiablo.profiler.SystemProfiler; public class NetworkProfiler extends IntervalBaseSystem implements Pinger.PacketListener { @@ -14,6 +15,7 @@ public class NetworkProfiler extends IntervalBaseSystem implements Pinger.Packet */ private static final float SAMPLES_DURATION = 30; + protected ProfilerManager profilers; protected Pinger pinger; protected SystemProfiler pingProfiler; @@ -30,10 +32,10 @@ public class NetworkProfiler extends IntervalBaseSystem implements Pinger.Packet protected void initialize() { pinger.addPacketListener(this); - pingProfiler = SystemProfiler.create("Ping"); + pingProfiler = profilers.create("Ping"); pingProfiler.setColor(0, 1, 1, 1); - rttProfiler = SystemProfiler.create("RTT"); + rttProfiler = profilers.create("RTT"); rttProfiler.setColor(1, 0, 1, 1); } diff --git a/core/src/com/riiablo/profiler/ProfilerInvocationStrategy.java b/core/src/com/riiablo/profiler/ProfilerInvocationStrategy.java index 932475fd..13d4e86d 100644 --- a/core/src/com/riiablo/profiler/ProfilerInvocationStrategy.java +++ b/core/src/com/riiablo/profiler/ProfilerInvocationStrategy.java @@ -19,6 +19,8 @@ import com.riiablo.Riiablo; * @author Daan van Yperen */ public class ProfilerInvocationStrategy extends SystemInvocationStrategy { + protected ProfilerManager profilerManager; + private boolean initialized = false; protected SystemProfiler frameProfiler; @@ -74,6 +76,7 @@ public class ProfilerInvocationStrategy extends SystemInvocationStrategy { @Override protected void initialize() { + world.inject(this); createFrameProfiler(); createCpuProfiler(); createGpuProfiler(); @@ -89,25 +92,25 @@ public class ProfilerInvocationStrategy extends SystemInvocationStrategy { } private SystemProfiler createSystemProfiler(BaseSystem system) { - SystemProfiler old = SystemProfiler.getFor(system); + SystemProfiler old = profilerManager.getFor(system); if (old == null) { - old = SystemProfiler.createFor(system, world); + old = profilerManager.createFor(system, world); } return old; } private void createFrameProfiler() { - frameProfiler = SystemProfiler.create("Frame"); + frameProfiler = profilerManager.create("Frame"); frameProfiler.setColor(1, 1, 1, 1); } private void createCpuProfiler() { - cpuProfiler = SystemProfiler.create("CPU"); + cpuProfiler = profilerManager.create("CPU"); cpuProfiler.setColor(0, 0, 1, 1); } private void createGpuProfiler() { - gpuProfiler = SystemProfiler.create("GPU"); + gpuProfiler = profilerManager.create("GPU"); gpuProfiler.setColor(0, 1, 0, 1); } } diff --git a/core/src/com/riiablo/profiler/ProfilerManager.java b/core/src/com/riiablo/profiler/ProfilerManager.java new file mode 100644 index 00000000..cbb7cfe7 --- /dev/null +++ b/core/src/com/riiablo/profiler/ProfilerManager.java @@ -0,0 +1,78 @@ +package com.riiablo.profiler; + +import com.artemis.BaseSystem; +import com.artemis.World; +import net.mostlyoriginal.api.system.core.PassiveSystem; + +import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.ObjectMap; + +public class ProfilerManager extends PassiveSystem { + private static Array profilers = new Array<>(SystemProfiler.class); + private static ObjectMap profilerByName = new ObjectMap<>(); + + private boolean RUNNING = true; + + @Override + protected void dispose() { + profilers.clear(); + profilerByName.clear(); + } + + public void pause() { + RUNNING = false; + } + + public void resume() { + RUNNING = true; + } + + public boolean isRunning() { + return RUNNING; + } + + Array get() { + return profilers; + } + + public int size() { + return profilers.size; + } + + public SystemProfiler add(SystemProfiler profiler) { + if (profiler.added) return profiler; + profiler.added = true; + profilers.add(profiler); + profilerByName.put(profiler.getName(), profiler); + return profiler; + } + + public SystemProfiler get(String name) { + return profilerByName.get(name, null); + } + + public SystemProfiler get(int index) { + return profilers.get(index); + } + + public SystemProfiler create(String name) { + SystemProfiler profiler = get(name); + if (profiler != null) return profiler; + return add(new SystemProfiler(name)); + } + + public SystemProfiler getFor(BaseSystem system) { + final SystemProfiler[] profilers = this.profilers.items; + for (int i = 0, s = this.profilers.size; i < s; i++) { + SystemProfiler profiler = profilers[i]; + if (profiler.system == system) { + return profiler; + } + } + return null; + } + + public SystemProfiler createFor(BaseSystem system, World world) { + return add(new SystemProfiler(system, world)); + } +} diff --git a/core/src/com/riiablo/profiler/ProfilerPlugin.java b/core/src/com/riiablo/profiler/ProfilerPlugin.java index f998edda..a0bb2bd6 100644 --- a/core/src/com/riiablo/profiler/ProfilerPlugin.java +++ b/core/src/com/riiablo/profiler/ProfilerPlugin.java @@ -17,10 +17,9 @@ import com.artemis.WorldConfigurationBuilder; * @author Daan van Yperen (Integration) */ public class ProfilerPlugin implements ArtemisPlugin { - @Override public void setup(WorldConfigurationBuilder b) { b.register(new ProfilerInvocationStrategy()); - b.dependsOn(WorldConfigurationBuilder.Priority.LOWEST + 1000, ProfilerSystem.class); + b.dependsOn(WorldConfigurationBuilder.Priority.LOWEST + 1000, ProfilerManager.class, ProfilerSystem.class); } } diff --git a/core/src/com/riiablo/profiler/ProfilerSystem.java b/core/src/com/riiablo/profiler/ProfilerSystem.java index 1db95a6f..92cd54db 100644 --- a/core/src/com/riiablo/profiler/ProfilerSystem.java +++ b/core/src/com/riiablo/profiler/ProfilerSystem.java @@ -8,8 +8,10 @@ import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Skin; +import com.badlogic.gdx.utils.Align; /** * Example profiling system. @@ -30,8 +32,17 @@ public class ProfilerSystem extends BaseSystem { private int key = DEFAULT_PROFILER_KEY; + ProfilerManager profilers; SystemProfilerGUI gui; private boolean f3ButtonDown; + private boolean initialized; + private final Vector2 tmpVec2 = new Vector2(); + + public boolean hit() { + if (!gui.isVisible()) return false; + stage.screenToStageCoordinates(tmpVec2.set(Gdx.input.getX(), Gdx.input.getY())); + return stage.hit(tmpVec2.x, tmpVec2.y, true) != null; + } @Override protected void initialize() { @@ -48,7 +59,8 @@ public class ProfilerSystem extends BaseSystem { gui = new SystemProfilerGUI(skin, "default"); gui.setResizeBorder(8); gui.show(stage); - gui.setWidth(Gdx.graphics.getWidth()); + world.inject(gui, true); + gui.initialize(); } @Override @@ -74,6 +86,10 @@ public class ProfilerSystem extends BaseSystem { stage.draw(); renderer.setProjectionMatrix(camera.combined); renderer.begin(ShapeRenderer.ShapeType.Line); + if (!initialized) { + gui.setWidth(stage.getWidth()); // initial width not applying properly otherwise + initialized = true; + } gui.updateAndRender(world.delta, renderer); renderer.end(); } @@ -82,14 +98,15 @@ public class ProfilerSystem extends BaseSystem { if (Gdx.input.isKeyPressed(key)) { if (!f3ButtonDown) { if (!gui.isVisible()) { - gui.setHeight(Gdx.graphics.getHeight() / 2); + gui.setHeight(stage.getHeight() / 2); + gui.setY(stage.getHeight(), Align.topLeft); gui.setVisible(true); - SystemProfiler.getFor(this).gpu = true; - } else if (gui.getHeight() != Gdx.graphics.getHeight()) { - gui.setHeight(Gdx.graphics.getHeight()); + profilers.getFor(this).gpu = true; + } else if (gui.getHeight() != stage.getHeight()) { + gui.setHeight(stage.getHeight()); } else { gui.setVisible(false); - SystemProfiler.getFor(this).gpu = false; + profilers.getFor(this).gpu = false; } } f3ButtonDown = true; @@ -115,11 +132,6 @@ public class ProfilerSystem extends BaseSystem { } } - @Override - protected void dispose() { - SystemProfiler.dispose(); - } - public int getKey() { return key; } diff --git a/core/src/com/riiablo/profiler/SystemProfiler.java b/core/src/com/riiablo/profiler/SystemProfiler.java index cf060e23..dacd1e0e 100644 --- a/core/src/com/riiablo/profiler/SystemProfiler.java +++ b/core/src/com/riiablo/profiler/SystemProfiler.java @@ -5,15 +5,12 @@ import com.artemis.World; import com.artemis.utils.ArtemisProfiler; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.utils.Array; -import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.TimeUtils; import com.riiablo.util.ClassUtils; /** - * {@link ArtemisProfiler} implementation, {@link SystemProfiler#dispose()} should be called to - * clean static references as needed + * {@link ArtemisProfiler} implementation * * @author piotr-j */ @@ -22,129 +19,9 @@ public class SystemProfiler implements ArtemisProfiler { * Samples to store per system, only changes before initialization have effect */ public static int SAMPLES = 60 * 5; - private static boolean RUNNING = true; + private boolean RUNNING = true; - private static Array profilers = new Array<>(SystemProfiler.class); - private static ObjectMap profilerByName = new ObjectMap<>(); - - /** - * Add manually created profiler - * - * @param profiler to add - * - * @return added profiler - */ - public static SystemProfiler add(SystemProfiler profiler) { - if (profiler.added) return profiler; - profiler.added = true; - profilers.add(profiler); - profilerByName.put(profiler.getName(), profiler); - return profiler; - } - - /** - * @param name of the profiler - * - * @return profiler registered with given name or null - */ - public static SystemProfiler get(String name) { - return profilerByName.get(name, null); - } - - /** - * @param index of profiler to get, no bounds check! - * - * @return profiler with given index - */ - public static SystemProfiler get(int index) { - return profilers.get(index); - } - - /** - * Get profiler for given system - * - * @return profiler or null - */ - public static SystemProfiler getFor(BaseSystem system) { - Object[] items = profilers.items; - for (int i = 0; i < profilers.size; i++) { - SystemProfiler profiler = (SystemProfiler) items[i]; - if (profiler.system == system) { - return profiler; - } - } - return null; - } - - /** - * Create a profiler with given name - * - * @param name of the profiler - */ - public static SystemProfiler create(String name) { - return SystemProfiler.add(new SystemProfiler(name)); - } - - /** - * Create a profiler for given system - * - * @param system to profiler - * @param world to init profiler with - * - * @return created profiler - */ - public static SystemProfiler createFor(BaseSystem system, World world) { - return SystemProfiler.add(new SystemProfiler(system, world)); - } - - /** - * @return {@link Array} with all registered profilers - */ - public static Array get() { - return profilers; - } - - /** - * @return number of registered profilers - */ - public static int size() { - return profilers.size; - } - - - /** - * Pause all profilers - */ - public static void pause() { - RUNNING = false; - } - - /** - * Resume all profilers - */ - public static void resume() { - RUNNING = true; - } - - /** - * @return if profilers are running - */ - public static boolean isRunning() { - return RUNNING; - } - - /** - * Clear registered profilers, should be called when {@link World} is disposed - */ - public static void dispose() { - profilers.clear(); - profilerByName.clear(); - } - - /* - If this profiler was already added via SystemProfiler.add() - */ - private boolean added; + boolean added; // If this profiler was already added via SystemProfiler.add() protected long[] times = new long[SAMPLES]; protected int index; @@ -162,7 +39,7 @@ public class SystemProfiler implements ArtemisProfiler { protected BaseSystem system; protected boolean gpu; - public SystemProfiler() {} + SystemProfiler() {} /** * Create not initialized profiler, must be initialized with {@link @@ -170,7 +47,7 @@ public class SystemProfiler implements ArtemisProfiler { * * @param name of the profiler */ - public SystemProfiler(String name) { + SystemProfiler(String name) { this.name = name; } @@ -180,7 +57,7 @@ public class SystemProfiler implements ArtemisProfiler { * @param system to profiler * @param world to init with */ - public SystemProfiler(BaseSystem system, World world) { + SystemProfiler(BaseSystem system, World world) { this(null, system, world); } @@ -191,7 +68,7 @@ public class SystemProfiler implements ArtemisProfiler { * @param system to profiler * @param world to init with */ - public SystemProfiler(String name, BaseSystem system, World world) { + SystemProfiler(String name, BaseSystem system, World world) { this.name = name; initialize(system, world); } @@ -206,7 +83,6 @@ public class SystemProfiler implements ArtemisProfiler { if (color == null) { calculateColor(toString().hashCode(), color = new Color()); } - SystemProfiler.add(this); } private long startTime; diff --git a/core/src/com/riiablo/profiler/SystemProfilerGUI.java b/core/src/com/riiablo/profiler/SystemProfilerGUI.java index c9d37673..26a4793b 100644 --- a/core/src/com/riiablo/profiler/SystemProfilerGUI.java +++ b/core/src/com/riiablo/profiler/SystemProfilerGUI.java @@ -67,6 +67,8 @@ public class SystemProfilerGUI extends Window { protected Table profilersTable; protected Array rows = new Array<>(); + protected ProfilerManager profilers; + public SystemProfilerGUI(Skin skin, String style) { super("Profiler", skin, style); this.skin = skin; @@ -82,7 +84,9 @@ public class SystemProfilerGUI extends Window { hide(); } }); + } + public void initialize() { Table graphTable = new Table(); Table graphLabels = new Table(); for (int i = 32; i >= 0; i /= 2) { @@ -99,7 +103,7 @@ public class SystemProfilerGUI extends Window { profilerLabels.add(label("lmax", skin, Align.right)).minWidth(MIN_LABEL_WIDTH); profilerLabels.add(label("avg", skin, Align.right)).minWidth(MIN_LABEL_WIDTH); - for (SystemProfiler profiler : SystemProfiler.get()) { + for (SystemProfiler profiler : profilers.get()) { rows.add(new ProfilerRow(profiler, skin)); } profilersTable = new Table(); @@ -151,7 +155,7 @@ public class SystemProfilerGUI extends Window { if (refreshTimer < REFRESH_RATE) return; refreshTimer -= REFRESH_RATE; - if (rows.size != SystemProfiler.size()) { + if (rows.size != profilers.size()) { rebuildRows(); } @@ -169,7 +173,7 @@ public class SystemProfilerGUI extends Window { } private void rebuildRows() { - int target = SystemProfiler.size(); + int target = profilers.size(); if (target > rows.size) { for (int i = rows.size; i < target; i++) { rows.add(new ProfilerRow(skin)); @@ -178,7 +182,7 @@ public class SystemProfilerGUI extends Window { rows.removeRange(rows.size - target + 1, rows.size - 1); } for (int i = 0; i < target; i++) { - SystemProfiler profiler = SystemProfiler.get(i); + SystemProfiler profiler = profilers.get(i); rows.get(i).init(profiler); } } @@ -201,7 +205,7 @@ public class SystemProfilerGUI extends Window { * * @param renderer {@link ShapeRenderer} to use, must be ready and set to Line type */ - public static void drawGraph(ShapeRenderer renderer, float x, float y, float width, float height, float alpha) { + public void drawGraph(ShapeRenderer renderer, float x, float y, float width, float height, float alpha) { Gdx.gl.glEnable(GL20.GL_BLEND); // we do this so the logical 0 and top are in the middle of the labels drawGraphAxis(renderer, x, y, width, height, alpha); @@ -211,7 +215,7 @@ public class SystemProfilerGUI extends Window { graphProfileTimes(renderer, x, y, width, height, alpha); } - private static void drawGraphAxis(ShapeRenderer renderer, float x, float y, float width, float height, float alpha) { + private void drawGraphAxis(ShapeRenderer renderer, float x, float y, float width, float height, float alpha) { float sep = height / 7; y += sep / 2; renderer.setColor(GRAPH_V_LINE.r, GRAPH_V_LINE.g, GRAPH_V_LINE.b, alpha); @@ -233,10 +237,10 @@ public class SystemProfilerGUI extends Window { } }; - private static void graphProfileTimes(ShapeRenderer renderer, float x, float y, float width, float height, float alpha) { - Sort.instance().sort(SystemProfiler.get(), byLocalMax); + private void graphProfileTimes(ShapeRenderer renderer, float x, float y, float width, float height, float alpha) { + Sort.instance().sort(profilers.get(), byLocalMax); int drawn = 0; - for (SystemProfiler profiler : SystemProfiler.get()) { + for (SystemProfiler profiler : profilers.get()) { if (!profiler.getDrawGraph()) continue; if (drawn++ > DRAW_MAX_COUNT)