1. package j2dgame.gfx;
  2.  
  3. public class Screen {
  4.  
  5. public static final int MAP_WIDTH = 64;
  6. public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
  7.  
  8. public int[] pixels;
  9.  
  10. public int xOffset = 0;
  11. public int yOffset = 0;
  12.  
  13. public int width;
  14. public int height;
  15.  
  16. public SpriteSheet sheet;
  17.  
  18. public Screen(int width, int height, SpriteSheet sheet) {
  19. this.width = width;
  20. this.height = height;
  21. this.sheet = sheet;
  22.  
  23. pixels = new int[width * height];
  24. }
  25.  
  26. public void render(int xPos, int yPos, int tile, int color) {
  27. xPos -= xOffset;
  28. yPos -= yOffset;
  29.  
  30. int xTile = tile % 32;
  31. int yTile = tile / 32;
  32. int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
  33. for (int y = 0; y < 8; y++) {
  34. if (y + yPos < 0 || y + yPos >= height)
  35. continue;
  36. int ySheet = y;
  37. for (int x = 0; x < 8; x++) {
  38. if (x + xPos < 0 || x + xPos >= width)
  39. continue;
  40. int xSheet = x;
  41. int col = (color >> (sheet.pixels[xSheet + ySheet * sheet.width
  42. + tileOffset] * 8)) & 255;
  43. if (color < 255)
  44. pixels[(x + xPos) + (y + yPos) * width] = col;
  45. }
  46. }
  47.  
  48. }
  49. }