CMT.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 "../lib/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. #include "../lib/CScriptingModule.h"
  49. #ifdef _WIN32
  50. #include "SDL_syswm.h"
  51. #endif
  52. #include <boost/foreach.hpp>
  53. #include "../lib/CDefObjInfoHandler.h"
  54. #if __MINGW32__
  55. #undef main
  56. #endif
  57. namespace po = boost::program_options;
  58. /*
  59. * CMT.cpp, part of VCMI engine
  60. *
  61. * Authors: listed in file AUTHORS in main folder
  62. *
  63. * License: GNU General Public License v2.0 or later
  64. * Full text of license available in license.txt file, in main folder
  65. *
  66. */
  67. std::string NAME_AFFIX = "client";
  68. std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
  69. CGuiHandler GH;
  70. static CClient *client;
  71. SDL_Surface *screen = NULL, //main screen surface
  72. *screen2 = NULL,//and hlp surface (used to store not-active interfaces layer)
  73. *screenBuf = screen; //points to screen (if only advmapint is present) or screen2 (else) - should be used when updating controls which are not regularly redrawed
  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. static bool ermInteractiveMode = false; //structurize when time is right
  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. CCS->musich->init();
  129. CCS->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. CLoadFile *replayLoader;
  171. #ifdef _WIN32
  172. int _tmain(int argc, _TCHAR* argv[])
  173. #else
  174. int main(int argc, char** argv)
  175. #endif
  176. {
  177. tlog0 << "Starting... " << std::endl;
  178. po::options_description opts("Allowed options");
  179. opts.add_options()
  180. ("help,h", "display help and exit")
  181. ("version,v", "display version information and exit")
  182. ("battle,b", po::value<std::string>(), "runs game in duel mode (battle-only)")
  183. ("replay,r", "replays a recorded battle, use together with -b");
  184. ("nointro,i", "skips intro movies");
  185. po::variables_map vm;
  186. if(argc > 1)
  187. {
  188. try
  189. {
  190. po::store(po::parse_command_line(argc, argv, opts), vm);
  191. }
  192. catch(std::exception &e)
  193. {
  194. tlog1 << "Failure during parsing command-line options:\n" << e.what() << std::endl;
  195. }
  196. }
  197. po::notify(vm);
  198. if(vm.count("help"))
  199. {
  200. prog_help(0);
  201. return 0;
  202. }
  203. if(vm.count("version"))
  204. {
  205. prog_version();
  206. return 0;
  207. }
  208. //Set environment vars to make window centered. Sometimes work, sometimes not. :/
  209. putenv((char*)"SDL_VIDEO_WINDOW_POS");
  210. putenv((char*)"SDL_VIDEO_CENTERED=1");
  211. timeHandler total, pomtime;
  212. std::cout.flags(std::ios::unitbuf);
  213. logfile = new std::ofstream("VCMI_Client_log.txt");
  214. console = new CConsoleHandler;
  215. *console->cb = boost::bind(&processCommand, _1);
  216. console->start();
  217. atexit(dispose);
  218. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  219. conf.init();
  220. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  221. tlog0 << NAME << std::endl;
  222. srand ( time(NULL) );
  223. CCS = new CClientState;
  224. CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler etc.)
  225. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO))
  226. {
  227. tlog1<<"Something was wrong: "<< SDL_GetError() << std::endl;
  228. exit(-1);
  229. }
  230. atexit(SDL_Quit);
  231. setScreenRes(conf.cc.pregameResx, conf.cc.pregameResy, conf.cc.bpp, conf.cc.fullscreen);
  232. tlog0 <<"\tInitializing screen: "<<pomtime.getDif() << std::endl;
  233. // Initialize video
  234. #if defined _M_X64 && defined _WIN32 //Win64 -> cannot load 32-bit DLLs for video handling
  235. CCS->videoh = new CEmptyVideoPlayer;
  236. #else
  237. CCS->videoh = new CVideoPlayer;
  238. #endif
  239. tlog0<<"\tInitializing video: "<<pomtime.getDif()<<std::endl;
  240. //we can properly play intro only in the main thread, so we have to move loading to the separate thread
  241. boost::thread loading(init);
  242. if(!vm.count("battle") && !vm.count("nointro"))
  243. playIntro();
  244. SDL_FillRect(screen,NULL,0);
  245. CSDL_Ext::update(screen);
  246. loading.join();
  247. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  248. if(!vm.count("battle"))
  249. {
  250. //CCS->musich->playMusic(musicBase::mainMenu, -1);
  251. GH.curInt = new CGPreGame; //will set CGP pointer to itself
  252. }
  253. else
  254. {
  255. StartInfo *si = new StartInfo();
  256. if(vm.count("replay"))
  257. {
  258. si->mode = StartInfo::DUEL_REPLAY;
  259. replayLoader = new CLoadFile(vm["battle"].as<std::string>());
  260. replayLoader->smartPointerSerialization = false;
  261. if(!replayLoader->sfile)
  262. {
  263. tlog1 << "Cannot find file with recorded battle (" << si->mapname << ")!\n";
  264. exit(1);
  265. }
  266. std::string bname, ai1, ai2;
  267. ui8 magic;
  268. *replayLoader >> bname >> ai1 >> ai2 >> magic;
  269. assert(magic == '$');
  270. si->mapname = bname;
  271. tlog0 << "Replaying battle between " <<ai1 << " and " << ai2 << " on " << bname << std::endl;
  272. }
  273. else
  274. si->mode = StartInfo::DUEL;
  275. startGame(si);
  276. }
  277. mainGUIThread = new boost::thread(&CGuiHandler::run, boost::ref(GH));
  278. listenForEvents();
  279. return 0;
  280. }
  281. void printInfoAboutIntObject(const CIntObject *obj, int level)
  282. {
  283. int tabs = level;
  284. while(tabs--) tlog4 << '\t';
  285. tlog4 << typeid(*obj).name() << " *** " << (obj->active ? "" : "not ") << "active\n";
  286. BOOST_FOREACH(const CIntObject *child, obj->children)
  287. printInfoAboutIntObject(child, level+1);
  288. }
  289. void processCommand(const std::string &message)
  290. {
  291. std::istringstream readed;
  292. readed.str(message);
  293. std::string cn; //command name
  294. readed >> cn;
  295. if(LOCPLINT && LOCPLINT->cingconsole)
  296. LOCPLINT->cingconsole->print(message);
  297. if(ermInteractiveMode)
  298. {
  299. if(cn == "exit")
  300. {
  301. ermInteractiveMode = false;
  302. return;
  303. }
  304. else
  305. {
  306. if(client && client->erm)
  307. client->erm->executeUserCommand(message);
  308. tlog0 << "erm>";
  309. }
  310. }
  311. else if(message==std::string("die, fool"))
  312. {
  313. exit(EXIT_SUCCESS);
  314. }
  315. else if(cn == "erm")
  316. {
  317. ermInteractiveMode = true;
  318. tlog0 << "erm>";
  319. }
  320. else if(cn==std::string("activate"))
  321. {
  322. int what;
  323. readed >> what;
  324. switch (what)
  325. {
  326. case 0:
  327. GH.topInt()->activate();
  328. break;
  329. case 1:
  330. adventureInt->activate();
  331. break;
  332. case 2:
  333. LOCPLINT->castleInt->activate();
  334. break;
  335. }
  336. }
  337. else if(cn=="redraw")
  338. {
  339. GH.totalRedraw();
  340. }
  341. else if(cn=="screen")
  342. {
  343. tlog0 << "Screenbuf points to ";
  344. if(screenBuf == screen)
  345. tlog1 << "screen";
  346. else if(screenBuf == screen2)
  347. tlog1 << "screen2";
  348. else
  349. tlog1 << "?!?";
  350. tlog1 << std::endl;
  351. SDL_SaveBMP(screen, "Screen_c.bmp");
  352. SDL_SaveBMP(screen2, "Screen2_c.bmp");
  353. }
  354. else if(cn=="save")
  355. {
  356. std::string fname;
  357. readed >> fname;
  358. client->save(fname);
  359. }
  360. //else if(cn=="list")
  361. //{
  362. // if(CPG)
  363. // for(int i = 0; i < CPG->ourScenSel->mapsel.ourGames.size(); i++)
  364. // tlog0 << i << ".\t" << CPG->ourScenSel->mapsel.ourGames[i]->filename << std::endl;
  365. //}
  366. else if(cn=="load")
  367. {
  368. // TODO: this code should end the running game and manage to call startGame instead
  369. std::string fname;
  370. readed >> fname;
  371. client->loadGame(fname);
  372. }
  373. //else if(cn=="ln")
  374. //{
  375. // int num;
  376. // readed >> num;
  377. // std::string &name = CPG->ourScenSel->mapsel.ourGames[num]->filename;
  378. // client->load(name.substr(0, name.size()-6));
  379. //}
  380. else if(cn=="resolution" || cn == "r")
  381. {
  382. if(LOCPLINT)
  383. {
  384. tlog1 << "Resolution can be set only before starting the game.\n";
  385. return;
  386. }
  387. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  388. int i=1, hlp=1;
  389. tlog4 << "Available screen resolutions:\n";
  390. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  391. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  392. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  393. std::cin >> i;
  394. if(i < 0 || i > conf.guiOptions.size() || std::cin.bad() || std::cin.fail())
  395. {
  396. std::cin.clear();
  397. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  398. }
  399. else if(!i)
  400. {
  401. return;
  402. }
  403. else
  404. {
  405. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  406. conf.cc.resx = conf.cc.screenx = j->first.first;
  407. conf.cc.resy = conf.cc.screeny = j->first.second;
  408. conf.SetResolution(conf.cc.screenx, conf.cc.screeny);
  409. tlog0 << "Screen resolution set to " << conf.cc.screenx << " x " << conf.cc.screeny <<". It will be applied when the game starts.\n";
  410. }
  411. }
  412. else if(message=="get txt")
  413. {
  414. boost::filesystem::create_directory("Extracted_txts");
  415. tlog0<<"Command accepted. Opening .lod file...\t";
  416. CLodHandler * txth = new CLodHandler;
  417. txth->init(std::string(DATA_DIR "/Data/H3bitmap.lod"),"");
  418. tlog0<<"done.\nScanning .lod file\n";
  419. BOOST_FOREACH(Entry e, txth->entries)
  420. if( e.type == FILE_TEXT )
  421. txth->extractFile(std::string(DATA_DIR "/Extracted_txts/")+e.name,e.name);
  422. tlog0<<"\rExtracting done :)\n";
  423. }
  424. else if(cn=="crash")
  425. {
  426. int *ptr = NULL;
  427. *ptr = 666;
  428. //disaster!
  429. }
  430. else if(cn == "onlyai")
  431. {
  432. gOnlyAI = true;
  433. }
  434. else if (cn == "ai")
  435. {
  436. VLC->IS_AI_ENABLED = !VLC->IS_AI_ENABLED;
  437. tlog4 << "Current AI status: " << (VLC->IS_AI_ENABLED ? "enabled" : "disabled") << std::endl;
  438. }
  439. else if(cn == "mp" && adventureInt)
  440. {
  441. if(const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(adventureInt->selection))
  442. tlog0 << h->movement << "; max: " << h->maxMovePoints(true) << "/" << h->maxMovePoints(false) << std::endl;
  443. }
  444. else if(cn == "bonuses")
  445. {
  446. tlog0 << "Bonuses of " << adventureInt->selection->getHoverText() << std::endl
  447. << adventureInt->selection->getBonusList() << std::endl;
  448. tlog0 << "\nInherited bonuses:\n";
  449. TCNodes parents;
  450. adventureInt->selection->getParents(parents);
  451. BOOST_FOREACH(const CBonusSystemNode *parent, parents)
  452. {
  453. tlog0 << "\nBonuses from " << typeid(*parent).name() << std::endl << parent->getBonusList() << std::endl;
  454. }
  455. }
  456. else if(cn == "not dialog")
  457. {
  458. LOCPLINT->showingDialog->setn(false);
  459. }
  460. else if(cn == "gui")
  461. {
  462. BOOST_FOREACH(const IShowActivable *child, GH.listInt)
  463. {
  464. if(const CIntObject *obj = dynamic_cast<const CIntObject *>(child))
  465. printInfoAboutIntObject(obj, 0);
  466. else
  467. tlog4 << typeid(*obj).name() << std::endl;
  468. }
  469. }
  470. else if(cn=="tell")
  471. {
  472. std::string what;
  473. int id1, id2;
  474. readed >> what >> id1 >> id2;
  475. if(what == "hs")
  476. {
  477. BOOST_FOREACH(const CGHeroInstance *h, LOCPLINT->cb->getHeroesInfo())
  478. if(h->type->ID == id1)
  479. if(const CArtifactInstance *a = h->getArt(id2))
  480. tlog4 << a->nodeName();
  481. }
  482. else if (what == "anim" )
  483. {
  484. CAnimation::getAnimInfo();
  485. }
  486. }
  487. else if (cn == "switchCreWin" )
  488. {
  489. conf.cc.classicCreatureWindow = !conf.cc.classicCreatureWindow;
  490. }
  491. else if(client && client->serv && client->serv->connected) //send to server
  492. {
  493. PlayerMessage pm(LOCPLINT->playerID,message);
  494. *client->serv << &pm;
  495. }
  496. }
  497. //plays intro, ends when intro is over or button has been pressed (handles events)
  498. void playIntro()
  499. {
  500. if(CCS->videoh->openAndPlayVideo("3DOLOGO.SMK", 60, 40, screen, true))
  501. {
  502. CCS->videoh->openAndPlayVideo("AZVS.SMK", 60, 80, screen, true);
  503. }
  504. }
  505. void dispose()
  506. {
  507. if (console)
  508. delete console;
  509. delete logfile;
  510. }
  511. static void setScreenRes(int w, int h, int bpp, bool fullscreen)
  512. {
  513. // VCMI will only work with 2, 3 or 4 bytes per pixel
  514. amax(bpp, 16);
  515. amin(bpp, 32);
  516. // Try to use the best screen depth for the display
  517. int suggestedBpp = SDL_VideoModeOK(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0));
  518. if(suggestedBpp == 0)
  519. {
  520. tlog1 << "Error: SDL says that " << w << "x" << h << " resolution is not available!\n";
  521. return;
  522. }
  523. bool bufOnScreen = (screenBuf == screen);
  524. if(suggestedBpp != bpp)
  525. {
  526. tlog2 << "Warning: SDL says that " << bpp << "bpp is wrong and suggests " << suggestedBpp << std::endl;
  527. }
  528. if(screen) //screen has been already initialized
  529. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  530. SDL_InitSubSystem(SDL_INIT_VIDEO);
  531. if((screen = SDL_SetVideoMode(w, h, suggestedBpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == NULL)
  532. {
  533. tlog1 << "Requested screen resolution is not available (" << w << "x" << h << "x" << suggestedBpp << "bpp)\n";
  534. throw "Requested screen resolution is not available\n";
  535. }
  536. tlog0 << "New screen flags: " << screen->flags << std::endl;
  537. if(screen2)
  538. SDL_FreeSurface(screen2);
  539. screen2 = CSDL_Ext::copySurface(screen);
  540. SDL_EnableUNICODE(1);
  541. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  542. SDL_ShowCursor(SDL_DISABLE);
  543. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  544. #ifdef _WIN32
  545. SDL_SysWMinfo wm;
  546. SDL_VERSION(&wm.version);
  547. int getwm = SDL_GetWMInfo(&wm);
  548. if(getwm == 1)
  549. {
  550. int sw = GetSystemMetrics(SM_CXSCREEN),
  551. sh = GetSystemMetrics(SM_CYSCREEN);
  552. RECT curpos;
  553. GetWindowRect(wm.window,&curpos);
  554. int ourw = curpos.right - curpos.left,
  555. ourh = curpos.bottom - curpos.top;
  556. SetWindowPos(wm.window, 0, (sw - ourw)/2, (sh - ourh)/2, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
  557. }
  558. else
  559. {
  560. tlog3 << "Something went wrong, getwm=" << getwm << std::endl;
  561. tlog3 << "SDL says: " << SDL_GetError() << std::endl;
  562. tlog3 << "Window won't be centered.\n";
  563. }
  564. #endif
  565. //TODO: centering game window on other platforms (or does the environment do their job correctly there?)
  566. screenBuf = bufOnScreen ? screen : screen2;
  567. setResolution = true;
  568. }
  569. static void listenForEvents()
  570. {
  571. while(1) //main SDL events loop
  572. {
  573. SDL_Event *ev = new SDL_Event();
  574. //tlog0 << "Waiting... ";
  575. int ret = SDL_WaitEvent(ev);
  576. //tlog0 << "got " << (int)ev->type;
  577. if (ret == 0 || (ev->type==SDL_QUIT) ||
  578. (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  579. {
  580. if (client)
  581. client->endGame();
  582. if (mainGUIThread)
  583. {
  584. GH.terminate = true;
  585. mainGUIThread->join();
  586. delete mainGUIThread;
  587. mainGUIThread = NULL;
  588. }
  589. delete console;
  590. console = NULL;
  591. SDL_Delay(750);
  592. SDL_Quit();
  593. tlog0 << "Ending...\n";
  594. break;
  595. }
  596. else if(LOCPLINT && ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  597. {
  598. boost::unique_lock<boost::recursive_mutex> lock(*LOCPLINT->pim);
  599. bool full = !(screen->flags&SDL_FULLSCREEN);
  600. setScreenRes(conf.cc.screenx, conf.cc.screeny, conf.cc.bpp, full);
  601. GH.totalRedraw();
  602. delete ev;
  603. continue;
  604. }
  605. else if(ev->type == SDL_USEREVENT)
  606. {
  607. switch(ev->user.code)
  608. {
  609. case 1:
  610. tlog0 << "Changing resolution has been requested\n";
  611. setScreenRes(conf.cc.screenx, conf.cc.screeny, conf.cc.bpp, conf.cc.fullscreen);
  612. break;
  613. case 2:
  614. client->endGame();
  615. delete client;
  616. client = NULL;
  617. delete CGI->dobjinfo.get();
  618. const_cast<CGameInfo*>(CGI)->dobjinfo = new CDefObjInfoHandler;
  619. const_cast<CGameInfo*>(CGI)->dobjinfo->load();
  620. GH.curInt = CGP;
  621. GH.defActionsDef = 63;
  622. break;
  623. case 3:
  624. client->endGame(false);
  625. break;
  626. }
  627. delete ev;
  628. continue;
  629. }
  630. //tlog0 << " pushing ";
  631. eventsM.lock();
  632. events.push(ev);
  633. eventsM.unlock();
  634. //tlog0 << " done\n";
  635. }
  636. }
  637. void startGame(StartInfo * options, CConnection *serv/* = NULL*/)
  638. {
  639. GH.curInt =NULL;
  640. SDL_FillRect(screen, 0, 0);
  641. if(gOnlyAI)
  642. {
  643. for(std::map<int, PlayerSettings>::iterator it = options->playerInfos.begin();
  644. it != options->playerInfos.end(); ++it)
  645. {
  646. it->second.human = false;
  647. }
  648. }
  649. if(screen->w != conf.cc.screenx || screen->h != conf.cc.screeny)
  650. {
  651. requestChangingResolution();
  652. //allow event handling thread change resolution
  653. eventsM.unlock();
  654. while(!setResolution) boost::this_thread::sleep(boost::posix_time::milliseconds(50));
  655. eventsM.lock();
  656. }
  657. else
  658. setResolution = true;
  659. client = new CClient;
  660. CPlayerInterface::howManyPeople = 0;
  661. switch(options->mode) //new game
  662. {
  663. case StartInfo::NEW_GAME:
  664. case StartInfo::CAMPAIGN:
  665. client->newGame(serv, options);
  666. break;
  667. case StartInfo::DUEL:
  668. case StartInfo::DUEL_REPLAY:
  669. client->newDuel(serv, options);
  670. break;
  671. case StartInfo::LOAD_GAME:
  672. std::string fname = options->mapname;
  673. boost::algorithm::erase_last(fname,".vlgm1");
  674. client->loadGame(fname);
  675. break;
  676. }
  677. if(client->serv)
  678. client->connectionHandler = new boost::thread(&CClient::run, client);
  679. else
  680. client->connectionHandler = new boost::thread(&CClient::runReplay, client, replayLoader);
  681. }
  682. void requestChangingResolution()
  683. {
  684. //mark that we are going to change resolution
  685. setResolution = false;
  686. //push special event to order event reading thread to change resolution
  687. SDL_Event ev;
  688. ev.type = SDL_USEREVENT;
  689. ev.user.code = 1;
  690. SDL_PushEvent(&ev);
  691. }