No Description

tileset.cpp 3.9KB

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