[libgdx] Minor cleanup & optimizations, timing code in ClipTest

This commit is contained in:
badlogic 2017-03-27 19:05:35 +02:00
parent af3b5655fc
commit 6ae0c319ac
3 changed files with 19 additions and 11 deletions

View File

@ -37,29 +37,36 @@ import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.WindowedMean;
import com.esotericsoftware.spine.attachments.ClippingAttachment; import com.esotericsoftware.spine.attachments.ClippingAttachment;
import com.esotericsoftware.spine.utils.TwoColorPolygonBatch; import com.esotericsoftware.spine.utils.TwoColorPolygonBatch;
public class ClippingTest extends ApplicationAdapter { public class ClippingTest extends ApplicationAdapter {
OrthographicCamera camera; OrthographicCamera camera;
TwoColorPolygonBatch batch; PolygonSpriteBatch batch;
SkeletonRenderer renderer; SkeletonRenderer renderer;
SkeletonRendererDebug debugRenderer; SkeletonRendererDebug debugRenderer;
BitmapFont font;
TextureAtlas atlas; TextureAtlas atlas;
Skeleton skeleton; Skeleton skeleton;
AnimationState state; AnimationState state;
WindowedMean mean = new WindowedMean(30);
public void create () { public void create () {
camera = new OrthographicCamera(); camera = new OrthographicCamera();
batch = new TwoColorPolygonBatch(2048); batch = new PolygonSpriteBatch(2048);
renderer = new SkeletonRenderer(); renderer = new SkeletonRenderer();
renderer.setPremultipliedAlpha(true); renderer.setPremultipliedAlpha(true);
renderer.setSoftwareClipping(true); renderer.setSoftwareClipping(true);
debugRenderer = new SkeletonRendererDebug(); debugRenderer = new SkeletonRendererDebug();
debugRenderer.setBoundingBoxes(false); debugRenderer.setBoundingBoxes(false);
debugRenderer.setRegionAttachments(false); debugRenderer.setRegionAttachments(false);
font = new BitmapFont();
atlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy-pma.atlas")); atlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy-pma.atlas"));
SkeletonJson json = new SkeletonJson(atlas); SkeletonJson json = new SkeletonJson(atlas);
@ -122,7 +129,11 @@ public class ClippingTest extends ApplicationAdapter {
debugRenderer.getShapeRenderer().setProjectionMatrix(camera.combined); debugRenderer.getShapeRenderer().setProjectionMatrix(camera.combined);
batch.begin(); batch.begin();
long start = System.nanoTime();
renderer.draw(batch, skeleton); renderer.draw(batch, skeleton);
mean.addValue((System.nanoTime() - start) / 1000000.0f);
renderer.setPremultipliedAlpha(false);
font.draw(batch, "Time: " + mean.getMean() + "ms", 10, Gdx.graphics.getHeight() - font.getLineHeight());
batch.end(); batch.end();
debugRenderer.draw(skeleton); debugRenderer.draw(skeleton);

View File

@ -444,9 +444,11 @@ public class SkeletonRenderer {
float denom = 1 / (d0 * d2 + d1 * d3); float denom = 1 / (d0 * d2 + d1 * d3);
for (int j = 0; j < clipOutput.size; j += 2) { float[] clipVertices = clipOutput.items;
float x = clipOutput.get(j);
float y = clipOutput.get(j + 1); for (int j = 0, n = clipOutput.size; j < n; j += 2) {
float x = clipVertices[j];
float y = clipVertices[j + 1];
float a = (d0 * (x - x3) + d1 * (y - y3)) * denom; float a = (d0 * (x - x3) + d1 * (y - y3)) * denom;
float b = (d4 * (x - x3) + d2 * (y - y3)) * denom; float b = (d4 * (x - x3) + d2 * (y - y3)) * denom;
@ -462,7 +464,7 @@ public class SkeletonRenderer {
clippedVertices.add(v); clippedVertices.add(v);
} }
for (int j = 1; j < (clipOutput.size >> 1) - 1; j++) { for (int j = 1, n = (clipOutput.size >> 1) - 1; j < n; j++) {
clippedTriangles.add(idx); clippedTriangles.add(idx);
clippedTriangles.add(idx + j); clippedTriangles.add(idx + j);
clippedTriangles.add(idx + j + 1); clippedTriangles.add(idx + j + 1);

View File

@ -43,19 +43,14 @@ public class SutherlandHodgmanClipper {
edgeY2 = tmp; edgeY2 = tmp;
} }
// System.out.println("-- Edge " + i / 2 + ": (" + edgeX + ", " + edgeY + ")-(" + edgeX2 + ", " + edgeY2 + ")");
for (int j = 0; j < input.size; j += 2) { for (int j = 0; j < input.size; j += 2) {
float inputX = input.items[j % input.size]; float inputX = input.items[j % input.size];
float inputY = input.items[(j + 1) % input.size]; float inputY = input.items[(j + 1) % input.size];
float inputX2 = input.items[(j + 2) % input.size]; float inputX2 = input.items[(j + 2) % input.size];
float inputY2 = input.items[(j + 3) % input.size]; float inputY2 = input.items[(j + 3) % input.size];
// System.out.println("\tinput " + j / 2 + ": (" + inputX + ", " + inputY + ")-(" + inputX2 + ", " + inputY2 + ")");
int side = pointLineSide(edgeX2, edgeY2, edgeX, edgeY, inputX, inputY); int side = pointLineSide(edgeX2, edgeY2, edgeX, edgeY, inputX, inputY);
int side2 = pointLineSide(edgeX2, edgeY2, edgeX, edgeY, inputX2, inputY2); int side2 = pointLineSide(edgeX2, edgeY2, edgeX, edgeY, inputX2, inputY2);
// System.out.println("\tv1: " + (side < 0 ? "outside" : "inside" ) + ", v2: " + (side2 < 0 ? "outside" : "inside"));
// v1 inside, v2 inside // v1 inside, v2 inside
if (side >= 0 && side2 >= 0) { if (side >= 0 && side2 >= 0) {