CMT.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. startGame(si);
  250. }
  251. mainGUIThread = new boost::thread(&CGuiHandler::run, boost::ref(GH));
  252. listenForEvents();
  253. return 0;
  254. }
  255. void printInfoAboutIntObject(const CIntObject *obj, int level)
  256. {
  257. int tabs = level;
  258. while(tabs--) tlog4 << '\t';
  259. tlog4 << typeid(*obj).name() << " *** " << (obj->active ? "" : "not ") << "active\n";
  260. BOOST_FOREACH(const CIntObject *child, obj->children)
  261. printInfoAboutIntObject(child, level+1);
  262. }
  263. void processCommand(const std::string &message)
  264. {
  265. std::istringstream readed;
  266. readed.str(message);
  267. std::string cn; //command name
  268. readed >> cn;
  269. int3 src, dst;
  270. // int heronum;//TODO use me
  271. int3 dest;
  272. if(LOCPLINT && LOCPLINT->cingconsole)
  273. LOCPLINT->cingconsole->print(message);
  274. if(message==std::string("die, fool"))
  275. exit(EXIT_SUCCESS);
  276. else if(cn==std::string("activate"))
  277. {
  278. int what;
  279. readed >> what;
  280. switch (what)
  281. {
  282. case 0:
  283. GH.topInt()->activate();
  284. break;
  285. case 1:
  286. adventureInt->activate();
  287. break;
  288. case 2:
  289. LOCPLINT->castleInt->activate();
  290. break;
  291. }
  292. }
  293. else if(cn=="redraw")
  294. {
  295. GH.totalRedraw();
  296. }
  297. else if(cn=="screen")
  298. {
  299. tlog0 << "Screenbuf points to ";
  300. if(screenBuf == screen)
  301. tlog1 << "screen";
  302. else if(screenBuf == screen2)
  303. tlog1 << "screen2";
  304. else
  305. tlog1 << "?!?";
  306. tlog1 << std::endl;
  307. SDL_SaveBMP(screen, "Screen_c.bmp");
  308. SDL_SaveBMP(screen2, "Screen2_c.bmp");
  309. }
  310. else if(cn=="save")
  311. {
  312. std::string fname;
  313. readed >> fname;
  314. client->save(fname);
  315. }
  316. //else if(cn=="list")
  317. //{
  318. // if(CPG)
  319. // for(int i = 0; i < CPG->ourScenSel->mapsel.ourGames.size(); i++)
  320. // tlog0 << i << ".\t" << CPG->ourScenSel->mapsel.ourGames[i]->filename << std::endl;
  321. //}
  322. else if(cn=="load")
  323. {
  324. // TODO: this code should end the running game and manage to call startGame instead
  325. std::string fname;
  326. readed >> fname;
  327. client->loadGame(fname);
  328. }
  329. //else if(cn=="ln")
  330. //{
  331. // int num;
  332. // readed >> num;
  333. // std::string &name = CPG->ourScenSel->mapsel.ourGames[num]->filename;
  334. // client->load(name.substr(0, name.size()-6));
  335. //}
  336. else if(cn=="resolution" || cn == "r")
  337. {
  338. if(LOCPLINT)
  339. {
  340. tlog1 << "Resolution can be set only before starting the game.\n";
  341. return;
  342. }
  343. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  344. int i=1, hlp=1;
  345. tlog4 << "Available screen resolutions:\n";
  346. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  347. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  348. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  349. std::cin >> i;
  350. if(i < 0 || i > conf.guiOptions.size() || std::cin.bad() || std::cin.fail())
  351. {
  352. std::cin.clear();
  353. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  354. }
  355. else if(!i)
  356. {
  357. return;
  358. }
  359. else
  360. {
  361. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  362. conf.cc.resx = j->first.first;
  363. conf.cc.resy = j->first.second;
  364. conf.cc.screenx = j->first.first;
  365. conf.cc.screeny = j->first.second;
  366. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  367. }
  368. }
  369. else if(message=="get txt")
  370. {
  371. boost::filesystem::create_directory("Extracted_txts");
  372. tlog0<<"Command accepted. Opening .lod file...\t";
  373. CLodHandler * txth = new CLodHandler;
  374. txth->init(std::string(DATA_DIR "/Data/H3bitmap.lod"),"");
  375. tlog0<<"done.\nScanning .lod file\n";
  376. BOOST_FOREACH(Entry e, txth->entries)
  377. if( e.type == FILE_TEXT )
  378. txth->extractFile(std::string(DATA_DIR "/Extracted_txts/")+e.name,e.name);
  379. tlog0<<"\rExtracting done :)\n";
  380. }
  381. else if(cn=="crash")
  382. {
  383. int *ptr = NULL;
  384. *ptr = 666;
  385. //disaster!
  386. }
  387. else if(cn == "onlyai")
  388. {
  389. gOnlyAI = true;
  390. }
  391. else if (cn == "ai")
  392. {
  393. VLC->IS_AI_ENABLED = !VLC->IS_AI_ENABLED;
  394. tlog4 << "Current AI status: " << (VLC->IS_AI_ENABLED ? "enabled" : "disabled") << std::endl;
  395. }
  396. else if(cn == "mp" && adventureInt)
  397. {
  398. if(const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(adventureInt->selection))
  399. tlog0 << h->movement << "; max: " << h->maxMovePoints(true) << "/" << h->maxMovePoints(false) << std::endl;
  400. }
  401. else if(cn == "bonuses")
  402. {
  403. tlog0 << "Bonuses of " << adventureInt->selection->getHoverText() << std::endl
  404. << adventureInt->selection->bonuses << std::endl;
  405. tlog0 << "\nInherited bonuses:\n";
  406. TCNodes parents;
  407. adventureInt->selection->getParents(parents);
  408. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  409. {
  410. tlog0 << "\nBonuses from " << typeid(*parent).name() << std::endl << parent->bonuses << std::endl;
  411. }
  412. }
  413. else if(cn == "not dialog")
  414. {
  415. LOCPLINT->showingDialog->setn(false);
  416. }
  417. else if(cn == "gui")
  418. {
  419. BOOST_FOREACH(const IShowActivable *child, GH.listInt)
  420. {
  421. if(const CIntObject *obj = dynamic_cast<const CIntObject *>(child))
  422. printInfoAboutIntObject(obj, 0);
  423. else
  424. tlog4 << typeid(*obj).name() << std::endl;
  425. }
  426. }
  427. else if(client && client->serv && client->serv->connected) //send to server
  428. {
  429. PlayerMessage pm(LOCPLINT->playerID,message);
  430. *client->serv << &pm;
  431. }
  432. }
  433. //plays intro, ends when intro is over or button has been pressed (handles events)
  434. void playIntro()
  435. {
  436. if(CCS->videoh->openAndPlayVideo("3DOLOGO.SMK", 60, 40, screen, true))
  437. {
  438. CCS->videoh->openAndPlayVideo("AZVS.SMK", 60, 80, screen, true);
  439. }
  440. }
  441. void dispose()
  442. {
  443. delete logfile;
  444. if (console)
  445. delete console;
  446. }
  447. static void setScreenRes(int w, int h, int bpp, bool fullscreen)
  448. {
  449. // VCMI will only work with 2, 3 or 4 bytes per pixel
  450. amax(bpp, 16);
  451. amin(bpp, 32);
  452. // Try to use the best screen depth for the display
  453. int suggestedBpp = SDL_VideoModeOK(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0));
  454. if(suggestedBpp == 0)
  455. {
  456. tlog1 << "Error: SDL says that " << w << "x" << h << " resolution is not available!\n";
  457. return;
  458. }
  459. bool bufOnScreen = (screenBuf == screen);
  460. if(suggestedBpp != bpp)
  461. {
  462. tlog2 << "Warning: SDL says that " << bpp << "bpp is wrong and suggests " << suggestedBpp << std::endl;
  463. }
  464. if(screen) //screen has been already initialized
  465. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  466. SDL_InitSubSystem(SDL_INIT_VIDEO);
  467. if((screen = SDL_SetVideoMode(w, h, suggestedBpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == NULL)
  468. {
  469. tlog1 << "Requested screen resolution is not available (" << w << "x" << h << "x" << suggestedBpp << "bpp)\n";
  470. throw "Requested screen resolution is not available\n";
  471. }
  472. tlog0 << "New screen flags: " << screen->flags << std::endl;
  473. if(screen2)
  474. SDL_FreeSurface(screen2);
  475. screen2 = CSDL_Ext::copySurface(screen);
  476. SDL_EnableUNICODE(1);
  477. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  478. SDL_ShowCursor(SDL_DISABLE);
  479. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  480. screenLT = Point(0, 0);
  481. #ifdef _WIN32
  482. SDL_SysWMinfo wm;
  483. SDL_VERSION(&wm.version);
  484. int getwm = SDL_GetWMInfo(&wm);
  485. if(getwm == 1)
  486. {
  487. int sw = GetSystemMetrics(SM_CXSCREEN),
  488. sh = GetSystemMetrics(SM_CYSCREEN);
  489. RECT curpos;
  490. GetWindowRect(wm.window,&curpos);
  491. int ourw = curpos.right - curpos.left,
  492. ourh = curpos.bottom - curpos.top;
  493. SetWindowPos(wm.window, 0, (sw - ourw)/2, (sh - ourh)/2, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
  494. }
  495. else
  496. {
  497. tlog3 << "Something went wrong, getwm=" << getwm << std::endl;
  498. tlog3 << "SDL says: " << SDL_GetError() << std::endl;
  499. tlog3 << "Window won't be centered.\n";
  500. }
  501. #endif
  502. //TODO: centering game window on other platforms (or does the environment do their job correctly there?)
  503. screenBuf = bufOnScreen ? screen : screen2;
  504. setResolution = true;
  505. }
  506. static void listenForEvents()
  507. {
  508. while(1) //main SDL events loop
  509. {
  510. SDL_Event *ev = new SDL_Event();
  511. //tlog0 << "Waiting... ";
  512. int ret = SDL_WaitEvent(ev);
  513. //tlog0 << "got " << (int)ev->type;
  514. if (ret == 0 || (ev->type==SDL_QUIT) ||
  515. (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  516. {
  517. if (client)
  518. client->endGame();
  519. if (mainGUIThread)
  520. {
  521. GH.terminate = true;
  522. mainGUIThread->join();
  523. delete mainGUIThread;
  524. mainGUIThread = NULL;
  525. }
  526. delete console;
  527. console = NULL;
  528. SDL_Delay(750);
  529. SDL_Quit();
  530. tlog0 << "Ending...\n";
  531. break;
  532. }
  533. else if(LOCPLINT && ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  534. {
  535. boost::unique_lock<boost::recursive_mutex> lock(*LOCPLINT->pim);
  536. bool full = !(screen->flags&SDL_FULLSCREEN);
  537. setScreenRes(conf.cc.screenx, conf.cc.screeny, conf.cc.bpp, full);
  538. GH.totalRedraw();
  539. delete ev;
  540. continue;
  541. }
  542. else if(ev->type == SDL_USEREVENT)
  543. {
  544. switch(ev->user.code)
  545. {
  546. case 1:
  547. tlog0 << "Changing resolution has been requested\n";
  548. setScreenRes(conf.cc.screenx, conf.cc.screeny, conf.cc.bpp, conf.cc.fullscreen);
  549. break;
  550. case 2:
  551. client->endGame();
  552. delete client;
  553. client = NULL;
  554. delete CGI->dobjinfo;
  555. const_cast<CGameInfo*>(CGI)->dobjinfo = new CDefObjInfoHandler;
  556. const_cast<CGameInfo*>(CGI)->dobjinfo->load();
  557. GH.curInt = CGP;
  558. GH.defActionsDef = 63;
  559. break;
  560. case 3:
  561. client->endGame(false);
  562. break;
  563. }
  564. delete ev;
  565. continue;
  566. }
  567. //tlog0 << " pushing ";
  568. eventsM.lock();
  569. events.push(ev);
  570. eventsM.unlock();
  571. //tlog0 << " done\n";
  572. }
  573. }
  574. void startGame(StartInfo * options, CConnection *serv/* = NULL*/)
  575. {
  576. GH.curInt =NULL;
  577. if(gOnlyAI)
  578. {
  579. for(std::map<int, PlayerSettings>::iterator it = options->playerInfos.begin();
  580. it != options->playerInfos.end(); ++it)
  581. {
  582. it->second.human = false;
  583. }
  584. }
  585. if(screen->w != conf.cc.screenx || screen->h != conf.cc.screeny)
  586. {
  587. requestChangingResolution();
  588. //allow event handling thread change resolution
  589. eventsM.unlock();
  590. while(!setResolution) boost::this_thread::sleep(boost::posix_time::milliseconds(50));
  591. eventsM.lock();
  592. }
  593. else
  594. setResolution = true;
  595. client = new CClient;
  596. CPlayerInterface::howManyPeople = 0;
  597. switch(options->mode) //new game
  598. {
  599. case StartInfo::NEW_GAME:
  600. case StartInfo::CAMPAIGN:
  601. case StartInfo::DUEL:
  602. client->newGame(serv, options);
  603. break;
  604. case StartInfo::LOAD_GAME:
  605. std::string fname = options->mapname;
  606. boost::algorithm::erase_last(fname,".vlgm1");
  607. client->loadGame(fname);
  608. break;
  609. }
  610. CCS->musich->stopMusic();
  611. client->connectionHandler = new boost::thread(&CClient::run, client);
  612. }
  613. void requestChangingResolution()
  614. {
  615. //mark that we are going to change resolution
  616. setResolution = false;
  617. //push special event to order event reading thread to change resolution
  618. SDL_Event ev;
  619. ev.type = SDL_USEREVENT;
  620. ev.user.code = 1;
  621. SDL_PushEvent(&ev);
  622. }