CCursorHandler.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_BlitSurface(screen, &genRect(40,40,x,y), help, &genRect(40,40,0,0));
  54. // if (dndImage)
  55. // blitAt(dndImage, x - dndImage->w/2, y - dndImage->h/2);
  56. // else
  57. // blitAt(cursors[mode]->ourImages[number].bitmap,x,y);
  58. if (dndImage)
  59. SDL_BlitSurface(dndImage, NULL, screen, &genRect(40, 40, x - dndImage->w/2, y - dndImage->h/2));
  60. else
  61. SDL_BlitSurface(cursors[mode]->ourImages[number].bitmap, NULL, screen, &genRect(40,40,x,y));
  62. }
  63. void CCursorHandler::draw2()
  64. {
  65. if(!Show) return;
  66. int x = xpos, y = ypos;
  67. shiftPos(x, y);
  68. SDL_BlitSurface(help, NULL, screen, &genRect(40, 40, x, y));
  69. //blitAt(help,x,y);
  70. }
  71. void CCursorHandler::draw(SDL_Surface *to)
  72. {
  73. CSDL_Ext::blitSurface(cursors[mode]->ourImages[number].bitmap, 0, to, &genRect(40, 40, xpos, ypos));
  74. }
  75. void CCursorHandler::shiftPos( int &x, int &y )
  76. {
  77. if((mode==1 && number!=6) || mode ==3)
  78. {
  79. x-=16;
  80. y-=16;
  81. // Properly align the melee attack cursors.
  82. if (mode == 1)
  83. {
  84. switch (number)
  85. {
  86. case 7: // Bottom left
  87. x -= 6;
  88. y += 16;
  89. break;
  90. case 8: // Left
  91. x -= 16;
  92. y += 10;
  93. break;
  94. case 9: // Top left
  95. x -= 6;
  96. y -= 6;
  97. break;
  98. case 10: // Top right
  99. x += 16;
  100. y -= 6;
  101. break;
  102. case 11: // Right
  103. x += 16;
  104. y += 11;
  105. break;
  106. case 12: // Bottom right
  107. x += 16;
  108. y += 16;
  109. break;
  110. case 13: // Below
  111. x += 9;
  112. y += 16;
  113. break;
  114. case 14: // Above
  115. x += 9;
  116. y -= 15;
  117. break;
  118. }
  119. }
  120. }
  121. else if(mode==0)
  122. {
  123. if(number == 0); //to exclude
  124. else if(number == 2)
  125. {
  126. x -= 12;
  127. y -= 10;
  128. }
  129. else if(number == 3)
  130. {
  131. x -= 12;
  132. y -= 12;
  133. }
  134. else if(number < 27)
  135. {
  136. int hlpNum = (number - 4)%6;
  137. if(hlpNum == 0)
  138. {
  139. x -= 15;
  140. y -= 13;
  141. }
  142. else if(hlpNum == 1)
  143. {
  144. x -= 13;
  145. y -= 13;
  146. }
  147. else if(hlpNum == 2)
  148. {
  149. x -= 20;
  150. y -= 20;
  151. }
  152. else if(hlpNum == 3)
  153. {
  154. x -= 13;
  155. y -= 16;
  156. }
  157. else if(hlpNum == 4)
  158. {
  159. x -= 8;
  160. y -= 9;
  161. }
  162. else if(hlpNum == 5)
  163. {
  164. x -= 14;
  165. y -= 16;
  166. }
  167. }
  168. else if(number == 41)
  169. {
  170. x -= 14;
  171. y -= 16;
  172. }
  173. else if(number < 31 || number == 42)
  174. {
  175. x -= 20;
  176. y -= 20;
  177. }
  178. }
  179. }
  180. void CCursorHandler::centerCursor()
  181. {
  182. SDL_Surface *cursor = this->cursors[mode]->ourImages[number].bitmap;
  183. this->xpos = (screen->w / 2.) - (cursor->w / 2.);
  184. this->ypos = (screen->h / 2.) - (cursor->h / 2.);
  185. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  186. SDL_WarpMouse(this->xpos, this->ypos);
  187. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  188. }
  189. CCursorHandler::~CCursorHandler()
  190. {
  191. if(help)
  192. SDL_FreeSurface(help);
  193. for(int g=0; g<cursors.size(); ++g)
  194. delete cursors[g];
  195. }