From f269d5a68e6a22676c49961c7529b9c00dc90649 Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Sun, 9 May 2010 14:31:43 -0600 Subject: Move target cursor management into ActorSprite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This simplifies handling of target cursors, centralizing their logic into a single class, instead of two. Also make them more flexible and move the image files outside of the theme so servers can can control them and give them better names. Reviewed-by: Thorbjørn Lindeijer --- data/graphics/CMakeLists.txt | 9 +++ data/graphics/Makefile.am | 8 ++ data/graphics/gui/CMakeLists.txt | 6 -- data/graphics/gui/Makefile.am | 6 -- data/graphics/gui/target-cursor-blue-l.png | Bin 23803 -> 0 bytes data/graphics/gui/target-cursor-blue-m.png | Bin 18439 -> 0 bytes data/graphics/gui/target-cursor-blue-s.png | Bin 8353 -> 0 bytes data/graphics/gui/target-cursor-red-l.png | Bin 25586 -> 0 bytes data/graphics/gui/target-cursor-red-m.png | Bin 17950 -> 0 bytes data/graphics/gui/target-cursor-red-s.png | Bin 8361 -> 0 bytes data/graphics/target-cursor-in-range-l.png | Bin 0 -> 23803 bytes data/graphics/target-cursor-in-range-m.png | Bin 0 -> 18439 bytes data/graphics/target-cursor-in-range-s.png | Bin 0 -> 8353 bytes data/graphics/target-cursor-normal-l.png | Bin 0 -> 25586 bytes data/graphics/target-cursor-normal-m.png | Bin 0 -> 17950 bytes data/graphics/target-cursor-normal-s.png | Bin 0 -> 8361 bytes src/actorsprite.cpp | 121 +++++++++++++++++++++++++++-- src/actorsprite.h | 34 +++++++- src/client.cpp | 23 ++++-- src/game.cpp | 2 + src/localplayer.cpp | 58 ++------------ src/localplayer.h | 16 ---- 22 files changed, 187 insertions(+), 96 deletions(-) delete mode 100644 data/graphics/gui/target-cursor-blue-l.png delete mode 100644 data/graphics/gui/target-cursor-blue-m.png delete mode 100644 data/graphics/gui/target-cursor-blue-s.png delete mode 100644 data/graphics/gui/target-cursor-red-l.png delete mode 100644 data/graphics/gui/target-cursor-red-m.png delete mode 100644 data/graphics/gui/target-cursor-red-s.png create mode 100644 data/graphics/target-cursor-in-range-l.png create mode 100644 data/graphics/target-cursor-in-range-m.png create mode 100644 data/graphics/target-cursor-in-range-s.png create mode 100644 data/graphics/target-cursor-normal-l.png create mode 100644 data/graphics/target-cursor-normal-m.png create mode 100644 data/graphics/target-cursor-normal-s.png diff --git a/data/graphics/CMakeLists.txt b/data/graphics/CMakeLists.txt index 58d75523..d7e78c30 100644 --- a/data/graphics/CMakeLists.txt +++ b/data/graphics/CMakeLists.txt @@ -1,2 +1,11 @@ ADD_SUBDIRECTORY(gui) ADD_SUBDIRECTORY(images) + +SET (FILES + target-cursor-in-range-l.png + target-cursor-in-range-m.png + target-cursor-in-range-s.png + target-cursor-normal-l.png + target-cursor-normal-m.png + target-cursor-normal-s.png + ) \ No newline at end of file diff --git a/data/graphics/Makefile.am b/data/graphics/Makefile.am index e0003154..babe2b96 100644 --- a/data/graphics/Makefile.am +++ b/data/graphics/Makefile.am @@ -2,3 +2,11 @@ EXTRA_DIST = CMakeLists.txt SUBDIRS = gui images + +EXTRA_DIST = \ + target-cursor-in-range-l.png \ + target-cursor-in-range-m.png \ + target-cursor-in-range-s.png \ + target-cursor-normal-l.png \ + target-cursor-normal-m.png \ + target-cursor-normal-s.png \ No newline at end of file diff --git a/data/graphics/gui/CMakeLists.txt b/data/graphics/gui/CMakeLists.txt index fc825139..9943fb0f 100644 --- a/data/graphics/gui/CMakeLists.txt +++ b/data/graphics/gui/CMakeLists.txt @@ -31,12 +31,6 @@ SET (FILES tab.png tab_hilight.png tabselected.png - target-cursor-blue-l.png - target-cursor-blue-m.png - target-cursor-blue-s.png - target-cursor-red-l.png - target-cursor-red-m.png - target-cursor-red-s.png unknown-item.png vscroll_down_default.png vscroll_down_highlight.png diff --git a/data/graphics/gui/Makefile.am b/data/graphics/gui/Makefile.am index 6d04666a..04f957b7 100644 --- a/data/graphics/gui/Makefile.am +++ b/data/graphics/gui/Makefile.am @@ -35,12 +35,6 @@ gui_DATA = \ tab.png \ tab_hilight.png \ tabselected.png \ - target-cursor-blue-l.png \ - target-cursor-blue-m.png \ - target-cursor-blue-s.png \ - target-cursor-red-l.png \ - target-cursor-red-m.png \ - target-cursor-red-s.png \ unknown-item.png \ vscroll_down_default.png \ vscroll_down_highlight.png \ diff --git a/data/graphics/gui/target-cursor-blue-l.png b/data/graphics/gui/target-cursor-blue-l.png deleted file mode 100644 index 77cdf4b6..00000000 Binary files a/data/graphics/gui/target-cursor-blue-l.png and /dev/null differ diff --git a/data/graphics/gui/target-cursor-blue-m.png b/data/graphics/gui/target-cursor-blue-m.png deleted file mode 100644 index c0107357..00000000 Binary files a/data/graphics/gui/target-cursor-blue-m.png and /dev/null differ diff --git a/data/graphics/gui/target-cursor-blue-s.png b/data/graphics/gui/target-cursor-blue-s.png deleted file mode 100644 index 3e81c75d..00000000 Binary files a/data/graphics/gui/target-cursor-blue-s.png and /dev/null differ diff --git a/data/graphics/gui/target-cursor-red-l.png b/data/graphics/gui/target-cursor-red-l.png deleted file mode 100644 index 76d2101b..00000000 Binary files a/data/graphics/gui/target-cursor-red-l.png and /dev/null differ diff --git a/data/graphics/gui/target-cursor-red-m.png b/data/graphics/gui/target-cursor-red-m.png deleted file mode 100644 index e2ab79c2..00000000 Binary files a/data/graphics/gui/target-cursor-red-m.png and /dev/null differ diff --git a/data/graphics/gui/target-cursor-red-s.png b/data/graphics/gui/target-cursor-red-s.png deleted file mode 100644 index 09195f44..00000000 Binary files a/data/graphics/gui/target-cursor-red-s.png and /dev/null differ diff --git a/data/graphics/target-cursor-in-range-l.png b/data/graphics/target-cursor-in-range-l.png new file mode 100644 index 00000000..77cdf4b6 Binary files /dev/null and b/data/graphics/target-cursor-in-range-l.png differ diff --git a/data/graphics/target-cursor-in-range-m.png b/data/graphics/target-cursor-in-range-m.png new file mode 100644 index 00000000..c0107357 Binary files /dev/null and b/data/graphics/target-cursor-in-range-m.png differ diff --git a/data/graphics/target-cursor-in-range-s.png b/data/graphics/target-cursor-in-range-s.png new file mode 100644 index 00000000..3e81c75d Binary files /dev/null and b/data/graphics/target-cursor-in-range-s.png differ diff --git a/data/graphics/target-cursor-normal-l.png b/data/graphics/target-cursor-normal-l.png new file mode 100644 index 00000000..76d2101b Binary files /dev/null and b/data/graphics/target-cursor-normal-l.png differ diff --git a/data/graphics/target-cursor-normal-m.png b/data/graphics/target-cursor-normal-m.png new file mode 100644 index 00000000..e2ab79c2 Binary files /dev/null and b/data/graphics/target-cursor-normal-m.png differ diff --git a/data/graphics/target-cursor-normal-s.png b/data/graphics/target-cursor-normal-s.png new file mode 100644 index 00000000..09195f44 Binary files /dev/null and b/data/graphics/target-cursor-normal-s.png differ diff --git a/src/actorsprite.cpp b/src/actorsprite.cpp index ad703bc2..da81873f 100644 --- a/src/actorsprite.cpp +++ b/src/actorsprite.cpp @@ -34,10 +34,16 @@ #include "net/net.h" #include "resources/image.h" +#include "resources/imageset.h" #include "resources/resourcemanager.h" +#include + #define EFFECTS_FILE "effects.xml" +ImageSet *ActorSprite::targetCursorImages[2][NUM_TC]; +SimpleAnimation *ActorSprite::targetCursor[2][NUM_TC]; + ActorSprite::ActorSprite(int id): mId(id), mStunMode(0), @@ -83,9 +89,6 @@ bool ActorSprite::drawSpriteAt(Graphics *graphics, int x, int y) const void ActorSprite::logic() { // Update sprite animations - if (mUsedTargetCursor) - mUsedTargetCursor->update(tick_time * MILLISECONDS_IN_A_TICK); - update(tick_time * MILLISECONDS_IN_A_TICK); // Restart status/particle effects, if needed @@ -105,6 +108,18 @@ void ActorSprite::logic() mChildParticleEffects.moveTo(mPos.x, mPos.y); } +void ActorSprite::actorLogic() +{ + // Update sprite animations + for (int size = TC_SMALL; size < NUM_TC; size++) + { + for (int type = TCT_NORMAL; type < NUM_TCT; type++) + { + targetCursor[type][size]->update(tick_time * MILLISECONDS_IN_A_TICK); + } + } +} + void ActorSprite::setMap(Map* map) { Actor::setMap(map); @@ -119,10 +134,12 @@ void ActorSprite::controlParticle(Particle *particle) mChildParticleEffects.addLocally(particle); } -void ActorSprite::setTargetAnimation(SimpleAnimation *animation) +void ActorSprite::setTargetType(TargetCursorType type) { - mUsedTargetCursor = animation; - mUsedTargetCursor->reset(); + if (type == TCT_NONE) + untarget(); + else + mUsedTargetCursor = targetCursor[type][getTargetCursorSize()]; } struct EffectDescription { @@ -324,3 +341,95 @@ void ActorSprite::setupSpriteDisplay(const SpriteDisplay &display, mMustResetParticles = true; } + +void ActorSprite::load() +{ + initTargetCursor(); +} + +void ActorSprite::unload() +{ + cleanupTargetCursors(); +} + +static const char *cursorType(int type) +{ + switch (type) + { + case ActorSprite::TCT_IN_RANGE: + return "in-range"; + case ActorSprite::TCT_NORMAL: + return "normal"; + default: + assert(false); + } +} + +static const char *cursorSize(int size) +{ + switch (size) + { + case ActorSprite::TC_LARGE: + return "l"; + case ActorSprite::TC_MEDIUM: + return "m"; + case ActorSprite::TC_SMALL: + return "s"; + default: + assert(false); + } +} + +void ActorSprite::initTargetCursor() +{ + static std::string targetCursor = "graphics/target-cursor-%s-%s.png"; + static int targetWidths[NUM_TC] = {44, 62, 82}; + static int targetHeights[NUM_TC] = {35, 44, 60}; + + // Load target cursors + for (int size = TC_SMALL; size < NUM_TC; size++) + { + for (int type = TCT_NORMAL; type < NUM_TCT; type++) + { + loadTargetCursor(strprintf(targetCursor.c_str(), cursorType(type), + cursorSize(size)), targetWidths[size], + targetHeights[size], type, size); + } + } +} + +void ActorSprite::cleanupTargetCursors() +{ + for (int size = TC_SMALL; size < NUM_TC; size++) + { + for (int type = TCT_NORMAL; type < NUM_TCT; type++) + { + delete targetCursor[type][size]; + targetCursorImages[type][size]->decRef(); + } + } +} + +void ActorSprite::loadTargetCursor(const std::string &filename, + int width, int height, int type, int size) +{ + assert(size > -1); + assert(size < 3); + + ResourceManager *resman = ResourceManager::getInstance(); + ImageSet *currentImageSet = resman->getImageSet(filename, width, height); + + Animation *anim = new Animation; + + for (unsigned int i = 0; i < currentImageSet->size(); ++i) + { + anim->addFrame(currentImageSet->get(i), 750, + (16 - (currentImageSet->getWidth() / 2)), + (16 - (currentImageSet->getHeight() / 2))); + } + + SimpleAnimation *currentCursor = new SimpleAnimation(anim); + + targetCursorImages[type][size] = currentImageSet; + targetCursor[type][size] = currentCursor; +} diff --git a/src/actorsprite.h b/src/actorsprite.h index b5c402eb..a56be125 100644 --- a/src/actorsprite.h +++ b/src/actorsprite.h @@ -53,6 +53,14 @@ public: NUM_TC }; + enum TargetCursorType + { + TCT_NONE = -1, + TCT_NORMAL = 0, + TCT_IN_RANGE, + NUM_TCT + }; + ActorSprite(int id); ~ActorSprite(); @@ -73,6 +81,8 @@ public: virtual void logic(); + static void actorLogic(); + void setMap(Map* map); /** @@ -95,7 +105,7 @@ public: /** * Sets the target animation for this actor. */ - void setTargetAnimation(SimpleAnimation *animation); + void setTargetType(TargetCursorType type); /** * Untargets the actor. @@ -140,6 +150,10 @@ public: virtual float getAlpha() const { return CompoundSprite::getAlpha(); } + static void load(); + + static void unload(); + protected: /** * Trigger visual effect, with components @@ -185,6 +199,24 @@ private: /** Reset particle status effects on next redraw? */ bool mMustResetParticles; + /** Load the target cursors into memory */ + static void initTargetCursor(); + + /** Remove the target cursors from memory */ + static void cleanupTargetCursors(); + + /** + * Helper function for loading target cursors + */ + static void loadTargetCursor(const std::string &filename, + int width, int height, int type, int size); + + /** Images of the target cursor. */ + static ImageSet *targetCursorImages[NUM_TCT][NUM_TC]; + + /** Animated target cursors. */ + static SimpleAnimation *targetCursor[NUM_TCT][NUM_TC]; + /** Target cursor being used */ SimpleAnimation *mUsedTargetCursor; }; diff --git a/src/client.cpp b/src/client.cpp index afc1ae1d..137210b1 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -446,14 +446,6 @@ Client::~Client() // Shutdown sound sound.close(); - // Unload XML databases - ColorDB::unload(); - EmoteDB::unload(); - ItemDB::unload(); - MonsterDB::unload(); - NPCDB::unload(); - StatusEffect::unload(); - ResourceManager::deleteInstance(); SDL_FreeSurface(mIcon); @@ -594,6 +586,19 @@ int Client::exec() { delete game; game = 0; + + if (mState != STATE_CHANGE_MAP) + { + // Unload XML databases + ColorDB::unload(); + EmoteDB::unload(); + ItemDB::unload(); + MonsterDB::unload(); + NPCDB::unload(); + StatusEffect::unload(); + + ActorSprite::unload(); + } } mOldState = mState; @@ -748,6 +753,8 @@ int Client::exec() StatusEffect::load(); Units::loadUnits(); + ActorSprite::load(); + mDesktop->reloadWallpaper(); mState = STATE_GET_CHARACTERS; diff --git a/src/game.cpp b/src/game.cpp index 74997b90..b75d3cdf 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -21,6 +21,7 @@ #include "game.h" +#include "actorsprite.h" #include "beingmanager.h" #include "channelmanager.h" #include "client.h" @@ -334,6 +335,7 @@ void Game::logic() handleInput(); // Handle all necessary game logic + ActorSprite::actorLogic(); beingManager->logic(); floorItemManager->logic(); particleEngine->update(); diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 19ddec51..9796b2ac 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -116,8 +116,6 @@ LocalPlayer::LocalPlayer(int id, int subtype): mTextColor = &Theme::getThemeColor(Theme::PLAYER); mNameColor = &userPalette->getColor(UserPalette::SELF); - initTargetCursor(); - config.addListener("showownname", this); setShowName(config.getValue("showownname", 1)); } @@ -127,14 +125,6 @@ LocalPlayer::~LocalPlayer() delete mInventory; config.removeListener("showownname", this); - - for (int i = Being::TC_SMALL; i < Being::NUM_TC; i++) - { - delete mTargetCursor[0][i]; - delete mTargetCursor[1][i]; - mTargetCursorImages[0][i]->decRef(); - mTargetCursorImages[1][i]->decRef(); - } } void LocalPlayer::logic() @@ -200,8 +190,7 @@ void LocalPlayer::logic() if (mTarget->getType() == Being::NPC) { // NPCs are always in range - mTarget->setTargetAnimation( - mTargetCursor[0][mTarget->getTargetCursorSize()]); + mTarget->setTargetType(TCT_IN_RANGE); } else { @@ -217,10 +206,10 @@ void LocalPlayer::logic() abs(mTarget->getTileY() - getTileY()); const int attackRange = getAttackRange(); - const int inRange = rangeX > attackRange || rangeY > attackRange - ? 1 : 0; - mTarget->setTargetAnimation( - mTargetCursor[inRange][mTarget->getTargetCursorSize()]); + const TargetCursorType targetType = rangeX > attackRange || + rangeY > attackRange ? + TCT_NORMAL : TCT_IN_RANGE; + mTarget->setTargetType(targetType); if (!mTarget->isAlive()) stopAttack(); @@ -1374,43 +1363,6 @@ void LocalPlayer::handleStatusEffect(StatusEffect *effect, int effectId) } } -void LocalPlayer::initTargetCursor() -{ - // Load target cursors - loadTargetCursor("target-cursor-blue-s.png", 44, 35, false, TC_SMALL); - loadTargetCursor("target-cursor-red-s.png", 44, 35, true, TC_SMALL); - loadTargetCursor("target-cursor-blue-m.png", 62, 44, false, TC_MEDIUM); - loadTargetCursor("target-cursor-red-m.png", 62, 44, true, TC_MEDIUM); - loadTargetCursor("target-cursor-blue-l.png", 82, 60, false, TC_LARGE); - loadTargetCursor("target-cursor-red-l.png", 82, 60, true, TC_LARGE); -} - -void LocalPlayer::loadTargetCursor(const std::string &filename, - int width, int height, - bool outRange, TargetCursorSize size) -{ - assert(size > -1); - assert(size < 3); - - ImageSet *currentImageSet = Theme::getImageSetFromTheme(filename, - width, height); - Animation *anim = new Animation; - - for (unsigned int i = 0; i < currentImageSet->size(); ++i) - { - anim->addFrame(currentImageSet->get(i), 75, - (16 - (currentImageSet->getWidth() / 2)), - (16 - (currentImageSet->getHeight() / 2))); - } - - SimpleAnimation *currentCursor = new SimpleAnimation(anim); - - const int index = outRange ? 1 : 0; - - mTargetCursorImages[index][size] = currentImageSet; - mTargetCursor[index][size] = currentCursor; -} - void LocalPlayer::addMessageToQueue(const std::string &message, int color) { mMessages.push_back(MessagePair(message, color)); diff --git a/src/localplayer.h b/src/localplayer.h index 2c06dfb5..38212a60 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -479,22 +479,6 @@ class LocalPlayer : public Player int mLocalWalkTime; /**< Timestamp used to control keyboard walk messages flooding */ - /** Load the target cursors into memory */ - void initTargetCursor(); - - /** - * Helper function for loading target cursors - */ - void loadTargetCursor(const std::string &filename, - int width, int height, - bool outRange, Being::TargetCursorSize size); - - /** Images of the target cursor. */ - ImageSet *mTargetCursorImages[2][NUM_TC]; - - /** Animated target cursors. */ - SimpleAnimation *mTargetCursor[2][NUM_TC]; - typedef std::pair MessagePair; /** Queued exp messages*/ std::list mMessages; -- cgit v1.2.3-70-g09d2