| 1 | #include "VramSpriteView.h" |
| 2 | #include "VDPDataStore.h" |
| 3 | #include <QPainter> |
| 4 | #include <algorithm> |
| 5 | #include <cstdio> |
| 6 | #include "Convert.h" |
| 7 | #include <cmath> |
| 8 | |
| 9 | |
| 10 | VramSpriteView::VramSpriteView(QWidget* parent, mode drawer, bool singleSprite) |
| 11 | : QWidget(parent), image(512, 512, QImage::Format_RGB32), drawMode(drawer), isSingleSpriteDrawer(singleSprite) |
| 12 | { |
| 13 | // sprite size = 8x8, 16x16, 32x32 or 40x8, 48x16, 64x32 if EC bit respected |
| 14 | calculateSizeOfSprites(); |
| 15 | |
| 16 | if (isSingleSpriteDrawer) { |
| 17 | setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); |
| 18 | zoomFactor = 4; |
| 19 | nrOfSpritesToShow = 1; |
| 20 | recalcSpriteLayout(sizeOfSpritesHorizontal * zoomFactor); |
| 21 | } else { |
| 22 | QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 23 | sizePolicy.setHeightForWidth(true); |
| 24 | setSizePolicy(sizePolicy); |
| 25 | switch (drawer) { |
| 26 | case PatternMode: |
| 27 | nrOfSpritesToShow = 256; |
| 28 | nrOfSpritesHorizontal = 32; |
| 29 | nrOfSpritesVertical = 8; |
| 30 | break; |
| 31 | case SpriteAttributeMode: |
| 32 | case ColorMode: |
| 33 | nrOfSpritesToShow = 32; |
| 34 | nrOfSpritesHorizontal = 16; |
| 35 | nrOfSpritesVertical = 2; |
| 36 | break; |
| 37 | } |
| 38 | zoomFactor = 2; |
| 39 | recalcSpriteLayout(512); // do as if we are 512 pixels wide, just to have some imageWidth/height etc etc values |
| 40 | } |
| 41 | |
| 42 | // Mouse update events when mouse is moved over the image, Quibus likes this |
| 43 | // better than my preferred click-on-the-image |
| 44 | setMouseTracking(true); |
| 45 | } |
| 46 | |
| 47 | int VramSpriteView::heightForWidth(int width) const |
| 48 | { |
| 49 | // How many sprites do fit in this width? |
| 50 | float x = int(width / (sizeOfSpritesHorizontal * zoomFactor)); // Does an int divided by an int gives an int that would be converted to float? |
| 51 | int h = int(ceilf(float(nrOfSpritesToShow) / x)) * sizeOfSpritesVertical * zoomFactor; |
| 52 | //printf("\nheightForWidth sizeOfSpritesHorizontal %d sizeOfSpritesVertical %d zoomFactor %d \n", sizeOfSpritesHorizontal, sizeOfSpritesVertical, zoomFactor); |
| 53 | //printf("heightForWidth w=%d => total=%d x=%f => h=%d \n", width, nr_of_sprites_to_show, x, h); |
| 54 | return h; |
| 55 | } |
| 56 | |
| 57 | QSize VramSpriteView::sizeHint() const |
| 58 | { |
| 59 | return {imageWidth, imageHeight}; |
| 60 | } |
| 61 | |
| 62 | void VramSpriteView::calculateImageSize() |
| 63 | { |
| 64 | recalcSpriteLayout(size().width()); |
| 65 | updateGeometry(); |
| 66 | } |
| 67 | |
| 68 | void VramSpriteView::setVramSource(const uint8_t* adr) |
| 69 | { |
| 70 | if (vramBase == adr) return; |
| 71 | vramBase = adr; |
| 72 | decodePalette(); |
| 73 | decode(); |
| 74 | } |
| 75 | |
| 76 | void VramSpriteView::setPaletteSource(const uint8_t* adr, bool useVDP) |
| 77 | { |
| 78 | useVDPpalette = useVDP; |
| 79 | if (palette == adr) return; |
| 80 | palette = adr; |
| 81 | decodePalette(); |
| 82 | decode(); |
| 83 | } |
| 84 | |
| 85 | void VramSpriteView::mousePressEvent(QMouseEvent* e) |
| 86 | { |
| 87 | int x = 0; // TODO is this correct? 'x' and 'y' always remain '0'. |
| 88 | int y = 0; |
| 89 | |
| 90 | if (auto info = infoFromMouseEvent(e)) { |
| 91 | QString text; |
| 92 | switch (drawMode) { |
| 93 | case PatternMode: |
| 94 | text = patternInfo(info->character); |
| 95 | currentSpriteboxSelected = info->spriteBox; |
| 96 | emit characterClicked(info->character); |
| 97 | refresh(); // to do the drawgrid with selection |
| 98 | break; |
| 99 | case SpriteAttributeMode: |
| 100 | case ColorMode: |
| 101 | if (info->character < 32) { |
| 102 | int pat = vramBase[attributeTableAddress + 4 * info->character + 2]; |
| 103 | text = patternInfo(pat, info->spriteBox); |
| 104 | currentSpriteboxSelected = info->spriteBox; |
| 105 | emit spriteboxClicked(info->spriteBox); |
| 106 | refresh(); // to do the drawgrid with selection |
| 107 | } |
| 108 | break; |
| 109 | } |
| 110 | emit imageClicked(x, y, info->character, text); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void VramSpriteView::mouseMoveEvent(QMouseEvent* e) |
| 115 | { |
| 116 | int x = 0; // TODO is this correct? 'x' and 'y' always remain '0'. |
| 117 | int y = 0; |
| 118 | if (auto info = infoFromMouseEvent(e)) { |
| 119 | emit imagePosition(x, y, info->character); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | std::optional<VramSpriteView::MouseEventInfo> VramSpriteView::infoFromMouseEvent(QMouseEvent* e) |
| 124 | { |
| 125 | //printf(" VramSpriteView::infoFromMouseEvent(%i, %i) \n", e->x(), e->y()); |
| 126 | if (!vramBase) return {}; |
| 127 | |
| 128 | // I see negative y-coords sometimes, so for safety clip the coords to zero as lowest value |
| 129 | int x = std::max(0, e->x()); |
| 130 | int y = std::max(0, e->y()); |
| 131 | // maybe clip to maximum to furthest spritepixel drawn? |
| 132 | //x = std::clamp(e->x(), 0, nrOfSpritesHorizontal * sizeOfSpritesHorizontal * zoomFactor - 1); |
| 133 | //y = std::clamp(e->y(), 0, nrOfSpritesVertical * sizeOfSpritesVertical * zoomFactor - 1); |
| 134 | if (x >= imageWidth) return {}; |
| 135 | if (y >= imageHeight) return {}; |
| 136 | |
| 137 | int spriteBox = nrOfSpritesHorizontal * int(y / (sizeOfSpritesVertical * zoomFactor)) + int(x / (sizeOfSpritesHorizontal * zoomFactor)); |
| 138 | if (spriteBox >= nrOfSpritesToShow) return {}; |
| 139 | |
| 140 | int character = (size16x16 && drawMode == PatternMode) ? 4 * spriteBox : spriteBox; |
| 141 | |
| 142 | return MouseEventInfo{spriteBox, character}; |
| 143 | } |
| 144 | |
| 145 | void VramSpriteView::warningImage() |
| 146 | { |
| 147 | QPainter qp(&image); |
| 148 | qp.setPen(Qt::black); |
| 149 | qp.drawText(16, 16, "No sprites in this screenmode!"); |
| 150 | } |
| 151 | |
| 152 | void VramSpriteView::setZoom(float zoom) |
| 153 | { |
| 154 | zoomFactor = std::max(1, int(zoom)); |
| 155 | calculateImageSize(); |
| 156 | } |
| 157 | |
| 158 | void VramSpriteView::paintEvent(QPaintEvent* /*e*/) |
| 159 | { |
| 160 | QRect srcRect(0, 0, imageWidth, imageHeight); |
| 161 | QRect dstRect(0, 0, imageWidth, imageHeight); |
| 162 | //QRect dstRect(0, 0, int(2 * imageWidth * zoomFactor), int(2 * imageHeight * zoomFactor)); |
| 163 | QPainter qp(this); |
| 164 | //qp.drawImage(rect(), image, srcRect); |
| 165 | |
| 166 | //debug code |
| 167 | // static bool fw = true; |
| 168 | // qp.fillRect(0, 0, size().width(), size().height(), fw ? Qt::black : Qt::white); |
| 169 | // fw = !fw; |
| 170 | qp.drawPixmap(dstRect, pixImage, srcRect); |
| 171 | // qp.setPen(Qt::black); |
| 172 | // qp.drawText(QPoint(3, 30), QString("w=%1 h=%2 nr_total=%3").arg(imageWidth).arg(imageHeight).arg(nr_of_sprites_to_show)); |
| 173 | // qp.drawText(QPoint(3, 50), QString("sw=%1 sh=%2 nr_x=%3 nr_y=%4 => %5"). |
| 174 | // arg(sizeOfSpritesHorizontal). |
| 175 | // arg(sizeOfSpritesVertical). |
| 176 | // arg(nrOfSpritesHorizontal). |
| 177 | // arg(nrOfSpritesVertical). |
| 178 | // arg(nrOfSpritesVertical * nrOfSpritesHorizontal)); |
| 179 | } |
| 180 | |
| 181 | void VramSpriteView::decode() |
| 182 | { |
| 183 | if (!vramBase) return; |
| 184 | image.fill(Qt::lightGray); |
| 185 | if (spriteMode == 0) { |
| 186 | warningImage(); |
| 187 | } else { |
| 188 | switch (drawMode) { |
| 189 | case PatternMode: |
| 190 | decodePgt(); |
| 191 | break; |
| 192 | case SpriteAttributeMode: |
| 193 | decodeSpat(); |
| 194 | break; |
| 195 | case ColorMode: |
| 196 | decodeCol(); |
| 197 | break; |
| 198 | } |
| 199 | //and now draw grid if any |
| 200 | if (!isSingleSpriteDrawer && gridEnabled && spriteMode) { |
| 201 | drawGrid(); |
| 202 | } |
| 203 | } |
| 204 | pixImage = pixImage.fromImage(image); |
| 205 | update(); |
| 206 | } |
| 207 | |
| 208 | void VramSpriteView::drawGrid() |
| 209 | { |
| 210 | QPainter qp(&image); |
| 211 | qp.setPen(QColor(55, 55, 55, 128)); |
| 212 | |
| 213 | int startPosXX = sizeOfSpritesHorizontal * zoomFactor - 1; |
| 214 | int startPosYY = sizeOfSpritesVertical * zoomFactor - 1; |
| 215 | int gridHeight = nrOfSpritesVertical * sizeOfSpritesVertical * zoomFactor; |
| 216 | int gridWidth = nrOfSpritesHorizontal * sizeOfSpritesHorizontal * zoomFactor; |
| 217 | |
| 218 | //first draw upper and left line |
| 219 | qp.drawLine(0, 0, gridWidth, 0); |
| 220 | qp.drawLine(0, 0, 0, gridHeight); |
| 221 | |
| 222 | for (int yy = startPosYY; yy < gridHeight; yy += sizeOfSpritesVertical * zoomFactor) { |
| 223 | qp.drawLine(0, yy, gridWidth, yy); |
| 224 | } |
| 225 | for (int xx = startPosXX; xx < gridWidth; xx += sizeOfSpritesHorizontal * zoomFactor) { |
| 226 | qp.drawLine(xx, 0, xx, gridHeight); |
| 227 | } |
| 228 | |
| 229 | if (currentSpriteboxSelected >= 0 && currentSpriteboxSelected < nrOfSpritesToShow) { |
| 230 | int x = sizeOfSpritesHorizontal * zoomFactor * (currentSpriteboxSelected % nrOfSpritesHorizontal); |
| 231 | int y = sizeOfSpritesVertical * zoomFactor * int(currentSpriteboxSelected / nrOfSpritesHorizontal); |
| 232 | int w = sizeOfSpritesHorizontal * zoomFactor; |
| 233 | int h = sizeOfSpritesVertical * zoomFactor; |
| 234 | if (x == 0) { |
| 235 | w--; |
| 236 | } else { |
| 237 | x--; |
| 238 | } |
| 239 | if (y == 0) { |
| 240 | h--; |
| 241 | } else { |
| 242 | y--; |
| 243 | } |
| 244 | qp.setPen(QColor(255, 0, 0, 192)); |
| 245 | qp.setBrush(Qt::NoBrush); |
| 246 | qp.drawRect(x, y, w, h); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void VramSpriteView::decodePalette() |
| 251 | { |
| 252 | if (!palette) return; |
| 253 | |
| 254 | for (int i = 0; i < 16; ++i) { |
| 255 | int r = (palette[2 * i + 0] & 0xf0) >> 4; |
| 256 | int b = (palette[2 * i + 0] & 0x0f); |
| 257 | int g = (palette[2 * i + 1] & 0x0f); |
| 258 | |
| 259 | auto scale = [](int x) { return (x >> 1) | (x << 2) | (x << 5); }; |
| 260 | msxPalette[i] = qRgb(scale(r), scale(g), scale(b)); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void VramSpriteView::decodePgt() |
| 265 | { |
| 266 | image.fill(QColor(Qt::lightGray)); |
| 267 | //scope for painter of drawSpriteAt calling setSpritePixel will fail |
| 268 | //QPainter::begin: A paint device can only be painted by one painter at a time. |
| 269 | //{ |
| 270 | //QPainter qp(&image); |
| 271 | //qp.setPen(Qt::black); |
| 272 | //qp.drawText(32, 32, "decodePgt!"); |
| 273 | //} |
| 274 | auto fg = QColor(Qt::white).rgb(); |
| 275 | auto bg = QColor(Qt::lightGray).rgb(); |
| 276 | if (isSingleSpriteDrawer) { |
| 277 | drawMonochromeSpriteAt(charToDisplay, 0, 0, 0, fg, bg); |
| 278 | if (size16x16) { |
| 279 | drawMonochromeSpriteAt(charToDisplay + 1, 0, 0, 1, fg, bg); |
| 280 | drawMonochromeSpriteAt(charToDisplay + 2, 0, 1, 0, fg, bg); |
| 281 | drawMonochromeSpriteAt(charToDisplay + 3, 0, 1, 1, fg, bg); |
| 282 | } |
| 283 | } else { |
| 284 | for (int i = 0; i < nrOfSpritesToShow; ++i) { |
| 285 | int j = i * (size16x16 ? 4 : 1); |
| 286 | drawMonochromeSpriteAt(j, i, 0, 0, fg, bg); |
| 287 | if (size16x16) { |
| 288 | drawMonochromeSpriteAt(j + 1, i, 0, 1, fg, bg); |
| 289 | drawMonochromeSpriteAt(j + 2, i, 1, 0, fg, bg); |
| 290 | drawMonochromeSpriteAt(j + 3, i, 1, 1, fg, bg); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void VramSpriteView::decodeSpat() |
| 297 | { |
| 298 | // Scope for painter of drawSpriteAt calling setSpritePixel will fail |
| 299 | //QPainter::begin: A paint device can only be painted by one painter at a time. |
| 300 | //{ |
| 301 | // QPainter qp(&image); |
| 302 | // qp.setPen(Qt::black); |
| 303 | // qp.drawText(232, 32, "decodespat!"); |
| 304 | //} |
| 305 | if (spriteMode == 0) return; |
| 306 | |
| 307 | auto bgcolor = QColor(Qt::lightGray); |
| 308 | for (int i = 0; i < 32; ++i) { |
| 309 | drawSpatSprite(i, bgcolor); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void VramSpriteView::decodeCol() |
| 314 | { |
| 315 | if (spriteMode == 0) return; |
| 316 | |
| 317 | auto bgcolor = QColor(Qt::lightGray); |
| 318 | for (int i = 0; i < 32; ++i) { |
| 319 | drawColSprite(i, bgcolor); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void VramSpriteView::drawColSprite(int entry, QColor& /*bgcolor*/) |
| 324 | { |
| 325 | int color = vramBase[attributeTableAddress + 4 * entry + 3]; |
| 326 | int x = (entry % nrOfSpritesHorizontal) * sizeOfSpritesHorizontal * zoomFactor; |
| 327 | int y = int(entry / nrOfSpritesHorizontal) * sizeOfSpritesVertical * zoomFactor; |
| 328 | QPainter qp(&image); |
| 329 | // Mode 0 already tested, we will not end up here with spritemode == 0 |
| 330 | int z = zoomFactor * ((useMagnification && useECbit) ? 2 : 1); |
| 331 | if (spriteMode == 1) { |
| 332 | bool ec = color & 128; |
| 333 | QRgb fgColor = msxPalette[color & 15]; // drop EC and unused bits |
| 334 | qp.fillRect(x, y, sizeOfSpritesHorizontal * z, sizeOfSpritesVertical * z, fgColor); |
| 335 | qp.fillRect(x, y, z, sizeOfSpritesVertical * z, ec ? QRgb(Qt::white) : QRgb(Qt::black)); |
| 336 | } else if (spriteMode == 2) { |
| 337 | for (int charrow = 0; charrow < (size16x16 ? 16 : 8); ++charrow) { |
| 338 | uint8_t colorbyte = vramBase[colorTableAddress + 16 * entry + charrow]; |
| 339 | bool ec = colorbyte & 128; |
| 340 | bool cc = colorbyte & 64; |
| 341 | bool ic = colorbyte & 32; |
| 342 | QRgb fgColor = msxPalette[colorbyte & 15]; // drop EC, CC and IC bits |
| 343 | qp.fillRect(x, y + charrow * z, sizeOfSpritesHorizontal * z, z, fgColor); // line with color |
| 344 | qp.fillRect(x, y + charrow * z, z, z, (ec ? QColor(Qt::white) : QColor(Qt::black))); // EC bit set -> white |
| 345 | qp.fillRect(x + z, y + charrow * z, z, z, (cc ? QColor(Qt::green) : QColor(Qt::black))); // CC bit set -> green |
| 346 | qp.fillRect(x + 2 * z, y + charrow * z, z, z, (ic ? QColor(Qt::red) : QColor(Qt::black))); // IC bit set -> blue |
| 347 | |
| 348 | if (z > 2) { // Nice black seperation line if possible |
| 349 | qp.fillRect(x + 1 * z - 1, y + charrow * z, 1, z, QColor(Qt::black)); |
| 350 | qp.fillRect(x + 2 * z - 1, y + charrow * z, 1, z, QColor(Qt::black)); |
| 351 | qp.fillRect(x + 3 * z - 1, y + charrow * z, 1, z, QColor(Qt::black)); |
| 352 | } |
| 353 | } |
| 354 | } else { |
| 355 | image.fill(QColor(Qt::darkCyan)); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | void VramSpriteView::drawSpatSprite(int entry, QColor &bgColor) |
| 360 | { |
| 361 | auto addr = attributeTableAddress + 4 * entry; |
| 362 | int attrY = vramBase[addr + 0]; |
| 363 | //int attrX = vramBase[addr + 1]; |
| 364 | int pattern = vramBase[addr + 2]; |
| 365 | int color = vramBase[addr + 3]; |
| 366 | |
| 367 | // Mode 0 already tested, we will not end up here with spritemode == 0 |
| 368 | if (spriteMode == 1) { |
| 369 | bool ec = color & 128; |
| 370 | QRgb fgColor = msxPalette[color & 15]; // drop EC and unused bits |
| 371 | if (attrY == 208) { // in spritemode 1 if Y is equal to 208 this and all lower priority sprites will not be displayed, so red bgcolor! |
| 372 | bgColor = QColor(Qt::red); |
| 373 | } |
| 374 | if (size16x16) { |
| 375 | pattern = pattern & 0xFC; |
| 376 | drawMonochromeSpriteAt(pattern + 0, entry, 0, 0, fgColor, bgColor.rgb(), ec); |
| 377 | drawMonochromeSpriteAt(pattern + 1, entry, 0, 1, fgColor, bgColor.rgb(), ec); |
| 378 | drawMonochromeSpriteAt(pattern + 2, entry, 1, 0, fgColor, bgColor.rgb(), ec); |
| 379 | drawMonochromeSpriteAt(pattern + 3, entry, 1, 1, fgColor, bgColor.rgb(), ec); |
| 380 | } else { |
| 381 | drawMonochromeSpriteAt(pattern, entry, 0, 0, fgColor, bgColor.rgb(), ec); |
| 382 | } |
| 383 | } else if (spriteMode == 2) { |
| 384 | if (attrY == 216) { // in spritemode 2 if Y is equal to 216 this and all lower priority sprites will not be displayed, so red bgcolor! |
| 385 | bgColor = QColor(Qt::red); |
| 386 | } |
| 387 | if (size16x16) { |
| 388 | pattern &= 0xFC; |
| 389 | drawLineColoredSpriteAt(pattern + 0, entry, 0, 0, 0, bgColor.rgb()); |
| 390 | drawLineColoredSpriteAt(pattern + 1, entry, 0, 1, 8, bgColor.rgb()); |
| 391 | drawLineColoredSpriteAt(pattern + 2, entry, 1, 0, 0, bgColor.rgb()); |
| 392 | drawLineColoredSpriteAt(pattern + 3, entry, 1, 1, 8, bgColor.rgb()); |
| 393 | } else { |
| 394 | drawLineColoredSpriteAt(pattern, entry, 0, 0, 0, bgColor.rgb()); |
| 395 | } |
| 396 | }else { |
| 397 | image.fill(QColor(Qt::darkCyan)); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | void VramSpriteView::setSpritePixel(int x, int y, QRgb c) |
| 402 | { |
| 403 | QPainter qp(&image); |
| 404 | qp.fillRect(x * zoomFactor, y * zoomFactor, zoomFactor, zoomFactor, c); |
| 405 | //image.setPixel(); |
| 406 | } |
| 407 | |
| 408 | QRgb VramSpriteView::getColor(int c) |
| 409 | { |
| 410 | // TODO do we need to look at the TP bit??? |
| 411 | return msxPalette[c]; |
| 412 | } |
| 413 | |
| 414 | void VramSpriteView::setAttributeTableAddress(int value) |
| 415 | { |
| 416 | attributeTableAddress = value; |
| 417 | decode(); |
| 418 | } |
| 419 | |
| 420 | void VramSpriteView::setColorTableAddress(int value) |
| 421 | { |
| 422 | colorTableAddress = value; |
| 423 | decode(); |
| 424 | } |
| 425 | |
| 426 | QString VramSpriteView::colorInfo(uint8_t color) const |
| 427 | { |
| 428 | bool ec = color & 128; |
| 429 | int c = color & 15; |
| 430 | |
| 431 | QString colortext = QString("%1 (%2)").arg(int(color), 2) |
| 432 | .arg(hexValue(color, 2)); |
| 433 | |
| 434 | if (spriteMode == 1) { |
| 435 | if (color > 15) { |
| 436 | colortext.append(QString(" -> %1 %2").arg(ec ? "EC," : "") |
| 437 | .arg(c, 2)); |
| 438 | } |
| 439 | } else { |
| 440 | if (color > 15) { |
| 441 | bool cc = color & 64; |
| 442 | bool ic = color & 32; |
| 443 | colortext.append( |
| 444 | QString(" -> %1%2%3 %4").arg(ec ? "EC," : "") |
| 445 | .arg(cc ? "CC," : "") |
| 446 | .arg(ic ? "IC," : "") |
| 447 | .arg(c, 2)); |
| 448 | } |
| 449 | } |
| 450 | return colortext; |
| 451 | } |
| 452 | |
| 453 | QString VramSpriteView::patternInfo(int character, int spriteNr) |
| 454 | { |
| 455 | // Mode 1 then gives |
| 456 | QString info; |
| 457 | if (spriteMode == 1 && spriteNr >= 0) { |
| 458 | info.append(QString("Color data: %1\n").arg(colorInfo(vramBase[attributeTableAddress + 4 * spriteNr + 3]))); |
| 459 | } |
| 460 | |
| 461 | // Create header |
| 462 | info.append("Pattern Data "); |
| 463 | if (spriteMode == 2 && spriteNr >= 0) { |
| 464 | if (size16x16) { |
| 465 | info.append(" "); |
| 466 | } |
| 467 | info.append("Color data"); |
| 468 | } |
| 469 | info.append("\n"); |
| 470 | |
| 471 | // Now build the text |
| 472 | auto patBase = patternTableAddress + 8 * character; |
| 473 | auto colBase = colorTableAddress + 16 * spriteNr; |
| 474 | if (!size16x16) { |
| 475 | // 8x8 character |
| 476 | for (int charRow = 0; charRow < 8; ++charRow) { |
| 477 | auto patAddr = patBase + charRow; |
| 478 | auto colAddr = colBase + charRow; |
| 479 | QString colorData; |
| 480 | if (spriteMode == 2 && spriteNr >= 0) { |
| 481 | colorData = QString(" %1: %2").arg( |
| 482 | hexValue(colAddr, 4), |
| 483 | colorInfo(vramBase[colAddr])); |
| 484 | } |
| 485 | info.append(QString("%1: %2 %3 %4\n").arg( |
| 486 | hexValue(patAddr, 4), |
| 487 | byteAsPattern(vramBase[patAddr]), |
| 488 | hexValue(vramBase[patAddr], 2), |
| 489 | colorData)); |
| 490 | } |
| 491 | } else { |
| 492 | for (int charRow = 0; charRow < 16; ++charRow) { |
| 493 | auto patAddr = patBase + charRow; |
| 494 | auto colAddr = colBase + charRow; |
| 495 | QString colorData; |
| 496 | if (spriteMode == 2 && spriteNr >= 0) { |
| 497 | colorData = QString(" %1: %2").arg( |
| 498 | hexValue(colAddr, 4), |
| 499 | colorInfo(vramBase[colAddr])); |
| 500 | } |
| 501 | auto pat0 = vramBase[patAddr + 0]; |
| 502 | auto pat1 = vramBase[patAddr + 16]; |
| 503 | info.append(QString("%1: %2 %3 %4 %5 %6\n").arg( |
| 504 | hexValue(patAddr, 4), |
| 505 | byteAsPattern(pat0), |
| 506 | byteAsPattern(pat1), |
| 507 | hexValue(pat0, 2), |
| 508 | hexValue(pat1, 2), |
| 509 | colorData)); |
| 510 | } |
| 511 | } |
| 512 | return info; |
| 513 | } |
| 514 | |
| 515 | QString VramSpriteView::byteAsPattern(uint8_t byte) |
| 516 | { |
| 517 | QString val; |
| 518 | for (int i = 7; i >= 0; --i) { |
| 519 | val.append(QChar(byte & (1 << i) ? '1' : '.')); |
| 520 | } |
| 521 | return val; |
| 522 | } |
| 523 | |
| 524 | void VramSpriteView::setPatternTableAddress(int value) |
| 525 | { |
| 526 | patternTableAddress = value; |
| 527 | decode(); |
| 528 | } |
| 529 | |
| 530 | void VramSpriteView::setDrawgrid(bool value) |
| 531 | { |
| 532 | if (gridEnabled == value) return; |
| 533 | |
| 534 | gridEnabled = value; |
| 535 | refresh(); |
| 536 | } |
| 537 | |
| 538 | void VramSpriteView::setUseMagnification(bool value) |
| 539 | { |
| 540 | if (useMagnification == value) return; |
| 541 | |
| 542 | useMagnification = value; |
| 543 | calculateSizeOfSprites(); |
| 544 | calculateImageSize(); |
| 545 | refresh(); |
| 546 | } |
| 547 | |
| 548 | void VramSpriteView::setCharToDisplay(int character) |
| 549 | { |
| 550 | if (charToDisplay == character) return; |
| 551 | |
| 552 | charToDisplay = character; |
| 553 | refresh(); |
| 554 | } |
| 555 | |
| 556 | void VramSpriteView::setSpriteboxClicked(int spBox) |
| 557 | { |
| 558 | if (currentSpriteboxSelected == spBox) return; |
| 559 | |
| 560 | currentSpriteboxSelected = spBox; |
| 561 | refresh(); |
| 562 | } |
| 563 | |
| 564 | void VramSpriteView::setCharacterClicked(int charBox) |
| 565 | { |
| 566 | setSpriteboxClicked(charBox); |
| 567 | } |
| 568 | |
| 569 | void VramSpriteView::recalcSpriteLayout(int width) |
| 570 | { |
| 571 | nrOfSpritesHorizontal = std::max(1, width / (zoomFactor * sizeOfSpritesHorizontal)); // If to small to fit, then still set at least 1 otherwise we would be dividing by zero down the line!! |
| 572 | nrOfSpritesVertical = int(ceilf(float(nrOfSpritesToShow) / float(nrOfSpritesHorizontal))); |
| 573 | |
| 574 | imageWidth = nrOfSpritesHorizontal *sizeOfSpritesHorizontal * zoomFactor; |
| 575 | imageHeight = nrOfSpritesVertical * sizeOfSpritesVertical * zoomFactor; |
| 576 | image = QImage(imageWidth, imageHeight, QImage::Format_RGB32); |
| 577 | refresh(); |
| 578 | } |
| 579 | |
| 580 | void VramSpriteView::resizeEvent(QResizeEvent *event) |
| 581 | { |
| 582 | recalcSpriteLayout(event->size().width()); |
| 583 | } |
| 584 | |
| 585 | void VramSpriteView::refresh() |
| 586 | { |
| 587 | // Reset pointers in case during boot these pointers weren't correctly set due to openMSX not having send over vram size... |
| 588 | setVramSource(VDPDataStore::instance().getVramPointer()); |
| 589 | if (useVDPpalette) { |
| 590 | setPaletteSource(VDPDataStore::instance().getPalettePointer(), true); |
| 591 | } |
| 592 | decodePalette(); |
| 593 | decode(); |
| 594 | } |
| 595 | |
| 596 | void VramSpriteView::setSpritemode(int value) |
| 597 | { |
| 598 | spriteMode = value; |
| 599 | refresh(); |
| 600 | } |
| 601 | |
| 602 | void VramSpriteView::setSize16x16(bool value) |
| 603 | { |
| 604 | if (value == size16x16) return; |
| 605 | |
| 606 | size16x16 = value; |
| 607 | |
| 608 | if (isSingleSpriteDrawer) { |
| 609 | calculateSizeOfSprites(); |
| 610 | imageWidth = sizeOfSpritesHorizontal * zoomFactor; |
| 611 | imageHeight = sizeOfSpritesVertical * zoomFactor; |
| 612 | image = QImage(imageWidth, imageHeight, QImage::Format_RGB32); |
| 613 | setMaximumSize(imageWidth, imageHeight); |
| 614 | setMinimumSize(imageWidth, imageHeight); |
| 615 | updateGeometry(); |
| 616 | } else { |
| 617 | if (drawMode == PatternMode) { |
| 618 | nrOfSpritesToShow = size16x16 ? 64 : 256; |
| 619 | } |
| 620 | calculateSizeOfSprites(); |
| 621 | calculateImageSize(); |
| 622 | } |
| 623 | refresh(); |
| 624 | } |
| 625 | |
| 626 | void VramSpriteView::setECinfluence(bool value) |
| 627 | { |
| 628 | // EC bit is used when displaying Sprite attribute Table (and Sprite Color Table has same size for visual effect) |
| 629 | // so make sure it remains false for the display of the Sprite Pattern Generator Table display |
| 630 | bool newval = drawMode != PatternMode ? value : false; |
| 631 | |
| 632 | if (newval == useECbit) return; |
| 633 | |
| 634 | useECbit = newval; |
| 635 | calculateSizeOfSprites(); |
| 636 | calculateImageSize(); |
| 637 | refresh(); |
| 638 | } |
| 639 | |
| 640 | void VramSpriteView::calculateSizeOfSprites() |
| 641 | { |
| 642 | if (useECbit) { |
| 643 | //in case of EC we need 32 pixels "to the left" extra. |
| 644 | // but in that case also the magnification becomes important |
| 645 | // when drawing a mode2 line shifted 32 pixels of a 16x16 sprite and magnification is off you get a 16 pixels gap. If magnified there is no gap |
| 646 | sizeOfSpritesVertical = sizeOfSpritesHorizontal = (useMagnification ? 2 : 1) * (size16x16 ? 16 : 8); |
| 647 | sizeOfSpritesHorizontal += 32; |
| 648 | } else { |
| 649 | sizeOfSpritesVertical = sizeOfSpritesHorizontal = size16x16 ? 16 : 8; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | void VramSpriteView::drawMonochromeSpriteAt(int character, int spritebox, int xoffset, int yoffset, QRgb fg, QRgb bg, bool ec) |
| 654 | { |
| 655 | // x and y without zoomfactor, zoomfactor correction is done in setSpritePixel |
| 656 | int x = (spritebox%nrOfSpritesHorizontal) * sizeOfSpritesHorizontal; |
| 657 | int y = int(spritebox/nrOfSpritesHorizontal) * sizeOfSpritesVertical; |
| 658 | |
| 659 | int ec_offset = useECbit && !ec? 32 : 0; //draw more to the right if no ec but spritebox is extra wide to display ec effect |
| 660 | |
| 661 | QRgb backG = bg; |
| 662 | bool doGrid = gridEnabled && 4 < (zoomFactor * ((useMagnification && useECbit) ? 2 : 1)); |
| 663 | for (int charRow = 0; charRow<8; ++charRow) { |
| 664 | uint8_t patternByte = vramBase[patternTableAddress + 8 * character + charRow]; |
| 665 | for (int charCol = 0; charCol < 8; ++charCol) { |
| 666 | uint8_t mask = 1 << (7 - charCol); |
| 667 | if (doGrid) { |
| 668 | backG = ((charRow + charCol) & 1) ? bg : QColor(bg).darker(110).rgb(); |
| 669 | } |
| 670 | auto c = (patternByte & mask) ? fg : backG; |
| 671 | if (useMagnification && useECbit) { |
| 672 | auto xx = x + ec_offset + 2 * (8 * xoffset + charCol); |
| 673 | auto yy = y + 2 * (8 * yoffset + charRow); |
| 674 | setSpritePixel(xx + 0, yy + 0, c); |
| 675 | setSpritePixel(xx + 1, yy + 0, c); |
| 676 | setSpritePixel(xx + 0, yy + 1, c); |
| 677 | setSpritePixel(xx + 1, yy + 1, c); |
| 678 | } else { |
| 679 | setSpritePixel(x + 8 * xoffset + charCol + ec_offset, y + 8 * yoffset + charRow, c); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void VramSpriteView::drawLineColoredSpriteAt(int character, int spriteBox, int xOffset, int yOffset, int rowOffset, QRgb bg) |
| 686 | { |
| 687 | // x and y without zoomfactor, zoomfactor correction is done in setSpritePixel |
| 688 | int x = (spriteBox % nrOfSpritesHorizontal) * sizeOfSpritesHorizontal; |
| 689 | int y = int(spriteBox / nrOfSpritesHorizontal) * sizeOfSpritesVertical; |
| 690 | |
| 691 | QRgb backG = bg; |
| 692 | for (int charRow = 0; charRow < 8; ++charRow) { |
| 693 | uint8_t patternByte = vramBase[patternTableAddress + 8 * character + charRow]; |
| 694 | uint8_t colorByte = vramBase[colorTableAddress + 16 * spriteBox + charRow + rowOffset]; |
| 695 | bool ec = colorByte & 128; |
| 696 | int ec_offset = (useECbit && !ec) ? 32 : 0; // Draw more to the right if no ec but spritebox is extra wide to display ec effect |
| 697 | |
| 698 | QRgb fg = msxPalette[colorByte & 15]; // Drop EC, CC and IC bits |
| 699 | for (int charCol = 0; charCol < 8; ++charCol) { |
| 700 | uint8_t mask = 1 << (7 - charCol); |
| 701 | if (gridEnabled && zoomFactor > 4) { |
| 702 | backG = ((charRow + charCol) & 1) ? bg : QColor(bg).darker(110).rgb(); |
| 703 | } |
| 704 | auto c = (patternByte & mask) ? fg : backG; |
| 705 | if (useMagnification && useECbit) { |
| 706 | auto xx = x + ec_offset + 2 * (8 * xOffset + charCol); |
| 707 | auto yy = y + 2 * (8 * yOffset + charRow); |
| 708 | setSpritePixel(xx + 0, yy + 0, c); |
| 709 | setSpritePixel(xx + 1, yy + 0, c); |
| 710 | setSpritePixel(xx + 0, yy + 1, c); |
| 711 | setSpritePixel(xx + 1, yy + 1, c); |
| 712 | } else { |
| 713 | setSpritePixel(x + 8 * xOffset + charCol + ec_offset, y + 8 * yOffset + charRow, c); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |