CMT.cpp 18 KB

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