TexturList.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "TexturList.h"
  2. #include "Textur.h"
  3. #include "Text.h"
  4. using namespace Framework;
  5. int TexturList::id = 0;
  6. CRITICAL_SECTION TexturList::cs;
  7. // Inhalt der TexturList Klasse
  8. // Konstruktor
  9. TexturList::TexturList()
  10. {
  11. textures = new RCArray< Textur >();
  12. names = new RCArray< Text >();
  13. ref = 1;
  14. }
  15. // Destruktor
  16. TexturList::~TexturList()
  17. {
  18. textures->release();
  19. names->release();
  20. }
  21. // Fügt der Liste eine Textur hinzu
  22. // t: Die Textur
  23. // name: Der name, unter dem die Textur in der Liste gespeichert wird
  24. bool TexturList::addTextur( Textur *t, const char *name )
  25. {
  26. EnterCriticalSection( &cs );
  27. for( auto i = names->getArray(); i.set; i++ )
  28. {
  29. if( i.var->istGleich( name ) )
  30. {
  31. t->release();
  32. LeaveCriticalSection( &cs );
  33. return 0;
  34. }
  35. }
  36. t->id = id++;
  37. textures->add( t );
  38. names->add( new Text( name ) );
  39. LeaveCriticalSection( &cs );
  40. return 1;
  41. }
  42. // Entfernt eine Textur aus der Liste
  43. // name: Der Name der Textur
  44. void TexturList::removeTextur( const char *name )
  45. {
  46. EnterCriticalSection( &cs );
  47. int index = 0;
  48. for( auto i = names->getArray(); i.set; i++ )
  49. {
  50. if( i.var->istGleich( name ) )
  51. {
  52. names->remove( index );
  53. textures->remove( index );
  54. LeaveCriticalSection( &cs );
  55. return;
  56. }
  57. index++;
  58. }
  59. LeaveCriticalSection( &cs );
  60. }
  61. // Überprüft, ob unter einem bestimmten Namen eine Textur abgespeichert wurde
  62. // name: Der Name
  63. // return: true, wenn eine Textur mit dem Namen existiert
  64. bool TexturList::hatTextur( const char *name ) const
  65. {
  66. EnterCriticalSection( &cs );
  67. for( auto i = names->getArray(); i.set; i++ )
  68. {
  69. if( i.var->istGleich( name ) )
  70. {
  71. LeaveCriticalSection( &cs );
  72. return 1;
  73. }
  74. }
  75. LeaveCriticalSection( &cs );
  76. return 0;
  77. }
  78. // Gibt eine bestimmte Textur zurück
  79. // name: Der Name der Textur
  80. Textur *TexturList::getTextur( const char *name ) const
  81. {
  82. EnterCriticalSection( &cs );
  83. int index = 0;
  84. for( auto i = names->getArray(); i.set; i++ )
  85. {
  86. if( i.var->istGleich( name ) )
  87. {
  88. LeaveCriticalSection( &cs );
  89. return textures->get( index );
  90. }
  91. index++;
  92. }
  93. LeaveCriticalSection( &cs );
  94. return 0;
  95. }
  96. // Gibt eine bestimmte Textur zurück
  97. // id: Die Id der Textur
  98. Textur *TexturList::getTextur( int id ) const
  99. {
  100. EnterCriticalSection( &cs );
  101. for( auto i = textures->getArray(); i.set; i++ )
  102. {
  103. if( i.var->getId() == id )
  104. {
  105. LeaveCriticalSection( &cs );
  106. return i.var->getThis();
  107. }
  108. }
  109. LeaveCriticalSection( &cs );
  110. return 0;
  111. }
  112. // Gibt eine bestimmte Textur ohne erhöhten Reference Counter zurück
  113. // name: Der Name der Textur
  114. Textur *TexturList::zTextur( const char *name ) const
  115. {
  116. EnterCriticalSection( &cs );
  117. int index = 0;
  118. for( auto i = names->getArray(); i.set; i++ )
  119. {
  120. if( i.var->istGleich( name ) )
  121. {
  122. LeaveCriticalSection( &cs );
  123. return textures->z( index );
  124. }
  125. index++;
  126. }
  127. LeaveCriticalSection( &cs );
  128. return 0;
  129. }
  130. // Gibt eine bestimmte Textur ohne erhöhten Reference Counter zurück
  131. // id: Die Id der Textur
  132. Textur *TexturList::zTextur( int id ) const
  133. {
  134. EnterCriticalSection( &cs );
  135. for( auto i = textures->getArray(); i.set; i++ )
  136. {
  137. if( i.var->getId() == id )
  138. {
  139. LeaveCriticalSection( &cs );
  140. return i.var;
  141. }
  142. }
  143. LeaveCriticalSection( &cs );
  144. return 0;
  145. }
  146. // Erhöht den Reference Counting Zähler.
  147. // return: this.
  148. TexturList *TexturList::getThis()
  149. {
  150. ref++;
  151. return this;
  152. }
  153. // Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Zeichnung automatisch gelöscht.
  154. // return: 0.
  155. TexturList *TexturList::release()
  156. {
  157. ref--;
  158. if( !ref )
  159. delete this;
  160. return 0;
  161. }
  162. // statische Funktionen
  163. // Initialisiert statische private member. Wird vom Framework automatisch aufgerufen.
  164. void TexturList::init()
  165. {
  166. id = 0;
  167. InitializeCriticalSection( &cs );
  168. }
  169. // Löscht statische private member. Wird vom Framework automatisch aufgerufen.
  170. void TexturList::destroy()
  171. {
  172. DeleteCriticalSection( &cs );
  173. }