CMT.cpp 7.0 KB

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