CCastleInterface.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "stdafx.h"
  2. #include "CCastleInterface.h"
  3. #include "hch/CObjectHandler.h"
  4. #include "CGameInfo.h"
  5. #include "hch/CLodHandler.h"
  6. #include "SDL_Extensions.h"
  7. #include "CAdvmapInterface.h"
  8. #include "hch/CTownHandler.h"
  9. #include "AdventureMapButton.h"
  10. #include <sstream>
  11. CBuildingRect::CBuildingRect(Structure *Str)
  12. :str(Str)
  13. {
  14. def = CGI->spriteh->giveDef(Str->defName);
  15. if (border = CGI->bitmaph->loadBitmap(str->borderName))
  16. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  17. else
  18. std::cout << "Warning: no border for "<<Str->ID<<std::endl;
  19. if (area = CGI->bitmaph->loadBitmap(str->areaName))
  20. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  21. else
  22. std::cout << "Warning: no area for "<<Str->ID<<std::endl;
  23. pos.x = str->pos.x;
  24. pos.y = str->pos.y;
  25. pos.w = def->ourImages[0].bitmap->w;
  26. pos.h = def->ourImages[0].bitmap->h;
  27. }
  28. CBuildingRect::~CBuildingRect()
  29. {
  30. delete def;
  31. if(border)
  32. SDL_FreeSurface(border);
  33. if(area)
  34. SDL_FreeSurface(area);
  35. }
  36. void CBuildingRect::activate()
  37. {
  38. Hoverable::activate();
  39. ClickableL::activate();
  40. ClickableR::activate();
  41. }
  42. void CBuildingRect::deactivate()
  43. {
  44. Hoverable::deactivate();
  45. ClickableL::deactivate();
  46. ClickableR::deactivate();
  47. }
  48. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  49. {
  50. if(str->pos.z != p2.str->pos.z)
  51. return (str->pos.z) < (p2.str->pos.z);
  52. else
  53. return (str->ID) < (p2.str->ID);
  54. }
  55. void CBuildingRect::hover(bool on)
  56. {
  57. if(area)
  58. {
  59. if(CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->current->motion.y-pos.y) == 0)
  60. {
  61. Hoverable::hover(false);
  62. return;
  63. }
  64. }
  65. Hoverable::hover(on);
  66. if(border)
  67. blitAt(border,pos.x,pos.y);
  68. }
  69. void CBuildingRect::clickLeft (tribool down)
  70. {
  71. //todo - handle
  72. }
  73. void CBuildingRect::clickRight (tribool down)
  74. {
  75. //todo - handle
  76. }
  77. std::string getBgName(int type) //TODO - co z tym zrobiæ?
  78. {
  79. switch (type)
  80. {
  81. case 0:
  82. return "TBCSBACK.bmp";
  83. case 1:
  84. return "TBRMBACK.bmp";
  85. case 2:
  86. return "TBTWBACK.bmp";
  87. case 3:
  88. return "TBINBACK.bmp";
  89. case 4:
  90. return "TBNCBACK.bmp";
  91. case 5:
  92. return "TBDNBACK.bmp";
  93. case 6:
  94. return "TBSTBACK.bmp";
  95. case 7:
  96. return "TBFRBACK.bmp";
  97. case 8:
  98. return "TBELBACK.bmp";
  99. default:
  100. throw new std::exception("std::string getBgName(int type): invalid type");
  101. }
  102. }
  103. class SORTHELP
  104. {
  105. public:
  106. bool operator ()
  107. (const CBuildingRect *a ,
  108. const CBuildingRect *b)
  109. {
  110. return (*a)<(*b);
  111. }
  112. } srthlp ;
  113. CCastleInterface::CCastleInterface(const CGTownInstance * Town, bool Activate)
  114. {
  115. count=0;
  116. town = Town;
  117. townInt = CGI->bitmaph->loadBitmap("TOWNSCRN.bmp");
  118. cityBg = CGI->bitmaph->loadBitmap(getBgName(town->subID));
  119. hall = CGI->spriteh->giveDef("ITMTL.DEF");
  120. fort = CGI->spriteh->giveDef("ITMCL.DEF");
  121. bigTownPic = CGI->spriteh->giveDef("ITPT.DEF");
  122. flag = CGI->spriteh->giveDef("CREST58.DEF");
  123. CSDL_Ext::blueToPlayersAdv(townInt,LOCPLINT->playerID);
  124. exit = new AdventureMapButton<CCastleInterface>
  125. (CGI->townh->tcommands[8],"",&CCastleInterface::close,744,544,"TSBTNS.DEF",this,Activate);
  126. exit->bitmapOffset = 4;
  127. for (std::set<int>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  128. {
  129. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end())
  130. {
  131. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end())
  132. {
  133. //CDefHandler *b = CGI->spriteh->giveDef(CGI->townh->structures[town->subID][*i]->defName);
  134. buildings.push_back(new CBuildingRect(CGI->townh->structures[town->subID][*i]));
  135. //boost::tuples::tuple<int,CDefHandler*,Structure*,SDL_Surface*,SDL_Surface*> *t
  136. // = new boost::tuples::tuple<int,CDefHandler*,Structure*,SDL_Surface*,SDL_Surface*>
  137. // (*i,b,CGI->townh->structures[town->subID][*i],NULL,NULL);
  138. ////TODO: obwódki i pola
  139. //buildings.push_back(t);
  140. }
  141. else continue;
  142. }
  143. else
  144. break;
  145. }
  146. std::sort(buildings.begin(),buildings.end(),srthlp);
  147. if(Activate)
  148. {
  149. LOCPLINT->objsToBlit.push_back(this);
  150. activate();
  151. showAll();
  152. }
  153. //blit buildings on bg
  154. //for(int i=0;i<buildings.size();i++)
  155. //{
  156. // blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,cityBg);
  157. //}
  158. }
  159. CCastleInterface::~CCastleInterface()
  160. {
  161. SDL_FreeSurface(townInt);
  162. SDL_FreeSurface(cityBg);
  163. delete exit;
  164. delete hall;
  165. delete fort;
  166. delete bigTownPic;
  167. delete flag;
  168. for(int i=0;i<buildings.size();i++)
  169. {
  170. delete buildings[i];
  171. }
  172. }
  173. void CCastleInterface::close()
  174. {
  175. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  176. deactivate();
  177. LOCPLINT->castleInt = NULL;
  178. LOCPLINT->adventureInt->show();
  179. delete this;
  180. }
  181. void CCastleInterface::showAll(SDL_Surface * to)
  182. {
  183. if (!to)
  184. to=ekran;
  185. blitAt(cityBg,0,0,to);
  186. blitAt(townInt,0,374,to);
  187. LOCPLINT->adventureInt->resdatabar.draw();
  188. int pom;
  189. //draw fort icon
  190. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  191. pom = 2;
  192. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  193. pom = 1;
  194. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  195. pom = 0;
  196. else pom = 3;
  197. blitAt(fort->ourImages[pom].bitmap,122,413,to);
  198. //draw ((village/town/city) hall)/capitol icon
  199. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  200. pom = 3;
  201. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  202. pom = 2;
  203. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  204. pom = 1;
  205. else pom = 0;
  206. blitAt(hall->ourImages[pom].bitmap,80,413,to);
  207. //draw creatures icons and their growths
  208. for(int i=0;i<CREATURES_PER_TOWN;i++)
  209. {
  210. int cid = -1;
  211. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  212. {
  213. cid = (14*town->subID)+(i*2);
  214. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  215. {
  216. cid++;
  217. }
  218. }
  219. if (cid>=0)
  220. {
  221. int pomx, pomy;
  222. pomx = 22 + (55*((i>3)?(i-4):i));
  223. pomy = (i>3)?(507):(459);
  224. blitAt(CGI->creh->smallImgs[cid],pomx,pomy,to);
  225. std::ostringstream oss;
  226. oss << '+' << town->creatureIncome[i];
  227. CSDL_Ext::printAtMiddle(oss.str(),pomx+16,pomy+37,GEOR13,zwykly,to);
  228. }
  229. }
  230. //print name and income
  231. CSDL_Ext::printAt(town->name,85,389,GEOR13,zwykly,to);
  232. char temp[10];
  233. itoa(town->income,temp,10);
  234. CSDL_Ext::printAtMiddle(temp,195,442,GEOR13,zwykly,to);
  235. //blit town icon
  236. pom = town->subID*2;
  237. if (!town->hasFort())
  238. pom += F_NUMBER*2;
  239. if(town->builded >= MAX_BUILDING_PER_TURN)
  240. pom++;
  241. blitAt(bigTownPic->ourImages[pom].bitmap,15,387,to);
  242. //flag
  243. blitAt(flag->ourImages[town->getOwner()].bitmap,241,387,to);
  244. //print garrison
  245. for(
  246. std::map<int,std::pair<CCreature*,int> >::const_iterator i=town->garrison.slots.begin();
  247. i!=town->garrison.slots.end();
  248. i++
  249. )
  250. {
  251. blitAt(CGI->creh->bigImgs[i->second.first->idNumber],305+(62*(i->first)),387,to);
  252. itoa(i->second.second,temp,10);
  253. CSDL_Ext::printTo(temp,305+(62*(i->first))+57,387+61,GEOR13,zwykly,to);
  254. }
  255. show();
  256. }
  257. void CCastleInterface::show(SDL_Surface * to)
  258. {
  259. if (!to)
  260. to=ekran;
  261. count++;
  262. if(count==5)
  263. {
  264. count=0;
  265. animval++;
  266. }
  267. blitAt(cityBg,0,0,to);
  268. //blit buildings
  269. for(int i=0;i<buildings.size();i++)
  270. {
  271. if((animval)%(buildings[i]->def->ourImages.size()))
  272. {
  273. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  274. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  275. }
  276. else
  277. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  278. if(buildings[i]->hovered && buildings[i]->border)
  279. blitAt(buildings[i]->border,buildings[i]->pos.x,buildings[i]->pos.y);
  280. }
  281. //for(int i=0;i<buildings.size();i++)
  282. //{
  283. // if((animval)%(buildings[i]->def->ourImages.size())==0)
  284. // blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  285. // else continue;
  286. //}
  287. }
  288. void CCastleInterface::activate()
  289. {
  290. LOCPLINT->curint = this;
  291. for(int i=0;i<buildings.size();i++)
  292. buildings[i]->activate();
  293. }
  294. void CCastleInterface::deactivate()
  295. {
  296. exit->deactivate();
  297. for(int i=0;i<buildings.size();i++)
  298. buildings[i]->deactivate();
  299. }