autobitmap.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <tataki/api__tataki.h>
  2. #include "autobitmap.h"
  3. #include <bfc/assert.h>
  4. #define TIMER_ID_RESET 0x1664
  5. #ifdef DROP_BITMAP_ON_IDLE
  6. // these are in seconds
  7. #define DROP_MINDELAY 3
  8. #define DROP_MAXDELAY 15
  9. #define DROP_INITIALBUMP -5
  10. #define DROP_MINDELAYSINCELASTUSE 7
  11. #endif
  12. /*
  13. #ifdef _WIN32
  14. extern HINSTANCE hInstance;
  15. #endif
  16. */
  17. AutoSkinBitmap::AutoSkinBitmap(const wchar_t *_name)
  18. {
  19. bitmap = NULL;
  20. use = 0;
  21. id = 0;
  22. colorgroup = 0;
  23. name = 0;
  24. resamplingMode = RESAMPLING_MODE_NONE;
  25. #ifdef WIN32
  26. myInstance = 0;
  27. #endif
  28. #ifdef DROP_BITMAP_ON_IDLE
  29. lastuse = 0;
  30. #endif
  31. setBitmap(_name);
  32. }
  33. AutoSkinBitmap::~AutoSkinBitmap()
  34. {
  35. #ifdef DROP_BITMAP_ON_IDLE
  36. timerclient_killTimer(TIMER_ID_RESET);
  37. #endif
  38. if (bitmap) bitmap->Release();
  39. ASSERT(WASABI_API_SYSCB != NULL);
  40. WASABI_API_SYSCB->syscb_deregisterCallback(this);
  41. free(colorgroup);
  42. free(name);
  43. }
  44. const wchar_t *AutoSkinBitmap::setBitmap(const wchar_t *_name)
  45. {
  46. if (_name == NULL) return NULL;
  47. if (name == NULL || wcscmp(name, _name))
  48. {
  49. reset();
  50. free(name);
  51. name = _wcsdup(_name);
  52. }
  53. return name;
  54. }
  55. int AutoSkinBitmap::setBitmap(int _id)
  56. {
  57. if (_id == 0) return 0;
  58. if (_id != id)
  59. {
  60. reset();
  61. id = _id;
  62. }
  63. return id;
  64. }
  65. #ifdef _WIN32
  66. void AutoSkinBitmap::setHInstance(HINSTANCE hinstance)
  67. {
  68. myInstance = hinstance;
  69. }
  70. #endif
  71. void AutoSkinBitmap::reset()
  72. {
  73. if (bitmap) bitmap->Release(); bitmap = NULL;
  74. #ifdef DROP_BITMAP_ON_IDLE
  75. timerclient_killTimer(TIMER_ID_RESET);
  76. #endif
  77. }
  78. static int modrandom(int max)
  79. {
  80. int ret = AGAVE_API_RANDOM->GetPositiveNumber();
  81. ret %= max;
  82. return ret;
  83. }
  84. SkinBitmap *AutoSkinBitmap::getBitmap()
  85. {
  86. //FG ASSERT(name != NULL);
  87. if ((name == NULL || (name != NULL && *name == NULL)) && id == NULL) return NULL;
  88. if (bitmap == NULL)
  89. {
  90. if (name)
  91. {
  92. switch (resamplingMode)
  93. {
  94. case RESAMPLING_MODE_SUPERSAMPLING:
  95. bitmap = new HQSkinBitmap(name);
  96. break;
  97. case RESAMPLING_MODE_NONE:
  98. default:
  99. bitmap = new SkinBitmap(name);
  100. break;
  101. }
  102. }
  103. else
  104. {
  105. #ifdef WIN32
  106. switch (resamplingMode)
  107. {
  108. case RESAMPLING_MODE_SUPERSAMPLING:
  109. bitmap = new HQSkinBitmap(myInstance, id, colorgroup);
  110. break;
  111. case RESAMPLING_MODE_NONE:
  112. default:
  113. bitmap = new SkinBitmap(myInstance, id, colorgroup);
  114. break;
  115. }
  116. #endif
  117. }
  118. ASSERT(WASABI_API_SYSCB != NULL);
  119. if (bitmap)
  120. WASABI_API_SYSCB->syscb_registerCallback(this);
  121. #ifdef DROP_BITMAP_ON_IDLE
  122. if (bitmap)
  123. {
  124. lastuse = GetTickCount() + DROP_INITIALBUMP;
  125. timerclient_setTimer(TIMER_ID_RESET, DROP_MINDELAY*1000 + modrandom((DROP_MAXDELAY - DROP_MINDELAY)*1000));
  126. return bitmap;
  127. }
  128. #endif
  129. }
  130. #ifdef DROP_BITMAP_ON_IDLE
  131. if (bitmap) lastuse = GetTickCount();
  132. #endif
  133. return bitmap;
  134. }
  135. const wchar_t *AutoSkinBitmap::getBitmapName()
  136. {
  137. return name;
  138. }
  139. int AutoSkinBitmap::skincb_onReset()
  140. {
  141. reset();
  142. return 1;
  143. }
  144. void AutoSkinBitmap::setHInstanceBitmapColorGroup(const wchar_t *_colorgroup)
  145. {
  146. free(colorgroup);
  147. if (_colorgroup)
  148. colorgroup = _wcsdup(_colorgroup);
  149. else
  150. colorgroup=0;
  151. }
  152. #ifdef DROP_BITMAP_ON_IDLE
  153. void AutoSkinBitmap::timerclient_timerCallback(int id)
  154. {
  155. if (id == TIMER_ID_RESET)
  156. {
  157. tryUnload();
  158. }
  159. else TimerClientDI::timerclient_timerCallback(id);
  160. }
  161. void AutoSkinBitmap::tryUnload()
  162. {
  163. DWORD now = GetTickCount();
  164. if (now < lastuse + DROP_MINDELAYSINCELASTUSE*1000) return ;
  165. reset();
  166. }
  167. #endif
  168. void AutoSkinBitmap::setResamplingMode(int mode)
  169. {
  170. this->resamplingMode = mode;
  171. this->reset();
  172. }
  173. int AutoSkinBitmap::getResamplingMode()
  174. {
  175. return this->resamplingMode;
  176. }
  177. #define CBCLASS AutoSkinBitmap
  178. START_DISPATCH;
  179. CB(SYSCALLBACK_GETEVENTTYPE, getEventType);
  180. CB(SYSCALLBACK_NOTIFY, notify);
  181. END_DISPATCH;
  182. #undef CBCLASS