From 19c9c38d28cdaafcc1b496027f53dcd1914037cf Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 17 Apr 2011 19:54:02 +0200 Subject: get rid of two dynamic_casts and use qgraphicitem_cast which is A LOT faster (it makes use of static casts) --- pacman-c++/actor.cpp | 2 ++ pacman-c++/actor.h | 8 +++++++- pacman-c++/block.cpp | 2 ++ pacman-c++/block.h | 5 +++++ pacman-c++/bonuspoint.cpp | 2 ++ pacman-c++/bonuspoint.h | 6 ++++++ pacman-c++/gameentity.cpp | 4 ++-- pacman-c++/gameentity.h | 14 ++++++++++++++ pacman-c++/point.cpp | 2 ++ pacman-c++/point.h | 7 ++++++- pacman-c++/sceneholder.cpp | 4 ++-- 11 files changed, 50 insertions(+), 6 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index e8f5496..fb4b38a 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -13,6 +13,8 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) : GameEntity(color, parent),m_direction(Actor::None), m_local(local), m_wakaPlayer(NULL), m_roundPoints(0), m_gamePoints(0) { + m_type = Type; + /* DON'T set any pixmap here. we've a pixmap in the animation * but we need a sprite for the collision detection */ diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 507a8e2..0738593 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h @@ -14,7 +14,8 @@ class Actor Q_OBJECT public: - enum Movement { + enum Movement + { None = 0, Left, Right, @@ -22,6 +23,11 @@ public: Down, }; + enum + { + Type = UserType + Transmission::pacman + }; + Actor(Color::Color color, bool local = false, QGraphicsItem *parent = 0); virtual ~Actor() {}; diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index eb51d89..4087662 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp @@ -9,6 +9,8 @@ QMap Block::m_pixmaps; Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) : GameEntity(color, parent), m_neighbours(neighbours) { + m_type = Type; + /* empty object for servers */ if (Constants::server) return; diff --git a/pacman-c++/block.h b/pacman-c++/block.h index 6d97a9a..2e47646 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h @@ -18,6 +18,11 @@ public: Down = (1 << 3) }; + enum + { + Type = UserType + Transmission::block + }; + public: Block(Color::Color color, unsigned int neighbours = None, QGraphicsItem *parent = 0); virtual ~Block() diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index 6e1c9d0..8cb8c7e 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp @@ -12,6 +12,8 @@ namespace BonusPoint::BonusPoint(QGraphicsItem *parent) : GameEntity(parent) { + m_type = Type; + /* empty object for servers */ if (Constants::server) return; diff --git a/pacman-c++/bonuspoint.h b/pacman-c++/bonuspoint.h index 2c5a07d..fbb5ba2 100644 --- a/pacman-c++/bonuspoint.h +++ b/pacman-c++/bonuspoint.h @@ -6,6 +6,12 @@ class BonusPoint : public GameEntity { +public: + enum + { + Type = UserType + Transmission::bonuspoint + }; + public: BonusPoint(QGraphicsItem *parent=0); virtual ~BonusPoint() diff --git a/pacman-c++/gameentity.cpp b/pacman-c++/gameentity.cpp index 9dc72ec..156deda 100644 --- a/pacman-c++/gameentity.cpp +++ b/pacman-c++/gameentity.cpp @@ -1,9 +1,9 @@ #include "gameentity.h" GameEntity::GameEntity(Color::Color color, QGraphicsItem *parent) - : PixmapItem(parent), m_eaten(false), m_color(color) + : PixmapItem(parent), m_type(Type), m_eaten(false), m_color(color) {} GameEntity::GameEntity(QGraphicsItem *parent) - : PixmapItem(parent), m_eaten(false), m_color(Color::none) + : PixmapItem(parent), m_type(Type), m_eaten(false), m_color(Color::none) {} diff --git a/pacman-c++/gameentity.h b/pacman-c++/gameentity.h index 2fde095..92b485e 100644 --- a/pacman-c++/gameentity.h +++ b/pacman-c++/gameentity.h @@ -4,6 +4,7 @@ #include "constants.h" #include "pixmapitem.h" #include +#include class Actor; @@ -13,6 +14,12 @@ class Actor; class GameEntity : public PixmapItem { +public: + enum + { + Type = UserType + 1 + }; + public: GameEntity(Color::Color color = Color::none, QGraphicsItem *parent = 0); GameEntity(QGraphicsItem *parent); @@ -52,7 +59,14 @@ public: virtual void onDie(Actor *) {}; + /* enable the use of qgraphicsitem_cast with this item */ + int type() const + { + return m_type; + } + protected: + int m_type; bool m_eaten; Color::Color m_color; }; diff --git a/pacman-c++/point.cpp b/pacman-c++/point.cpp index 7be09c0..2257b12 100644 --- a/pacman-c++/point.cpp +++ b/pacman-c++/point.cpp @@ -10,6 +10,8 @@ namespace Point::Point(QGraphicsItem *parent) : GameEntity(parent) { + m_type = Type; + /* empty object for servers */ if (Constants::server) return; diff --git a/pacman-c++/point.h b/pacman-c++/point.h index 9fd9929..a406194 100644 --- a/pacman-c++/point.h +++ b/pacman-c++/point.h @@ -6,13 +6,18 @@ class Point : public GameEntity { +public: + enum + { + Type = UserType + Transmission::point + }; + public: Point(QGraphicsItem *parent=0); virtual ~Point() {}; virtual bool enter(Actor *actor); - virtual void onDie(Actor *actor); }; diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index 38b49e5..6879ea1 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -59,7 +59,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x /* special handling for purging field * remove elements (in case it's not an actor) */ - if (oldItem != NULL && dynamic_cast(oldItem) == NULL) + if (oldItem != NULL && qgraphicsitem_cast(oldItem) == NULL) { visualMap[x][y] = NULL; Actor *actor = NULL; @@ -94,7 +94,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x // check for old block first if (visualMap[x][y] != NULL) { - Block *oldItem = dynamic_cast(visualMap[x][y]); + Block *oldItem = qgraphicsitem_cast(visualMap[x][y]); if (oldItem != NULL) neighbours = oldItem->neighbours(); } -- cgit v1.2.3