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