From 7fa999cba46e122f2538968cd827b3f2380f320c Mon Sep 17 00:00:00 2001 From: Mohamed Omar Date: Fri, 16 Feb 2024 16:19:21 +0000 Subject: [PATCH 1/2] feat: added support for rendering text on a second screen --- src/com/redomar/game/Game.java | 28 ++++++++++++++++++- src/com/redomar/game/gfx/Screen.java | 42 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/com/redomar/game/Game.java b/src/com/redomar/game/Game.java index 5a86c4a..6a3f209 100644 --- a/src/com/redomar/game/Game.java +++ b/src/com/redomar/game/Game.java @@ -7,6 +7,7 @@ import com.redomar.game.entities.trees.Spruce; import com.redomar.game.event.InputHandler; import com.redomar.game.event.MouseHandler; +import com.redomar.game.gfx.Colours; import com.redomar.game.gfx.Screen; import com.redomar.game.gfx.SpriteSheet; import com.redomar.game.gfx.lighting.Night; @@ -44,6 +45,7 @@ public class Game extends Canvas implements Runnable { private static final int HEIGHT = (2 * SCALE); private static final int SCREEN_HEIGHT = (HEIGHT * 2) + 30; private static final Screen screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png")); + private static final Screen screen2 = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png")); private static final String NAME = "Game"; // The name of the JFrame panel private static final Time time = new Time(); // Represents the calendar's time value, in hh:mm:ss private static final boolean[] alternateCols = new boolean[2]; // Boolean array describing shirt and face colour @@ -70,7 +72,9 @@ public class Game extends Canvas implements Runnable { private static InputContext context; // Provides methods to control text input facilities // Graphics private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); + private final BufferedImage image3 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); private final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel + private final int[] pixels3 = ((DataBufferInt) image3.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel 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) private final BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, BufferedImage.TYPE_INT_RGB); private final Font font = new Font(); // Font object capable of displaying 2 fonts: Arial and Segoe UI @@ -335,7 +339,9 @@ public void init() { } screen.setViewPortHeight(SCREEN_HEIGHT); + screen2.setViewPortHeight(SCREEN_HEIGHT); screen.setViewPortWidth(SCREEN_WIDTH); + screen2.setViewPortWidth(SCREEN_WIDTH); input = new InputHandler(this); // Input begins to record key presses setMouse(new MouseHandler(this)); // Mouse tracking and clicking is now recorded // setWindow(new WindowHandler(this)); @@ -449,6 +455,7 @@ public void render() { level.renderTiles(screen, xOffset, yOffset); level.renderEntities(screen); level.renderProjectileEntities(screen); + screen2.renderText("JAVAGAME ", 0, 0, Colours.get(000, -1, -1, 500), 1); for (int y = 0; y < screen.getHeight(); y++) { @@ -459,6 +466,24 @@ public void render() { } } } + for (int y = 0; y < screen2.getHeight(); y++) { + for (int x = 0; x < screen2.getWidth(); x++) { + int colourCode = screen2.getPixels()[x + y * screen2.getWidth()]; + if (colourCode < 1){ + pixels3[x + y * WIDTH] = 0xff0000; + } else if (colourCode < 255) { + pixels3[x + y * WIDTH] = colours[colourCode]; + } + + if (y == 0 || y == screen2.getHeight() - 1) { + pixels3[x + y * WIDTH] = 0x55000000; + } + + if (x == 0 || x == screen2.getWidth() - 1) { + pixels3[x + y * WIDTH] = 0x55000000; + } + } + } if (isChangeLevel() && getTickCount() % 60 == 0) { Game.setChangeLevel(true); @@ -522,7 +547,8 @@ public void render() { * This method renders the overlay of the game, which is a transparent layer that is drawn over the game. */ private void overlayRender(Graphics2D g) { - g.setColor(new Color(0f, 0f, 0f, .192f)); // Transparent color + g.setColor(new Color(0f, 0f, 0f, 0f)); // Transparent color + g.drawImage(image3, 0, 0, getWidth()/3, getHeight()/3 - 30, null); g.fillRect(0, 0, getWidth(), getHeight()-30); } diff --git a/src/com/redomar/game/gfx/Screen.java b/src/com/redomar/game/gfx/Screen.java index d58e767..695f232 100644 --- a/src/com/redomar/game/gfx/Screen.java +++ b/src/com/redomar/game/gfx/Screen.java @@ -105,6 +105,48 @@ public void render(int xPos, int yPos, int tile, int colour, int mirrorDir, int } } + private static final String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ " + "0123456789.,:;'\"!?$%()-=+/ "; + + public void renderText(String msg, int xPos, int yPos, int colour, int scale) { + for (int i = 0; i < msg.length(); i++) { + int charIndex = chars.indexOf(msg.charAt(i)); + if (charIndex >= 0) { // Only render if the character is found + int tileInSprite = charIndex + 30 * 32; // Calculate the tile position based on the charIndex + renderCharacter(xPos + i * (8 * scale), yPos, tileInSprite, colour, scale); + } + } + } + + private void renderCharacter(int xPos, int yPos, int tile, int colour, int scale) { + xPos -= xOffset; + yPos -= yOffset; + + int xTile = tile % 32; + int yTile = tile / 32; + int tileOffset = (xTile << 3) + (yTile << 3) * sheet.getWidth(); + + for (int y = 0; y < 8; y++) { + int yPixel = yPos + y * scale; + + for (int x = 0; x < 8; x++) { + int xPixel = xPos + x * scale; + + int col = (colour >> (sheet.pixels[x + y * sheet.getWidth() + tileOffset] * 8)) & 255; + if (col < 255) { + for (int yScale = 0; yScale < scale; yScale++) { + if (yPixel + yScale < 0 || yPixel + yScale >= height) continue; + for (int xScale = 0; xScale < scale; xScale++) { + if (xPixel + xScale < 0 || xPixel + xScale >= width) continue; + pixels[(xPixel + xScale) + (yPixel + yScale) * width] = col; + } + } + } + } + } + } + + + public void setOffset(int xOffset, int yOffset) { this.xOffset = xOffset; From 9620188cf8f09dd90e57a4611e1b218822d7e45f Mon Sep 17 00:00:00 2001 From: Mohamed Omar Date: Fri, 16 Feb 2024 16:27:13 +0000 Subject: [PATCH 2/2] refactor: remove unused code and unnecessary rendering of text and image --- src/com/redomar/game/Game.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/com/redomar/game/Game.java b/src/com/redomar/game/Game.java index 6a3f209..c7c2f2a 100644 --- a/src/com/redomar/game/Game.java +++ b/src/com/redomar/game/Game.java @@ -7,7 +7,6 @@ import com.redomar.game.entities.trees.Spruce; import com.redomar.game.event.InputHandler; import com.redomar.game.event.MouseHandler; -import com.redomar.game.gfx.Colours; import com.redomar.game.gfx.Screen; import com.redomar.game.gfx.SpriteSheet; import com.redomar.game.gfx.lighting.Night; @@ -455,8 +454,6 @@ public void render() { level.renderTiles(screen, xOffset, yOffset); level.renderEntities(screen); level.renderProjectileEntities(screen); - screen2.renderText("JAVAGAME ", 0, 0, Colours.get(000, -1, -1, 500), 1); - for (int y = 0; y < screen.getHeight(); y++) { for (int x = 0; x < screen.getWidth(); x++) { @@ -474,14 +471,6 @@ public void render() { } else if (colourCode < 255) { pixels3[x + y * WIDTH] = colours[colourCode]; } - - if (y == 0 || y == screen2.getHeight() - 1) { - pixels3[x + y * WIDTH] = 0x55000000; - } - - if (x == 0 || x == screen2.getWidth() - 1) { - pixels3[x + y * WIDTH] = 0x55000000; - } } } @@ -548,7 +537,6 @@ public void render() { */ private void overlayRender(Graphics2D g) { g.setColor(new Color(0f, 0f, 0f, 0f)); // Transparent color - g.drawImage(image3, 0, 0, getWidth()/3, getHeight()/3 - 30, null); g.fillRect(0, 0, getWidth(), getHeight()-30); }