CMT.cpp 7.7 KB

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