Keine Beschreibung

tileset.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "tileset.h"
  2. #include <QPainter>
  3. #include <QImage>
  4. Tileset::Tileset()
  5. {
  6. }
  7. Metatile::Metatile()
  8. {
  9. tiles = new QList<Tile>;
  10. }
  11. QImage Metatile::getMetatileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
  12. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  13. Metatile* metatile = Metatile::getMetatile(tile, primaryTileset, secondaryTileset);
  14. if (!metatile || !metatile->tiles) {
  15. metatile_image.fill(0xffffffff);
  16. return metatile_image;
  17. }
  18. Tileset* blockTileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset);
  19. if (!blockTileset) {
  20. metatile_image.fill(0xffffffff);
  21. return metatile_image;
  22. }
  23. QList<QList<QRgb>> palettes = Metatile::getBlockPalettes(primaryTileset, secondaryTileset);
  24. QPainter metatile_painter(&metatile_image);
  25. for (int layer = 0; layer < 2; layer++)
  26. for (int y = 0; y < 2; y++)
  27. for (int x = 0; x < 2; x++) {
  28. Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
  29. QImage tile_image = Metatile::getMetatileTile(tile_.tile, primaryTileset, secondaryTileset);
  30. if (tile_image.isNull()) {
  31. // Some metatiles specify tiles that are outside the valid range.
  32. // These are treated as completely transparent, so they can be skipped without
  33. // being drawn.
  34. continue;
  35. }
  36. // Colorize the metatile tiles with its palette.
  37. QList<QRgb> palette = palettes.value(tile_.palette);
  38. for (int j = 0; j < palette.length(); j++) {
  39. tile_image.setColor(j, palette.value(j));
  40. }
  41. // The top layer of the metatile has its last color displayed at transparent.
  42. if (layer > 0) {
  43. QColor color(tile_image.color(15));
  44. color.setAlpha(0);
  45. tile_image.setColor(15, color.rgba());
  46. }
  47. QPoint origin = QPoint(x*8, y*8);
  48. metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
  49. }
  50. metatile_painter.end();
  51. return metatile_image;
  52. }
  53. Metatile* Metatile::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) {
  54. Tileset *tileset = Metatile::getBlockTileset(index, primaryTileset, secondaryTileset);
  55. int local_index = Metatile::getBlockIndex(index);
  56. if (!tileset || !tileset->metatiles) {
  57. return NULL;
  58. }
  59. Metatile *metatile = tileset->metatiles->value(local_index, NULL);
  60. return metatile;
  61. }
  62. QImage Metatile::getMetatileTile(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
  63. Tileset *tileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset);
  64. int local_index = Metatile::getBlockIndex(tile);
  65. if (!tileset || !tileset->tiles) {
  66. return QImage();
  67. }
  68. return tileset->tiles->value(local_index, QImage());
  69. }
  70. Tileset* Metatile::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) {
  71. int primary_size = 0x200;
  72. if (metatile_index < primary_size) {
  73. return primaryTileset;
  74. } else {
  75. return secondaryTileset;
  76. }
  77. }
  78. int Metatile::getBlockIndex(int index) {
  79. int primary_size = 0x200;
  80. if (index < primary_size) {
  81. return index;
  82. } else {
  83. return index - primary_size;
  84. }
  85. }
  86. QList<QList<QRgb>> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) {
  87. QList<QList<QRgb>> palettes;
  88. for (int i = 0; i < 6; i++) {
  89. palettes.append(primaryTileset->palettes->at(i));
  90. }
  91. for (int i = 6; i < secondaryTileset->palettes->length(); i++) {
  92. palettes.append(secondaryTileset->palettes->at(i));
  93. }
  94. return palettes;
  95. }