Skip to content

Commit

Permalink
update rendering of game overlay to make it transparent
Browse files Browse the repository at this point in the history
  • Loading branch information
redomar committed Feb 12, 2024
1 parent 5683624 commit 6ce9848
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/com/redomar/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Game extends Canvas implements Runnable {
private static MouseHandler mouse; // Tracks mouse movement and clicks, and follows the appropriate actions
private static InputContext context; // Provides methods to control text input facilities
// Graphics
private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private final BufferedImage image = 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[] 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);
Expand Down Expand Up @@ -326,7 +326,10 @@ public void init() {
int rr = (r * 255 / 5); // Split all 256 colours into 6 shades (0, 51, 102 ... 255)
int gg = (g * 255 / 5);
int bb = (b * 255 / 5);
colours[index++] = rr << 16 | gg << 8 | bb; // All colour values (RGB) are placed into one 32-bit integer, populating the colour array
// All colour values (RGB) are placed into one 32-bit integer, populating the colour array
// The first 8 bits are for alpha, the next 8 for red, the next 8 for green, and the last 8 for blue
// 0xFF000000 is ignored in BufferedImage.TYPE_INT_RGB, but is used in BufferedImage.TYPE_INT_ARGB
colours[index++] = 0xFF << 24 | rr << 16 | gg << 8 | bb;
}
}
}
Expand Down Expand Up @@ -484,9 +487,9 @@ public void render() {
}

Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.drawRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null);
status(g, isDevMode(), isClosing());
overlayRender(g);
g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null);
g.setColor(Color.WHITE);
g.setFont(font.getSegoe());
Expand Down Expand Up @@ -515,6 +518,14 @@ public void render() {
bs.show();
}

/**
* 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.fillRect(0, 0, getWidth(), getHeight()-30);
}

/*
* This method displays information regarding various aspects/stats of the game, dependent upon
* whether it is running in developer mode, or if the application is closing.
Expand Down

0 comments on commit 6ce9848

Please sign in to comment.