I am following a tutorial on YouTube for making a 2D game. There is no clear explanation on what its actually doing.
I get bit-shifting and & operator but I don't know what they mean in this context and I understand basic Java implementation.
I've done some basic trial and error with the below code by seeing how it works, I see the effects happening but don't quite comprehend on what it is supposed to be doing in the code.
Could someone please explain how the lines marked with "//*******" work in the given context?
public class Screen { public static final int MAP_WIDTH= 64; public static final int MAP_WIDTH_MASK= MAP_WIDTH - 1; public static final int PIXEL_SIZE= 8; public static final int PIXEL_SIZE_MASK= MAP_WIDTH - 1; public int[] tiles = new int[MAP_WIDTH*MAP_WIDTH]; public int[] colours = new int[MAP_WIDTH*MAP_WIDTH*4]; public int xOffset = 0; public int yOffset = 0; public int width; public int height; public SpriteSheet sheet; public Screen(int width, int height, SpriteSheet sheet) { this.width = width; this.height = height; this.sheet = sheet; // ************ explain what the for loop is doing for(int i=0; i< (tiles.length); i++) { colours[i*4+ 0] = 0xff00ff; colours[i*4+ 1] = 0x0000ff; colours[i*4+ 2] = 0xffff00; colours[i*4+ 3] = 0xffffff; } } public void render(int[] pixels, int offset, int row) { //current x and y offset is 0 meaning that the player/map isn't moving in either direction - changing them repeats the map // >>3 - same as multiplying by 8 (size of pixel) //(0 * 8 ) + yOffset, (height * 8) + yOffset // ************ what is the >> doing in this context? for(int yTile = (0 + yOffset) >> 3; yTile<=(height + yOffset) >> 3; yTile++) { // ************ what actually is yMin? int yMin = (yTile * PIXEL_SIZE) - yOffset; // ************ what actually is yMax? int yMax = yMin + PIXEL_SIZE; //sanity check if you go off the map - otherwise would crash if (yMin<0) yMin=0; // yMin could be below 0 because of the yOffset variable being changed is greater than (yTile * PIXEL_SIZE) if (yMax>height) yMax = height; // yMax could be above height because of the yOffset variable being changed // see above; applies to xTile as well for(int xTile = (0 + xOffset) >> 3; xTile<=(width + xOffset) >> 3; xTile++) { int xMin = (xTile * PIXEL_SIZE) - xOffset; int xMax = xMin + PIXEL_SIZE; if(xMin<0) xMin=0; if (xMax>width) xMax = width; // ************ obviously this gives current tile you're on but what does & do in this context? int tileIndex = (xTile &(MAP_WIDTH_MASK)) + (yTile &(MAP_WIDTH_MASK))* MAP_WIDTH; for (int y = yMin; y < yMax; y++){ // ************ what does & do in this context? int sheetPixel = ((y + yOffset) & PIXEL_SIZE_MASK)*sheet.width + ((xMin + xOffset)& PIXEL_SIZE_MASK); // ************ is the tilePixel individual pixels in the tile? int tilePixel = offset + xMin + y * row; for(int x = xMin; x< xMax; x++) { // ************ what is tileIndex * 4 doing? int colour = tileIndex * 4 + sheet.pixels[sheetPixel++]; pixels[tilePixel++] = colours[colour]; } } } } } }