CMT.cpp 20 KB

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