CMT.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // CMT.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <cmath>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <cmath>
  9. #include <boost/thread.hpp>
  10. #include "SDL_TTF.h"
  11. #include "SDL_mixer.h"
  12. #include "SDL_Extensions.h"
  13. #include "SDL_framerate.h"
  14. #include "CGameInfo.h"
  15. #include "mapHandler.h"
  16. #include "global.h"
  17. #include "CPreGame.h"
  18. #include "CConsoleHandler.h"
  19. #include "CCursorHandler.h"
  20. #include "CScreenHandler.h"
  21. #include "CPathfinder.h"
  22. #include "CGameState.h"
  23. #include "CCallback.h"
  24. #include "CPlayerInterface.h"
  25. #include "CLuaHandler.h"
  26. #include "CLua.h"
  27. #include "CAdvmapInterface.h"
  28. #include "hch/CBuildingHandler.h"
  29. #include "hch/CVideoHandler.h"
  30. #include "hch/CAbilityHandler.h"
  31. #include "hch/CHeroHandler.h"
  32. #include "hch/CCreatureHandler.h"
  33. #include "hch/CSpellHandler.h"
  34. #include "hch/CBuildingHandler.h"
  35. #include "hch/CMusicHandler.h"
  36. #include "hch/CLodHandler.h"
  37. #include "hch/CDefHandler.h"
  38. #include "hch/CAmbarCendamo.h"
  39. #include "hch/CGeneralTextHandler.h"
  40. #include "client/Graphics.h"
  41. #include "Client/Client.h"
  42. #include "lib/Connection.h"
  43. #include "lib/VCMI_Lib.h"
  44. std::string NAME = NAME_VER + std::string(" (client)");
  45. DLL_EXPORT void initDLL(CLodHandler *b);
  46. SDL_Surface * screen, * screen2;
  47. extern SDL_Surface * CSDL_Ext::std32bppSurface;
  48. std::queue<SDL_Event> events;
  49. boost::mutex eventsM;
  50. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  51. void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script)
  52. {
  53. std::vector<int> tempv = script->yourObjects();
  54. for (int i=0;i<tempv.size();i++)
  55. {
  56. (*mapa)[tempv[i]]=script;
  57. }
  58. CGI->state->cppscripts.insert(script);
  59. }
  60. int _tmain(int argc, _TCHAR* argv[])
  61. {
  62. std::cout.flags(ios::unitbuf);
  63. std::cout << NAME << std::endl;
  64. srand ( time(NULL) );
  65. CPG=NULL;
  66. atexit(SDL_Quit);
  67. CGameInfo * cgi = CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  68. //CLuaHandler luatest;
  69. //luatest.test();
  70. //CBIKHandler cb;
  71. //cb.open("CSECRET.BIK");
  72. std::cout << "Starting... " << std::endl;
  73. THC timeHandler tmh, total, pomtime;
  74. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  75. {
  76. screen = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/); //initializing important global surface
  77. THC std::cout<<"\tInitializing screen: "<<pomtime.getDif()<<std::endl;
  78. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  79. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  80. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  81. #else
  82. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  83. #endif
  84. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  85. THC std::cout<<"\tInitializing minors: "<<pomtime.getDif()<<std::endl;
  86. TTF_Init();
  87. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  88. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  89. GEOR16 = TTF_OpenFont("Fonts\\georgia.ttf",16);
  90. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  91. GEORM = TTF_OpenFont("Fonts\\georgia.ttf",10);
  92. atexit(TTF_Quit);
  93. THC std::cout<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  94. CMusicHandler * mush = new CMusicHandler; //initializing audio
  95. mush->initMusics();
  96. //audio initialized
  97. cgi->consoleh = new CConsoleHandler;
  98. cgi->mush = mush;
  99. cgi->curh = new CCursorHandler;
  100. THC std::cout<<"\tInitializing sound and cursor: "<<pomtime.getDif()<<std::endl;
  101. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  102. CDefHandler::Spriteh = cgi->spriteh = new CLodHandler();
  103. cgi->spriteh->init("Data\\H3sprite.lod","Sprites");
  104. BitmapHandler::bitmaph = cgi->bitmaph = new CLodHandler;
  105. cgi->bitmaph->init("Data\\H3bitmap.lod","Data");
  106. THC std::cout<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  107. initDLL(cgi->bitmaph);
  108. CGI->arth = VLC->arth;
  109. CGI->creh = VLC->creh;
  110. CGI->townh = VLC->townh;
  111. CGI->heroh = VLC->heroh;
  112. CGI->objh = VLC->objh;
  113. CGI->dobjinfo = VLC->dobjinfo;
  114. THC std::cout<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  115. //cgi->curh->initCursor();
  116. //cgi->curh->showGraphicCursor();
  117. pomtime.getDif();
  118. cgi->screenh = new CScreenHandler;
  119. cgi->screenh->initScreen();
  120. THC std::cout<<"\tScreen handler: "<<pomtime.getDif()<<std::endl;
  121. CAbilityHandler * abilh = new CAbilityHandler;
  122. abilh->loadAbilities();
  123. cgi->abilh = abilh;
  124. THC std::cout<<"\tAbility handler: "<<pomtime.getDif()<<std::endl;
  125. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  126. pomtime.getDif();
  127. graphics = new Graphics();
  128. THC std::cout<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  129. std::vector<CDefHandler **> animacje;
  130. for(std::vector<CHeroClass *>::iterator i = cgi->heroh->heroClasses.begin();i!=cgi->heroh->heroClasses.end();i++)
  131. animacje.push_back(&((*i)->*(&CHeroClass::moveAnim)));
  132. graphics->loadHeroAnim(animacje);
  133. THC std::cout<<"\tHero animations: "<<tmh.getDif()<<std::endl;
  134. THC std::cout<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  135. CMessage::init();
  136. cgi->generaltexth = new CGeneralTextHandler;
  137. cgi->generaltexth->load();
  138. THC std::cout<<"Preparing more handlers: "<<tmh.getDif()<<std::endl;
  139. CPreGame * cpg = new CPreGame(); //main menu and submenus
  140. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  141. THC std::cout<<"Initialization of VCMI (togeter): "<<total.getDif()<<std::endl;
  142. cpg->mush = mush;
  143. StartInfo *options = new StartInfo(cpg->runLoop());
  144. ///////////////////////////////////////////////////////////////////////////////////////
  145. boost::thread servthr(boost::bind(system,"VCMI_server.exe > server_log.txt")); //runs server executable;
  146. //TODO: will it work on non-windows platforms?
  147. THC tmh.getDif();pomtime.getDif();//reset timers
  148. CSpellHandler * spellh = new CSpellHandler;
  149. spellh->loadSpells();
  150. cgi->spellh = spellh;
  151. THC std::cout<<"\tSpell handler: "<<pomtime.getDif()<<std::endl;
  152. CBuildingHandler * buildh = new CBuildingHandler;
  153. buildh->loadBuildings();
  154. cgi->buildh = buildh;
  155. THC std::cout<<"\tBuilding handler: "<<pomtime.getDif()<<std::endl;
  156. cgi->pathf = new CPathfinder();
  157. THC std::cout<<"\tPathfinder: "<<pomtime.getDif()<<std::endl;
  158. cgi->consoleh->runConsole();
  159. THC std::cout<<"\tCallback and console: "<<pomtime.getDif()<<std::endl;
  160. THC std::cout<<"Handlers initialization (together): "<<tmh.getDif()<<std::endl;
  161. std::ofstream lll("client_log.txt");
  162. CConnection c("localhost","3030",NAME,lll);
  163. THC std::cout<<"\tConnecting to the server: "<<tmh.getDif()<<std::endl;
  164. CClient cl(&c,options);
  165. boost::thread t(boost::bind(&CClient::run,&cl));
  166. SDL_Event ev;
  167. while(1) //main SDL events loop
  168. {
  169. SDL_WaitEvent(&ev);
  170. if(ev.type==SDL_QUIT)
  171. {
  172. t.interrupt();
  173. exit(0);
  174. }
  175. eventsM.lock();
  176. events.push(ev);
  177. eventsM.unlock();
  178. }
  179. ///claculating FoWs for minimap
  180. /****************************Minimaps' FoW******************************************/
  181. //for(int g=0; g<cgi->playerint.size(); ++g)
  182. //{
  183. // if(!cgi->playerint[g]->human)
  184. // continue;
  185. // CMinimap & mm = ((CPlayerInterface*)cgi->playerint[g])->adventureInt->minimap;
  186. // int mw = mm.map[0]->w, mh = mm.map[0]->h,
  187. // wo = mw/CGI->mh->sizes.x, ho = mh/CGI->mh->sizes.y;
  188. // for(int d=0; d<cgi->mh->map->twoLevel+1; ++d)
  189. // {
  190. // SDL_Surface * pt = CSDL_Ext::newSurface(mm.pos.w, mm.pos.h, CSDL_Ext::std32bppSurface);
  191. // for (int i=0; i<mw; i++)
  192. // {
  193. // for (int j=0; j<mh; j++)
  194. // {
  195. // int3 pp( ((i*CGI->mh->sizes.x)/mw), ((j*CGI->mh->sizes.y)/mh), d );
  196. // if ( !((CPlayerInterface*)cgi->playerint[g])->cb->isVisible(pp) )
  197. // {
  198. // CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0);
  199. // }
  200. // }
  201. // }
  202. // CSDL_Ext::update(pt);
  203. // mm.FoW.push_back(pt);
  204. // }
  205. //}
  206. }
  207. else
  208. {
  209. printf("Something was wrong: %s/n", SDL_GetError());
  210. return -1;
  211. }
  212. }