2
0

CCursorHandler.cpp 3.8 KB

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