Code cleanup and formatting.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / RenderingContext.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.gui;
11
12 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseClick;
13 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
14
15 import java.awt.*;
16 import java.awt.image.BufferedImage;
17 import java.awt.image.DataBufferByte;
18 import java.awt.image.WritableRaster;
19
20 public class RenderingContext {
21
22     public static final int bufferedImageType = BufferedImage.TYPE_4BYTE_ABGR;
23     public final Graphics2D graphics;
24     public final byte[] pixels;
25     public final int width;
26     public final int height;
27     public final int xCenter;
28     public final int yCenter;
29     public final double zoom;
30     final BufferedImage bufferedImage;
31     public int frameNumber = 0;
32
33     /**
34      * Used to signal that actual rendering should be performed. It is useful to
35      * skip rendering when we only want to detect mouse clicks intersections
36      * without actually updating rendered frame.
37      */
38     public boolean doRender = true; // TODO: make use of the variable
39
40     /**
41      * Mouse click. During rendering we can detect which item user clicked on.
42      */
43     public MouseClick mouseClick;
44
45     /**
46      * Item that user clicked on.
47      */
48     public MouseInteractionController clickedItem;
49
50     public RenderingContext(final int width, final int height) {
51         this.width = width;
52         this.height = height;
53
54         this.xCenter = width / 2;
55         this.yCenter = height / 2;
56
57         this.zoom = width / 3d;
58
59         bufferedImage = new BufferedImage(width, height, bufferedImageType);
60
61         final WritableRaster raster = bufferedImage.getRaster();
62         final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
63         pixels = dbi.getData();
64
65         graphics = (Graphics2D) bufferedImage.getGraphics();
66         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
67         graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
68     }
69
70 }