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)
00024 {
00025 m_background = 0;
00026 show();
00027 }
00028
00029 KGrPlayField::~KGrPlayField()
00030 {
00031
00032 while (!m_tilesprites.isEmpty())
00033 delete m_tilesprites.takeFirst();
00034 delete m_background;
00035 }
00036
00037 void KGrPlayField::setTile (int x, int y, int tilenum)
00038 {
00039 Q_ASSERT(m_tileset);
00040
00041 if ((m_background != 0) && (tilenum == 0)) {
00042 m_tilesprites.at(y*m_numTilesH + x)->hide();
00043 }
00044 else {
00045 m_tilesprites.at(y*m_numTilesH + x)->setPixmap(m_tileset->at(tilenum));
00046 m_tilesprites.at(y*m_numTilesH + x)->show();
00047 m_tilesprites.at(y*m_numTilesH + x)->raise();
00048 }
00049 }
00050
00051 void KGrPlayField::setBackground (const bool create, const QImage * background,
00052 const QPoint & tl)
00053 {
00054 if (create) {
00055 delete m_background;
00056 m_background = 0;
00057 if (background != 0) {
00058 m_background = new KGameCanvasPixmap (this);
00059 m_background->moveTo (tl.x(), tl.y());
00060 m_background->setPixmap (QPixmap::fromImage (* background));
00061 m_background->show();
00062 }
00063 }
00064 else {
00065 m_background->moveTo (tl.x(), tl.y());
00066 }
00067 }
00068
00069 void KGrPlayField::setTiles (QList<QPixmap> * tileset, const QPoint & topLeft,
00070 const int h, const int v, const int tilewidth, const int tileheight)
00071 {
00072 QPixmap pm;
00073 m_tilew = tilewidth;
00074 m_tileh = tileheight;
00075 m_numTilesH = h;
00076 m_numTilesV = v;
00077
00078 Q_ASSERT(tileset);
00079
00080 while (!m_tilesprites.isEmpty())
00081 delete m_tilesprites.takeFirst();
00082
00083
00084 m_tileset = tileset;
00085
00086
00087 int totaltiles = m_numTilesH * m_numTilesV;
00088 for (int i=0; i < totaltiles; ++i)
00089 {
00090 KGameCanvasPixmap * thissprite = new KGameCanvasPixmap(this);
00091 thissprite->moveTo((i % m_numTilesH) * m_tilew + topLeft.x(),
00092 (i / m_numTilesH) * m_tileh + topLeft.y());
00093 if (m_background == 0) {
00094
00095 thissprite->setPixmap(m_tileset->at(0));
00096 thissprite->show();
00097 }
00098
00099
00100 m_tilesprites.append(thissprite);
00101 }
00102 }