00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "resources/emotedb.h"
00023
00024 #include "animatedsprite.h"
00025 #include "log.h"
00026
00027 #include "utils/xml.h"
00028
00029 namespace
00030 {
00031 EmoteInfos mEmoteInfos;
00032 EmoteInfo mUnknown;
00033 bool mLoaded = false;
00034 int mLastEmote = 0;
00035 }
00036
00037 void EmoteDB::load()
00038 {
00039 if (mLoaded)
00040 return;
00041
00042 mLastEmote = 0;
00043
00044 EmoteSprite *unknownSprite = new EmoteSprite;
00045 unknownSprite->sprite = AnimatedSprite::load("error.xml");
00046 unknownSprite->name = "unknown";
00047 mUnknown.sprites.push_back(unknownSprite);
00048
00049 logger->log("Initializing emote database...");
00050
00051 XML::Document doc("emotes.xml");
00052 xmlNodePtr rootNode = doc.rootNode();
00053
00054 if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "emotes"))
00055 {
00056 logger->log("Emote Database: Error while loading emotes.xml!");
00057 return;
00058 }
00059
00060
00061 for_each_xml_child_node(emoteNode, rootNode)
00062 {
00063 if (!xmlStrEqual(emoteNode->name, BAD_CAST "emote"))
00064 continue;
00065
00066 int id = XML::getProperty(emoteNode, "id", -1);
00067 if (id == -1)
00068 {
00069 logger->log("Emote Database: Emote with missing ID in emotes.xml!");
00070 continue;
00071 }
00072
00073 EmoteInfo *currentInfo = new EmoteInfo;
00074
00075 for_each_xml_child_node(spriteNode, emoteNode)
00076 {
00077 if (xmlStrEqual(spriteNode->name, BAD_CAST "sprite"))
00078 {
00079 EmoteSprite *currentSprite = new EmoteSprite;
00080 std::string file = "graphics/sprites/" + (std::string)
00081 (const char*) spriteNode->xmlChildrenNode->content;
00082 currentSprite->sprite = AnimatedSprite::load(file,
00083 XML::getProperty(spriteNode, "variant", 0));
00084 currentInfo->sprites.push_back(currentSprite);
00085 }
00086 else if (xmlStrEqual(spriteNode->name, BAD_CAST "particlefx"))
00087 {
00088 std::string particlefx = (const char*) spriteNode->xmlChildrenNode->content;
00089 currentInfo->particles.push_back(particlefx);
00090 }
00091 }
00092 mEmoteInfos[id] = currentInfo;
00093 if (id > mLastEmote) mLastEmote = id;
00094 }
00095
00096 mLoaded = true;
00097 }
00098
00099 void EmoteDB::unload()
00100 {
00101 for ( EmoteInfosIterator i = mEmoteInfos.begin();
00102 i != mEmoteInfos.end();
00103 i++)
00104 {
00105 while (!i->second->sprites.empty())
00106 {
00107 delete i->second->sprites.front()->sprite;
00108 delete i->second->sprites.front();
00109 i->second->sprites.pop_front();
00110 }
00111 delete i->second;
00112 }
00113
00114 mEmoteInfos.clear();
00115
00116 while (!mUnknown.sprites.empty())
00117 {
00118 delete mUnknown.sprites.front()->sprite;
00119 delete mUnknown.sprites.front();
00120 mUnknown.sprites.pop_front();
00121 }
00122
00123 mLoaded = false;
00124 }
00125
00126 const EmoteInfo *EmoteDB::get(int id)
00127 {
00128 EmoteInfosIterator i = mEmoteInfos.find(id);
00129
00130 if (i == mEmoteInfos.end())
00131 {
00132 logger->log("EmoteDB: Warning, unknown emote ID %d requested", id);
00133 return &mUnknown;
00134 }
00135 else
00136 {
00137 return i->second;
00138 }
00139 }
00140
00141 const AnimatedSprite *EmoteDB::getAnimation(int id)
00142 {
00143 const EmoteInfo *info = get(id);
00144 return info->sprites.front()->sprite;
00145 }
00146
00147 const int &EmoteDB::getLast()
00148 {
00149 return mLastEmote;
00150 }