static void CollectRenderables() { renderables.Clear(); renderablePositions.Clear(); Box cameraWorldBox = new Box(camera.position, cameraHalfSize); for (int i = 0; i < Systems.renderables.renderables.count; i++) { // @NOTE: assuming everything renderable has a position, so why not just group them together dummy // @TODO: need to resolve two things in the same spot if (cameraWorldBox.PointInBox(Systems.positions.positions[i])) { renderables.PushBack(Systems.renderables.renderables[i]); renderablePositions.PushBack(Systems.positions.positions[i]); } } // @PERF: this is VERY BAD. If we're rendering 100 entities we do this potentialy 100^2 times!!! // What are some ways we could make it better? // @TODO: sort the renderables! Vector2 currPosition = new Vector2(cameraWorldBox.min.x, cameraWorldBox.min.y); int r = 0; while (r < renderableEntities) { bool foundRenderableAtPosition = false; for (int i = 0; i < renderables.count; i++) { // @PERF: because multiple things could be at the same position we have to check EVERYTHING // to make sure we pick the thing with the topmost layer, which is very unfortunate if (currPosition.Equals(renderablePositions[i])) { if (foundRenderableAtPosition) { if (renderablesSorted[r].layer < renderables[i].layer) { renderablesSorted[r] = renderables[i]; renderablePositionsSorted[r] = renderablePositions[i]; } } else { renderablePositionsSorted[r] = renderablePositions[i]; renderablesSorted[r] = renderables[i]; foundRenderableAtPosition = true; } } } if (foundRenderableAtPosition) { r++; currPosition.x++; if (currPosition.x > cameraWorldBox.max.x) { currPosition.x = cameraWorldBox.min.x; currPosition.y++; } } } }