Material Palette.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. Int MaterialPalette::getMaterialIndex0(C MaterialPtr &material) {return Max(0, getMaterialIndex(material));}
  6. Int MaterialPalette::getMaterialIndex (C MaterialPtr &material)
  7. {
  8. if(InRange(_last, T) && T[_last]==material)return _last;
  9. if(!material && elms())return 0; // 0 is reserved to be null material (however return it only if it was already added to the palette, so 'operator[]' will not crash)
  10. Int find=T.find(material);
  11. if( find<0 && elms()<255) // limit to 255 because we're saving later number of elements as byte
  12. {
  13. if( !elms()){New(); if(!material)return 0;} // 0 is reserved to be null material
  14. find=elms(); add(material);
  15. }
  16. if(find>0)_last=find; // don't adjust '_last' for null because that one is easy to find
  17. return find;
  18. }
  19. /******************************************************************************/
  20. Int IDPalette::getIDIndex0(C UID &id) {return Max(0, getIDIndex(id));}
  21. Int IDPalette::getIDIndex (C UID &id)
  22. {
  23. if(InRange(_last, T) && T[_last]==id)return _last;
  24. if(!id.valid() && elms())return 0; // 0 is reserved to be 'UIDZero' id (return it only if it was added to the palette)
  25. Int find=T.find(id);
  26. if( find<0 && elms()<255) // limit to 255 because we're saving later number of elements as byte
  27. {
  28. if( !elms()){New(); if(!id.valid())return 0;} // 0 is reserved to be 'UIDZero' id
  29. find=elms(); add(id);
  30. }
  31. if(find>0)_last=find; // don't adjust '_last' for null because that one is easy to find
  32. return find;
  33. }
  34. /******************************************************************************/
  35. void MaterialPalette::remove(Int i)
  36. {
  37. if(i)super::remove(i, true);
  38. }
  39. /******************************************************************************/
  40. Bool MaterialPalette::clean(Bool is[256], Byte remap[256])
  41. {
  42. for(Int i=1; i<elms(); i++)if(!is[i])
  43. {
  44. remap[0]=0;
  45. for(Int i=1, next=1; i<elms(); i++)if( is[i])remap[i]=next++;else remap[i]=0;
  46. for(Int i=elms(); --i>=1 ; )if(!is[i])super::remove(i, true); // need to go from the end
  47. return true;
  48. }
  49. return false;
  50. }
  51. /******************************************************************************/
  52. Bool MaterialPalette::save(File &f, CChar *path)C
  53. {
  54. f.putMulti(Byte(1), Byte(elms())); // version
  55. FREPA(T)f.putAsset(T[i].id());
  56. return f.ok();
  57. }
  58. Bool MaterialPalette::load(File &f, CChar *path)
  59. {
  60. switch(f.decUIntV()) // version
  61. {
  62. case 1:
  63. {
  64. setNum(f.getByte());
  65. FREPA(T)super::operator[](i).require(f.getAssetID(), path);
  66. if(f.ok())return true;
  67. }break;
  68. case 0:
  69. {
  70. setNum(f.getByte());
  71. FREPA(T)super::operator[](i).require(f._getStr(), path);
  72. if(f.ok())return true;
  73. }break;
  74. }
  75. del(); return false;
  76. }
  77. Bool MaterialPalette::loadOld(File &f, CChar *path)
  78. {
  79. setNum(f.getInt());
  80. FREPA(T)super::operator[](i).require(f._getStr(), path);
  81. if(f.ok())return true;
  82. del(); return false;
  83. }
  84. /******************************************************************************/
  85. Bool IDPalette::save(File &f)C
  86. {
  87. f.cmpUIntV(0); // version
  88. if(saveRaw(f))
  89. return f.ok();
  90. return false;
  91. }
  92. Bool IDPalette::load(File &f)
  93. {
  94. switch(f.decUIntV()) // version
  95. {
  96. case 0:
  97. {
  98. if(loadRaw(f))
  99. if(f.ok())return true;
  100. }break;
  101. }
  102. del(); return false;
  103. }
  104. /******************************************************************************/
  105. }
  106. /******************************************************************************/