gcsx_scene.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* GCSx
  2. ** SCENE.CPP
  3. **
  4. ** Scene support, entities and drawing
  5. ** Non-EDITOR members
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #include "all.h"
  25. Scene::Scene(World* myWorld, int myId) : name(blankString), nameL(blankString) { start_func
  26. if (myId) assert(myWorld);
  27. cached = 0;
  28. lockCount = 0;
  29. cacheFile = NULL;
  30. world = myWorld;
  31. id = myId;
  32. for (int pos = 0; pos < MAX_LAYERS; ++pos) {
  33. layers[pos] = NULL;
  34. }
  35. layerCount = 0;
  36. vpX = 0;
  37. vpY = 0;
  38. }
  39. Scene::~Scene() { start_func
  40. for (int pos = 0; pos < layerCount; ++pos) {
  41. delete layers[pos];
  42. }
  43. delete cacheFile;
  44. }
  45. int Scene::findLayer(const string& fName) const { start_func
  46. for (int pos = 0; pos < layerCount; ++pos) {
  47. assert(layers[pos]);
  48. if (layers[pos]->getNameL() == fName) return pos;
  49. }
  50. return -1;
  51. }
  52. int Scene::findLayer(const Layer* fLayer) const { start_func
  53. for (int pos = 0; pos < layerCount; ++pos) {
  54. assert(layers[pos]);
  55. if (layers[pos] == fLayer) return pos;
  56. }
  57. return -1;
  58. }
  59. const Layer* Scene::getLayer(int pos) const { start_func
  60. assert(pos >= 0);
  61. assert(pos < layerCount);
  62. return layers[pos];
  63. }
  64. Layer* Scene::getLayer(int pos) { start_func
  65. assert(pos >= 0);
  66. assert(pos < layerCount);
  67. return layers[pos];
  68. }
  69. void Scene::cacheLoad() throw_File { start_func
  70. if (cached) {
  71. // load into memory
  72. assert(cacheFile);
  73. assert(cacheFile->getVersion() <= 1);
  74. cacheFile->rewind();
  75. for (int pos = 0; pos < layerCount; ++pos) {
  76. assert(layers[pos]);
  77. try {
  78. layers[pos]->loadContent(cacheFile);
  79. }
  80. catch (...) {
  81. for (--pos; pos >= 0; --pos) {
  82. layers[pos]->recacheContent();
  83. }
  84. throw;
  85. }
  86. }
  87. // We retain this pointer and reuse it IF we
  88. // unlock to 0 AND aren't modified
  89. cached = 0;
  90. }
  91. }
  92. void Scene::loadHeader(FileRead* file) throw_File { start_func
  93. assert(world);
  94. if (file->getVersion() > 1) {
  95. throw FileException("Unsupported scene version %d", file->getVersion());
  96. }
  97. file->readStr(name);
  98. nameL = name;
  99. toLower(nameL);
  100. layerCount = file->readInt();
  101. id = file->readInt();
  102. for (int pos = 0; pos < layerCount; ++pos) {
  103. layers[pos] = new Layer(world, this);
  104. layers[pos]->loadHeader(file);
  105. }
  106. // Check validity
  107. if ((layerCount > MAX_LAYERS) || (layerCount < 0) || (!id)) {
  108. throw FileException("Corrupted scene header");
  109. }
  110. }
  111. void Scene::loadContent(FileRead* file) { start_func
  112. delete cacheFile;
  113. cacheFile = file;
  114. cached = 1;
  115. }
  116. int Scene::isContentCached() const { start_func
  117. return cached;
  118. }
  119. int Scene::doLock(int play) throw_File { start_func
  120. cacheLoad();
  121. for (int pos = 0; pos < layerCount; ++pos) {
  122. assert(layers[pos]);
  123. // @TODO: throw_File
  124. if (play) layers[pos]->markLockPlay();
  125. else layers[pos]->markLock();
  126. }
  127. if (!lockCount) {
  128. EntityOrder::iterator end = entitysByOrder.end();
  129. for (EntityOrder::iterator pos = entitysByOrder.begin(); pos != end; ++pos) {
  130. (*pos)->setActive();
  131. }
  132. }
  133. return ++lockCount;
  134. }
  135. int Scene::doUnlock(int play) throw_File { start_func
  136. --lockCount;
  137. assert(lockCount >= 0);
  138. if (!lockCount) {
  139. EntityOrder::iterator end = entitysByOrder.end();
  140. for (EntityOrder::iterator pos = entitysByOrder.begin(); pos != end; ++pos) {
  141. (*pos)->setInactive();
  142. }
  143. }
  144. for (int pos = 0; pos < layerCount; ++pos) {
  145. assert(layers[pos]);
  146. if (play) layers[pos]->markUnlockPlay();
  147. else layers[pos]->markUnlock();
  148. // Recache ourselves
  149. if ((lockCount == 0) && (cacheFile)) layers[pos]->recacheContent();
  150. }
  151. if ((lockCount == 0) && (cacheFile)) {
  152. // Recache ourselves
  153. cached = 1;
  154. }
  155. return lockCount;
  156. }
  157. int Scene::markLock() throw_File { start_func
  158. return doLock(0);
  159. }
  160. int Scene::markUnlock() { start_func
  161. return doUnlock(0);
  162. }
  163. int Scene::markLockPlay() throw_File { start_func
  164. return doLock(1);
  165. }
  166. int Scene::markUnlockPlay() { start_func
  167. return doUnlock(1);
  168. }
  169. int Scene::isLocked() const { start_func
  170. return lockCount;
  171. }
  172. void Scene::draw() { start_func
  173. for (int pos = layerCount - 1; pos >= 0; --pos) {
  174. assert(layers[pos]);
  175. layers[pos]->draw(vpX, vpY);
  176. }
  177. }
  178. void Scene::spawnScene() { start_func
  179. for (int pos = 0; pos < layerCount; ++pos) {
  180. assert(layers[pos]);
  181. layers[pos]->spawnLayer();
  182. }
  183. }
  184. void Scene::indexEntity(Entity* addEntity) { start_func
  185. assert(addEntity);
  186. WorldPlay* wp = dynamic_cast<WorldPlay*>(world);
  187. assert(wp);
  188. wp->indexEntity(addEntity);
  189. orderEntity(addEntity);
  190. nameEntity(addEntity);
  191. if (lockCount) addEntity->setActive();
  192. }
  193. void Scene::deindexEntity(Entity* remEntity) { start_func
  194. assert(remEntity);
  195. WorldPlay* wp = dynamic_cast<WorldPlay*>(world);
  196. assert(wp);
  197. if (lockCount) remEntity->setInactive();
  198. wp->deindexEntity(remEntity);
  199. deorderEntity(remEntity);
  200. denameEntity(remEntity);
  201. }
  202. void Scene::orderEntity(Entity* addEntity) { start_func
  203. assert(addEntity);
  204. assert(entitysByOrder.find(addEntity) == entitysByOrder.end());
  205. entitysByOrder.insert(addEntity);
  206. }
  207. void Scene::deorderEntity(Entity* remEntity) { start_func
  208. assert(remEntity);
  209. entitysByOrder.erase(remEntity);
  210. }
  211. void Scene::nameEntity(Entity* addEntity) { start_func
  212. assert(addEntity);
  213. entitysByName.insert(pair<const char*, Entity*>(addEntity->getName()->c_str(), addEntity));
  214. }
  215. void Scene::denameEntity(Entity* remEntity) { start_func
  216. assert(remEntity);
  217. // Have to search range for matching entity
  218. pair<EntityMap::iterator, EntityMap::iterator> range = entitysByName.equal_range(remEntity->getName()->c_str());
  219. for (EntityMap::iterator pos = range.first; pos != range.second; ++pos) {
  220. if ((*pos).second == remEntity) {
  221. entitysByName.erase(pos);
  222. return;
  223. }
  224. }
  225. }
  226. void Scene::matchEntity(const char* fMatch, EntityMap::const_iterator& first, EntityMap::const_iterator& last) const { start_func
  227. assert(fMatch);
  228. // Optimization one- * or *something
  229. if (fMatch[0] == '*') {
  230. first = entitysByName.begin();
  231. last = entitysByName.end();
  232. return;
  233. }
  234. // Search for position of first *
  235. string toMatch = fMatch;
  236. string::size_type pos = toMatch.find('*');
  237. // Optimization two- exact match
  238. if (pos == string::npos) {
  239. pair<EntityMap::const_iterator, EntityMap::const_iterator> range = entitysByName.equal_range(toMatch.c_str());
  240. first = range.first;
  241. last = range.second;
  242. return;
  243. }
  244. // We need to determine range
  245. // First in range is portion before wildcard
  246. // Last in range is portion before wildcard with last character lexically incremented
  247. assert(pos > 0);
  248. toMatch = toMatch.substr(0, pos);
  249. first = entitysByName.lower_bound(toMatch.c_str());
  250. ++toMatch[pos - 1];
  251. last = entitysByName.lower_bound(toMatch.c_str());
  252. }
  253. void Scene::cycle() { start_func
  254. EntityOrder::iterator end = entitysByOrder.end();
  255. for (EntityOrder::iterator pos = entitysByOrder.begin(); pos != end; ++pos) {
  256. (*pos)->cycle();
  257. }
  258. }