From ecb4f3ef928af01f8a6cf91e5749d9efa0d13c27 Mon Sep 17 00:00:00 2001 From: ediazal Date: Tue, 19 Apr 2016 16:09:13 +0200 Subject: [PATCH] SmothCamWorld.points to ArrayList --- .../smoothcam/SmoothCamDebugRenderer.java | 13 +++++---- .../smoothcam/SmoothCamTest.java | 4 +-- .../smoothcam/SmoothCamWorld.java | 27 ++++++++----------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/com/semperhilaris/smoothcam/SmoothCamDebugRenderer.java b/src/com/semperhilaris/smoothcam/SmoothCamDebugRenderer.java index c8f199f..c191fef 100644 --- a/src/com/semperhilaris/smoothcam/SmoothCamDebugRenderer.java +++ b/src/com/semperhilaris/smoothcam/SmoothCamDebugRenderer.java @@ -1,6 +1,8 @@ package com.semperhilaris.smoothcam; +import java.util.ArrayList; + import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.Matrix4; @@ -19,18 +21,19 @@ public SmoothCamDebugRenderer () { * @param world * @param projMatrix */ public void render (SmoothCamWorld world, Matrix4 projMatrix) { - SmoothCamPoint[] points = world.getPoints(); + ArrayList points = world.getPoints(); renderer.setProjectionMatrix(projMatrix); renderer.begin(ShapeType.Line); - for (int i = 0; i < points.length; i++) { + for (int i = 0, size = points.size(); i < size; i++) { + SmoothCamPoint point = points.get(i); renderer.setColor(0, 1, 0, 1); - renderer.circle(points[i].getX(), points[i].getY(), points[i].getInnerRadius()); - if (points[i].getPolarity() == SmoothCamPoint.REPULSE) { + renderer.circle(point.getX(), point.getY(), point.getInnerRadius()); + if (point.getPolarity() == SmoothCamPoint.REPULSE) { renderer.setColor(1, 0, 0, 1); } else { renderer.setColor(0, 0, 1, 1); } - renderer.circle(points[i].getX(), points[i].getY(), points[i].getOuterRadius()); + renderer.circle(point.getX(), point.getY(), point.getOuterRadius()); } renderer.setColor(0, 0, 0, 1); SmoothCamSubject subject = world.getSubject(); diff --git a/src/com/semperhilaris/smoothcam/SmoothCamTest.java b/src/com/semperhilaris/smoothcam/SmoothCamTest.java index cf43e33..a8461bb 100644 --- a/src/com/semperhilaris/smoothcam/SmoothCamTest.java +++ b/src/com/semperhilaris/smoothcam/SmoothCamTest.java @@ -12,7 +12,7 @@ import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.Input.Peripheral; import com.badlogic.gdx.InputAdapter; -import com.badlogic.gdx.graphics.GL10; +import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; @@ -273,7 +273,7 @@ public void render () { camera.update(); Gdx.gl.glClearColor(1, 1, 1, 1); - Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); debugRenderer.render(world, camera.combined); diff --git a/src/com/semperhilaris/smoothcam/SmoothCamWorld.java b/src/com/semperhilaris/smoothcam/SmoothCamWorld.java index 36fcaf7..a5d6569 100644 --- a/src/com/semperhilaris/smoothcam/SmoothCamWorld.java +++ b/src/com/semperhilaris/smoothcam/SmoothCamWorld.java @@ -2,14 +2,13 @@ package com.semperhilaris.smoothcam; import java.util.ArrayList; -import java.util.Arrays; /** Provides smooth camera movement between one {@link SmoothCamSubject} and a set of {@link SmoothCamPoint} * @author David Froehlich */ public class SmoothCamWorld { private SmoothCamSubject subject; private SmoothCamBoundingBox boundingBox = new SmoothCamBoundingBox(0, 0); - private SmoothCamPoint[] points = {}; + private ArrayList points = new ArrayList(15); private float x = 0f; private float y = 0f; private float zoom = 1f; @@ -28,21 +27,16 @@ public SmoothCamWorld (SmoothCamSubject subject) { /** Adds a {@link SmoothCamPoint} to the world * @param point */ public void addPoint (SmoothCamPoint point) { - SmoothCamPoint[] newPoints = new SmoothCamPoint[points.length + 1]; - newPoints[0] = point; - System.arraycopy(points, 0, newPoints, 1, points.length); - points = newPoints; + points.add(point); } /** Removes a {@link SmoothCamPoint} from the world * @param point */ public void removePoint (SmoothCamPoint point) { - ArrayList temp = new ArrayList(Arrays.asList(points)); - temp.remove(point); - points = temp.toArray(new SmoothCamPoint[temp.size()]); + points.remove(point); } - public SmoothCamPoint[] getPoints () { + public ArrayList getPoints () { return points; } @@ -127,12 +121,13 @@ public SmoothCamPoint getNearestPoint (SmoothCamPoint p1) { double distance = 0; double currentBest = 0; SmoothCamPoint nearestPoint = new SmoothCamPoint(); - for (int i = 0; i < points.length; i++) { - distance = getDistance(p1, points[i]); - if (distance <= points[i].getOuterRadius()) { - return points[i]; + for (int i = 0, size = points.size(); i < size; i++) { + SmoothCamPoint point = points.get(i); + distance = getDistance(p1, point); + if (distance <= point.getOuterRadius()) { + return point; } else if (distance < currentBest || currentBest == 0) { - nearestPoint = points[i]; + nearestPoint = point; currentBest = distance; } } @@ -145,7 +140,7 @@ public SmoothCamPoint getNearestPoint (SmoothCamPoint p1) { * camera focus switches completely to the point of interest. */ public void update () { float coeff = 0f; - if (points.length > 0) { + if (points.size() > 0) { SmoothCamPoint nearestPoint = getNearestPoint(this.subject); double distance = getDistance(subject, nearestPoint); if (distance > nearestPoint.getOuterRadius()) {