Resource.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "Resource.h"
  2. #include <DateiSystem.h>
  3. ColorMode::ColorMode()
  4. : ReferenceCounter()
  5. {}
  6. Bild *ColorMode::colorImage( Bild *zImg, int color )
  7. {
  8. Bild *result = new Bild();
  9. result->neuBild( zImg->getBreite(), zImg->getHeight(), 0 );
  10. result->drawBild( 0, 0, zImg->getBreite(), zImg->getHeight(), *zImg );
  11. return result;
  12. }
  13. AlphaColorMode::AlphaColorMode( unsigned char alpha )
  14. {
  15. this->alpha = alpha;
  16. }
  17. Bild *AlphaColorMode::colorImage( Bild *zImg, int color )
  18. {
  19. Bild *result = ColorMode::colorImage( zImg, color );
  20. result->alphaRegion( 0, 0, result->getBreite(), result->getHeight(), ( (int)alpha << 24 ) | ( color & 0xFFFFFF ) );
  21. return result;
  22. }
  23. MaskColorMode::MaskColorMode( int colorToReplace )
  24. {
  25. this->colorToReplace = colorToReplace;
  26. }
  27. Bild *MaskColorMode::colorImage( Bild *zImg, int color )
  28. {
  29. Bild *result = ColorMode::colorImage( zImg, color );
  30. int *buffer = result->getBuffer();
  31. int size = result->getBreite() * result->getHeight();
  32. for( int i = 0; i < size; i++ )
  33. {
  34. if( ( buffer[ i ] & 0xFFFFFF ) == colorToReplace )
  35. buffer[ i ] = ( buffer[ i ] & 0xFF000000 ) | ( color & 0xFFFFFF );
  36. }
  37. return result;
  38. }
  39. Resource::Resource( ResourceId id, int color )
  40. : ReferenceCounter()
  41. {
  42. this->id = id;
  43. this->color = color;
  44. }
  45. Resource *Resource::createColoredResource( int color, ColorMode *mode ) const
  46. {
  47. Resource *r = new Resource( id, color );
  48. for( auto i = images.getIterator(); i; i++ )
  49. r->images.add( mode->colorImage( i, color ) );
  50. mode->release();
  51. return r;
  52. }
  53. Iterator< Bild * > Resource::getImages() const
  54. {
  55. return images.getIterator();
  56. }
  57. ResourceId Resource::getId() const
  58. {
  59. return id;
  60. }
  61. int Resource::getColor() const
  62. {
  63. return color;
  64. }
  65. Bild *Resource::zImage( int id ) const
  66. {
  67. return images.z( id );
  68. }
  69. Bild *Resource::getImage( int id ) const
  70. {
  71. return images.get( id );
  72. }
  73. int Resource::getImageCount() const
  74. {
  75. return images.getEintragAnzahl();
  76. }
  77. ResourceRegistry::ResourceRegistry( Text spielPfad, Text mapPfad )
  78. : ReferenceCounter()
  79. {
  80. this->spielPfad = spielPfad;
  81. this->mapPfad = mapPfad;
  82. }
  83. ResourceRegistry::~ResourceRegistry()
  84. {}
  85. void ResourceRegistry::setUIFactory( UIInit &uiFactory )
  86. {
  87. this->uiFactory = uiFactory;
  88. }
  89. UIInit &ResourceRegistry::getUIFactory()
  90. {
  91. return uiFactory;
  92. }
  93. Resource *ResourceRegistry::getResource( ResourceId id, int color, ColorMode *mode, Text path )
  94. {
  95. Resource *r = zResource( id, color, mode, path );
  96. return r ? dynamic_cast<Resource *>( r->getThis() ) : 0;
  97. }
  98. Resource *ResourceRegistry::zResource( ResourceId id, int color, ColorMode *mode, Text path )
  99. {
  100. path.ersetzen( "map:", mapPfad );
  101. path.ersetzen( "spiel:", spielPfad );
  102. for( auto r = resources.getIterator(); r; r++ )
  103. {
  104. if( r->getId() == id && r->getColor() == color )
  105. {
  106. if( mode )
  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( dynamic_cast<Text *>( path.getThis() ) );
  141. dat.leseDaten( 0 );
  142. if( !dat.getBildAnzahl() )
  143. {
  144. mode->release();
  145. return 0;
  146. }
  147. Resource *r = new Resource( id, color );
  148. resources.add( r );
  149. for( int i = 0; i < dat.getBildAnzahl(); i++ )
  150. {
  151. Bild *b = dat.laden( 0, dat.zBildListe()->get( i ) );
  152. r->images.add( mode->colorImage( b, color ) );
  153. b->release();
  154. }
  155. mode->release();
  156. return r;
  157. }