CMT.cpp 19 KB

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