Animation.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include "Animation.h"
  2. #include "Bild.h"
  3. #include "DateiSystem.h"
  4. #include "Text.h"
  5. #include "InitDatei.h"
  6. #include "ToolTip.h"
  7. #include "Rahmen.h"
  8. using namespace Framework;
  9. // Inhalt der Animation2DData Klasse aus Animation.h
  10. // Konstruktor
  11. Animation2DData::Animation2DData()
  12. : ReferenceCounter(),
  13. bilder( 0 ),
  14. bildAnzahl( 0 ),
  15. fps( 0 ),
  16. wiederhohlen( 0 ),
  17. transparent( 0 )
  18. {}
  19. // Destruktor
  20. Animation2DData::~Animation2DData()
  21. {
  22. reset();
  23. }
  24. // nicht constant
  25. void Animation2DData::lock()
  26. {
  27. cs.lock();
  28. }
  29. void Animation2DData::unlock()
  30. {
  31. cs.unlock();
  32. }
  33. void Animation2DData::ladeAnimation( InitDatei *datei )
  34. {
  35. if( !datei )
  36. return;
  37. reset();
  38. int anz = datei->getWertAnzahl();
  39. lock();
  40. if( datei->wertExistiert( "fps" ) )
  41. {
  42. --anz;
  43. fps = TextZuInt( datei->zWert( "fps" )->getText(), 10 );
  44. }
  45. if( datei->wertExistiert( "wiederhohlen" ) )
  46. {
  47. --anz;
  48. wiederhohlen = datei->zWert( "wiederhohlen" )->istGleich( "true" );
  49. }
  50. if( datei->wertExistiert( "transparent" ) )
  51. {
  52. --anz;
  53. transparent = datei->zWert( "transparent" )->istGleich( "true" );
  54. }
  55. Bild **bilder = new Bild * [ anz ];
  56. int j = 0;
  57. for( int i = 0; i < anz; ++i )
  58. {
  59. bilder[ j ] = 0;
  60. if( datei->zName( i )->istGleich( "fps" ) ||
  61. datei->zName( i )->istGleich( "wiederhohlen" ) ||
  62. datei->zName( i )->istGleich( "transparent" ) )
  63. continue;
  64. Text pfad = datei->zWert( i )->getText();
  65. if( pfad.hat( ".ltdb/" ) && pfad.getLength() > 7 )
  66. {
  67. Text *name = pfad.getTeilText( pfad.positionVon( ".ltdb/", pfad.anzahlVon( ".ltdb/" ) - 1 ) + 6 );
  68. pfad.setText( pfad.getTeilText( 0, pfad.getLength() - name->getLength() - 1 ) );
  69. LTDBDatei *dat = new LTDBDatei();
  70. dat->setDatei( (Text *)pfad.getThis() );
  71. dat->leseDaten( 0 );
  72. bilder[ j ] = dat->laden( 0, name );
  73. dat->release();
  74. }
  75. ++j;
  76. }
  77. this->bilder = new Bild * [ bildAnzahl ];
  78. j = 0;
  79. for( int i = 0; i < anz; ++i )
  80. {
  81. if( !bilder[ i ] )
  82. ++j;
  83. else
  84. this->bilder[ i - j ] = bilder[ i ];
  85. }
  86. delete[] bilder;
  87. unlock();
  88. datei->release();
  89. }
  90. void Animation2DData::ladeAnimation( LTDBDatei *datei )
  91. {
  92. if( !datei )
  93. return;
  94. reset();
  95. datei->leseDaten( 0 );
  96. int anz = datei->getBildAnzahl();
  97. RCArray< Text > *list = datei->zBildListe();
  98. lock();
  99. Bild **bilder = new Bild * [ anz ];
  100. for( int i = 0; i < anz; ++i )
  101. {
  102. bilder[ i ] = datei->laden( 0, list->get( i ) );
  103. if( bilder[ i ] )
  104. ++bildAnzahl;
  105. }
  106. this->bilder = new Bild * [ bildAnzahl ];
  107. int j = 0;
  108. for( int i = 0; i < anz; ++i )
  109. {
  110. if( !bilder[ i ] )
  111. ++j;
  112. else
  113. this->bilder[ i - j ] = bilder[ i ];
  114. }
  115. delete[] bilder;
  116. unlock();
  117. datei->release();
  118. }
  119. void Animation2DData::setFPS( int fps )
  120. {
  121. this->fps = fps;
  122. }
  123. void Animation2DData::setWiederhohlend( bool wh )
  124. {
  125. wiederhohlen = wh;
  126. }
  127. void Animation2DData::setTransparent( bool trp )
  128. {
  129. transparent = trp;
  130. }
  131. void Animation2DData::reset()
  132. {
  133. lock();
  134. for( int i = 0; i < bildAnzahl; ++i )
  135. bilder[ i ] = (Bild *)bilder[ i ]->release();
  136. delete[] bilder;
  137. bilder = 0;
  138. bildAnzahl = 0;
  139. fps = 30;
  140. wiederhohlen = 0;
  141. transparent = 0;
  142. unlock();
  143. }
  144. // constant
  145. Bild *Animation2DData::getBild( int i ) const
  146. {
  147. return ( i >= 0 && i < bildAnzahl ) ? (Bild *)bilder[ i ]->getThis() : 0;
  148. }
  149. Bild *Animation2DData::zBild( int i ) const
  150. {
  151. return ( i >= 0 && i < bildAnzahl ) ? bilder[ i ] : 0;
  152. }
  153. int Animation2DData::getBildAnzahl() const
  154. {
  155. return bildAnzahl;
  156. }
  157. int Animation2DData::getFPS() const
  158. {
  159. return fps;
  160. }
  161. bool Animation2DData::istWiederhohlend() const
  162. {
  163. return wiederhohlen;
  164. }
  165. bool Animation2DData::istTransparent() const
  166. {
  167. return transparent;
  168. }
  169. // Inhalt der Animation2D Klasse aus Animation.h
  170. // Konstruktor
  171. Animation2D::Animation2D()
  172. : Zeichnung(),
  173. data( 0 ),
  174. jetzt( 0 ),
  175. ausgleich( 0 ),
  176. alpha( 0 ),
  177. maxAlpha( 255 ),
  178. rahmen( 0 ),
  179. ram( 0 ),
  180. aps( 255 * 60 ),
  181. sichtbar( 0 )
  182. {}
  183. // Destruktor
  184. Animation2D::~Animation2D()
  185. {
  186. if( data )
  187. data->release();
  188. if( ram )
  189. ram->release();
  190. }
  191. // nicht constant
  192. void Animation2D::setRahmen( bool ram )
  193. {
  194. rahmen = ram;
  195. }
  196. void Animation2D::setRahmenZ( Rahmen *ram )
  197. {
  198. if( this->ram )
  199. this->ram->release();
  200. this->ram = ram;
  201. }
  202. void Animation2D::setRahmenBreite( int br )
  203. {
  204. if( !ram )
  205. ram = new LRahmen();
  206. ram->setRamenBreite( br );
  207. }
  208. void Animation2D::setRahmenFarbe( int f )
  209. {
  210. if( !ram )
  211. ram = new LRahmen();
  212. ram->setFarbe( f );
  213. }
  214. void Animation2D::setAnimationDataZ( Animation2DData *data )
  215. {
  216. lockZeichnung();
  217. if( this->data )
  218. this->data->release();
  219. this->data = data;
  220. if( alpha )
  221. rend = 1;
  222. unlockZeichnung();
  223. }
  224. void Animation2D::setAlphaMaske( unsigned char alpha )
  225. {
  226. maxAlpha = alpha;
  227. }
  228. void Animation2D::setAPS( int aps )
  229. {
  230. this->aps = aps;
  231. }
  232. void Animation2D::setSichtbar( bool sichtbar )
  233. {
  234. this->sichtbar = sichtbar;
  235. }
  236. bool Animation2D::tick( double zeit )
  237. {
  238. lockZeichnung();
  239. if( !data || ( !alpha && !sichtbar ) )
  240. {
  241. bool ret = rend;
  242. rend = 0;
  243. unlockZeichnung();
  244. return ret;
  245. }
  246. if( sichtbar && alpha < maxAlpha )
  247. {
  248. if( alpha + aps * zeit >= maxAlpha )
  249. alpha = maxAlpha;
  250. else
  251. alpha = (unsigned char)( alpha + aps * zeit );
  252. rend = 1;
  253. }
  254. else if( !sichtbar && alpha > 0 )
  255. {
  256. if( alpha - aps * zeit <= 0 )
  257. alpha = 0;
  258. else
  259. alpha = (unsigned char)( alpha - aps * zeit );
  260. rend = 1;
  261. }
  262. ausgleich += zeit;
  263. int tmp = jetzt;
  264. data->lock();
  265. if( ausgleich >= 1.0 / data->getFPS() )
  266. {
  267. ausgleich -= 1.0 / data->getFPS();
  268. ++jetzt;
  269. if( jetzt >= data->getBildAnzahl() )
  270. {
  271. if( data->istWiederhohlend() )
  272. jetzt = 0;
  273. else
  274. jetzt = data->getBildAnzahl();
  275. }
  276. }
  277. data->unlock();
  278. if( tmp != jetzt )
  279. rend = 1;
  280. unlockZeichnung();
  281. return Zeichnung::tick( zeit );
  282. }
  283. void Animation2D::render( Bild &zRObj )
  284. {
  285. lockZeichnung();
  286. if( !data )
  287. {
  288. unlockZeichnung();
  289. return;
  290. }
  291. Zeichnung::render( zRObj );
  292. data->lock();
  293. if( data->zBild( jetzt ) )
  294. {
  295. zRObj.setAlpha( alpha );
  296. if( data->istTransparent() )
  297. zRObj.alphaBild( pos.x, pos.y, gr.x, gr.y, *data->zBild( jetzt ) );
  298. else
  299. zRObj.drawBild( pos.x, pos.y, gr.x, gr.y, *data->zBild( jetzt ) );
  300. if( ram && rahmen )
  301. {
  302. ram->setPosition( pos );
  303. ram->setSize( gr );
  304. ram->render( zRObj );
  305. }
  306. zRObj.releaseAlpha();
  307. }
  308. data->unlock();
  309. unlockZeichnung();
  310. }
  311. // constant
  312. Animation2DData *Animation2D::getAnimationData() const
  313. {
  314. return data ? (Animation2DData *)data->getThis() : 0;
  315. }
  316. Animation2DData *Animation2D::zAnimationData() const
  317. {
  318. return data;
  319. }
  320. bool Animation2D::istSichtbar() const
  321. {
  322. return sichtbar;
  323. }
  324. int Animation2D::getJetzt() const
  325. {
  326. return jetzt;
  327. }
  328. unsigned char Animation2D::getAlphaMaske() const
  329. {
  330. return maxAlpha;
  331. }
  332. bool Animation2D::hatRahmen() const
  333. {
  334. return rahmen;
  335. }
  336. Rahmen *Animation2D::getRahmen() const
  337. {
  338. return ram ? (Rahmen *)ram->getThis() : 0;
  339. }
  340. Rahmen *Animation2D::zRahmen() const
  341. {
  342. return ram;
  343. }
  344. int Animation2D::getRahmenBreite() const
  345. {
  346. return ram ? ram->getRBreite() : 0;
  347. }
  348. int Animation2D::getRahmenFarbe() const
  349. {
  350. return ram ? ram->getFarbe() : 0;
  351. }
  352. Zeichnung *Animation2D::dublizieren() const
  353. {
  354. Animation2D *ret = new Animation2D();
  355. ret->setPosition( pos );
  356. ret->setSize( gr );
  357. ret->setMausEreignisParameter( makParam );
  358. ret->setTastaturEreignisParameter( takParam );
  359. ret->setMausEreignis( mak );
  360. ret->setTastaturEreignis( tak );
  361. if( toolTip )
  362. ret->setToolTipZ( (ToolTip *)toolTip->dublizieren() );
  363. if( data )
  364. ret->setAnimationDataZ( (Animation2DData *)data->getThis() );
  365. ret->setAPS( aps );
  366. ret->setSichtbar( sichtbar );
  367. ret->setAlphaMaske( maxAlpha );
  368. ret->setRahmen( rahmen );
  369. if( ram )
  370. {
  371. ret->setRahmenBreite( ram->getRBreite() );
  372. ret->setRahmenFarbe( ram->getFarbe() );
  373. }
  374. return ret;
  375. }