Resource.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "Resource.h"
  2. #include <DateiSystem.h>
  3. ColorMode::ColorMode()
  4. {
  5. ref = 1;
  6. }
  7. Bild *ColorMode::colorImage( Bild *zImg, int color )
  8. {
  9. Bild *result = new Bild();
  10. result->neuBild( zImg->getBreite(), zImg->getHeight(), 0 );
  11. result->drawBild( 0, 0, zImg->getBreite(), zImg->getHeight(), *zImg );
  12. return result;
  13. }
  14. ColorMode *ColorMode::getThis()
  15. {
  16. ref++;
  17. return this;
  18. }
  19. ColorMode *ColorMode::release()
  20. {
  21. if( !--ref )
  22. delete this;
  23. return 0;
  24. }
  25. AlphaColorMode::AlphaColorMode( unsigned char alpha )
  26. {
  27. this->alpha = alpha;
  28. }
  29. Bild *AlphaColorMode::colorImage( Bild *zImg, int color )
  30. {
  31. Bild *result = ColorMode::colorImage( zImg, color );
  32. result->alphaRegion( 0, 0, result->getBreite(), result->getHeight(), ( (int)alpha << 24 ) | ( color & 0xFFFFFF ) );
  33. return result;
  34. }
  35. MaskColorMode::MaskColorMode( int colorToReplace )
  36. {
  37. this->colorToReplace = colorToReplace;
  38. }
  39. Bild *MaskColorMode::colorImage( Bild *zImg, int color )
  40. {
  41. Bild *result = ColorMode::colorImage( zImg, color );
  42. int *buffer = result->getBuffer();
  43. int size = result->getBreite() * result->getHeight();
  44. for( int i = 0; i < size; i++ )
  45. {
  46. if( ( buffer[ i ] & 0xFFFFFF ) == colorToReplace )
  47. buffer[ i ] = ( buffer[ i ] & 0xFF000000 ) | ( color & 0xFFFFFF );
  48. }
  49. return result;
  50. }
  51. Resource::Resource( ResourceIds id, int color )
  52. {
  53. this->id = id;
  54. this->color = color;
  55. ref = 1;
  56. }
  57. Resource *Resource::createColoredResource( int color, ColorMode *mode ) const
  58. {
  59. Resource *r = new Resource( id, color );
  60. for( auto i = images.getIterator(); i; i++ )
  61. r->images.add( mode->colorImage( i, color ) );
  62. mode->release();
  63. return r;
  64. }
  65. Iterator< Bild * > Resource::getImages() const
  66. {
  67. return images.getIterator();
  68. }
  69. ResourceIds Resource::getId() const
  70. {
  71. return id;
  72. }
  73. int Resource::getColor() const
  74. {
  75. return color;
  76. }
  77. Bild *Resource::zImage( int id ) const
  78. {
  79. return images.z( id );
  80. }
  81. Bild *Resource::getImage( int id ) const
  82. {
  83. return images.get( id );
  84. }
  85. int Resource::getImageCount() const
  86. {
  87. return images.getEintragAnzahl();
  88. }
  89. Resource *Resource::getThis()
  90. {
  91. ref++;
  92. return this;
  93. }
  94. Resource *Resource::release()
  95. {
  96. if( !--ref )
  97. delete this;
  98. return 0;
  99. }
  100. ResourceRegistry::ResourceRegistry( Text spielPfad, Text mapPfad )
  101. {
  102. this->spielPfad = spielPfad;
  103. this->mapPfad = mapPfad;
  104. ref = 1;
  105. }
  106. ResourceRegistry::~ResourceRegistry()
  107. {
  108. if( schrift )
  109. schrift->release();
  110. }
  111. void ResourceRegistry::setSchrift( Schrift *schrift )
  112. {
  113. if( this->schrift )
  114. this->schrift->release();
  115. this->schrift = schrift;
  116. }
  117. Schrift *ResourceRegistry::zSchrift() const
  118. {
  119. return schrift;
  120. }
  121. Schrift *ResourceRegistry::getSchrift() const
  122. {
  123. return schrift ? schrift->getThis() : 0;
  124. }
  125. Resource *ResourceRegistry::getResource( ResourceIds id, int color, ColorMode *mode, Text path )
  126. {
  127. Resource *r = zResource( id, color, mode, path );
  128. return r ? r->getThis() : 0;
  129. }
  130. Resource *ResourceRegistry::zResource( ResourceIds id, int color, ColorMode *mode, Text path )
  131. {
  132. path.ersetzen( "map:", mapPfad );
  133. path.ersetzen( "spiel:", spielPfad );
  134. for( auto r = resources.getIterator(); r; r++ )
  135. {
  136. if( r->getId() == id && r->getColor() == color )
  137. {
  138. if( mode )
  139. mode->release();
  140. return r;
  141. }
  142. }
  143. if( !mode )
  144. return 0;
  145. for( auto r = resources.getIterator(); r; r++ )
  146. {
  147. if( r->getId() == id )
  148. {
  149. Resource *nr = r->createColoredResource( color, mode );
  150. resources.add( nr );
  151. return nr;
  152. }
  153. }
  154. if( path.hat( ".ltdb/" ) && path.positionVon( "/", path.anzahlVon( "/" ) - 1 ) == path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 )
  155. {
  156. LTDBDatei dat;
  157. dat.setDatei( path.getTeilText( 0, path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 ) );
  158. Bild *b = dat.laden( 0, path.getTeilText( path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 6 ) );
  159. if( b )
  160. {
  161. Resource *r = new Resource( id, color );
  162. resources.add( r );
  163. r->images.add( mode->colorImage( b, color ) );
  164. b->release();
  165. mode->release();
  166. return r;
  167. }
  168. mode->release();
  169. return 0;
  170. }
  171. LTDBDatei dat;
  172. dat.setDatei( path.getThis() );
  173. dat.leseDaten( 0 );
  174. if( !dat.getBildAnzahl() )
  175. {
  176. mode->release();
  177. return 0;
  178. }
  179. Resource *r = new Resource( id, color );
  180. resources.add( r );
  181. for( int i = 0; i < dat.getBildAnzahl(); i++ )
  182. {
  183. Bild *b = dat.laden( 0, dat.zBildListe()->get( i ) );
  184. r->images.add( mode->colorImage( b, color ) );
  185. b->release();
  186. }
  187. mode->release();
  188. return r;
  189. }
  190. ResourceRegistry *ResourceRegistry::getThis()
  191. {
  192. ref++;
  193. return this;
  194. }
  195. ResourceRegistry *ResourceRegistry::release()
  196. {
  197. if( !--ref )
  198. delete this;
  199. return 0;
  200. }