Skip to content

Commit

Permalink
updated test to include filled shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
earlygrey committed Sep 26, 2019
1 parent 414d18f commit 698c93d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 32 deletions.
109 changes: 77 additions & 32 deletions test/src/space/earlygrey/shapedrawer/test/ShapeDrawerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.EarClippingTriangulator;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
Expand All @@ -31,12 +33,14 @@
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ShortArray;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;

import space.earlygrey.shapedrawer.JoinType;
import space.earlygrey.shapedrawer.ShapeDrawer;
import space.earlygrey.shapedrawer.ShapeUtils;
import space.earlygrey.shapedrawer.test.TestUtils.RollingAverage;

public class ShapeDrawerTest extends ApplicationAdapter {

Expand All @@ -45,12 +49,14 @@ public class ShapeDrawerTest extends ApplicationAdapter {
*/

ShapeDrawer drawer;
SpriteBatch batch;
Batch batch;
Texture texture;
Stage stage;
Skin skin;

float[] vertices = new float[16];
ShortArray triangles = new ShortArray();
static final EarClippingTriangulator triangulator = new EarClippingTriangulator();
Array<Vector2> path = new Array<Vector2>();

ShapeRenderer sr;
Expand All @@ -61,6 +67,7 @@ public class ShapeDrawerTest extends ApplicationAdapter {

int sides = 8;
boolean touched = false, closedPath = false;
boolean filled = false;

Table preview, srPreview;

Expand All @@ -71,11 +78,19 @@ enum ShapeMode {
static float runTime;
Vector2 v = new Vector2(), anchor = new Vector2(), v2 = new Vector2();

RollingAverage average = new RollingAverage(10);

boolean usePoly = true;

@Override
public void create () {

if (usePoly) {
batch = new PolygonSpriteBatch(32767);
} else {
batch = new SpriteBatch(8191);
}

batch = new SpriteBatch();
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);

//create single white pixel
Expand Down Expand Up @@ -105,6 +120,7 @@ public void create () {
final TextButton clearPathButton = new TextButton("Clear", skin);
final CheckBox dragPathCheckbox = new CheckBox("drag path", skin);
final CheckBox closedPathCheckbox = new CheckBox("closed", skin);
final CheckBox filledCheckbox = new CheckBox("filled", skin);

Table root = new Table();
root.setFillParent(true);
Expand Down Expand Up @@ -134,6 +150,7 @@ public void changed(ChangeEvent event, Actor actor) {
sidesSelectorTable.setVisible(false);
clearPathButton.setVisible(false);
closedPathCheckbox.setVisible(false);
filledCheckbox.setVisible(false);
instructionLabel.setText("drag to move endpoint");
dragPathCheckbox.setVisible(false);
drawerMethodLabel.setText("ShapeDrawer#line()");
Expand All @@ -145,6 +162,7 @@ public void changed(ChangeEvent event, Actor actor) {
sidesSelectorTable.setVisible(false);
clearPathButton.setVisible(true);
closedPathCheckbox.setVisible(true);
filledCheckbox.setVisible(false);
instructionLabel.setText("click to set a waypoint");
dragPathCheckbox.setVisible(true);
drawerMethodLabel.setText("ShapeDrawer#path()");
Expand All @@ -157,6 +175,7 @@ public void changed(ChangeEvent event, Actor actor) {
sidesSelectorTable.setVisible(true);
clearPathButton.setVisible(false);
closedPathCheckbox.setVisible(false);
filledCheckbox.setVisible(true);
instructionLabel.setText("drag to adjust\nsize and rotation");
dragPathCheckbox.setVisible(false);
drawerMethodLabel.setText("ShapeDrawer#polygon()");
Expand All @@ -168,6 +187,7 @@ public void changed(ChangeEvent event, Actor actor) {
sidesSelectorTable.setVisible(false);
clearPathButton.setVisible(false);
closedPathCheckbox.setVisible(false);
filledCheckbox.setVisible(true);
instructionLabel.setText("drag to adjust\nsize and rotation");
dragPathCheckbox.setVisible(false);
drawerMethodLabel.setText("ShapeDrawer#ellipse()");
Expand All @@ -178,6 +198,7 @@ public void changed(ChangeEvent event, Actor actor) {
sidesSelectorTable.setVisible(false);
clearPathButton.setVisible(false);
closedPathCheckbox.setVisible(false);
filledCheckbox.setVisible(true);
instructionLabel.setText("drag to adjust arc");
dragPathCheckbox.setVisible(false);
drawerMethodLabel.setText("ShapeDrawer#arc()");
Expand All @@ -189,6 +210,7 @@ public void changed(ChangeEvent event, Actor actor) {
sidesSelectorTable.setVisible(false);
clearPathButton.setVisible(false);
closedPathCheckbox.setVisible(false);
filledCheckbox.setVisible(true);
instructionLabel.setText("drag to adjust size");
dragPathCheckbox.setVisible(false);
drawerMethodLabel.setText("ShapeDrawer#rectangle()");
Expand Down Expand Up @@ -273,13 +295,14 @@ public void draw(Batch batch, float parentAlpha) {

controlsTable.add(clearPathButton).padTop(10).space(30);
controlsTable.add(closedPathCheckbox).padTop(10).space(30);
controlsTable.add(filledCheckbox).padTop(10).space(30);
controlsTable.add(shapeSelectorTable).padTop(10).space(30);
controlsTable.add(joinSelectorTable).padTop(10).space(30);
controlsTable.add(dragPathCheckbox).padTop(10).space(30);
controlsTable.row();
controlsTable.add(widthSelectorTable).padTop(10).colspan(5);
controlsTable.add(widthSelectorTable).padTop(10).colspan(6);
controlsTable.row();
controlsTable.add(sidesSelectorTable).padTop(10).colspan(5);
controlsTable.add(sidesSelectorTable).padTop(10).colspan(6);


root.add(previewTable).pad(2).grow().top().padBottom(10);
Expand Down Expand Up @@ -342,6 +365,12 @@ public void clicked(InputEvent event, float x, float y) {
closedPath = closedPathCheckbox.isChecked();
}
});
filledCheckbox.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
filled = !filled;
}
});

Gdx.input.setInputProcessor(stage);

Expand All @@ -352,6 +381,7 @@ public void clicked(InputEvent event, float x, float y) {
joinSelectorTable.setVisible(false);
sidesSelectorTable.setVisible(false);
clearPathButton.setVisible(false);
filledCheckbox.setVisible(false);
dragPathCheckbox.setVisible(false);
closedPathCheckbox.setVisible(false);
preview.localToStageCoordinates(v.set(preview.getWidth(), preview.getHeight()).scl(0.5f));
Expand All @@ -376,6 +406,8 @@ void refreshPolygonVertices() {
vertices[i+1] = x*sin+y*cos + srY;
v2.rotateRad(angleInterval);
}

triangles = triangulator.computeTriangles(vertices);
}

@Override
Expand All @@ -384,10 +416,6 @@ public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
/*if (true) {
test();
return;
}*/

runTime += Gdx.graphics.getDeltaTime();

Expand All @@ -401,7 +429,6 @@ public void render () {
rotation = ShapeUtils.normaliseAngleToPositive(rotation);
float scale = anchor.dst(x, y);


switch(shapeMode) {
case LINE: default:
sr.begin(ShapeType.Filled);
Expand All @@ -427,16 +454,16 @@ public void render () {
sr.polygon(vertices);
break;
case ELLIPSE:
sr.begin(ShapeType.Line);
sr.begin(filled?ShapeType.Filled:ShapeType.Line);
float w = scale, h = 200;
sr.ellipse(srX-w, srY-h, 2*w, 2*h, rotation*MathUtils.radiansToDegrees);
break;
case ARC:
sr.begin(ShapeType.Line);
sr.begin(filled?ShapeType.Filled:ShapeType.Line);
sr.arc(srX, srY, 200, 0, rotation*MathUtils.radiansToDegrees);
break;
case RECTANGLE:
sr.begin(ShapeType.Line);
sr.begin(filled?ShapeType.Filled:ShapeType.Line);
w = Math.abs(anchor.x - x); h = Math.abs(anchor.y - y);
sr.rect(srX-w, srY-h, 2*w, 2*h);

Expand All @@ -447,55 +474,72 @@ public void render () {

batch.begin();

long time = TimeUtils.nanoTime();
switch(shapeMode) {
case LINE: default:
drawer.line(x, y,anchor.x, anchor.y);
drawer.line(x, y,anchor.x, anchor.y);
break;
case PATH:
drawer.path(path, joinType, !closedPath);
break;
case POLYGON:
drawer.polygon(x, y, sides, scale, 200, rotation, joinType);
if (filled) {
drawer.filledPolygon(x, y, sides, scale, 200, rotation);
//drawer.filledPolygon(vertices, triangles);
} else {
drawer.polygon(x, y, sides, scale, 200, rotation, joinType);
}
break;
case ELLIPSE:
drawer.ellipse(x, y, scale, 200, rotation);
if (filled) {
drawer.filledEllipse(x, y, scale, 200, rotation);
} else {
drawer.ellipse(x, y, scale, 200, rotation);
}
break;
case ARC:
drawer.arc(x, y, 200, 0f, rotation);
if (filled) {
drawer.sector(x, y, 200, 0, rotation);
} else {
drawer.arc(x, y, 200, 0f, rotation);
}
break;
case RECTANGLE:
int w = 2 * (int) Math.abs(anchor.x - x), h = 2 * (int) Math.abs(anchor.y - y);
drawer.rectangle(x-0.5f*w, y-0.5f*h, w, h);
if (filled) {
drawer.filledRectangle(x-0.5f*w, y-0.5f*h, w, h);
} else {
drawer.rectangle(x-0.5f*w, y-0.5f*h, w, h);
}

break;
}

average.add(TimeUtils.nanosToMillis(TimeUtils.timeSinceNanos(time)));

batch.end();

int calls = 0;
if (batch instanceof PolygonSpriteBatch) {
calls = ((PolygonSpriteBatch)batch).renderCalls;
} else if (batch instanceof SpriteBatch) {
calls = ((SpriteBatch)batch).renderCalls;
}

stage.act();
stage.draw();

batch.begin();

debugFont.draw(batch, Gdx.graphics.getFramesPerSecond()+"fps", stage.getWidth()-80, 20);
debugFont.draw(batch, Gdx.graphics.getFramesPerSecond()+"fps", stage.getWidth()-100, 20);
debugFont.draw(batch, "draw: "+average+"ms", stage.getWidth()-100, 40);
debugFont.draw(batch, "calls: "+calls, stage.getWidth()-100, 60);

batch.end();
}

final boolean useDrawer = false;
private void test() {

if (useDrawer) batch.begin();
else sr.begin(ShapeType.Line);
long time = TimeUtils.nanoTime();
for (int i = 0; i < 10000; i++) {
if (useDrawer) drawer.circle(100,100,50, JoinType.NONE);
else sr.circle(100,100,50);
}
Gdx.app.log("TEST", TimeUtils.nanosToMillis(TimeUtils.timeSinceNanos(time))+"ms");
if (useDrawer) batch.end();
else sr.end();

}


Vector2 setToMouse(Vector2 vec) {
return vec.set(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());
Expand All @@ -507,6 +551,7 @@ public void resize(int width, int height) {
stage.getViewport().update(width, height);
}


@Override
public void dispose () {
batch.dispose();
Expand Down
37 changes: 37 additions & 0 deletions test/src/space/earlygrey/shapedrawer/test/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package space.earlygrey.shapedrawer.test;

import java.text.DecimalFormat;

public class TestUtils {

public static class RollingAverage {

private int size;
private double total = 0d;
private int index = 0;
private double samples[];
private static final DecimalFormat format = new DecimalFormat("##.##");

public RollingAverage(int size) {
this.size = size;
samples = new double[size];
for (int i = 0; i < size; i++) samples[i] = 0d;
}

public void add(double x) {
total -= samples[index];
samples[index] = x;
total += x;
if (++index == size) index = 0; // cheaper than modulus
}

public double getAverage() {
return total / size;
}

@Override
public String toString() {
return format.format(getAverage());
}
}
}

0 comments on commit 698c93d

Please sign in to comment.