I want to add a screenshot function to my game.
int width = 320;
int height = width / 4*3;
pixels is an int[] containing 76800 RGB int values corresponding to every pixel present on the screen at any time
public static void buildBitmap() {
File f = new File("C:/scr/game_name" + LocalDate.now().toString() +".bmp");
try(FileOutputStream fos = new FileOutputStream(f)){
fos.write(66);
fos.write(77);
fos.write(230428); //width * height * 3
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(26);
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(12);
fos.write(0);
fos.write(0);
fos.write(0);
fos.write(320);
fos.write(0);
fos.write(240);
fos.write(0);
fos.write(1);
fos.write(0);
fos.write(24);
fos.write(0);
for(int y = height-1; y > 0; y--) {
for(int x = 0; x < width-1; x++){
fos.write(pixels[x + y * width] & 0xFF); //blue
fos.write((pixels[x + y * width] >> 8) & 0xFF); //green
fos.write((pixels[x + y * width] >> 16) & 0xFF); //red
}
}
fos.write(0);
fos.write(0);
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("Screenshot saved to " + f);
}
The nested loop that should fill write to the file the actual image data is made to iterate through the array bottom to top: left to right, convert the RGB int value into seperate blue, green, red and write them to the file (in that order).
It's mathematically sound and the resulting image, while warped and disfigured, is at least recognisable to be from the game. What's wrong with my code?
Also the output image comes out with a width of 64, why is this?