Commit 10ede4c5 authored by Ian Wadham's avatar Ian Wadham

Rewrite paintCell(). Create tiles in KGrRenderer. Remove some debugging code.

svn path=/branches/work/kgoldrunner-qgv/; revision=1299199
parent 590207be
...@@ -150,11 +150,11 @@ void GS::redrawScene (QSize size) ...@@ -150,11 +150,11 @@ void GS::redrawScene (QSize size)
void GS::loadTestItems() void GS::loadTestItems()
{ {
m_tiles.fill (0, m_tilesWide * m_tilesHigh); m_tiles.fill (0, m_tilesWide * m_tilesHigh);
qDebug() << "Tile count" << m_tiles.count() << "width" << m_tilesWide
<< "height" << m_tilesHigh;
int index = 0; int index = 0;
while (test[index].type != FREE) { 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); paintCell (test[index].i, test[index].j, test[index].type);
index++; index++;
} }
...@@ -196,8 +196,6 @@ void GS::redrawTestItems (const int tileSize) ...@@ -196,8 +196,6 @@ void GS::redrawTestItems (const int tileSize)
} }
m_tiles.at(t)->setPos (m_gridTopLeft.x() + i * tileSize, m_tiles.at(t)->setPos (m_gridTopLeft.x() + i * tileSize,
m_gridTopLeft.y() + j * 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) ...@@ -225,26 +223,14 @@ void GS::redrawTestItems (const int tileSize)
void GS::paintCell (const int i, const int j, const char type) void GS::paintCell (const int i, const int j, const char type)
{ {
int offset = i * m_tilesHigh + j; int index = i * m_tilesHigh + j;
qDebug() << "Offset" << offset << "i,j" << i << j; KGameRenderedItem * t = m_renderer->getTileItem (type, m_tiles.at(index));
if (m_tiles.at(offset) != 0) { m_tiles[index] = t;
// TODO: Delete this tile? Replace it with another type? 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. // Minimal QGraphicsView code.
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
// TODO - Add attributes to theme: HasBorderTiles, HasDisplayTiles. // TODO - Add attributes to theme: HasBorderTiles, HasDisplayTiles.
#include <KGameRenderer> #include <KGameRenderer>
#include <KGameRenderedItem>
#include <KgThemeProvider> #include <KgThemeProvider>
#include <KgThemeSelector> #include <KgThemeSelector>
#include <KLocalizedString> #include <KLocalizedString>
...@@ -92,12 +93,9 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme) ...@@ -92,12 +93,9 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme)
// Make the Actors theme (hero, etc.) match the Set theme (bricks, etc.). // Make the Actors theme (hero, etc.) match the Set theme (bricks, etc.).
qDebug() << "KGrRenderer::currentThemeChanged()" << currentSetTheme->name(); qDebug() << "KGrRenderer::currentThemeChanged()" << currentSetTheme->name();
foreach (const KgTheme * actorsTheme, m_actorsProvider->themes()) { foreach (const KgTheme * actorsTheme, m_actorsProvider->themes()) {
qDebug() << "KGrRenderer::currentThemeChanged() Actors" << actorsTheme->customData("Set") << currentSetTheme->customData("Set");
if (actorsTheme->customData("Set") == if (actorsTheme->customData("Set") ==
currentSetTheme->customData("Set")) { currentSetTheme->customData("Set")) {
m_actorsProvider->setCurrentTheme (actorsTheme); m_actorsProvider->setCurrentTheme (actorsTheme);
qDebug() << "actorsTheme" << actorsTheme->customData("Set")
<< actorsTheme->customData("Actors");
break; break;
} }
} }
...@@ -105,13 +103,10 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme) ...@@ -105,13 +103,10 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme)
// Save the KGoldrunner attributes of the current theme. // Save the KGoldrunner attributes of the current theme.
QString s = currentSetTheme->customData("DrawCanvasBorder", "0"); QString s = currentSetTheme->customData("DrawCanvasBorder", "0");
m_hasBorder = (s == QString ("1")); m_hasBorder = (s == QString ("1"));
qDebug() << "THEME HAS BORDER?" << s << m_hasBorder;
s = currentSetTheme->customData("BorderColor", "#000000"); s = currentSetTheme->customData("BorderColor", "#000000");
m_borderColor = QColor (s); m_borderColor = QColor (s);
qDebug() << "SET BORDER COLOR" << s << m_borderColor;
s = currentSetTheme->customData("TextColor", "#FFFFFF"); s = currentSetTheme->customData("TextColor", "#FFFFFF");
m_textColor = QColor (s); 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 // 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 // the background has no border, to avoid ugly white rectangles appearing
...@@ -136,23 +131,46 @@ KGrRenderer::PixmapSpec KGrRenderer::keyTable [] = { ...@@ -136,23 +131,46 @@ KGrRenderer::PixmapSpec KGrRenderer::keyTable [] = {
{NUGGET, Set, "gold", "-%1", 0, -2}, {NUGGET, Set, "gold", "-%1", 0, -2},
{BAR, Set, "bar", "-%1", 0, -2}, {BAR, Set, "bar", "-%1", 0, -2},
{BACKDROP, Set, "background", "%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() void KGrRenderer::initPixmapKeys()
{ {
// Set all pixmaps in keyTable[] as "not counted yet" (frameCount -2). // Set all pixmaps in keyTable[] as "not counted yet" (frameCount -2).
int index = 0; int index = 0;
while (keyTable[index].picType != FREE) { do {
keyTable[index].frameCount = -2; keyTable[index].frameCount = -2;
index++; 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 = ""; QString pixmapKey = "";
int index = findKeyTableIndex (picType); // int index = findKeyTableIndex (picType);
int frameCount = (index < 0) ? -1 : keyTable[index].frameCount; int frameCount = (index < 0) ? -1 : keyTable[index].frameCount;
if (frameCount > -1) { if (frameCount > -1) {
pixmapKey = keyTable[index].picKey; // No suffix. pixmapKey = keyTable[index].picKey; // No suffix.
...@@ -164,7 +182,6 @@ QString KGrRenderer::getPixmapKey (const char picType) ...@@ -164,7 +182,6 @@ QString KGrRenderer::getPixmapKey (const char picType)
(rand() % frameCount)); (rand() % frameCount));
} }
} }
qDebug() << "picType" << picType << "pixmapKey" << pixmapKey;
return pixmapKey; return pixmapKey;
} }
...@@ -181,24 +198,23 @@ QString KGrRenderer::getBackgroundKey (const int level) ...@@ -181,24 +198,23 @@ QString KGrRenderer::getBackgroundKey (const int level)
pixmapKey = pixmapKey.arg (level % frameCount); pixmapKey = pixmapKey.arg (level % frameCount);
} }
} }
qDebug() << "picType" << BACKDROP << "pixmapKey" << pixmapKey;
return pixmapKey; return pixmapKey;
} }
int KGrRenderer::findKeyTableIndex (const char picType) int KGrRenderer::findKeyTableIndex (const char picType)
{ {
int index = 0; int index = 0;
while (true) { while (true) { // Find ANY picType, including FREE.
if (keyTable[index].picType == FREE) { if (keyTable[index].picType == picType) {
index = -1; // Pixmap key not found.
break;
}
else if (keyTable[index].picType == picType) {
if (keyTable[index].frameCount == -2) { if (keyTable[index].frameCount == -2) {
keyTable[index].frameCount = countFrames (index); keyTable[index].frameCount = countFrames (index);
} }
break; break;
} }
else if (keyTable[index].picType == FREE) {
index = -1; // Not found.
break;
}
index++; index++;
} }
return index; return index;
...@@ -214,7 +230,6 @@ int KGrRenderer::countFrames (const int index) ...@@ -214,7 +230,6 @@ int KGrRenderer::countFrames (const int index)
count++; count++;
} }
qDebug() << "KGrRenderer::countFrames 1" << keyTable[index].picKey << count;
if ((count == 0) && (QString(keyTable[index].picKey) != QString("brick"))) { if ((count == 0) && (QString(keyTable[index].picKey) != QString("brick"))) {
return count; return count;
} }
...@@ -227,12 +242,10 @@ int KGrRenderer::countFrames (const int index) ...@@ -227,12 +242,10 @@ int KGrRenderer::countFrames (const int index)
QString pixmapKey = QString(keyTable[index].picKey) + QString pixmapKey = QString(keyTable[index].picKey) +
QString(keyTable[index].frameSuffix); QString(keyTable[index].frameSuffix);
while (r->spriteExists (pixmapKey.arg (frame))) { while (r->spriteExists (pixmapKey.arg (frame))) {
qDebug() << "KGrRenderer::countFrames found" << pixmapKey.arg (frame);
count++; count++;
frame++; frame++;
} }
qDebug() << "KGrRenderer::countFrames 2" << keyTable[index].picKey << count;
return count; return count;
} }
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
class QGraphicsScene; class QGraphicsScene;
class KgThemeProvider; class KgThemeProvider;
class KgThemeSelector; class KgThemeSelector;
class KGameRenderedItem;
/* @short A class to assist theme-handling and rendering in KGoldrunner. /* @short A class to assist theme-handling and rendering in KGoldrunner.
* *
...@@ -71,13 +72,25 @@ public: ...@@ -71,13 +72,25 @@ public:
*/ */
KGameRenderer * getActorsRenderer() { return m_actorsRenderer; } 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 * 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. * 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. * @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 * Get the SVG element name for a KGoldrunner background. If the theme has
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment