From 10ede4c5633b24f59615ba26a8790ed9b680370e Mon Sep 17 00:00:00 2001 From: Ian Wadham Date: Sat, 9 Jun 2012 08:09:57 +0000 Subject: [PATCH] Rewrite paintCell(). Create tiles in KGrRenderer. Remove some debugging code. svn path=/branches/work/kgoldrunner-qgv/; revision=1299199 --- src/g.cpp | 32 ++++++++------------------ src/kgrrenderer.cpp | 55 ++++++++++++++++++++++++++++----------------- src/kgrrenderer.h | 15 ++++++++++++- 3 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/g.cpp b/src/g.cpp index 0ce3bca..a9b3211 100644 --- a/src/g.cpp +++ b/src/g.cpp @@ -150,11 +150,11 @@ void GS::redrawScene (QSize size) void GS::loadTestItems() { m_tiles.fill (0, m_tilesWide * m_tilesHigh); + qDebug() << "Tile count" << m_tiles.count() << "width" << m_tilesWide + << "height" << m_tilesHigh; int index = 0; while (test[index].type != FREE) { - qDebug() << "Tile" << index << test[index].type << "at" << - test[index].i << test[index].j; paintCell (test[index].i, test[index].j, test[index].type); index++; } @@ -196,8 +196,6 @@ void GS::redrawTestItems (const int tileSize) } m_tiles.at(t)->setPos (m_gridTopLeft.x() + i * tileSize, m_gridTopLeft.y() + j * tileSize); - qDebug() << "Tile" << m_tiles.at(t)->pixmap().size() << - "at" << m_tiles.at(t)->pos(); } } } @@ -225,26 +223,14 @@ void GS::redrawTestItems (const int tileSize) void GS::paintCell (const int i, const int j, const char type) { - int offset = i * m_tilesHigh + j; - qDebug() << "Offset" << offset << "i,j" << i << j; - if (m_tiles.at(offset) != 0) { - // TODO: Delete this tile? Replace it with another type? + int index = i * m_tilesHigh + j; + KGameRenderedItem * t = m_renderer->getTileItem (type, m_tiles.at(index)); + m_tiles[index] = t; + if (t) { // t = 0 if tile was deleted (type FREE). + t->setRenderSize (QSize (m_tileSize, m_tileSize)); + t->setPos (m_gridTopLeft.x() + i * m_tileSize, + m_gridTopLeft.y() + j * m_tileSize); } - QString pixmapKey = m_renderer->getPixmapKey (type); - if ((type == HERO) || (type == ENEMY)) { - // Stationary pixmaps from the Actors file, used only by the editor. - m_tiles[offset] = new KGameRenderedItem (m_renderActors, pixmapKey); - } - else { - // Pixmaps from the Set file, used in level-layouts and in the editor. - m_tiles[offset] = new KGameRenderedItem (m_renderSet, pixmapKey); - } - addItem (m_tiles.at(offset)); - m_tiles.at(offset)->setRenderSize (QSize (m_tileSize, m_tileSize)); - m_tiles.at(offset)->setPos (m_gridTopLeft.x() + i * m_tileSize, - m_gridTopLeft.y() + j * m_tileSize); - qDebug() << "Tile" << type << "i,j" << i << j << "size" << m_tileSize << - "at" << m_tiles.at(offset)->pos(); } // Minimal QGraphicsView code. diff --git a/src/kgrrenderer.cpp b/src/kgrrenderer.cpp index 7d6795a..dbbf8fe 100644 --- a/src/kgrrenderer.cpp +++ b/src/kgrrenderer.cpp @@ -19,6 +19,7 @@ // TODO - Add attributes to theme: HasBorderTiles, HasDisplayTiles. #include +#include #include #include #include @@ -92,12 +93,9 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme) // Make the Actors theme (hero, etc.) match the Set theme (bricks, etc.). qDebug() << "KGrRenderer::currentThemeChanged()" << currentSetTheme->name(); foreach (const KgTheme * actorsTheme, m_actorsProvider->themes()) { - qDebug() << "KGrRenderer::currentThemeChanged() Actors" << actorsTheme->customData("Set") << currentSetTheme->customData("Set"); if (actorsTheme->customData("Set") == currentSetTheme->customData("Set")) { m_actorsProvider->setCurrentTheme (actorsTheme); - qDebug() << "actorsTheme" << actorsTheme->customData("Set") - << actorsTheme->customData("Actors"); break; } } @@ -105,13 +103,10 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme) // Save the KGoldrunner attributes of the current theme. QString s = currentSetTheme->customData("DrawCanvasBorder", "0"); m_hasBorder = (s == QString ("1")); - qDebug() << "THEME HAS BORDER?" << s << m_hasBorder; s = currentSetTheme->customData("BorderColor", "#000000"); m_borderColor = QColor (s); - qDebug() << "SET BORDER COLOR" << s << m_borderColor; s = currentSetTheme->customData("TextColor", "#FFFFFF"); m_textColor = QColor (s); - qDebug() << "SET TEXT COLOR" << s << m_textColor; // Fill the scene (and view) with the new background color. Do this even if // the background has no border, to avoid ugly white rectangles appearing @@ -136,23 +131,46 @@ KGrRenderer::PixmapSpec KGrRenderer::keyTable [] = { {NUGGET, Set, "gold", "-%1", 0, -2}, {BAR, Set, "bar", "-%1", 0, -2}, {BACKDROP, Set, "background", "%1", 0, -2}, - {FREE, Set, "", "", -1, -2} // Must be last entry. + {FREE, Set, "empty", "", -1, -2} // Must be last entry. }; void KGrRenderer::initPixmapKeys() { // Set all pixmaps in keyTable[] as "not counted yet" (frameCount -2). int index = 0; - while (keyTable[index].picType != FREE) { + do { keyTable[index].frameCount = -2; index++; + } while (keyTable[index].picType != FREE); +} + +KGameRenderedItem * KGrRenderer::getTileItem + (const char picType, KGameRenderedItem * currentTile) +{ + if (currentTile) { + // Remove the tile that was here before. + m_scene->removeItem (currentTile); + delete currentTile; + } + + int index; + if ((picType == FREE) || ((index = findKeyTableIndex (picType)) < 0)) { + return 0; // Empty place or missing type, so no KGameRenderedItem. } + + // Get the pixmap key and use one of the two renderers to create the tile. + QString key = getPixmapKey (picType, index); + KGameRenderedItem * tile = + new KGameRenderedItem ((keyTable[index].picSource == Set) ? + m_setRenderer : m_actorsRenderer, key); + m_scene->addItem (tile); + return tile; } -QString KGrRenderer::getPixmapKey (const char picType) +QString KGrRenderer::getPixmapKey (const char picType, const int index) { QString pixmapKey = ""; - int index = findKeyTableIndex (picType); + // int index = findKeyTableIndex (picType); int frameCount = (index < 0) ? -1 : keyTable[index].frameCount; if (frameCount > -1) { pixmapKey = keyTable[index].picKey; // No suffix. @@ -164,7 +182,6 @@ QString KGrRenderer::getPixmapKey (const char picType) (rand() % frameCount)); } } - qDebug() << "picType" << picType << "pixmapKey" << pixmapKey; return pixmapKey; } @@ -181,24 +198,23 @@ QString KGrRenderer::getBackgroundKey (const int level) pixmapKey = pixmapKey.arg (level % frameCount); } } - qDebug() << "picType" << BACKDROP << "pixmapKey" << pixmapKey; return pixmapKey; } int KGrRenderer::findKeyTableIndex (const char picType) { int index = 0; - while (true) { - if (keyTable[index].picType == FREE) { - index = -1; // Pixmap key not found. - break; - } - else if (keyTable[index].picType == picType) { + while (true) { // Find ANY picType, including FREE. + if (keyTable[index].picType == picType) { if (keyTable[index].frameCount == -2) { keyTable[index].frameCount = countFrames (index); } break; } + else if (keyTable[index].picType == FREE) { + index = -1; // Not found. + break; + } index++; } return index; @@ -214,7 +230,6 @@ int KGrRenderer::countFrames (const int index) count++; } - qDebug() << "KGrRenderer::countFrames 1" << keyTable[index].picKey << count; if ((count == 0) && (QString(keyTable[index].picKey) != QString("brick"))) { return count; } @@ -227,12 +242,10 @@ int KGrRenderer::countFrames (const int index) QString pixmapKey = QString(keyTable[index].picKey) + QString(keyTable[index].frameSuffix); while (r->spriteExists (pixmapKey.arg (frame))) { - qDebug() << "KGrRenderer::countFrames found" << pixmapKey.arg (frame); count++; frame++; } - qDebug() << "KGrRenderer::countFrames 2" << keyTable[index].picKey << count; return count; } diff --git a/src/kgrrenderer.h b/src/kgrrenderer.h index 2580ac7..d12fedf 100644 --- a/src/kgrrenderer.h +++ b/src/kgrrenderer.h @@ -25,6 +25,7 @@ class QGraphicsScene; class KgThemeProvider; class KgThemeSelector; +class KGameRenderedItem; /* @short A class to assist theme-handling and rendering in KGoldrunner. * @@ -71,13 +72,25 @@ public: */ KGameRenderer * getActorsRenderer() { return m_actorsRenderer; } + /* + * Create the QGraphicsScene item for a tile of a particular type (e.g. bar, + * gold, concrete, etc.) at a place in the on-screen KGoldrunner grid. + * + * @param picType The internal KGoldrunner type of the required tile. If + * FREE, just delete the previous tile (if any). + * @param currentTile The pre-existing tile that is to be replaced or + * deleted, or zero if the place is empty. + */ + KGameRenderedItem * getTileItem (const char picType, + KGameRenderedItem * currentTile); + /* * Get the SVG element name for a KGoldrunner tile type. If the theme has * more than one tile of that type (e.g. BRICK), make a random selection. * * @param picType The internal KGoldrunner type of a tile or background. */ - QString getPixmapKey (const char picType); + QString getPixmapKey (const char picType, const int index); /* * Get the SVG element name for a KGoldrunner background. If the theme has -- GitLab