CMT.cpp 19 KB

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