-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameBuffer.java
153 lines (134 loc) · 4.95 KB
/
FrameBuffer.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
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class FrameBuffer {
// It's an image with extra steps!
public int width;
public int height;
Color[] buffer;
public FrameBuffer(int width, int height) {
this.width = width;
this.height = height;
buffer = new Color[width * height];
// Initialize the buffer to black
for(int i = 0; i < buffer.length; i++) {
buffer[i] = new Color(0, 0, 0);
}
}
public void setPixel(int x, int y, Color c) {
// Check if the pixel is out of bounds
if(x < 0 || x >= width || y < 0 || y >= height) {
return;
}
buffer[y * width + x] = c;
}
public void clear(Color c) {
for(int i = 0; i < buffer.length; i++) {
buffer[i] = c;
}
}
public void drawRect(int x, int y, int w, int h, Color c) {
for(int i = x; i < x + w; i++) {
for(int j = y; j < y + h; j++) {
setPixel(i, j, c);
}
}
}
public void draw(JFrame frame) {
// Create a java.awt.image.BufferedImage from the framebuffer
java.awt.image.BufferedImage img = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
// Get the image's graphics context
java.awt.Graphics2D g = img.createGraphics();
// Draw the framebuffer to the image
for(int i = 0; i < buffer.length; i++) {
Color c = buffer[i];
g.setColor(new java.awt.Color(c.r & 0xFF, c.g & 0xFF, c.b & 0xFF, c.a & 0xFF));
g.fillRect(i % width, i / width, 1, 1);
}
// Draw the image to the frame scaled to the frame's size
frame.getContentPane().getGraphics().drawImage(img, 0, 0, frame.getContentPane().getWidth(), frame.getContentPane().getHeight(), null);
// Dispose of the graphics context
g.dispose();
}
public void drawFastVLine(int x, int y, int h, Color color) {
for(int i = y; i < y + h; i++) {
setPixel(x, i, color);
}
}
public void FadeBy(int amount) {
for(int i = 0; i < buffer.length; i++) {
Color c = buffer[i];
c.r -= amount;
c.g -= amount;
c.b -= amount;
// Clamp the color values
if (c.r < 0) c.r = 0;
if (c.g < 0) c.g = 0;
if (c.b < 0) c.b = 0;
buffer[i] = c;
}
}
public void BlitSpriteRect(String name, Vector2 screenPos, Vector2 texturePos, Vector2 textureSize) {
String wallTex = "texture/" + name + ".png";
// Load the texture using ImageIO
BufferedImage tex = null;
try {
tex = ImageIO.read(new File(wallTex));
} catch (IOException e) {
// Load texture/missing.png instead
try {
tex = ImageIO.read(new File("texture/missing.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Blit the texture to the framebuffer
for(int i = 0; i < textureSize.x; i++) {
for(int j = 0; j < textureSize.y; j++) {
// Ensure the pixel is in bounds using Util.wrap
int px_x = (int)Util.wrap(texturePos.x + i, 0, tex.getWidth());
int px_y = (int)Util.wrap(texturePos.y + j, 0, tex.getHeight());
int pixel = tex.getRGB(px_x, px_y);
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = (pixel >> 0) & 0xFF;
int a = (pixel >> 24) & 0xFF;
if(a == 0) {
continue;
}
setPixel((int)screenPos.x + i, (int)screenPos.y + j, new Color(r, g, b));
}
}
}
public void BlitSprite(String name, Vector2 pos) {
String wallTex = "texture/" + name + ".png";
// Load the texture using ImageIO
BufferedImage tex = null;
try {
tex = ImageIO.read(new File(wallTex));
} catch (IOException e) {
// Load texture/missing.png instead
try {
tex = ImageIO.read(new File("texture/missing.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Blit the texture to the framebuffer
for(int i = 0; i < tex.getWidth(); i++) {
for(int j = 0; j < tex.getHeight(); j++) {
int pixel = tex.getRGB(i, j);
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = (pixel >> 0) & 0xFF;
int a = (pixel >> 24) & 0xFF;
if(a == 0) {
continue;
}
setPixel((int)pos.x + i, (int)pos.y + j, new Color(r, g, b));
}
}
}
}