Animation.cpp 8.8 KB

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