Resource.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Bild *MaskColorMode::colorImage( Bild *zImg, int color )
  36. {
  37. Bild *result = ColorMode::colorImage( zImg, color );
  38. int *buffer = result->getBuffer();
  39. int size = result->getBreite() * result->getHeight();
  40. for( int i = 0; i < size; i++ )
  41. buffer[ i ] = ( buffer[ i ] & 0xFF000000 ) | ( color & 0xFFFFFF );
  42. return result;
  43. }
  44. Resource::Resource( ResourceIds id, int color )
  45. {
  46. this->id = id;
  47. this->color = color;
  48. ref = 1;
  49. }
  50. Resource *Resource::createColoredResource( int color, ColorMode *mode ) const
  51. {
  52. Resource *r = new Resource( id, color );
  53. for( auto i = images.getIterator(); i; i++ )
  54. r->images.add( mode->colorImage( i, color ) );
  55. mode->release();
  56. return r;
  57. }
  58. Iterator< Bild * > Resource::getImages() const
  59. {
  60. return images.getIterator();
  61. }
  62. ResourceIds Resource::getId() const
  63. {
  64. return id;
  65. }
  66. int Resource::getColor() const
  67. {
  68. return color;
  69. }
  70. Resource *Resource::getThis()
  71. {
  72. ref++;
  73. return this;
  74. }
  75. Resource *Resource::release()
  76. {
  77. if( !--ref )
  78. delete this;
  79. return 0;
  80. }
  81. ResourceRegistry::ResourceRegistry()
  82. {
  83. ref = 1;
  84. }
  85. Resource *ResourceRegistry::getResource( ResourceIds id, int color, ColorMode *mode, Text path )
  86. {
  87. Resource *r = zResource( id, color, mode, path );
  88. return r ? r->getThis() : 0;
  89. }
  90. Resource *ResourceRegistry::zResource( ResourceIds id, int color, ColorMode *mode, Text path = "" )
  91. {
  92. for( auto r = resources.getIterator(); r; r++ )
  93. {
  94. if( r->getId() == id && r->getColor() == color )
  95. {
  96. mode->release();
  97. return r;
  98. }
  99. }
  100. for( auto r = resources.getIterator(); r; r++ )
  101. {
  102. if( r->getId() == id )
  103. {
  104. Resource *nr = r->createColoredResource( color, mode );
  105. resources.add( nr );
  106. return nr;
  107. }
  108. }
  109. if( path.hat( ".ltdb/" ) && path.positionVon( "/", path.anzahlVon( "/" ) - 1 ) == path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 )
  110. {
  111. LTDBDatei dat;
  112. dat.setDatei( path.getTeilText( 0, path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 5 ) );
  113. Bild *b = dat.laden( 0, path.getTeilText( path.positionVon( ".ltdb/", path.anzahlVon( ".ltdb/" ) - 1 ) + 6 ) );
  114. if( b )
  115. {
  116. Resource *r = new Resource( id, color );
  117. resources.add( r );
  118. r->images.add( mode->colorImage( b, color ) );
  119. b->release();
  120. mode->release();
  121. return r;
  122. }
  123. mode->release();
  124. }
  125. Resource *r = new Resource( id, color );
  126. resources.add( r );
  127. LTDBDatei dat;
  128. dat.setDatei( path.getThis() );
  129. for( int i = 0; i < dat.getBildAnzahl(); i++ )
  130. {
  131. Bild *b = dat.laden( 0, dat.zBildListe()->get( i ) );
  132. r->images.add( mode->colorImage( b, color ) );
  133. b->release();
  134. }
  135. mode->release();
  136. return r;
  137. }
  138. ResourceRegistry *ResourceRegistry::getThis()
  139. {
  140. ref++;
  141. return this;
  142. }
  143. ResourceRegistry *ResourceRegistry::release()
  144. {
  145. if( !--ref )
  146. delete this;
  147. return 0;
  148. }