048bb53b67da5557ec37852aac9243563baff71f
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / RenderAggregator.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2016, 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.Collections;
18 import java.util.Comparator;
19
20 public class RenderAggregator {
21
22     ArrayList<AbstractCoordinateShape> shapes = new ArrayList<>();
23     ShapesComparator comparator = new ShapesComparator();
24
25     public void paint(final RenderingContext renderBuffer) {
26
27         Collections.sort(shapes, comparator);
28
29         for (final AbstractCoordinateShape shape : shapes)
30             shape.paint(renderBuffer);
31
32     }
33
34     public void queueShapeForRendering(final AbstractCoordinateShape shape) {
35         shapes.add(shape);
36     }
37
38     public void reset() {
39         shapes.clear();
40     }
41
42     static class ShapesComparator implements Comparator<AbstractCoordinateShape>, Serializable {
43
44         @Override
45         public int compare(final AbstractCoordinateShape o1, final AbstractCoordinateShape o2) {
46             if (o1.getZ() < o2.getZ())
47                 return 1;
48             else if (o1.getZ() > o2.getZ())
49                 return -1;
50
51             return Integer.compare(o1.shapeId, o2.shapeId);
52         }
53     }
54 }