CMT.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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_mixer.h>
  13. #include "SDL_Extensions.h"
  14. #include "SDL_framerate.h"
  15. #include "CGameInfo.h"
  16. #include "mapHandler.h"
  17. #include "../global.h"
  18. #include "CPreGame.h"
  19. #include "CCastleInterface.h"
  20. #include "../CConsoleHandler.h"
  21. #include "CCursorHandler.h"
  22. #include "../lib/CGameState.h"
  23. #include "../CCallback.h"
  24. #include "CPlayerInterface.h"
  25. #include "CAdvmapInterface.h"
  26. #include "../hch/CBuildingHandler.h"
  27. #include "../hch/CVideoHandler.h"
  28. #include "../hch/CHeroHandler.h"
  29. #include "../hch/CCreatureHandler.h"
  30. #include "../hch/CSpellHandler.h"
  31. #include "../hch/CMusicHandler.h"
  32. #include "../hch/CVideoHandler.h"
  33. #include "../hch/CLodHandler.h"
  34. #include "../hch/CDefHandler.h"
  35. #include "../hch/CGeneralTextHandler.h"
  36. #include "Graphics.h"
  37. #include "Client.h"
  38. #include "CConfigHandler.h"
  39. #include "../lib/Connection.h"
  40. #include "../lib/VCMI_Lib.h"
  41. #include "../lib/VCMIDirs.h"
  42. #include <cstdlib>
  43. #include "../lib/NetPacks.h"
  44. #include "CMessage.h"
  45. #include "../hch/CObjectHandler.h"
  46. #ifdef _WIN32
  47. #include "SDL_syswm.h"
  48. #endif
  49. #include <boost/foreach.hpp>
  50. #include "../hch/CDefObjInfoHandler.h"
  51. #if __MINGW32__
  52. #undef main
  53. #endif
  54. /*
  55. * CMT.cpp, part of VCMI engine
  56. *
  57. * Authors: listed in file AUTHORS in main folder
  58. *
  59. * License: GNU General Public License v2.0 or later
  60. * Full text of license available in license.txt file, in main folder
  61. *
  62. */
  63. std::string NAME_AFFIX = "client";
  64. std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
  65. CGuiHandler GH;
  66. static CClient *client;
  67. SDL_Surface *screen = NULL, //main screen surface
  68. *screen2 = NULL,//and hlp surface (used to store not-active interfaces layer)
  69. *screenBuf = screen; //points to screen (if only advmapint is present) or screen2 (else) - should be used when updating controls which are not regularly redrawed
  70. Point screenLT = Point(0, 0); //position of top left corner of the screen
  71. Point screenLTmax = Point(0, 0); //,maximal values for screenLT coordinates
  72. static boost::thread *mainGUIThread;
  73. SystemOptions GDefaultOptions;
  74. VCMIDirs GVCMIDirs;
  75. std::queue<SDL_Event*> events;
  76. boost::mutex eventsM;
  77. static bool gOnlyAI = false;
  78. static bool setResolution = false; //set by event handling thread after resolution is adjusted
  79. void processCommand(const std::string &message);
  80. static void setScreenRes(int w, int h, int bpp, bool fullscreen);
  81. void dispose();
  82. void playIntro();
  83. static void listenForEvents();
  84. void requestChangingResolution();
  85. #ifndef _WIN32
  86. #ifndef _GNU_SOURCE
  87. #define _GNU_SOURCE
  88. #endif
  89. #include <getopt.h>
  90. #endif
  91. void init()
  92. {
  93. timeHandler tmh, pomtime;
  94. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  95. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  96. #else
  97. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  98. #endif
  99. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  100. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  101. {
  102. //read system options
  103. CLoadFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin", false);
  104. if(settings.sfile)
  105. {
  106. settings >> GDefaultOptions;
  107. }
  108. else //file not found (probably, may be also some kind of access problem
  109. {
  110. tlog2 << "Warning: Cannot read system options, default settings will be used.\n";
  111. //Try to create file
  112. tlog2 << "VCMI will try to save default system options...\n";
  113. GDefaultOptions.settingsChanged();
  114. }
  115. }
  116. THC tlog0<<"\tLoading default system settings: "<<pomtime.getDif()<<std::endl;
  117. //initializing audio
  118. // Note: because of interface button range, volume can only be a
  119. // multiple of 11, from 0 to 99.
  120. CGI->soundh = new CSoundHandler;
  121. CGI->soundh->init();
  122. CGI->soundh->setVolume(GDefaultOptions.soundVolume);
  123. CGI->musich = new CMusicHandler;
  124. //CGI->musich->init();
  125. //CGI->musich->setVolume(GDefaultOptions.musicVolume);
  126. tlog0<<"\tInitializing sound: "<<pomtime.getDif()<<std::endl;
  127. tlog0<<"Initializing screen and sound handling: "<<tmh.getDif()<<std::endl;
  128. initDLL(::console,logfile);
  129. CGI->setFromLib();
  130. CGI->soundh->initCreaturesSounds(CGI->creh->creatures);
  131. CGI->soundh->initSpellsSounds(CGI->spellh->spells);
  132. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  133. pomtime.getDif();
  134. CGI->curh = new CCursorHandler;
  135. CGI->curh->initCursor();
  136. CGI->curh->show();
  137. tlog0<<"Screen handler: "<<pomtime.getDif()<<std::endl;
  138. pomtime.getDif();
  139. graphics = new Graphics();
  140. graphics->loadHeroAnims();
  141. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  142. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  143. CMessage::init();
  144. tlog0<<"Message handler: "<<tmh.getDif()<<std::endl;
  145. //CPG = new CPreGame(); //main menu and submenus
  146. //tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  147. }
  148. #ifndef _WIN32
  149. static void prog_version(void)
  150. {
  151. printf("%s\n", NAME_VER);
  152. printf(" data directory: %s\n", DATA_DIR);
  153. printf(" library directory: %s\n", LIB_DIR);
  154. printf(" binary directory: %s\n", BIN_DIR);
  155. }
  156. static void prog_help(const char *progname)
  157. {
  158. printf("%s - A Heroes of Might and Magic 3 clone\n", NAME_VER);
  159. printf("Copyright (C) 2007-2010 VCMI dev team - see AUTHORS file\n");
  160. printf("This is free software; see the source for copying conditions. There is NO\n");
  161. printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
  162. printf("\n");
  163. printf("Usage:\n");
  164. printf(" -h, --help display this help and exit\n");
  165. printf(" -v, --version display version information and exit\n");
  166. }
  167. #endif
  168. #ifdef _WIN32
  169. int _tmain(int argc, _TCHAR* argv[])
  170. #else
  171. int main(int argc, char** argv)
  172. #endif
  173. {
  174. #ifndef _WIN32
  175. struct option long_options[] = {
  176. { "help", 0, NULL, 'h' },
  177. { "version", 0, NULL, 'v' },
  178. { NULL, 0, NULL, 0 }
  179. };
  180. while(1) {
  181. int c;
  182. int option_index;
  183. c = getopt_long (argc, argv, "hv",
  184. long_options, &option_index);
  185. if (c == EOF)
  186. break;
  187. else if (c == '?')
  188. return -1;
  189. switch(c) {
  190. case 'h':
  191. prog_help(argv[0]);
  192. return 0;
  193. break;
  194. case 'v':
  195. prog_version();
  196. return 0;
  197. break;
  198. }
  199. }
  200. if (optind < argc) {
  201. printf ("Extra arguments: %s\n", argv[optind++]);
  202. return 1;
  203. }
  204. #endif
  205. //Set environment vars to make window centered. Sometimes work, sometimes not. :/
  206. putenv("SDL_VIDEO_WINDOW_POS");
  207. putenv("SDL_VIDEO_CENTERED=1");
  208. tlog0 << "Starting... " << std::endl;
  209. timeHandler total, pomtime;
  210. std::cout.flags(std::ios::unitbuf);
  211. logfile = new std::ofstream("VCMI_Client_log.txt");
  212. console = new CConsoleHandler;
  213. *console->cb = boost::bind(&processCommand, _1);
  214. console->start();
  215. atexit(dispose);
  216. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  217. conf.init();
  218. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  219. tlog0 << NAME << std::endl;
  220. srand ( time(NULL) );
  221. //CPG=NULL;
  222. CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  223. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO))
  224. {
  225. tlog1<<"Something was wrong: "<< SDL_GetError() << std::endl;
  226. exit(-1);
  227. }
  228. atexit(SDL_Quit);
  229. setScreenRes(conf.cc.pregameResx, conf.cc.pregameResy, conf.cc.bpp, conf.cc.fullscreen);
  230. tlog0 <<"\tInitializing screen: "<<pomtime.getDif() << std::endl;
  231. // Initialize video
  232. CGI->videoh = new CVideoPlayer;
  233. tlog0<<"\tInitializing video: "<<pomtime.getDif()<<std::endl;
  234. //we can properly play intro only in the main thread, so we have to move loading to the separate thread
  235. boost::thread loading(init);
  236. playIntro();
  237. SDL_FillRect(screen,NULL,0);
  238. SDL_Flip(screen);
  239. loading.join();
  240. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  241. CGI->musich->playMusic(musicBase::mainMenu, -1);
  242. GH.curInt = new CGPreGame; //will set CGP pointer to itself
  243. mainGUIThread = new boost::thread(&CGuiHandler::run, boost::ref(GH));
  244. listenForEvents();
  245. return 0;
  246. }
  247. void processCommand(const std::string &message)
  248. {
  249. std::istringstream readed;
  250. readed.str(message);
  251. std::string cn; //command name
  252. readed >> cn;
  253. int3 src, dst;
  254. // int heronum;//TODO use me
  255. int3 dest;
  256. if(LOCPLINT && LOCPLINT->cingconsole)
  257. LOCPLINT->cingconsole->print(message);
  258. if(message==std::string("die, fool"))
  259. exit(EXIT_SUCCESS);
  260. else if(cn==std::string("activate"))
  261. {
  262. int what;
  263. readed >> what;
  264. switch (what)
  265. {
  266. case 0:
  267. GH.topInt()->activate();
  268. break;
  269. case 1:
  270. adventureInt->activate();
  271. break;
  272. case 2:
  273. LOCPLINT->castleInt->activate();
  274. break;
  275. }
  276. }
  277. else if(cn=="redraw")
  278. {
  279. GH.totalRedraw();
  280. }
  281. else if(cn=="screen")
  282. {
  283. tlog0 << "Screenbuf points to ";
  284. if(screenBuf == screen)
  285. tlog1 << "screen";
  286. else if(screenBuf == screen2)
  287. tlog1 << "screen2";
  288. else
  289. tlog1 << "?!?";
  290. tlog1 << std::endl;
  291. SDL_SaveBMP(screen, "Screen_c.bmp");
  292. SDL_SaveBMP(screen2, "Screen2_c.bmp");
  293. }
  294. else if(cn=="save")
  295. {
  296. std::string fname;
  297. readed >> fname;
  298. client->save(fname);
  299. }
  300. //else if(cn=="list")
  301. //{
  302. // if(CPG)
  303. // for(int i = 0; i < CPG->ourScenSel->mapsel.ourGames.size(); i++)
  304. // tlog0 << i << ".\t" << CPG->ourScenSel->mapsel.ourGames[i]->filename << std::endl;
  305. //}
  306. else if(cn=="load")
  307. {
  308. // TODO: this code should end the running game and manage to call startGame instead
  309. std::string fname;
  310. readed >> fname;
  311. client->loadGame(fname);
  312. }
  313. //else if(cn=="ln")
  314. //{
  315. // int num;
  316. // readed >> num;
  317. // std::string &name = CPG->ourScenSel->mapsel.ourGames[num]->filename;
  318. // client->load(name.substr(0, name.size()-6));
  319. //}
  320. else if(cn=="resolution" || cn == "r")
  321. {
  322. if(LOCPLINT)
  323. {
  324. tlog1 << "Resolution can be set only before starting the game.\n";
  325. return;
  326. }
  327. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  328. int i=1, hlp=1;
  329. tlog4 << "Available screen resolutions:\n";
  330. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  331. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  332. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  333. std::cin >> i;
  334. if(i < 0 || i > conf.guiOptions.size() || std::cin.bad() || std::cin.fail())
  335. {
  336. std::cin.clear();
  337. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  338. }
  339. else if(!i)
  340. {
  341. return;
  342. }
  343. else
  344. {
  345. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  346. conf.cc.resx = j->first.first;
  347. conf.cc.resy = j->first.second;
  348. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  349. }
  350. }
  351. else if(message=="get txt")
  352. {
  353. boost::filesystem::create_directory("Extracted_txts");
  354. tlog0<<"Command accepted. Opening .lod file...\t";
  355. CLodHandler * txth = new CLodHandler;
  356. txth->init(std::string(DATA_DIR "/Data/H3bitmap.lod"),"");
  357. tlog0<<"done.\nScanning .lod file\n";
  358. int curp=0;
  359. std::string pattern = ".TXT", pom;
  360. for(int i=0;i<txth->entries.size(); i++)
  361. {
  362. pom = txth->entries[i].nameStr;
  363. if(boost::algorithm::find_last(pom,pattern))
  364. {
  365. txth->extractFile(std::string(DATA_DIR "/Extracted_txts/")+pom,pom);
  366. }
  367. if(i%8) continue;
  368. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  369. if(p2!=curp)
  370. {
  371. curp = p2;
  372. tlog0<<"\r"<<curp<<"%";
  373. }
  374. }
  375. tlog0<<"\rExtracting done :)\n";
  376. }
  377. else if(cn=="crash")
  378. {
  379. int *ptr = NULL;
  380. *ptr = 666;
  381. //disaster!
  382. }
  383. else if(cn == "onlyai")
  384. {
  385. gOnlyAI = true;
  386. }
  387. else if(cn == "mp" && adventureInt)
  388. {
  389. if(const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(adventureInt->selection))
  390. tlog0 << h->movement << "; max: " << h->maxMovePoints(true) << "/" << h->maxMovePoints(false) << std::endl;
  391. }
  392. else if(cn == "bonuses")
  393. {
  394. tlog0 << "Bonuses of " << adventureInt->selection->getHoverText() << std::endl
  395. << adventureInt->selection->bonuses << std::endl;
  396. tlog0 << "\nInherited bonuses:\n";
  397. TCNodes parents;
  398. adventureInt->selection->getParents(parents);
  399. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  400. {
  401. tlog0 << "\nBonuses from " << typeid(*parent).name() << std::endl << parent->bonuses << std::endl;
  402. }
  403. }
  404. else if(cn == "not dialog")
  405. {
  406. LOCPLINT->showingDialog->setn(false);
  407. }
  408. else if(client && client->serv && client->serv->connected) //send to server
  409. {
  410. PlayerMessage pm(LOCPLINT->playerID,message);
  411. *client->serv << &pm;
  412. }
  413. }
  414. //plays intro, ends when intro is over or button has been pressed (handles events)
  415. void playIntro()
  416. {
  417. if(CGI->videoh->openAndPlayVideo("3DOLOGO.SMK", 60, 40, screen, true))
  418. {
  419. CGI->videoh->openAndPlayVideo("AZVS.SMK", 60, 80, screen, true);
  420. }
  421. }
  422. void dispose()
  423. {
  424. delete logfile;
  425. if (console)
  426. delete console;
  427. }
  428. static void setScreenRes(int w, int h, int bpp, bool fullscreen)
  429. {
  430. // VCMI will only work with 2, 3 or 4 bytes per pixel
  431. amax(bpp, 16);
  432. amin(bpp, 32);
  433. // Try to use the best screen depth for the display
  434. int suggestedBpp = SDL_VideoModeOK(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0));
  435. if(suggestedBpp == 0)
  436. {
  437. tlog1 << "Error: SDL says that " << w << "x" << h << " resolution is not available!\n";
  438. return;
  439. }
  440. bool bufOnScreen = (screenBuf == screen);
  441. if(suggestedBpp != bpp)
  442. {
  443. tlog2 << "Warning: SDL says that " << bpp << "bpp is wrong and suggests " << suggestedBpp << std::endl;
  444. }
  445. if(screen) //screen has been already initialized
  446. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  447. SDL_InitSubSystem(SDL_INIT_VIDEO);
  448. if((screen = SDL_SetVideoMode(w, h, suggestedBpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == NULL)
  449. {
  450. tlog1 << "Requested screen resolution is not available (" << w << "x" << h << "x" << suggestedBpp << "bpp)\n";
  451. throw "Requested screen resolution is not available\n";
  452. }
  453. tlog0 << "New screen flags: " << screen->flags << std::endl;
  454. if(screen2)
  455. SDL_FreeSurface(screen2);
  456. screen2 = CSDL_Ext::copySurface(screen);
  457. SDL_EnableUNICODE(1);
  458. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  459. SDL_ShowCursor(SDL_DISABLE);
  460. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  461. screenLT = Point(0, 0);
  462. #ifdef _WIN32
  463. SDL_SysWMinfo wm;
  464. SDL_VERSION(&wm.version);
  465. int getwm = SDL_GetWMInfo(&wm);
  466. if(getwm == 1)
  467. {
  468. int sw = GetSystemMetrics(SM_CXSCREEN),
  469. sh = GetSystemMetrics(SM_CYSCREEN);
  470. RECT curpos;
  471. GetWindowRect(wm.window,&curpos);
  472. int ourw = curpos.right - curpos.left,
  473. ourh = curpos.bottom - curpos.top;
  474. SetWindowPos(wm.window, 0, (sw - ourw)/2, (sh - ourh)/2, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
  475. }
  476. else
  477. {
  478. tlog3 << "Something went wrong, getwm=" << getwm << std::endl;
  479. tlog3 << "SDL says: " << SDL_GetError() << std::endl;
  480. tlog3 << "Window won't be centered.\n";
  481. }
  482. #endif
  483. //TODO: centering game window on other platforms (or does the environment do their job correctly there?)
  484. screenBuf = bufOnScreen ? screen : screen2;
  485. setResolution = true;
  486. }
  487. static void listenForEvents()
  488. {
  489. while(1) //main SDL events loop
  490. {
  491. SDL_Event *ev = new SDL_Event();
  492. //tlog0 << "Waiting... ";
  493. int ret = SDL_WaitEvent(ev);
  494. //tlog0 << "got " << (int)ev->type;
  495. if (ret == 0 || (ev->type==SDL_QUIT) ||
  496. (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  497. {
  498. if (client)
  499. client->endGame();
  500. if (mainGUIThread)
  501. {
  502. GH.terminate = true;
  503. mainGUIThread->join();
  504. delete mainGUIThread;
  505. mainGUIThread = NULL;
  506. }
  507. delete console;
  508. console = NULL;
  509. SDL_Delay(750);
  510. SDL_Quit();
  511. tlog0 << "Ending...\n";
  512. break;
  513. }
  514. else if(LOCPLINT && ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  515. {
  516. boost::unique_lock<boost::recursive_mutex> lock(*LOCPLINT->pim);
  517. bool full = !(screen->flags&SDL_FULLSCREEN);
  518. setScreenRes(conf.cc.screenx, conf.cc.screeny, conf.cc.bpp, full);
  519. GH.totalRedraw();
  520. delete ev;
  521. continue;
  522. }
  523. else if(ev->type == SDL_USEREVENT)
  524. {
  525. switch(ev->user.code)
  526. {
  527. case 1:
  528. tlog0 << "Changing resolution has been requested\n";
  529. setScreenRes(conf.cc.screenx, conf.cc.screeny, conf.cc.bpp, conf.cc.fullscreen);
  530. break;
  531. case 2:
  532. client->endGame();
  533. delete client;
  534. client = NULL;
  535. delete CGI->dobjinfo;
  536. CGI->dobjinfo = new CDefObjInfoHandler;
  537. CGI->dobjinfo->load();
  538. GH.curInt = CGP;
  539. GH.defActionsDef = 63;
  540. break;
  541. case 3:
  542. client->endGame(false);
  543. break;
  544. }
  545. delete ev;
  546. continue;
  547. }
  548. //tlog0 << " pushing ";
  549. eventsM.lock();
  550. events.push(ev);
  551. eventsM.unlock();
  552. //tlog0 << " done\n";
  553. }
  554. }
  555. void startGame(StartInfo * options)
  556. {
  557. GH.curInt =NULL;
  558. if(gOnlyAI)
  559. {
  560. for(std::map<int, PlayerSettings>::iterator it = options->playerInfos.begin();
  561. it != options->playerInfos.end(); ++it)
  562. {
  563. it->second.human = false;
  564. }
  565. }
  566. if(screen->w != conf.cc.screenx || screen->h != conf.cc.screeny)
  567. {
  568. requestChangingResolution();
  569. //allow event handling thread change resolution
  570. eventsM.unlock();
  571. while(!setResolution) boost::this_thread::sleep(boost::posix_time::milliseconds(50));
  572. eventsM.lock();
  573. }
  574. else
  575. setResolution = true;
  576. client = new CClient;
  577. CPlayerInterface::howManyPeople = 0;
  578. switch(options->mode) //new game
  579. {
  580. case StartInfo::NEW_GAME:
  581. case StartInfo::CAMPAIGN:
  582. client->newGame(NULL, options);
  583. break;
  584. case StartInfo::LOAD_GAME:
  585. std::string fname = options->mapname;
  586. boost::algorithm::erase_last(fname,".vlgm1");
  587. client->loadGame(fname);
  588. break;
  589. }
  590. CGI->musich->stopMusic();
  591. client->connectionHandler = new boost::thread(&CClient::run, client);
  592. }
  593. void requestChangingResolution()
  594. {
  595. //mark that we are going to change resolution
  596. setResolution = false;
  597. //push special event to order event reading thread to change resolution
  598. SDL_Event ev;
  599. ev.type = SDL_USEREVENT;
  600. ev.user.code = 1;
  601. SDL_PushEvent(&ev);
  602. }