Hey, so I’m rendering images onto the screen pixel-by-pixel. I created a BufferedImage with the width and height of the screen with type INT_ARGB in which its pixels are modified when I render something. When loading the sprites, I take the pixel data from the full sprite sheet and place it inside a new BufferedImage again with a type of INT_ARGB, as the original sprite sheet is of type 3BYTE_BGR. Not sure if it even matters since I then take the pixel data from the new BufferedImage, but it seems to have no effect. After taking the pixel data from the new BufferedImage, I change each color to a number between 0 and 3, for a total of four different colors being used in each sprite. I then take the corresponding pixel data for each sprite from the full pixel array of the whole sprite sheet and place it inside a HashMap with an Integer as a key. When rendering a sprite, I use its tile number to find it and take the pixel data from it, replacing each number (0-3) with that of the corresponding color inside an int[] array. The int[] array contains four colors, each of which have an alpha, red, green, and blue value. The issue is that when I specify a certain transparency, it doesn’t do anything, and when setting the transparency to 1, it does some strange fade effect when it starts up.
Here’s all the relevant code:
The SpriteSheet class, where I load all the sprites.
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
public class SpriteSheet {
public int w, h;
private Map<Integer, byte[]> sprites = new HashMap<Integer, byte[]>();
public SpriteSheet(BufferedImage image) {
if(image.getType() != BufferedImage.TYPE_INT_ARGB) System.out.println("[1] Not ARGB, " + image.getType()); // TYPE_3BYTE_BGR
w = image.getWidth();
h = image.getHeight();
BufferedImage newImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
int[] oldPixels = image.getRGB(0, 0, w, h, null, 0, w); // Not sure if these two lines are needed.
newImg.setRGB(0, 0, w, h, oldPixels, 0, w);
int[] pixels = newImg.getRGB(0, 0, w, h, null, 0, w);
int tiles = (w * h) / 64;
for(int i = 0; i < pixels.length; i++) {
pixels[i] = (pixels[i] & 0xff) / 64;
}
byte[] sprite;
for(int i = 0; i < tiles; i++) {
sprite = new byte[64];
int xTile = i % 8;
int yTile = i / 8;
int toffs = xTile * 8 + yTile * 8 * w;
for(int y = 0; y < 8; y++) {
for(int x = 0; x < 8; x++) {
sprite[x + y * 8] = (byte) pixels[x + y * w + toffs];
}
}
sprites.put(i, sprite);
}
}
public byte[] getSprite(int tile) {
if(tile > (w / 8) * (h / 8) - 1) return sprites.get(0);
return sprites.get(tile);
}
}
My render method, in my Game class.
int[] cols = {Color.getColor(0, 255, 0, 0)/*Should be 100% translucent, but instead it's an almost-white color*/, Color.getColor(255, 0, 255, 0), Color.getColor(255, 0, 0, 255), Color.getColor(255, 0, 0, 0)};
private void render() {
screen.clear(Color.getColor(100, 0, 0, 0)); // The screen should have a slightly translucent, black background, but instead it's solid black.
screen.render(25, 25, 0, cols, 0);
bs.getDrawGraphics().drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null); // image is "TYPE_INT_ARGB"
bs.getDrawGraphics().dispose();
Toolkit.getDefaultToolkit().sync();
bs.show();
fps++;
}
The getColor() method in my Color class.
public static int getColor(int a, int r, int g, int b) {
return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
}
And the render method in my Screen class. The code is partly borrowed from Minicraft, though it doesn’t shift any bits here.
int yy, xx;
boolean mirrorX, mirrorY;
public void render(int xp, int yp, int tile, int[] cols, int bits) {
mirrorX = (bits & 0x01) > 0;
mirrorY = (bits & 0x02) > 0;
for(int y = 0; y < 8; y++) {
yy = y;
if(mirrorY) yy = 7 - y;
if (y + yp < 0 || y + yp >= h) continue;
for(int x = 0; x < 8; x++) {
if (x + xp < 0 || x + xp >= w) continue;
xx = x;
if(mirrorX) xx = 7 - x;
int col = cols[sheet.getSprite(tile)[xx + yy * 8]];
pixels[(x + xp) + (y + yp) * w] = col;
}
}
}
Here are some images showing what the error is:
This is what happens when I set the background color to black with an alpha value of 1. It fades from completely translucent to a slightly translucent black when it starts up.
Any help would be greatly appreciated. I tried everything and can’t figure out why the alpha value isn’t working right. ???