CMT.cpp 20 KB

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