Commits

Michael Ludwig committed b4bc767

Simplify Controller implementations to build iterators, etc. within process methods instead of caching them in member variables. It cleans code up and doesn't seem to be a performance hit.

Comments (0)

Files changed (5)

ferox-scene/src/main/java/com/ferox/scene/controller/CameraController.java

 import com.ferox.scene.Camera;
 import com.ferox.scene.Transform;
 import com.lhkbob.entreri.ComponentIterator;
-import com.lhkbob.entreri.EntitySystem;
 import com.lhkbob.entreri.SimpleController;
 
 /**
  * @author Michael Ludwig
  */
 public class CameraController extends SimpleController {
-    private Camera camera;
-    private Transform transform;
-    
-    private ComponentIterator it;
-    
     @Override
     public void process(double dt) {
-        it.reset();
+        Camera camera = getEntitySystem().createDataInstance(Camera.ID);
+        Transform transform = getEntitySystem().createDataInstance(Transform.ID);
+        
+        ComponentIterator it = new ComponentIterator(getEntitySystem())
+            .addRequired(camera)
+            .addOptional(transform);
+        
         while(it.next()) {
             double aspect = camera.getSurface().getHeight() / (double) camera.getSurface().getWidth();
             Frustum f = new Frustum(camera.getFieldOfView(), aspect, 
             getEntitySystem().getControllerManager().report(new FrustumResult(camera.getComponent(), f));
         }
     }
-    
-    @Override
-    public void init(EntitySystem system) {
-        super.init(system);
-        camera = system.createDataInstance(Camera.ID);
-        transform = system.createDataInstance(Transform.ID);
-        
-        it = new ComponentIterator(system).addRequired(camera)
-                                          .addOptional(transform);
-    }
-    
-    @Override
-    public void destroy() {
-        camera = null;
-        transform = null;
-        it = null;
-        super.destroy();
-    }
 }

ferox-scene/src/main/java/com/ferox/scene/controller/HierarchySpeedTest.java

         system.getControllerManager().addController(render);
 
         
-        int numRuns = 1000;
-        for (int i = 0; i < numRuns; i++) {
+        int numRuns = 5000;
+        for (int i = 0; i < 100; i++) {
             system.getControllerManager().process();
         }
         

ferox-scene/src/main/java/com/ferox/scene/controller/SpatialIndexController.java

 package com.ferox.scene.controller;
 
+import java.util.Iterator;
+
 import com.ferox.math.bounds.SpatialIndex;
 import com.ferox.scene.Renderable;
-import com.lhkbob.entreri.ComponentIterator;
 import com.lhkbob.entreri.Entity;
-import com.lhkbob.entreri.EntitySystem;
 import com.lhkbob.entreri.SimpleController;
 
 public class SpatialIndexController extends SimpleController {
     private SpatialIndex<Entity> index;
     
-    private Renderable renderable;
-    private ComponentIterator it;
-    
     // FIXME: add a setter, too
     public SpatialIndexController(SpatialIndex<Entity> index) {
         this.index = index;
     
     @Override
     public void process(double dt) {
-        it.reset();
-        while(it.next()) {
-            index.add(renderable.getEntity(), renderable.getWorldBounds());
+        Renderable r;
+        Iterator<Renderable> it = getEntitySystem().iterator(Renderable.ID);
+        while(it.hasNext()) {
+            r = it.next();
+            index.add(r.getEntity(), r.getWorldBounds());
         }
         
         // send the built index to everyone listened
     }
     
     @Override
-    public void init(EntitySystem system) {
-        super.init(system);
-        renderable = system.createDataInstance(Renderable.ID);
-        it = new ComponentIterator(system).addRequired(renderable);
-    }
-    
-    @Override
     public void destroy() {
-        renderable = null;
-        it = null;
         index.clear();
         super.destroy();
     }

ferox-scene/src/main/java/com/ferox/scene/controller/VisibilityController.java

 package com.ferox.scene.controller;
 
+import java.util.Iterator;
+
 import com.ferox.math.Const;
 import com.ferox.math.bounds.AxisAlignedBox;
 import com.ferox.math.bounds.QueryCallback;
 import com.ferox.math.bounds.SpatialIndex;
 import com.ferox.scene.Renderable;
 import com.ferox.util.Bag;
-import com.lhkbob.entreri.ComponentIterator;
 import com.lhkbob.entreri.Entity;
 import com.lhkbob.entreri.Result;
 import com.lhkbob.entreri.SimpleController;
         frustums = new Bag<FrustumResult>();
         
         // reset visibility
-        Renderable renderable = getEntitySystem().createDataInstance(Renderable.ID);
-        ComponentIterator it = new ComponentIterator(getEntitySystem());
-        it.addRequired(renderable);
-        
-        while(it.next()) {
-            renderable.resetVisibility();
+        Iterator<Renderable> it = getEntitySystem().iterator(Renderable.ID);
+        while(it.hasNext()) {
+            it.next().resetVisibility();
         }
     }
     

ferox-scene/src/main/java/com/ferox/scene/controller/WorldBoundsController.java

 import com.ferox.scene.Renderable;
 import com.ferox.scene.Transform;
 import com.lhkbob.entreri.ComponentIterator;
-import com.lhkbob.entreri.EntitySystem;
 import com.lhkbob.entreri.SimpleController;
 
 public class WorldBoundsController extends SimpleController {
-    private Renderable renderable;
-    private Transform transform;
-    
-    private ComponentIterator it;
-    
     @Override
     public void process(double dt) {
         AxisAlignedBox worldBounds = new AxisAlignedBox();
         
+        Renderable renderable = getEntitySystem().createDataInstance(Renderable.ID);
+        Transform transform = getEntitySystem().createDataInstance(Transform.ID);
+        ComponentIterator it = new ComponentIterator(getEntitySystem())
+            .addRequired(renderable)
+            .addRequired(transform);
+        
         it.reset();
         while(it.next()) {
             worldBounds.transform(renderable.getLocalBounds(), transform.getMatrix());
             renderable.setWorldBounds(worldBounds);
         }
     }
-    
-    @Override
-    public void init(EntitySystem system) {
-        super.init(system);
-        renderable = system.createDataInstance(Renderable.ID);
-        transform = system.createDataInstance(Transform.ID);
-        it = new ComponentIterator(system).addRequired(renderable)
-                                          .addRequired(transform);
-    }
-    
-    @Override
-    public void destroy() {
-        renderable = null;
-        transform = null;
-        it = null;
-        super.destroy();
-    }
 }