From 0693d00da48d795c7ccb658e8b69fe17f4427337 Mon Sep 17 00:00:00 2001 From: totycro Date: Mon, 4 Apr 2011 19:20:32 +0200 Subject: Cache QPixmaps for Blocks --- pacman-c++/block.cpp | 6 +++++- pacman-c++/block.h | 7 +++++++ pacman-c++/main.cpp | 10 +++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index 11c2a32..d5435f3 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp @@ -4,5 +4,9 @@ Block::Block(Actor::Type type) { - setPixmap(QPixmap(":/" + QString("block%1").arg(type))); + if (m_pixmaps.find(type) == m_pixmaps.end()) { + QString pixmapName = ":/" + QString("block%1").arg(type); + m_pixmaps[type] = QPixmap( pixmapName ); + } + setPixmap( m_pixmaps.find(type)->second ); } diff --git a/pacman-c++/block.h b/pacman-c++/block.h index 25a331c..1e76aa9 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h @@ -1,14 +1,21 @@ #ifndef BLOCK_H #define BLOCK_H +#include + #include "pixmapitem.h" #include "actor.h" + class Block : public PixmapItem { public: Block(Actor::Type type); + private: + // map for saving QPixmaps for reuse + std::map m_pixmaps; + }; #endif // BLOCK_H diff --git a/pacman-c++/main.cpp b/pacman-c++/main.cpp index ed347fe..a34ff97 100644 --- a/pacman-c++/main.cpp +++ b/pacman-c++/main.cpp @@ -29,9 +29,17 @@ int main(int argc, char **argv) scene.addItem(actor4); actor4->setPos(160, 100); + /* Block *block1 = new Block(Actor::Player1); scene.addItem(block1); block1->setPos(200, 200); + */ + + for (unsigned int i=0; i<20; ++i) { + Block *b = new Block(Actor::Player1); + scene.addItem(b); + b->setPos( 100 + i*16, 200); + } QGraphicsView *window = new QGraphicsView(&scene); window->setFrameStyle(0); @@ -45,7 +53,7 @@ int main(int argc, char **argv) mainWin.show(); Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory, - Phonon::MediaSource("/home/manuel/uni/foop/pacman-c++/sound/intro.wav")); + Phonon::MediaSource("/home/totycro/stud/foop/repo/pacman-c++/sound/intro.wav")); music->play(); qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); -- cgit v1.2.3