CMT.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/algorithm/string.hpp>
  10. #include <boost/filesystem/operations.hpp>
  11. #include <boost/thread.hpp>
  12. #include <SDL_ttf.h>
  13. #include <SDL_mixer.h>
  14. #include "SDL_Extensions.h"
  15. #include "SDL_framerate.h"
  16. #include "CGameInfo.h"
  17. #include "../mapHandler.h"
  18. #include "../global.h"
  19. #include "CPreGame.h"
  20. #include "CCastleInterface.h"
  21. #include "../CConsoleHandler.h"
  22. #include "CCursorHandler.h"
  23. #include "../lib/CGameState.h"
  24. #include "../CCallback.h"
  25. #include "CPlayerInterface.h"
  26. #include "CAdvmapInterface.h"
  27. #include "../hch/CBuildingHandler.h"
  28. #include "../hch/CVideoHandler.h"
  29. #include "../hch/CHeroHandler.h"
  30. #include "../hch/CCreatureHandler.h"
  31. #include "../hch/CSpellHandler.h"
  32. #include "../hch/CMusicHandler.h"
  33. #include "../hch/CVideoHandler.h"
  34. #include "../hch/CLodHandler.h"
  35. #include "../hch/CDefHandler.h"
  36. #include "../hch/CAmbarCendamo.h"
  37. #include "../hch/CGeneralTextHandler.h"
  38. #include "Graphics.h"
  39. #include "Client.h"
  40. #include "CConfigHandler.h"
  41. #include "../lib/Connection.h"
  42. #include "../lib/VCMI_Lib.h"
  43. #include <cstdlib>
  44. #include "../lib/NetPacks.h"
  45. #if __MINGW32__
  46. #undef main
  47. #endif
  48. /*
  49. * CMT.cpp, part of VCMI engine
  50. *
  51. * Authors: listed in file AUTHORS in main folder
  52. *
  53. * License: GNU General Public License v2.0 or later
  54. * Full text of license available in license.txt file, in main folder
  55. *
  56. */
  57. std::string NAME = NAME_VER + std::string(" (client)"); //application name
  58. SDL_Surface *screen = NULL, //main screen surface
  59. *screen2 = NULL,//and hlp surface (used to store not-active interfaces layer)
  60. *screenBuf = screen; //points to screen (if only advmapint is present) or screen2 (else) - should be used when updating controls which are not regularly redrawed
  61. std::queue<SDL_Event*> events;
  62. boost::mutex eventsM;
  63. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  64. void processCommand(const std::string &message, CClient *&client);
  65. static void setScreenRes(int w, int h, int bpp, bool fullscreen);
  66. void dispose();
  67. void playIntro();
  68. void init()
  69. {
  70. timeHandler tmh, pomtime;
  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. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  78. TTF_Init();
  79. atexit(TTF_Quit);
  80. TNRB16 = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",16);
  81. GEOR13 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",13);
  82. GEOR16 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",16);
  83. GEORXX = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",22);
  84. GEORM = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",10);
  85. if(! (TNRB16 && GEOR16 && GEORXX && GEORM))
  86. {
  87. tlog1 << "One of the fonts couldn't be loaded!\n";
  88. exit(-1);
  89. }
  90. THC tlog0<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  91. //initializing audio
  92. // Note: because of interface button range, volume can only be a
  93. // multiple of 11, from 0 to 99.
  94. CGI->soundh = new CSoundHandler;
  95. CGI->soundh->init();
  96. CGI->soundh->setVolume(88);
  97. CGI->musich = new CMusicHandler;
  98. CGI->musich->init();
  99. CGI->musich->setVolume(88);
  100. tlog0<<"\tInitializing sound: "<<pomtime.getDif()<<std::endl;
  101. tlog0<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  102. initDLL(::console,logfile);
  103. CGI->setFromLib();
  104. CGI->soundh->initCreaturesSounds(CGI->creh->creatures);
  105. CGI->soundh->initSpellsSounds(CGI->spellh->spells);
  106. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  107. pomtime.getDif();
  108. CGI->curh = new CCursorHandler;
  109. CGI->curh->initCursor();
  110. CGI->curh->show();
  111. tlog0<<"Screen handler: "<<pomtime.getDif()<<std::endl;
  112. pomtime.getDif();
  113. graphics = new Graphics();
  114. graphics->loadHeroAnims();
  115. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  116. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  117. CMessage::init();
  118. tlog0<<"Message handler: "<<tmh.getDif()<<std::endl;
  119. CPG = new CPreGame(); //main menu and submenus
  120. tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  121. }
  122. #ifndef __GNUC__
  123. int _tmain(int argc, _TCHAR* argv[])
  124. #else
  125. int main(int argc, char** argv)
  126. #endif
  127. {
  128. tlog0 << "Starting... " << std::endl;
  129. timeHandler total, pomtime;
  130. CClient *client = NULL;
  131. std::cout.flags(std::ios::unitbuf);
  132. logfile = new std::ofstream("VCMI_Client_log.txt");
  133. console = new CConsoleHandler;
  134. *console->cb = boost::bind(&processCommand, _1, boost::ref(client));
  135. console->start();
  136. atexit(dispose);
  137. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  138. conf.init();
  139. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  140. tlog0 << NAME << std::endl;
  141. srand ( time(NULL) );
  142. CPG=NULL;
  143. atexit(SDL_Quit);
  144. CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  145. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO)==0)
  146. {
  147. setScreenRes(800,600,conf.cc.bpp,conf.cc.fullscreen);
  148. tlog0 <<"\tInitializing screen: "<<pomtime.getDif() << std::endl;
  149. // Initialize video
  150. CGI->videoh = new CVideoPlayer;
  151. tlog0<<"\tInitializing video: "<<pomtime.getDif()<<std::endl;
  152. //we can properly play intro only in the main thread, so we have to move loading to the separate thread
  153. boost::thread loading(init);
  154. playIntro();
  155. SDL_FillRect(screen,NULL,0);
  156. SDL_Flip(screen);
  157. loading.join();
  158. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  159. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  160. CGI->musich->playMusic(musicBase::mainMenu, -1);
  161. CPG->showMainMenu();
  162. StartInfo *options = new StartInfo(CPG->runLoop());
  163. if(screen->w != conf.cc.resx || screen->h != conf.cc.resy)
  164. {
  165. setScreenRes(conf.cc.resx,conf.cc.resy,conf.cc.bpp,conf.cc.fullscreen);
  166. }
  167. CClient cl;
  168. if(options->mode == 0) //new game
  169. {
  170. pomtime.getDif();
  171. char portc[10];
  172. SDL_itoa(conf.cc.port,portc,10);
  173. CClient::runServer(portc);
  174. tlog0<<"Preparing shared memory and starting server: "<<pomtime.getDif()<<std::endl;
  175. pomtime.getDif();//reset timers
  176. CConnection *c=NULL;
  177. //wait until server is ready
  178. tlog0<<"Waiting for server... ";
  179. cl.waitForServer();
  180. tlog0 << pomtime.getDif()<<std::endl;
  181. while(!c)
  182. {
  183. try
  184. {
  185. tlog0 << "Establishing connection...\n";
  186. c = new CConnection(conf.cc.server,portc,NAME);
  187. }
  188. catch(...)
  189. {
  190. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" <<std::endl;
  191. SDL_Delay(2000);
  192. }
  193. }
  194. THC tlog0<<"\tConnecting to the server: "<<pomtime.getDif()<<std::endl;
  195. cl.newGame(c,options);
  196. client = &cl;
  197. CGI->musich->stopMusic();
  198. boost::thread t(boost::bind(&CClient::run,&cl));
  199. }
  200. else //load game
  201. {
  202. std::string fname = options->mapname;
  203. boost::algorithm::erase_last(fname,".vlgm1");
  204. cl.load(fname);
  205. client = &cl;
  206. CGI->musich->stopMusic();
  207. boost::thread t(boost::bind(&CClient::run,&cl));
  208. }
  209. SDL_Event *ev = NULL;
  210. while(1) //main SDL events loop
  211. {
  212. ev = new SDL_Event();
  213. int ret = SDL_WaitEvent(ev);
  214. if(ret == 0 || (ev->type==SDL_QUIT) || (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  215. {
  216. LOCPLINT->pim->lock();
  217. cl.close();
  218. console->end();
  219. SDL_Delay(750);
  220. tlog0 << "Ending...\n";
  221. exit(EXIT_SUCCESS);
  222. }
  223. else if(ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  224. {
  225. boost::unique_lock<boost::recursive_mutex> lock(*LOCPLINT->pim);
  226. bool full = !(screen->flags&SDL_FULLSCREEN);
  227. setScreenRes(conf.cc.resx,conf.cc.resy,conf.cc.bpp,full);
  228. LOCPLINT->totalRedraw();
  229. }
  230. eventsM.lock();
  231. events.push(ev);
  232. eventsM.unlock();
  233. }
  234. }
  235. else
  236. {
  237. tlog1<<"Something was wrong: "<<SDL_GetError()<<std::endl;
  238. return -1;
  239. }
  240. }
  241. void processCommand(const std::string &message, CClient *&client)
  242. {
  243. std::istringstream readed;
  244. readed.str(message);
  245. std::string cn; //command name
  246. readed >> cn;
  247. int3 src, dst;
  248. // int heronum;//TODO use me
  249. int3 dest;
  250. if(LOCPLINT && LOCPLINT->cingconsole)
  251. LOCPLINT->cingconsole->print(message);
  252. if(message==std::string("die, fool"))
  253. exit(EXIT_SUCCESS);
  254. else if(cn==std::string("activate"))
  255. {
  256. int what;
  257. readed >> what;
  258. switch (what)
  259. {
  260. case 0:
  261. LOCPLINT->topInt()->activate();
  262. break;
  263. case 1:
  264. LOCPLINT->adventureInt->activate();
  265. break;
  266. case 2:
  267. LOCPLINT->castleInt->activate();
  268. break;
  269. }
  270. }
  271. else if(cn=="save")
  272. {
  273. std::string fname;
  274. readed >> fname;
  275. client->save(fname);
  276. }
  277. else if(cn=="load")
  278. {
  279. std::string fname;
  280. readed >> fname;
  281. client->load(fname);
  282. }
  283. else if(cn=="resolution")
  284. {
  285. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  286. int i=1, hlp=1;
  287. tlog4 << "Available screen resolutions:\n";
  288. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  289. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  290. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  291. std::cin >> i;
  292. if(i < 0 || i > conf.guiOptions.size() || std::cin.bad() || std::cin.fail())
  293. {
  294. std::cin.clear();
  295. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  296. }
  297. else if(!i)
  298. {
  299. return;
  300. }
  301. else
  302. {
  303. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  304. conf.cc.resx = j->first.first;
  305. conf.cc.resy = j->first.second;
  306. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  307. }
  308. }
  309. else if(message=="get txt")
  310. {
  311. boost::filesystem::create_directory("Extracted_txts");
  312. tlog0<<"Command accepted. Opening .lod file...\t";
  313. CLodHandler * txth = new CLodHandler;
  314. txth->init(std::string(DATA_DIR "Data" PATHSEPARATOR "H3bitmap.lod"),"");
  315. tlog0<<"done.\nScanning .lod file\n";
  316. int curp=0;
  317. std::string pattern = ".TXT", pom;
  318. for(int i=0;i<txth->entries.size(); i++)
  319. {
  320. pom = txth->entries[i].nameStr;
  321. if(boost::algorithm::find_last(pom,pattern))
  322. {
  323. txth->extractFile(std::string("Extracted_txts" PATHSEPARATOR)+pom,pom);
  324. }
  325. if(i%8) continue;
  326. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  327. if(p2!=curp)
  328. {
  329. curp = p2;
  330. tlog0<<"\r"<<curp<<"%";
  331. }
  332. }
  333. tlog0<<"\rExtracting done :)\n";
  334. }
  335. else if(client && client->serv && client->serv->connected) //send to server
  336. {
  337. PlayerMessage pm(LOCPLINT->playerID,message);
  338. *client->serv << &pm;
  339. }
  340. }
  341. //plays intro, ends when intro is over or button has been pressed (handles events)
  342. void playIntro()
  343. {
  344. if(CGI->videoh->openAndPlayVideo("3DOLOGO.SMK", 60, 40, screen, true))
  345. {
  346. CGI->videoh->openAndPlayVideo("AZVS.SMK", 60, 80, screen, true);
  347. }
  348. }
  349. void dispose()
  350. {
  351. delete logfile;
  352. delete console;
  353. }
  354. static void setScreenRes(int w, int h, int bpp, bool fullscreen)
  355. {
  356. if(screen) //screen has been already initialized
  357. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  358. SDL_InitSubSystem(SDL_INIT_VIDEO);
  359. // VCMI will only work with 3 or 4 bytes per pixel
  360. if (bpp < 24) bpp = 24;
  361. if (bpp > 32) bpp = 32;
  362. // Try to use the best screen depth for the display
  363. if (((bpp = SDL_VideoModeOK(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == 0) ||
  364. ((screen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == NULL)) {
  365. tlog1 << "Requested screen resolution is not available (" << w << "x" << h << ")\n";
  366. throw "Requested screen resolution is not available\n";
  367. }
  368. if(screen2)
  369. SDL_FreeSurface(screen2);
  370. screen2 = CSDL_Ext::copySurface(screen);
  371. SDL_EnableUNICODE(1);
  372. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  373. SDL_ShowCursor(SDL_DISABLE);
  374. }