CBattleInterface.cpp 8.1 KB

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