Mini Map.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. namespace Game{
  5. /******************************************************************************/
  6. void MiniMap::Settings::del()
  7. {
  8. image_size =0;
  9. areas_per_image=1; // use 1 instead of 0 to avoid div by 0 which could cause crash on Web
  10. area_size =1; // use 1 instead of 0 to avoid div by 0 which could cause crash on Web
  11. }
  12. Bool MiniMap::Settings::save(File &f)C
  13. {
  14. f.cmpUIntV(0); // version
  15. f<<areas_per_image<<image_size<<area_size;
  16. return f.ok();
  17. }
  18. Bool MiniMap::Settings::load(File &f)
  19. {
  20. switch(f.decUIntV()) // version
  21. {
  22. case 0:
  23. {
  24. f>>areas_per_image>>image_size>>area_size;
  25. if(f.ok())return true;
  26. }break;
  27. }
  28. del(); return false;
  29. }
  30. Bool MiniMap::Settings::save(C Str &name)C
  31. {
  32. File f; if(f.writeTry(name)){if(save(f) && f.flush())return true; f.del(); FDelFile(name);}
  33. return false;
  34. }
  35. Bool MiniMap::Settings::load(C Str &name)
  36. {
  37. File f; if(f.readTry(name))return load(f);
  38. del(); return false;
  39. }
  40. /******************************************************************************/
  41. Bool MiniMap::Create(Image &image, C VecI2 &key, Ptr user)
  42. {
  43. MiniMap &mm=*(MiniMap*)user;
  44. if(mm.name().is())image.load(mm.name()+'/'+key);
  45. return true;
  46. }
  47. /******************************************************************************/
  48. MiniMap::MiniMap() : _map(Compare, MiniMap::Create, this)
  49. {
  50. _id.zero();
  51. }
  52. void MiniMap::del()
  53. {
  54. _map .del();
  55. _settings.del();
  56. _name .del();
  57. _id .zero();
  58. }
  59. /******************************************************************************/
  60. Bool MiniMap::load(C Str &name)
  61. {
  62. del();
  63. if(name.is())
  64. {
  65. if(!DecodeFileName(name, _id))_id.zero();
  66. _name=name; _name.tailSlash(false);
  67. if(!_settings.load(_name+"/Settings"))goto error;
  68. }
  69. return true;
  70. error:
  71. del(); return false;
  72. }
  73. void MiniMap::operator=(C Str &name) {if(!load(name))Exit(S+"Can't load Mini Map \""+name+"\"");}
  74. Bool MiniMap::load (C UID &id ) {return load(id.valid() ? _EncodeFileName(id) : null);}
  75. void MiniMap::operator=(C UID &id ) { T=(id.valid() ? _EncodeFileName(id) : null);}
  76. /******************************************************************************/
  77. void MiniMap::clear(C RectI *leave)
  78. {
  79. if(!leave)_map.del();else
  80. {
  81. REPA(_map) // check all elements (images), order is important
  82. if(!Cuts(_map.key(i), *leave)) // if it shouldn't be left (coordinates aren't in the 'leave' rectangle)
  83. _map.remove(i); // remove this element
  84. }
  85. }
  86. /******************************************************************************/
  87. }}
  88. /******************************************************************************/