CCursorHandler.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "../stdafx.h"
  2. #include "CCursorHandler.h"
  3. #include "SDL.h"
  4. #include "SDL_Extensions.h"
  5. #include "CGameInfo.h"
  6. #include "CDefHandler.h"
  7. /*
  8. * CCursorHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. extern SDL_Surface * screen;
  17. void CCursorHandler::initCursor()
  18. {
  19. mode = number = xpos = ypos = 0;
  20. dndImage = NULL;
  21. help = CSDL_Ext::newSurface(40,40);
  22. cursors.push_back(CDefHandler::giveDef("CRADVNTR.DEF"));
  23. cursors.push_back(CDefHandler::giveDef("CRCOMBAT.DEF"));
  24. cursors.push_back(CDefHandler::giveDef("CRDEFLT.DEF"));
  25. cursors.push_back(CDefHandler::giveDef("CRSPELL.DEF"));
  26. SDL_ShowCursor(SDL_DISABLE);
  27. }
  28. void CCursorHandler::changeGraphic(const int & type, const int & no)
  29. {
  30. mode = type;
  31. number = no;
  32. }
  33. /**
  34. * Replaces the cursor with a custom image.
  35. *
  36. * @param image Image to replace cursor with or NULL to use the normal
  37. * cursor.
  38. */
  39. void CCursorHandler::dragAndDropCursor(SDL_Surface* image)
  40. {
  41. dndImage = image;
  42. }
  43. void CCursorHandler::cursorMove(const int & x, const int & y)
  44. {
  45. xpos = x;
  46. ypos = y;
  47. }
  48. void CCursorHandler::draw1()
  49. {
  50. if(!Show) return;
  51. int x = xpos, y = ypos;
  52. shiftPos(x, y);
  53. SDL_Rect temp_rect1 = genRect(40,40,x,y);
  54. SDL_Rect temp_rect2 = genRect(40,40,0,0);
  55. SDL_BlitSurface(screen, &temp_rect1, help, &temp_rect2);
  56. // if (dndImage)
  57. // blitAt(dndImage, x - dndImage->w/2, y - dndImage->h/2);
  58. // else
  59. // blitAt(cursors[mode]->ourImages[number].bitmap,x,y);
  60. if (dndImage) {
  61. SDL_Rect temp_rect =genRect(40, 40, x - dndImage->w/2, y - dndImage->h/2);
  62. SDL_BlitSurface(dndImage, NULL, screen, &temp_rect);
  63. } else {
  64. SDL_Rect temp_rect = genRect(40,40,x,y);
  65. SDL_BlitSurface(cursors[mode]->ourImages[number].bitmap, NULL, screen, &temp_rect);
  66. }
  67. }
  68. void CCursorHandler::draw2()
  69. {
  70. if(!Show) return;
  71. int x = xpos, y = ypos;
  72. shiftPos(x, y);
  73. SDL_Rect temp_rect = genRect(40, 40, x, y);
  74. SDL_BlitSurface(help, NULL, screen, &temp_rect);
  75. //blitAt(help,x,y);
  76. }
  77. void CCursorHandler::draw(SDL_Surface *to)
  78. {
  79. SDL_Rect temp_rect = genRect(40, 40, xpos, ypos);
  80. CSDL_Ext::blitSurface(cursors[mode]->ourImages[number].bitmap, 0, to, &temp_rect);
  81. }
  82. void CCursorHandler::shiftPos( int &x, int &y )
  83. {
  84. if((mode==1 && number!=6) || mode ==3)
  85. {
  86. x-=16;
  87. y-=16;
  88. // Properly align the melee attack cursors.
  89. if (mode == 1)
  90. {
  91. switch (number)
  92. {
  93. case 7: // Bottom left
  94. x -= 6;
  95. y += 16;
  96. break;
  97. case 8: // Left
  98. x -= 16;
  99. y += 10;
  100. break;
  101. case 9: // Top left
  102. x -= 6;
  103. y -= 6;
  104. break;
  105. case 10: // Top right
  106. x += 16;
  107. y -= 6;
  108. break;
  109. case 11: // Right
  110. x += 16;
  111. y += 11;
  112. break;
  113. case 12: // Bottom right
  114. x += 16;
  115. y += 16;
  116. break;
  117. case 13: // Below
  118. x += 9;
  119. y += 16;
  120. break;
  121. case 14: // Above
  122. x += 9;
  123. y -= 15;
  124. break;
  125. }
  126. }
  127. }
  128. else if(mode==0)
  129. {
  130. if(number == 0); //to exclude
  131. else if(number == 2)
  132. {
  133. x -= 12;
  134. y -= 10;
  135. }
  136. else if(number == 3)
  137. {
  138. x -= 12;
  139. y -= 12;
  140. }
  141. else if(number < 27)
  142. {
  143. int hlpNum = (number - 4)%6;
  144. if(hlpNum == 0)
  145. {
  146. x -= 15;
  147. y -= 13;
  148. }
  149. else if(hlpNum == 1)
  150. {
  151. x -= 13;
  152. y -= 13;
  153. }
  154. else if(hlpNum == 2)
  155. {
  156. x -= 20;
  157. y -= 20;
  158. }
  159. else if(hlpNum == 3)
  160. {
  161. x -= 13;
  162. y -= 16;
  163. }
  164. else if(hlpNum == 4)
  165. {
  166. x -= 8;
  167. y -= 9;
  168. }
  169. else if(hlpNum == 5)
  170. {
  171. x -= 14;
  172. y -= 16;
  173. }
  174. }
  175. else if(number == 41)
  176. {
  177. x -= 14;
  178. y -= 16;
  179. }
  180. else if(number < 31 || number == 42)
  181. {
  182. x -= 20;
  183. y -= 20;
  184. }
  185. }
  186. }
  187. void CCursorHandler::centerCursor()
  188. {
  189. SDL_Surface *cursor = this->cursors[mode]->ourImages[number].bitmap;
  190. this->xpos = (screen->w / 2.) - (cursor->w / 2.);
  191. this->ypos = (screen->h / 2.) - (cursor->h / 2.);
  192. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  193. SDL_WarpMouse(this->xpos, this->ypos);
  194. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  195. }
  196. CCursorHandler::~CCursorHandler()
  197. {
  198. if(help)
  199. SDL_FreeSurface(help);
  200. for(int g=0; g<cursors.size(); ++g)
  201. delete cursors[g];
  202. }