Resource.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. Resource *Resource::getThis()
  78. {
  79. ref++;
  80. return this;
  81. }
  82. Resource *Resource::release()
  83. {
  84. if( !--ref )
  85. delete this;
  86. return 0;
  87. }
  88. ResourceRegistry::ResourceRegistry( Text spielPfad, Text mapPfad )
  89. {
  90. this->spielPfad = spielPfad;
  91. this->mapPfad = mapPfad;
  92. ref = 1;
  93. }
  94. Resource *ResourceRegistry::getResource( ResourceIds id, int color, ColorMode *mode, Text path )
  95. {
  96. Resource *r = zResource( id, color, mode, path );
  97. return r ? r->getThis() : 0;
  98. }
  99. Resource *ResourceRegistry::zResource( ResourceIds id, int color, ColorMode *mode, Text path )
  100. {
  101. path.ersetzen( "map:", mapPfad );
  102. path.ersetzen( "spiel:", spielPfad );
  103. for( auto r = resources.getIterator(); r; r++ )
  104. {
  105. if( r->getId() == id && r->getColor() == color )
  106. {
  107. mode->release();
  108. return r;
  109. }
  110. }
  111. if( !mode )
  112. return 0;
  113. for( auto r = resources.getIterator(); r; r++ )
  114. {
  115. if( r->getId() == id )
  116. {
  117. Resource *nr = r->createColoredResource( color, mode );
  118. resources.add( nr );
  119. return nr;
  120. }
  121. }
  122. if( path.hat( ".ltdb/" ) && path.positionVon( "/", path.anzahlVon( "/" ) - 1 ) == path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 )
  123. {
  124. LTDBDatei dat;
  125. dat.setDatei( path.getTeilText( 0, path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 ) );
  126. Bild *b = dat.laden( 0, path.getTeilText( path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 6 ) );
  127. if( b )
  128. {
  129. Resource *r = new Resource( id, color );
  130. resources.add( r );
  131. r->images.add( mode->colorImage( b, color ) );
  132. b->release();
  133. mode->release();
  134. return r;
  135. }
  136. mode->release();
  137. return 0;
  138. }
  139. LTDBDatei dat;
  140. dat.setDatei( path.getThis() );
  141. if( !dat.getBildAnzahl() )
  142. {
  143. mode->release();
  144. return 0;
  145. }
  146. Resource *r = new Resource( id, color );
  147. resources.add( r );
  148. for( int i = 0; i < dat.getBildAnzahl(); i++ )
  149. {
  150. Bild *b = dat.laden( 0, dat.zBildListe()->get( i ) );
  151. r->images.add( mode->colorImage( b, color ) );
  152. b->release();
  153. }
  154. mode->release();
  155. return r;
  156. }
  157. ResourceRegistry *ResourceRegistry::getThis()
  158. {
  159. ref++;
  160. return this;
  161. }
  162. ResourceRegistry *ResourceRegistry::release()
  163. {
  164. if( !--ref )
  165. delete this;
  166. return 0;
  167. }