CCastleInterface.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. int t = 600;
  116. while(t--)
  117. {
  118. CDefHandler* defik = CGI->spriteh->giveDef("ITMTL.DEF");
  119. delete defik;
  120. }
  121. count=0;
  122. town = Town;
  123. townInt = CGI->bitmaph->loadBitmap("TOWNSCRN.bmp");
  124. cityBg = CGI->bitmaph->loadBitmap(getBgName(town->subID));
  125. hall = CGI->spriteh->giveDef("ITMTL.DEF");
  126. fort = CGI->spriteh->giveDef("ITMCL.DEF");
  127. bigTownPic = CGI->spriteh->giveDef("ITPT.DEF");
  128. flag = CGI->spriteh->giveDef("CREST58.DEF");
  129. CSDL_Ext::blueToPlayersAdv(townInt,LOCPLINT->playerID);
  130. exit = new AdventureMapButton<CCastleInterface>
  131. (CGI->townh->tcommands[8],"",&CCastleInterface::close,744,544,"TSBTNS.DEF",this,Activate);
  132. exit->bitmapOffset = 4;
  133. for (std::set<int>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  134. {
  135. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end())
  136. {
  137. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end())
  138. {
  139. //CDefHandler *b = CGI->spriteh->giveDef(CGI->townh->structures[town->subID][*i]->defName);
  140. buildings.push_back(new CBuildingRect(CGI->townh->structures[town->subID][*i]));
  141. //boost::tuples::tuple<int,CDefHandler*,Structure*,SDL_Surface*,SDL_Surface*> *t
  142. // = new boost::tuples::tuple<int,CDefHandler*,Structure*,SDL_Surface*,SDL_Surface*>
  143. // (*i,b,CGI->townh->structures[town->subID][*i],NULL,NULL);
  144. ////TODO: obwódki i pola
  145. //buildings.push_back(t);
  146. }
  147. else continue;
  148. }
  149. else
  150. break;
  151. }
  152. std::sort(buildings.begin(),buildings.end(),srthlp);
  153. if(Activate)
  154. {
  155. LOCPLINT->objsToBlit.push_back(this);
  156. activate();
  157. showAll();
  158. }
  159. //blit buildings on bg
  160. //for(int i=0;i<buildings.size();i++)
  161. //{
  162. // blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,cityBg);
  163. //}
  164. }
  165. CCastleInterface::~CCastleInterface()
  166. {
  167. SDL_FreeSurface(townInt);
  168. SDL_FreeSurface(cityBg);
  169. delete exit;
  170. delete hall;
  171. delete fort;
  172. delete bigTownPic;
  173. delete flag;
  174. for(int i=0;i<buildings.size();i++)
  175. {
  176. delete buildings[i];
  177. }
  178. }
  179. void CCastleInterface::close()
  180. {
  181. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  182. deactivate();
  183. LOCPLINT->castleInt = NULL;
  184. LOCPLINT->adventureInt->show();
  185. delete this;
  186. }
  187. void CCastleInterface::showAll(SDL_Surface * to)
  188. {
  189. if (!to)
  190. to=ekran;
  191. blitAt(cityBg,0,0,to);
  192. blitAt(townInt,0,374,to);
  193. LOCPLINT->adventureInt->resdatabar.draw();
  194. int pom;
  195. //draw fort icon
  196. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  197. pom = 2;
  198. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  199. pom = 1;
  200. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  201. pom = 0;
  202. else pom = 3;
  203. blitAt(fort->ourImages[pom].bitmap,122,413,to);
  204. //draw ((village/town/city) hall)/capitol icon
  205. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  206. pom = 3;
  207. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  208. pom = 2;
  209. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  210. pom = 1;
  211. else pom = 0;
  212. blitAt(hall->ourImages[pom].bitmap,80,413,to);
  213. //draw creatures icons and their growths
  214. for(int i=0;i<CREATURES_PER_TOWN;i++)
  215. {
  216. int cid = -1;
  217. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  218. {
  219. cid = (14*town->subID)+(i*2);
  220. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  221. {
  222. cid++;
  223. }
  224. }
  225. if (cid>=0)
  226. {
  227. int pomx, pomy;
  228. pomx = 22 + (55*((i>3)?(i-4):i));
  229. pomy = (i>3)?(507):(459);
  230. blitAt(CGI->creh->smallImgs[cid],pomx,pomy,to);
  231. std::ostringstream oss;
  232. oss << '+' << town->creatureIncome[i];
  233. CSDL_Ext::printAtMiddle(oss.str(),pomx+16,pomy+37,GEOR13,zwykly,to);
  234. }
  235. }
  236. //print name and income
  237. CSDL_Ext::printAt(town->name,85,389,GEOR13,zwykly,to);
  238. char temp[10];
  239. itoa(town->income,temp,10);
  240. CSDL_Ext::printAtMiddle(temp,195,442,GEOR13,zwykly,to);
  241. //blit town icon
  242. pom = town->subID*2;
  243. if (!town->hasFort())
  244. pom += F_NUMBER*2;
  245. if(town->builded >= MAX_BUILDING_PER_TURN)
  246. pom++;
  247. blitAt(bigTownPic->ourImages[pom].bitmap,15,387,to);
  248. //flag
  249. blitAt(flag->ourImages[town->getOwner()].bitmap,241,387,to);
  250. //print garrison
  251. for(
  252. std::map<int,std::pair<CCreature*,int> >::const_iterator i=town->garrison.slots.begin();
  253. i!=town->garrison.slots.end();
  254. i++
  255. )
  256. {
  257. blitAt(CGI->creh->bigImgs[i->second.first->idNumber],305+(62*(i->first)),387,to);
  258. itoa(i->second.second,temp,10);
  259. CSDL_Ext::printTo(temp,305+(62*(i->first))+57,387+61,GEOR13,zwykly,to);
  260. }
  261. show();
  262. }
  263. void CCastleInterface::show(SDL_Surface * to)
  264. {
  265. if (!to)
  266. to=ekran;
  267. count++;
  268. if(count==5)
  269. {
  270. count=0;
  271. animval++;
  272. }
  273. blitAt(cityBg,0,0,to);
  274. //blit buildings
  275. for(int i=0;i<buildings.size();i++)
  276. {
  277. if((animval)%(buildings[i]->def->ourImages.size()))
  278. {
  279. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  280. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  281. }
  282. else
  283. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  284. if(buildings[i]->hovered && buildings[i]->border)
  285. blitAt(buildings[i]->border,buildings[i]->pos.x,buildings[i]->pos.y);
  286. }
  287. //for(int i=0;i<buildings.size();i++)
  288. //{
  289. // if((animval)%(buildings[i]->def->ourImages.size())==0)
  290. // blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  291. // else continue;
  292. //}
  293. }
  294. void CCastleInterface::activate()
  295. {
  296. LOCPLINT->curint = this;
  297. for(int i=0;i<buildings.size();i++)
  298. buildings[i]->activate();
  299. }
  300. void CCastleInterface::deactivate()
  301. {
  302. exit->deactivate();
  303. for(int i=0;i<buildings.size();i++)
  304. buildings[i]->deactivate();
  305. }