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. extern SDL_Surface * screen;
  8. /* Creates a new mouse cursor from an XPM */
  9. /* XPM */
  10. static const char *arrow[] = { //no cursor mode
  11. /* width height num_colors chars_per_pixel */
  12. " 32 32 3 1",
  13. /* colors */
  14. "X c #000000",
  15. ". c #ffffff",
  16. " c None",
  17. /* pixels */
  18. " ",
  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. "0,0"
  51. };
  52. /* XPM */
  53. static const char *arrow2[] = { //normal cursor
  54. /* width height num_colors chars_per_pixel */
  55. " 32 32 3 1",
  56. /* colors */
  57. "X c #000000",
  58. ". c #ffffff",
  59. " c None",
  60. /* pixels */
  61. "X ",
  62. "XX ",
  63. "X.X ",
  64. "X..X ",
  65. "X...X ",
  66. "X....X ",
  67. "X.....X ",
  68. "X......X ",
  69. "X.......X ",
  70. "X........X ",
  71. "X.....XXXXX ",
  72. "X..X..X ",
  73. "X.X X..X ",
  74. "XX X..X ",
  75. "X X..X ",
  76. " X..X ",
  77. " X..X ",
  78. " X..X ",
  79. " XX ",
  80. " ",
  81. " ",
  82. " ",
  83. " ",
  84. " ",
  85. " ",
  86. " ",
  87. " ",
  88. " ",
  89. " ",
  90. " ",
  91. " ",
  92. " ",
  93. "0,0"
  94. };
  95. static SDL_Cursor *init_system_cursor(const char *image[])
  96. {
  97. int i, row, col;
  98. Uint8 data[4*32];
  99. Uint8 mask[4*32];
  100. int hot_x, hot_y;
  101. i = -1;
  102. for ( row=0; row<32; ++row ) {
  103. for ( col=0; col<32; ++col ) {
  104. if ( col % 8 ) {
  105. data[i] <<= 1;
  106. mask[i] <<= 1;
  107. } else {
  108. ++i;
  109. data[i] = mask[i] = 0;
  110. }
  111. switch (image[4+row][col]) {
  112. case 'X':
  113. data[i] |= 0x01;
  114. //k[i] |= 0x01;
  115. break;
  116. case '.':
  117. mask[i] |= 0x01;
  118. break;
  119. case ' ':
  120. break;
  121. }
  122. }
  123. }
  124. sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
  125. return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
  126. }
  127. //int cursorHandlerFunc(void * cursorHandler)
  128. //{
  129. // FPSmanager * cursorFramerateKeeper = new FPSmanager;
  130. // SDL_initFramerate(cursorFramerateKeeper);
  131. // SDL_setFramerate(cursorFramerateKeeper, 200);
  132. //
  133. // CCursorHandler * ch = (CCursorHandler *) cursorHandler;
  134. // while(true)
  135. // {
  136. // if(ch->xbef!=-1 && ch->ybef!=-1) //restore surface under cursor
  137. // {
  138. // blitAtWR(ch->behindCur, ch->xbef, ch->ybef);
  139. // }
  140. // ch->xbef = ch->xpos;
  141. // ch->ybef = ch->ypos;
  142. // //prepare part of surface to restore
  143. // SDL_BlitSurface(screen, &genRect(32, 32, ch->xpos, ch->ypos), ch->behindCur, NULL);
  144. // CSDL_Ext::update(ch->behindCur);
  145. //
  146. // //blit cursor
  147. // if(ch->curVisible)
  148. // {
  149. // switch(ch->mode)
  150. // {
  151. // case 0:
  152. // {
  153. // break;
  154. // }
  155. // case 1:
  156. // {
  157. // break;
  158. // }
  159. // case 2:
  160. // {
  161. // blitAtWR(ch->deflt->ourImages[ch->number].bitmap, ch->xpos, ch->ypos);
  162. // break;
  163. // }
  164. // }
  165. // }
  166. // SDL_framerateDelay(cursorFramerateKeeper);
  167. // //SDL_Delay(5); //to avoid great usage of CPU
  168. // }
  169. // return 0;
  170. //}
  171. void CCursorHandler::initCursor()
  172. {
  173. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  174. int rmask = 0xff000000;
  175. int gmask = 0x00ff0000;
  176. int bmask = 0x0000ff00;
  177. int amask = 0x000000ff;
  178. #else
  179. int rmask = 0x000000ff;
  180. int gmask = 0x0000ff00;
  181. int bmask = 0x00ff0000;
  182. int amask = 0xff000000;
  183. #endif
  184. curVisible = true;
  185. xpos = ypos = 0;
  186. behindCur = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32, rmask, gmask, bmask, amask);
  187. xbef = ybef = 0;
  188. std::vector<Entry> pom = CGI->spriteh->entries.vectorize();
  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. }