Updated copyright.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / RenderingContext.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.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
24     public final BufferedImage bufferedImage;
25
26     public final Graphics2D graphics;
27
28     public final byte[] bytes;
29
30     public final int width;
31     public final int height;
32
33     public final int xCenter;
34     public final int yCenter;
35
36     public final double zoom;
37
38     public int frameNumber = 0;
39
40     /**
41      * Used to signal that actual rendering should be performed. It is useful to
42      * skip rendering when we only want to detect mouse clicks intersections
43      * without actually updating rendered frame.
44      */
45     public boolean doRender = true; // TODO: make use of the variable
46
47     public MouseClick mouseClick;
48
49     public MouseInteractionController clickedItem;
50
51     public RenderingContext(final int width, final int height,
52                             final RenderingContext oldContext) {
53
54         this.width = width;
55         this.height = height;
56
57         this.xCenter = width / 2;
58         this.yCenter = height / 2;
59
60         this.zoom = width / 3d;
61
62         bufferedImage = new BufferedImage(width, height, bufferedImageType);
63
64         final WritableRaster raster = bufferedImage.getRaster();
65         final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
66         bytes = dbi.getData();
67
68         graphics = (Graphics2D) bufferedImage.getGraphics();
69
70         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
71                 RenderingHints.VALUE_ANTIALIAS_ON);
72
73         graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
74                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
75
76         if (oldContext != null) {
77             mouseClick = oldContext.mouseClick;
78             clickedItem = oldContext.clickedItem;
79         }
80     }
81
82 }