kgoldrunner
kgrplayfield.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kgrplayfield.h"
00020 #include <KDebug>
00021
00022 KGrPlayField::KGrPlayField (KGameCanvasAbstract* canvas)
00023 : KGameCanvasGroup (canvas), m_tileset (0), m_background (0)
00024 {
00025 show();
00026 }
00027
00028 KGrPlayField::~KGrPlayField()
00029 {
00030
00031 while (!m_tilesprites.isEmpty())
00032 delete m_tilesprites.takeFirst();
00033 delete m_background;
00034 }
00035
00036 void KGrPlayField::setTile (int x, int y, int tilenum)
00037 {
00038 Q_ASSERT (m_tileset);
00039
00040 if ((m_background != 0) && (tilenum == 0)) {
00041 m_tilesprites.at(y*m_numTilesH + x)->hide();
00042 }
00043 else {
00044 m_tilesprites.at(y*m_numTilesH + x)->setPixmap (m_tileset->at(tilenum));
00045 m_tilesprites.at(y*m_numTilesH + x)->show();
00046 m_tilesprites.at(y*m_numTilesH + x)->raise();
00047 }
00048 }
00049
00050 void KGrPlayField::setBackground (const bool create, const QPixmap &background,
00051 const QPoint & tl)
00052 {
00053 if (create) {
00054 delete m_background;
00055 m_background = 0;
00056 if (!background.isNull()) {
00057 m_background = new KGameCanvasPixmap (this);
00058 m_background->moveTo (tl.x(), tl.y());
00059 m_background->setPixmap (background);
00060 m_background->show();
00061 }
00062 }
00063 else {
00064 m_background->moveTo (tl.x(), tl.y());
00065 }
00066 }
00067
00068 void KGrPlayField::setTiles (QList<QPixmap> * tileset, const QPoint & topLeft,
00069 const int h, const int v, const int tilewidth, const int tileheight)
00070 {
00071 QPixmap pm;
00072 m_tilew = tilewidth;
00073 m_tileh = tileheight;
00074 m_numTilesH = h;
00075 m_numTilesV = v;
00076
00077 Q_ASSERT (tileset);
00078
00079 while (! m_tilesprites.isEmpty())
00080 delete m_tilesprites.takeFirst();
00081
00082
00083 m_tileset = tileset;
00084
00085
00086 int totaltiles = m_numTilesH * m_numTilesV;
00087 for (int i=0; i < totaltiles; ++i) {
00088 KGameCanvasPixmap * thissprite = new KGameCanvasPixmap (this);
00089 thissprite->moveTo ((i % m_numTilesH) * m_tilew + topLeft.x(),
00090 (i / m_numTilesH) * m_tileh + topLeft.y());
00091 if (m_background == 0) {
00092
00093 thissprite->setPixmap (m_tileset->at (0));
00094 thissprite->show();
00095 }
00096
00097
00098 m_tilesprites.append (thissprite);
00099 }
00100 }