CBattleInterface.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "CBattleInterface.h"
  2. #include "CGameInfo.h"
  3. #include "hch\CLodHandler.h"
  4. #include "SDL_Extensions.h"
  5. #include "CAdvmapInterface.h"
  6. #include "AdventureMapButton.h"
  7. #include "hch\CHeroHandler.h"
  8. #include "hch\CDefHandler.h"
  9. extern SDL_Surface * screen;
  10. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2) : printCellBorders(true)
  11. {
  12. //preparing menu background and terrain
  13. std::vector< std::string > & backref = CGI->mh->battleBacks[ CGI->mh->ttiles[tile.x][tile.y][tile.z].terType ];
  14. background = CGI->bitmaph->loadBitmap(backref[ rand() % backref.size()] );
  15. menu = CGI->bitmaph->loadBitmap("CBAR.BMP");
  16. CSDL_Ext::blueToPlayersAdv(menu, hero1->tempOwner);
  17. //blitting menu background and terrain
  18. blitAt(background, 0, 0);
  19. blitAt(menu, 0, 556);
  20. CSDL_Ext::update();
  21. //preparing buttons
  22. bOptions = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bOptionsf, 3, 561, "icm003.def", this, false, NULL, false);
  23. bSurrender = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bSurrenderf, 54, 561, "icm001.def", this, false, NULL, false);
  24. bFlee = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bFleef, 105, 561, "icm002.def", this, false, NULL, false);
  25. bAutofight = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bAutofightf, 157, 561, "icm004.def", this, false, NULL, false);
  26. bSpell = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bSpellf, 645, 561, "icm005.def", this, false, NULL, false);
  27. bWait = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bWaitf, 696, 561, "icm006.def", this, false, NULL, false);
  28. bDefence = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bDefencef, 747, 561, "icm007.def", this, false, NULL, false);
  29. bConsoleUp = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bConsoleUpf, 624, 561, "ComSlide.def", this, false, NULL, false);
  30. bConsoleDown = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bConsoleDownf, 624, 580, "ComSlide.def", this, false, NULL, false);
  31. bConsoleDown->bitmapOffset = 2;
  32. //loading hero animations
  33. if(hero1) // attacking hero
  34. {
  35. attackingHero = new CBattleHero(CGI->mh->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  36. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  37. }
  38. else
  39. {
  40. attackingHero = NULL;
  41. }
  42. if(hero2) // defending hero
  43. {
  44. defendingHero = new CBattleHero(CGI->mh->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  45. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  46. }
  47. else
  48. {
  49. defendingHero = NULL;
  50. }
  51. //preparing cells and hexes
  52. cellBorder = CGI->bitmaph->loadBitmap("CCELLGRD.BMP");
  53. cellBorder = CSDL_Ext::alphaTransform(cellBorder);
  54. cellShade = CGI->bitmaph->loadBitmap("CCELLSHD.BMP");
  55. cellShade = CSDL_Ext::alphaTransform(cellShade);
  56. }
  57. CBattleInterface::~CBattleInterface()
  58. {
  59. SDL_FreeSurface(background);
  60. SDL_FreeSurface(menu);
  61. delete bOptions;
  62. delete bSurrender;
  63. delete bFlee;
  64. delete bAutofight;
  65. delete bSpell;
  66. delete bWait;
  67. delete bDefence;
  68. delete bConsoleUp;
  69. delete bConsoleDown;
  70. delete attackingHero;
  71. delete defendingHero;
  72. SDL_FreeSurface(cellBorder);
  73. SDL_FreeSurface(cellShade);
  74. }
  75. void CBattleInterface::activate()
  76. {
  77. bOptions->activate();
  78. bSurrender->activate();
  79. bFlee->activate();
  80. bAutofight->activate();
  81. bSpell->activate();
  82. bWait->activate();
  83. bDefence->activate();
  84. bConsoleUp->activate();
  85. bConsoleDown->activate();
  86. }
  87. void CBattleInterface::deactivate()
  88. {
  89. bOptions->deactivate();
  90. bSurrender->deactivate();
  91. bFlee->deactivate();
  92. bAutofight->deactivate();
  93. bSpell->deactivate();
  94. bWait->deactivate();
  95. bDefence->deactivate();
  96. bConsoleUp->deactivate();
  97. bConsoleDown->deactivate();
  98. }
  99. void CBattleInterface::show(SDL_Surface * to)
  100. {
  101. if(!to) //"evaluating" to
  102. to = screen;
  103. //showing background
  104. blitAt(background, 0, 0, to);
  105. if(printCellBorders) //printing cell borders
  106. {
  107. for(int i=0; i<11; ++i) //rows
  108. {
  109. for(int j=0; j<15; ++j) //columns
  110. {
  111. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  112. int y = 86 + 42 * i;
  113. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorder, NULL, to, &genRect(cellBorder->h, cellBorder->w, x, y));
  114. }
  115. }
  116. }
  117. //showing menu background
  118. blitAt(menu, 0, 556, to);
  119. //showing buttons
  120. bOptions->show(to);
  121. bSurrender->show(to);
  122. bFlee->show(to);
  123. bAutofight->show(to);
  124. bSpell->show(to);
  125. bWait->show(to);
  126. bDefence->show(to);
  127. bConsoleUp->show(to);
  128. bConsoleDown->show(to);
  129. //showing hero animations
  130. attackingHero->show(to);
  131. defendingHero->show(to);
  132. CSDL_Ext::update();
  133. }
  134. void CBattleInterface::bOptionsf()
  135. {
  136. }
  137. void CBattleInterface::bSurrenderf()
  138. {
  139. }
  140. void CBattleInterface::bFleef()
  141. {
  142. }
  143. void CBattleInterface::bAutofightf()
  144. {
  145. }
  146. void CBattleInterface::bSpellf()
  147. {
  148. }
  149. void CBattleInterface::bWaitf()
  150. {
  151. }
  152. void CBattleInterface::bDefencef()
  153. {
  154. }
  155. void CBattleInterface::bConsoleUpf()
  156. {
  157. }
  158. void CBattleInterface::bConsoleDownf()
  159. {
  160. }
  161. void CBattleHero::show(SDL_Surface *to)
  162. {
  163. int tick=-1;
  164. for(int i=0; i<dh->ourImages.size(); ++i)
  165. {
  166. if(dh->ourImages[i].groupNumber==phase)
  167. ++tick;
  168. if(tick==image)
  169. {
  170. SDL_Rect posb = pos;
  171. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  172. ++image;
  173. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  174. {
  175. image = 0;
  176. }
  177. break;
  178. }
  179. }
  180. if(flip)
  181. {
  182. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  183. }
  184. else
  185. {
  186. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  187. }
  188. ++flagAnimCount;
  189. if(flagAnimCount%4==0)
  190. {
  191. ++flagAnim;
  192. flagAnim %= flag->ourImages.size();
  193. }
  194. }
  195. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  196. {
  197. dh = CGI->spriteh->giveDef( defName );
  198. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  199. {
  200. if(flip)
  201. dh->ourImages[i].bitmap = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  202. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  203. }
  204. dh->alphaTransformed = true;
  205. if(flip)
  206. flag = CGI->spriteh->giveDef("CMFLAGR.DEF");
  207. else
  208. flag = CGI->spriteh->giveDef("CMFLAGL.DEF");
  209. for(int i=0; i<flag->ourImages.size(); ++i)
  210. {
  211. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  212. CSDL_Ext::blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  213. }
  214. }
  215. CBattleHero::~CBattleHero()
  216. {
  217. delete dh;
  218. delete flag;
  219. }