-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.java
More file actions
135 lines (127 loc) · 5.63 KB
/
Screen.java
File metadata and controls
135 lines (127 loc) · 5.63 KB
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
import java.util.ArrayList;
import java.awt.Color;
public class Screen {
public int[][] map;
public int mapWidth, mapLength, width, height;
public ArrayList<Texture> textures;
public ArrayList<CrosshairTexture> crosshairTextures;
public Screen(int[][] m, int mapW, int mapL, ArrayList<Texture> tex, int w, int h, ArrayList<CrosshairTexture> crossTex) {
map = m;
mapWidth = mapW;
mapLength = mapL;
textures = tex;
width = w; //frame width
height = h; //frame height
crosshairTextures = crossTex;
}
public int[] update(Camera camera, int[] pixels) {
for(int n=0; n<pixels.length/2; n++) {
if(pixels[n] != Color.DARK_GRAY.getRGB()) pixels[n] = Color.DARK_GRAY.getRGB(); //ceiling color
}
for(int i=pixels.length/2; i<pixels.length; i++){
if(pixels[i] != Color.DARK_GRAY.getRGB()) pixels[i] = Color.DARK_GRAY.getRGB(); //floor color
}
for(int x=0; x<width; x=x+1) {
double cameraX = 2 * x / (double)(width) -1;
double rayDirX = camera.xDir + camera.xPlane * cameraX;
double rayDirY = camera.yDir + camera.yPlane * cameraX;
//Map position
int mapX = (int)camera.xPos;
int mapY = (int)camera.yPos;
//length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
//Length of ray from one side to next in map
double deltaDistX = Math.sqrt(1 + (rayDirY*rayDirY) / (rayDirX*rayDirX));
double deltaDistY = Math.sqrt(1 + (rayDirX*rayDirX) / (rayDirY*rayDirY));
double perpWallDist;
//Direction to go in x and y
int stepX, stepY;
boolean hit = false;//was a wall hit
int side=0;//was the wall vertical or horizontal
//Figure out the step direction and initial distance to a side
if (rayDirX < 0)
{
stepX = -1;
sideDistX = (camera.xPos - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - camera.xPos) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (camera.yPos - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - camera.yPos) * deltaDistY;
}
//Loop to find where the ray hits a wall
while(!hit) {
//Jump to next square
if (sideDistX < sideDistY)
{
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else
{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
//Check if ray has hit a wall
//System.out.println(mapX + ", " + mapY + ", " + map[mapX][mapY]);
if(map[mapX][mapY] > 0) hit = true;
}
//Calculate distance to the point of impact
if(side==0)
perpWallDist = Math.abs((mapX - camera.xPos + (1 - stepX) / 2) / rayDirX);
else
perpWallDist = Math.abs((mapY - camera.yPos + (1 - stepY) / 2) / rayDirY);
//Now calculate the height of the wall based on the distance from the camera
int lineHeight;
if(perpWallDist > 0) lineHeight = Math.abs((int)(height / perpWallDist));
else lineHeight = height;
//calculate lowest and highest pixel to fill in current stripe
int drawStart = -lineHeight/2+ height/2;
if(drawStart < 0)
drawStart = 0;
int drawEnd = lineHeight/2 + height/2;
if(drawEnd >= height)
drawEnd = height - 1;
//add a texture
int texNum = map[mapX][mapY] - 1;
double wallX;//Exact position of where wall was hit
if(side==1) {//If its a y-axis wall
wallX = (camera.xPos + ((mapY - camera.yPos + (1 - stepY) / 2) / rayDirY) * rayDirX);
} else {//X-axis wall
wallX = (camera.yPos + ((mapX - camera.xPos + (1 - stepX) / 2) / rayDirX) * rayDirY);
}
wallX-=Math.floor(wallX);
//x coordinate on the texture
int texX = (int)(wallX * (textures.get(texNum).getSize()));
if(side == 0 && rayDirX > 0) texX = textures.get(texNum).getSize() - texX - 1;
if(side == 1 && rayDirY < 0) texX = textures.get(texNum).getSize() - texX - 1;
//calculate y coordinate on texture
for(int y=drawStart; y<drawEnd; y++) {
int texY = (((y*2 - height + lineHeight) << 6) / lineHeight) / 2;
int color;
if(side==0) color = textures.get(texNum).pixels[texX + (texY * textures.get(texNum).getSize())];
else color = (textures.get(texNum).pixels[texX + (texY * textures.get(texNum).getSize())]>>1) & 8355711;//Make y sides darker
pixels[x + y*(width)] = color;
}
}
//applying filters...
Filters filters = new Filters(width, height);
pixels = filters.applyTVFilter(pixels);
//crosshair
pixels = crosshairTextures.get(1).applyCrosshair(pixels, width);
return pixels;
}
}