Skip to content

Commit e662bcb

Browse files
authored
Merge pull request #30 from redomar/Add-support-for-screen2
Add support for screen2
2 parents a18f841 + 9620188 commit e662bcb

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/com/redomar/game/Game.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class Game extends Canvas implements Runnable {
4444
private static final int HEIGHT = (2 * SCALE);
4545
private static final int SCREEN_HEIGHT = (HEIGHT * 2) + 30;
4646
private static final Screen screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
47+
private static final Screen screen2 = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
4748
private static final String NAME = "Game"; // The name of the JFrame panel
4849
private static final Time time = new Time(); // Represents the calendar's time value, in hh:mm:ss
4950
private static final boolean[] alternateCols = new boolean[2]; // Boolean array describing shirt and face colour
@@ -70,7 +71,9 @@ public class Game extends Canvas implements Runnable {
7071
private static InputContext context; // Provides methods to control text input facilities
7172
// Graphics
7273
private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
74+
private final BufferedImage image3 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
7375
private final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel
76+
private final int[] pixels3 = ((DataBufferInt) image3.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel
7477
private final int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours (6 shades of red, 6 of green, and 6 of blue)
7578
private final BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, BufferedImage.TYPE_INT_RGB);
7679
private final Font font = new Font(); // Font object capable of displaying 2 fonts: Arial and Segoe UI
@@ -335,7 +338,9 @@ public void init() {
335338
}
336339

337340
screen.setViewPortHeight(SCREEN_HEIGHT);
341+
screen2.setViewPortHeight(SCREEN_HEIGHT);
338342
screen.setViewPortWidth(SCREEN_WIDTH);
343+
screen2.setViewPortWidth(SCREEN_WIDTH);
339344
input = new InputHandler(this); // Input begins to record key presses
340345
setMouse(new MouseHandler(this)); // Mouse tracking and clicking is now recorded
341346
// setWindow(new WindowHandler(this));
@@ -450,7 +455,6 @@ public void render() {
450455
level.renderEntities(screen);
451456
level.renderProjectileEntities(screen);
452457

453-
454458
for (int y = 0; y < screen.getHeight(); y++) {
455459
for (int x = 0; x < screen.getWidth(); x++) {
456460
int colourCode = screen.getPixels()[x + y * screen.getWidth()];
@@ -459,6 +463,16 @@ public void render() {
459463
}
460464
}
461465
}
466+
for (int y = 0; y < screen2.getHeight(); y++) {
467+
for (int x = 0; x < screen2.getWidth(); x++) {
468+
int colourCode = screen2.getPixels()[x + y * screen2.getWidth()];
469+
if (colourCode < 1){
470+
pixels3[x + y * WIDTH] = 0xff0000;
471+
} else if (colourCode < 255) {
472+
pixels3[x + y * WIDTH] = colours[colourCode];
473+
}
474+
}
475+
}
462476

463477
if (isChangeLevel() && getTickCount() % 60 == 0) {
464478
Game.setChangeLevel(true);
@@ -522,7 +536,7 @@ public void render() {
522536
* This method renders the overlay of the game, which is a transparent layer that is drawn over the game.
523537
*/
524538
private void overlayRender(Graphics2D g) {
525-
g.setColor(new Color(0f, 0f, 0f, .192f)); // Transparent color
539+
g.setColor(new Color(0f, 0f, 0f, 0f)); // Transparent color
526540
g.fillRect(0, 0, getWidth(), getHeight()-30);
527541
}
528542

src/com/redomar/game/gfx/Screen.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,48 @@ public void render(int xPos, int yPos, int tile, int colour, int mirrorDir, int
105105
}
106106

107107
}
108+
private static final String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ " + "0123456789.,:;'\"!?$%()-=+/ ";
109+
110+
public void renderText(String msg, int xPos, int yPos, int colour, int scale) {
111+
for (int i = 0; i < msg.length(); i++) {
112+
int charIndex = chars.indexOf(msg.charAt(i));
113+
if (charIndex >= 0) { // Only render if the character is found
114+
int tileInSprite = charIndex + 30 * 32; // Calculate the tile position based on the charIndex
115+
renderCharacter(xPos + i * (8 * scale), yPos, tileInSprite, colour, scale);
116+
}
117+
}
118+
}
119+
120+
private void renderCharacter(int xPos, int yPos, int tile, int colour, int scale) {
121+
xPos -= xOffset;
122+
yPos -= yOffset;
123+
124+
int xTile = tile % 32;
125+
int yTile = tile / 32;
126+
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.getWidth();
127+
128+
for (int y = 0; y < 8; y++) {
129+
int yPixel = yPos + y * scale;
130+
131+
for (int x = 0; x < 8; x++) {
132+
int xPixel = xPos + x * scale;
133+
134+
int col = (colour >> (sheet.pixels[x + y * sheet.getWidth() + tileOffset] * 8)) & 255;
135+
if (col < 255) {
136+
for (int yScale = 0; yScale < scale; yScale++) {
137+
if (yPixel + yScale < 0 || yPixel + yScale >= height) continue;
138+
for (int xScale = 0; xScale < scale; xScale++) {
139+
if (xPixel + xScale < 0 || xPixel + xScale >= width) continue;
140+
pixels[(xPixel + xScale) + (yPixel + yScale) * width] = col;
141+
}
142+
}
143+
}
144+
}
145+
}
146+
}
147+
148+
149+
108150

109151
public void setOffset(int xOffset, int yOffset) {
110152
this.xOffset = xOffset;

0 commit comments

Comments
 (0)