33843510569e9c1fbdab5b05116d58f28c96b3f0
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / RenderAggregator.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.renderer.raster;
11
12 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
14
15 import java.io.Serializable;
16 import java.util.ArrayList;
17 import java.util.Comparator;
18
19 public class RenderAggregator {
20
21     private ArrayList<AbstractCoordinateShape> shapes = new ArrayList<>();
22     private ShapesZIndexComparator comparator = new ShapesZIndexComparator();
23
24     public void paint(final RenderingContext renderBuffer) {
25         shapes.sort(comparator);
26         shapes.forEach(shape -> shape.paint(renderBuffer));
27     }
28
29     public void queueShapeForRendering(final AbstractCoordinateShape shape) {
30         shapes.add(shape);
31     }
32
33     public void reset() {
34         shapes.clear();
35     }
36
37     static class ShapesZIndexComparator implements Comparator<AbstractCoordinateShape>, Serializable {
38
39         @Override
40         public int compare(final AbstractCoordinateShape o1, final AbstractCoordinateShape o2) {
41             if (o1.getZ() < o2.getZ())
42                 return 1;
43             else if (o1.getZ() > o2.getZ())
44                 return -1;
45
46             return Integer.compare(o1.shapeId, o2.shapeId);
47         }
48     }
49 }