Resource.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Resource *ResourceRegistry::getResource( ResourceIds id, int color, ColorMode *mode, Text path )
  107. {
  108. Resource *r = zResource( id, color, mode, path );
  109. return r ? r->getThis() : 0;
  110. }
  111. Resource *ResourceRegistry::zResource( ResourceIds id, int color, ColorMode *mode, Text path )
  112. {
  113. path.ersetzen( "map:", mapPfad );
  114. path.ersetzen( "spiel:", spielPfad );
  115. for( auto r = resources.getIterator(); r; r++ )
  116. {
  117. if( r->getId() == id && r->getColor() == color )
  118. {
  119. mode->release();
  120. return r;
  121. }
  122. }
  123. if( !mode )
  124. return 0;
  125. for( auto r = resources.getIterator(); r; r++ )
  126. {
  127. if( r->getId() == id )
  128. {
  129. Resource *nr = r->createColoredResource( color, mode );
  130. resources.add( nr );
  131. return nr;
  132. }
  133. }
  134. if( path.hat( ".ltdb/" ) && path.positionVon( "/", path.anzahlVon( "/" ) - 1 ) == path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 )
  135. {
  136. LTDBDatei dat;
  137. dat.setDatei( path.getTeilText( 0, path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 ) );
  138. Bild *b = dat.laden( 0, path.getTeilText( path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 6 ) );
  139. if( b )
  140. {
  141. Resource *r = new Resource( id, color );
  142. resources.add( r );
  143. r->images.add( mode->colorImage( b, color ) );
  144. b->release();
  145. mode->release();
  146. return r;
  147. }
  148. mode->release();
  149. return 0;
  150. }
  151. LTDBDatei dat;
  152. dat.setDatei( path.getThis() );
  153. if( !dat.getBildAnzahl() )
  154. {
  155. mode->release();
  156. return 0;
  157. }
  158. Resource *r = new Resource( id, color );
  159. resources.add( r );
  160. for( int i = 0; i < dat.getBildAnzahl(); i++ )
  161. {
  162. Bild *b = dat.laden( 0, dat.zBildListe()->get( i ) );
  163. r->images.add( mode->colorImage( b, color ) );
  164. b->release();
  165. }
  166. mode->release();
  167. return r;
  168. }
  169. ResourceRegistry *ResourceRegistry::getThis()
  170. {
  171. ref++;
  172. return this;
  173. }
  174. ResourceRegistry *ResourceRegistry::release()
  175. {
  176. if( !--ref )
  177. delete this;
  178. return 0;
  179. }