Search code examples
javarenderinggame-engine2d-games

Rendering sprite of 8 monochromatic colors


I want a 16x16 monochromatic sprite to be displayed on my screen. The sprite has 8 different shades, which correspond to 8 different colours. Each color can have his own shade as well, but I want to limit that to 8. So instead of FF00FF, I want it to be like 707.

I've got the project on github

What I've got so far is as follows:

This is where I create all the possible colors:

int[] colours = new int[8 * 8 * 8];
int index = 0;
for(int r = 0; r < 8; r++) {
    for(int g = 0; g < 8; g++) {
        for(int b = 0; b < 8; b++) {
            int rr = r * 255 / 7;
            int gg = g * 255 / 7;
            int bb = b * 255 / 7;

            colours[index++] = rr << 16 | gg << 8 | bb;
        }
    }
}

This is where I want to call a long int containing all the color information:

public class Colours {
    public static int get(int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8) {
    //bit shifting with 56 on an integer removes data
    return((get(c8) << 28) + (get(c7) << 24) + (get(c6) << 20) + (get(c5) >> 16) +
    (get(c4) << 12) + (get(c3) << 8) + (get(c2) << 4) + (get(c1)));
}

private static int get(int colour) {
    if(colour < 0) {
        return 255;
    }
    int r = colour / 100 % 10;
    int g = colour / 10 % 10;
    int b = colour % 10;
    return r * 64 + g * 8 + b;
    }
}

This is where the screen gets rendered:

public void render(int xPos, int yPos, int tile, int colour) {
    xPos -= xOffset;
    yPos -= yOffset;

    int xTile = tile % 32;
    int yTile = tile / 32;
    int tileOffset = (xTile << 4) + (yTile << 4) * sheet.width;

    for (int y = 0; y < 16; y++) {
        if (y + yPos < 0 || y + yPos >= height) {
            continue;
        }
        int ySheet = y;
        for (int x = 0; x < 16; x++) {
            if (x + xPos < 0 || x + xPos >= width) {
                continue;
            }
            int xSheet = x;
            int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 16)) & 255;
            if (col < 255) {
                pixels[(x + xPos) + (y + yPos) * width] = col;
            }
        }
    }
}

Solution

  • I fixed it by returning an integer array.