CMT.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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" PATHSEPARATOR "tnrb.ttf",16);
  80. GEOR13 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",13);
  81. GEOR16 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",16);
  82. GEORXX = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",22);
  83. GEORM = TTF_OpenFont("Fonts" PATHSEPARATOR "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" PATHSEPARATOR "H3sprite.lod","Sprites");
  95. BitmapHandler::bitmaph = cgi->bitmaph = new CLodHandler;
  96. cgi->bitmaph->init("Data" PATHSEPARATOR "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->spellh = VLC->spellh;
  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,SERVER_NAME " > server_log.txt")); //runs server executable;
  141. //TODO: will it work on non-windows platforms?
  142. THC tmh.getDif();pomtime.getDif();//reset timers
  143. cgi->pathf = new CPathfinder();
  144. THC std::cout<<"\tPathfinder: "<<pomtime.getDif()<<std::endl;
  145. cgi->consoleh->runConsole();
  146. THC std::cout<<"\tCallback and console: "<<pomtime.getDif()<<std::endl;
  147. THC std::cout<<"Handlers initialization (together): "<<tmh.getDif()<<std::endl;
  148. std::ofstream lll("client_log.txt");
  149. CConnection *c=NULL;
  150. while(!c)
  151. {
  152. try
  153. {
  154. std::cout << "Establishing connection...\t";
  155. c = new CConnection("localhost","3030",NAME,lll);
  156. std::cout << "done!" <<std::endl;
  157. }
  158. catch(...)
  159. {
  160. std::cout << "\nCannot establish connection! Retrying within 3 seconds" <<std::endl;
  161. SDL_Delay(3000);
  162. }
  163. }
  164. THC std::cout<<"\tConnecting to the server: "<<tmh.getDif()<<std::endl;
  165. CClient cl(c,options);
  166. boost::thread t(boost::bind(&CClient::run,&cl));
  167. SDL_Event ev;
  168. while(1) //main SDL events loop
  169. {
  170. SDL_WaitEvent(&ev);
  171. if(ev.type==SDL_QUIT)
  172. {
  173. t.interrupt();
  174. exit(0);
  175. }
  176. eventsM.lock();
  177. events.push(ev);
  178. eventsM.unlock();
  179. }
  180. }
  181. else
  182. {
  183. printf("Something was wrong: %s/n", SDL_GetError());
  184. return -1;
  185. }
  186. }