CCursorHandler.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "../stdafx.h"
  2. #include "CCursorHandler.h"
  3. #include "SDL.h"
  4. #include "SDL_Extensions.h"
  5. #include "CGameInfo.h"
  6. #include "../hch/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. }
  59. void CCursorHandler::draw2()
  60. {
  61. if(!Show) return;
  62. int x = xpos, y = ypos;
  63. shiftPos(x, y);
  64. blitAt(help,x,y);
  65. }
  66. void CCursorHandler::shiftPos( int &x, int &y )
  67. {
  68. if((mode==1 && number!=6) || mode ==3)
  69. {
  70. x-=16;
  71. y-=16;
  72. // Properly align the melee attack cursors.
  73. if (mode == 1)
  74. {
  75. switch (number)
  76. {
  77. case 7: // Bottom left
  78. x -= 6;
  79. y += 16;
  80. break;
  81. case 8: // Left
  82. x -= 16;
  83. y += 10;
  84. break;
  85. case 9: // Top left
  86. x -= 6;
  87. y -= 6;
  88. break;
  89. case 10: // Top right
  90. x += 16;
  91. y -= 6;
  92. break;
  93. case 11: // Right
  94. x += 16;
  95. y += 11;
  96. break;
  97. case 12: // Bottom right
  98. x += 16;
  99. y += 16;
  100. break;
  101. case 13: // Below
  102. x += 9;
  103. y += 16;
  104. break;
  105. case 14: // Above
  106. x += 9;
  107. y -= 15;
  108. break;
  109. }
  110. }
  111. }
  112. else if(mode==0)
  113. {
  114. if(number == 0); //to exclude
  115. else if(number == 2)
  116. {
  117. x -= 12;
  118. y -= 10;
  119. }
  120. else if(number == 3)
  121. {
  122. x -= 12;
  123. y -= 12;
  124. }
  125. else if(number < 27)
  126. {
  127. int hlpNum = (number - 4)%6;
  128. if(hlpNum == 0)
  129. {
  130. x -= 15;
  131. y -= 13;
  132. }
  133. else if(hlpNum == 1)
  134. {
  135. x -= 13;
  136. y -= 13;
  137. }
  138. else if(hlpNum == 2)
  139. {
  140. x -= 20;
  141. y -= 20;
  142. }
  143. else if(hlpNum == 3)
  144. {
  145. x -= 13;
  146. y -= 16;
  147. }
  148. else if(hlpNum == 4)
  149. {
  150. x -= 8;
  151. y -= 9;
  152. }
  153. else if(hlpNum == 5)
  154. {
  155. x -= 14;
  156. y -= 16;
  157. }
  158. }
  159. else if(number < 31)
  160. {
  161. x -= 20;
  162. y -= 20;
  163. }
  164. }
  165. }
  166. CCursorHandler::~CCursorHandler()
  167. {
  168. if(help)
  169. SDL_FreeSurface(help);
  170. for(int g=0; g<cursors.size(); ++g)
  171. delete cursors[g];
  172. }