I am creating a java game with the reference of RealtutsGML's java game programming series. I am having a lot of trouble understanding how certain parts of this code works. I understand the getWidth and getHeight of the image part but within the for loop, I am lost.
The Wizard class is the player and the block is the barrier surrounding the map. The map is a 64 by 64 image that I made in photoshop which consists of a bunch of red pixels that represent where the Block objects are supposed to be spawned in, as well as a single green pixel which represent the Wizard/Player spawn location.
private void loadLevel(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
for(int xx = 0; xx < w; xx++) {
for(int yy = 0; yy < h; yy++) {
int pixels = image.getRGB(xx, yy);
int red = (pixels >> 16) & 0xff;
int green = (pixels >> 8) & 0xff;
int blue = (pixels) & 0xff;
if(red == 255)
handler.addObject(new Block(xx * 32, yy * 32, ID.Block));
if(green == 255)
handler.addObject(new Wizard(xx * 32, yy * 32, ID.Player,
handler));
}
}
}
The parts I am struggling to understand is int pixels = image.getRGB(xx, yy)
Does this mean pixels stores the values of Red, Green and Blue, if so how can pixels store multiple values?
The next part I don't understand is
int red = (pixels >> 16) & 0xff;
int green = (pixels >> 8) & 0xff;
int blue = (pixels) & 0xff;
Why do we shift pixels for the red, blue and green variables right by 16 and 8, and then add & 0xff onto them?
The final part is why we multiply the xx and yy values within the handler.addObject method by 32. Each block object and wizard object is 32 by 32 but why do we need to multiply the whole image by 32.
I have already looked at the oracle site for explaining the & bitwise operator and it did help a little but I still struggle to understand how it works in this context.