CCursorHandler.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "stdafx.h"
  2. #include "CCursorHandler.h"
  3. #include "SDL.h"
  4. #include "SDL_thread.h"
  5. #include "CGameInfo.h"
  6. #include "SDL_framerate.h"
  7. #include "CLodHandler.h"
  8. extern SDL_Surface * screen;
  9. /* Creates a new mouse cursor from an XPM */
  10. /* XPM */
  11. static const char *arrow[] = { //no cursor mode
  12. /* width height num_colors chars_per_pixel */
  13. " 32 32 3 1",
  14. /* colors */
  15. "X c #000000",
  16. ". c #ffffff",
  17. " c None",
  18. /* pixels */
  19. " ",
  20. " ",
  21. " ",
  22. " ",
  23. " ",
  24. " ",
  25. " ",
  26. " ",
  27. " ",
  28. " ",
  29. " ",
  30. " ",
  31. " ",
  32. " ",
  33. " ",
  34. " ",
  35. " ",
  36. " ",
  37. " ",
  38. " ",
  39. " ",
  40. " ",
  41. " ",
  42. " ",
  43. " ",
  44. " ",
  45. " ",
  46. " ",
  47. " ",
  48. " ",
  49. " ",
  50. " ",
  51. "0,0"
  52. };
  53. /* XPM */
  54. static const char *arrow2[] = { //normal cursor
  55. /* width height num_colors chars_per_pixel */
  56. " 32 32 3 1",
  57. /* colors */
  58. "X c #000000",
  59. ". c #ffffff",
  60. " c None",
  61. /* pixels */
  62. "X ",
  63. "XX ",
  64. "X.X ",
  65. "X..X ",
  66. "X...X ",
  67. "X....X ",
  68. "X.....X ",
  69. "X......X ",
  70. "X.......X ",
  71. "X........X ",
  72. "X.....XXXXX ",
  73. "X..X..X ",
  74. "X.X X..X ",
  75. "XX X..X ",
  76. "X X..X ",
  77. " X..X ",
  78. " X..X ",
  79. " X..X ",
  80. " XX ",
  81. " ",
  82. " ",
  83. " ",
  84. " ",
  85. " ",
  86. " ",
  87. " ",
  88. " ",
  89. " ",
  90. " ",
  91. " ",
  92. " ",
  93. " ",
  94. "0,0"
  95. };
  96. static SDL_Cursor *init_system_cursor(const char *image[])
  97. {
  98. int i, row, col;
  99. Uint8 data[4*32];
  100. Uint8 mask[4*32];
  101. int hot_x, hot_y;
  102. i = -1;
  103. for ( row=0; row<32; ++row ) {
  104. for ( col=0; col<32; ++col ) {
  105. if ( col % 8 ) {
  106. data[i] <<= 1;
  107. mask[i] <<= 1;
  108. } else {
  109. ++i;
  110. data[i] = mask[i] = 0;
  111. }
  112. switch (image[4+row][col]) {
  113. case 'X':
  114. data[i] |= 0x01;
  115. //k[i] |= 0x01;
  116. break;
  117. case '.':
  118. mask[i] |= 0x01;
  119. break;
  120. case ' ':
  121. break;
  122. }
  123. }
  124. }
  125. sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
  126. return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
  127. }
  128. //int cursorHandlerFunc(void * cursorHandler)
  129. //{
  130. // FPSmanager * cursorFramerateKeeper = new FPSmanager;
  131. // SDL_initFramerate(cursorFramerateKeeper);
  132. // SDL_setFramerate(cursorFramerateKeeper, 200);
  133. //
  134. // CCursorHandler * ch = (CCursorHandler *) cursorHandler;
  135. // while(true)
  136. // {
  137. // if(ch->xbef!=-1 && ch->ybef!=-1) //restore surface under cursor
  138. // {
  139. // blitAtWR(ch->behindCur, ch->xbef, ch->ybef);
  140. // }
  141. // ch->xbef = ch->xpos;
  142. // ch->ybef = ch->ypos;
  143. // //prepare part of surface to restore
  144. // SDL_BlitSurface(screen, &genRect(32, 32, ch->xpos, ch->ypos), ch->behindCur, NULL);
  145. // CSDL_Ext::update(ch->behindCur);
  146. //
  147. // //blit cursor
  148. // if(ch->curVisible)
  149. // {
  150. // switch(ch->mode)
  151. // {
  152. // case 0:
  153. // {
  154. // break;
  155. // }
  156. // case 1:
  157. // {
  158. // break;
  159. // }
  160. // case 2:
  161. // {
  162. // blitAtWR(ch->deflt->ourImages[ch->number].bitmap, ch->xpos, ch->ypos);
  163. // break;
  164. // }
  165. // }
  166. // }
  167. // SDL_framerateDelay(cursorFramerateKeeper);
  168. // //SDL_Delay(5); //to avoid great usage of CPU
  169. // }
  170. // return 0;
  171. //}
  172. void CCursorHandler::initCursor()
  173. {
  174. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  175. int rmask = 0xff000000;
  176. int gmask = 0x00ff0000;
  177. int bmask = 0x0000ff00;
  178. int amask = 0x000000ff;
  179. #else
  180. int rmask = 0x000000ff;
  181. int gmask = 0x0000ff00;
  182. int bmask = 0x00ff0000;
  183. int amask = 0xff000000;
  184. #endif
  185. curVisible = true;
  186. xpos = ypos = 0;
  187. behindCur = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, rmask, gmask, bmask, amask);
  188. xbef = ybef = 0;
  189. adventure = CGI->spriteh->giveDef("CRADVNTR.DEF");
  190. combat = CGI->spriteh->giveDef("CRCOMBAT.DEF");
  191. deflt = CGI->spriteh->giveDef("CRDEFLT.DEF");
  192. spell = CGI->spriteh->giveDef("CRSPELL.DEF");
  193. //SDL_SetCursor(init_system_cursor(arrow));
  194. //SDL_Thread * myth = SDL_CreateThread(&cursorHandlerFunc, this);
  195. }
  196. void CCursorHandler::changeGraphic(int type, int no)
  197. {
  198. mode = type;
  199. number = no;
  200. }
  201. void CCursorHandler::cursorMove(int x, int y)
  202. {
  203. xbef = xpos;
  204. ybef = ypos;
  205. xpos = x;
  206. ypos = y;
  207. }
  208. void CCursorHandler::hardwareCursor()
  209. {
  210. curVisible = false;
  211. SDL_SetCursor(init_system_cursor(arrow2));
  212. }
  213. void CCursorHandler::hideCursor()
  214. {
  215. curVisible = false;
  216. SDL_SetCursor(init_system_cursor(arrow));
  217. }
  218. void CCursorHandler::showGraphicCursor()
  219. {
  220. curVisible = true;
  221. SDL_SetCursor(init_system_cursor(arrow));
  222. changeGraphic(0, 0);
  223. }