-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderHandler.java
182 lines (162 loc) · 6.08 KB
/
RenderHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
/**
* handles renders made throughout the entire FinalGame
*/
public class RenderHandler{
private static BufferedImage view;
private Rectangle camera;
private int[] pixels;
private int maxScreenWidth, maxScreenHeight;
public static GraphicsDevice[] graphicsDevices = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
/**
* defult Constructor
* @param width size
* @param height size
*/
public RenderHandler(int width, int height){
// gets the window size
for(int i = 0; i < graphicsDevices.length; i++) {
if(maxScreenWidth < graphicsDevices[i].getDisplayMode().getWidth()) maxScreenWidth = graphicsDevices[i].getDisplayMode().getWidth();
if(maxScreenHeight < graphicsDevices[i].getDisplayMode().getHeight()) maxScreenHeight = graphicsDevices[i].getDisplayMode().getHeight();
}
//Create a BufferedImage that will represent our view.
view = new BufferedImage(maxScreenWidth, maxScreenHeight, BufferedImage.TYPE_INT_RGB);
camera = new Rectangle(0, 0, width, height);
//Create an array for pixels
pixels = ((DataBufferInt) view.getRaster().getDataBuffer()).getData();
}
/**
* renders the graphics of a the camera
* @param graphics Graphics used to draw
*/
public void render(Graphics graphics){
graphics.drawImage(view.getSubimage(0, 0, camera.w, camera.h), 0, 0, camera.w, camera.h, null);
}
/**
* renders an Image
* @param image to be rendered
* @param xPosition pos on screen
* @param yPosition pos on Screen
* @param xZoom zoom ratio
* @param yZoom zoom ratio
* @param fixed if it moves with the camera or not
*/
public void renderImage(BufferedImage image, int xPosition, int yPosition, int xZoom, int yZoom, boolean fixed){
int[] imagePixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
renderArray(imagePixels, image.getWidth(), image.getHeight(), xPosition, yPosition, xZoom, yZoom, fixed);
}
/**
* prints strings onto the Screen
* @param graphics used to Draw
* @param string string to draw on screen
* @param posX x
* @param posY y
* @param size size of String
*/
public void renderString(Graphics graphics, String string, int posX, int posY, int size){
graphics.setFont(new Font("TimesRoman", Font.BOLD, size));
graphics.drawString(string, posX, posY);
}
/**
* renders a sprite
* @param sprite sprite to draw
* @param xPosition pos on screen
* @param yPosition pos on Screen
* @param xZoom zoom ratio
* @param yZoom zoom ratio
* @param fixed if it moves with the camera or not
*/
public void renderSprite(Sprite sprite, int xPosition, int yPosition, int xZoom, int yZoom, boolean fixed) {
renderArray(sprite.getPixels(), sprite.getWidth(), sprite.getHeight(), xPosition, yPosition, xZoom, yZoom, fixed);
}
/**
* renders a rectangle
* @param rectangle needed to render
* @param xZoom zoom ratio
* @param yZoom zoom ratio
* @param fixed if it moves with the camera or not
*/
public void renderRectangle(Rectangle rectangle, int xZoom, int yZoom, boolean fixed){
int[] rectanglePixels = rectangle.getPixels();
if(rectanglePixels != null) renderArray(rectanglePixels, rectangle.w, rectangle.h, rectangle.x, rectangle.y, xZoom, yZoom, fixed);
}
/**
* renders a rectangle but with an offset
* @param rectangle needed to render
* @param offset needed to render
* @param xZoom zoom ratio
* @param yZoom zoom ratio
* @param fixed if it moves with the camera or not
*/
public void renderRectangle(Rectangle rectangle, Rectangle offset, int xZoom, int yZoom, boolean fixed){
int[] rectanglePixels = rectangle.getPixels();
if(rectanglePixels != null)
renderArray(rectanglePixels, rectangle.w, rectangle.h, rectangle.x + offset.x, rectangle.y + offset.y, xZoom, yZoom, fixed);
}
/**
* renders an array of Pixels
* @param renderPixels color InformationDesc
* @param renderWidth width
* @param renderHeight height
* @param xPosition x
* @param yPosition y
* @param xZoom zoom ratio
* @param yZoom zoom ratio
* @param fixed if it is fixed on screen or moves with the camera
*/
public void renderArray(int[] renderPixels, int renderWidth, int renderHeight, int xPosition, int yPosition, int xZoom, int yZoom, boolean fixed){
for(int y = 0; y < renderHeight; y++)
for(int x = 0; x < renderWidth; x++)
for(int yZoomPosition = 0; yZoomPosition < yZoom; yZoomPosition++)
for(int xZoomPosition = 0; xZoomPosition < xZoom; xZoomPosition++)
setPixel(renderPixels[x + y * renderWidth], (x * xZoom) + xPosition + xZoomPosition, ((y * yZoom) + yPosition + yZoomPosition), fixed);
}
/**
* sets the pixels
* @param pixel color information
* @param x x
* @param y y Pos
* @param fixed if it is fixed or moves with the camera
*/
private void setPixel(int pixel, int x, int y, boolean fixed) {
int pixelIndex = 0;
if(!fixed){
if(x >= camera.x && y >= camera.y && x <= camera.x + camera.w && y <= camera.y + camera.h)
pixelIndex = (x - camera.x) + (y - camera.y) * view.getWidth();
}
else{
if(x >= 0 && y >= 0 && x <= camera.w && y <= camera.h)
pixelIndex = x + y * view.getWidth();
}
if(pixels.length > pixelIndex && pixel != Game.alpha)
pixels[pixelIndex] = pixel;
}
//getters and setters
public Rectangle getCamera() {
return camera;
}
public int getMaxWidth() {
return maxScreenWidth;
}
public int getMaxHeight() {
return maxScreenHeight;
}
public BufferedImage getView(){
return view;
}
public void clear(){
for(int i = 0; i < pixels.length; i++)
pixels[i] = 0;
}
}