Skip to content

Commit

Permalink
Merge pull request #30 from redomar/Add-support-for-screen2
Browse files Browse the repository at this point in the history
Add support for screen2
  • Loading branch information
redomar authored Feb 16, 2024
2 parents a18f841 + 9620188 commit e662bcb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/com/redomar/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
Expand All @@ -70,7 +71,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
Expand Down Expand Up @@ -335,7 +338,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));
Expand Down Expand Up @@ -450,7 +455,6 @@ public void render() {
level.renderEntities(screen);
level.renderProjectileEntities(screen);


for (int y = 0; y < screen.getHeight(); y++) {
for (int x = 0; x < screen.getWidth(); x++) {
int colourCode = screen.getPixels()[x + y * screen.getWidth()];
Expand All @@ -459,6 +463,16 @@ 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 (isChangeLevel() && getTickCount() % 60 == 0) {
Game.setChangeLevel(true);
Expand Down Expand Up @@ -522,7 +536,7 @@ 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.fillRect(0, 0, getWidth(), getHeight()-30);
}

Expand Down
42 changes: 42 additions & 0 deletions src/com/redomar/game/gfx/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e662bcb

Please sign in to comment.