CMT.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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_ttf.h>
  13. #include <SDL_mixer.h>
  14. #include "SDL_Extensions.h"
  15. #include "SDL_framerate.h"
  16. #include "CGameInfo.h"
  17. #include "../mapHandler.h"
  18. #include "../global.h"
  19. #include "CPreGame.h"
  20. #include "CCastleInterface.h"
  21. #include "../CConsoleHandler.h"
  22. #include "CCursorHandler.h"
  23. #include "../lib/CGameState.h"
  24. #include "../CCallback.h"
  25. #include "CPlayerInterface.h"
  26. #include "CAdvmapInterface.h"
  27. #include "../hch/CBuildingHandler.h"
  28. #include "../hch/CVideoHandler.h"
  29. #include "../hch/CHeroHandler.h"
  30. #include "../hch/CCreatureHandler.h"
  31. #include "../hch/CSpellHandler.h"
  32. #include "../hch/CMusicHandler.h"
  33. #include "../hch/CVideoHandler.h"
  34. #include "../hch/CLodHandler.h"
  35. #include "../hch/CDefHandler.h"
  36. #include "../hch/CAmbarCendamo.h"
  37. #include "../hch/CGeneralTextHandler.h"
  38. #include "Graphics.h"
  39. #include "Client.h"
  40. #include "CConfigHandler.h"
  41. #include "../lib/Connection.h"
  42. #include "../lib/VCMI_Lib.h"
  43. #include "../lib/VCMIDirs.h"
  44. #include <cstdlib>
  45. #include "../lib/NetPacks.h"
  46. #if __MINGW32__
  47. #undef main
  48. #endif
  49. /*
  50. * CMT.cpp, part of VCMI engine
  51. *
  52. * Authors: listed in file AUTHORS in main folder
  53. *
  54. * License: GNU General Public License v2.0 or later
  55. * Full text of license available in license.txt file, in main folder
  56. *
  57. */
  58. std::string NAME_AFFIX = "client";
  59. std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
  60. CGuiHandler GH;
  61. static CClient *client;
  62. SDL_Surface *screen = NULL, //main screen surface
  63. *screen2 = NULL,//and hlp surface (used to store not-active interfaces layer)
  64. *screenBuf = screen; //points to screen (if only advmapint is present) or screen2 (else) - should be used when updating controls which are not regularly redrawed
  65. static boost::thread *hhh;
  66. SystemOptions GDefaultOptions;
  67. VCMIDirs GVCMIDirs;
  68. std::queue<SDL_Event*> events;
  69. boost::mutex eventsM;
  70. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  71. static bool gOnlyAI = false;
  72. void processCommand(const std::string &message);
  73. static void setScreenRes(int w, int h, int bpp, bool fullscreen);
  74. void dispose();
  75. void playIntro();
  76. static void listenForEvents();
  77. void init()
  78. {
  79. timeHandler tmh, pomtime;
  80. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  81. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  82. #else
  83. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  84. #endif
  85. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  86. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  87. TTF_Init();
  88. atexit(TTF_Quit);
  89. TNRB16 = TTF_OpenFont(DATA_DIR "/Fonts/tnrb.ttf",16);
  90. GEOR13 = TTF_OpenFont(DATA_DIR "/Fonts/georgia.ttf",13);
  91. GEOR16 = TTF_OpenFont(DATA_DIR "/Fonts/georgia.ttf",16);
  92. GEORXX = TTF_OpenFont(DATA_DIR "/Fonts/tnrb.ttf",22);
  93. GEORM = TTF_OpenFont(DATA_DIR "/Fonts/georgia.ttf",10);
  94. if(! (TNRB16 && GEOR16 && GEORXX && GEORM))
  95. {
  96. tlog1 << "One of the fonts couldn't be loaded!\n";
  97. exit(-1);
  98. }
  99. THC tlog0<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  100. {
  101. //read system options
  102. CLoadFile settings(GVCMIDirs.UserPath + "/config/sysopts.bin");
  103. if(settings.sfile)
  104. {
  105. settings >> GDefaultOptions;
  106. }
  107. else //file not found (probably, may be also some kind of access problem
  108. {
  109. tlog2 << "Warning: Cannot read system options, default settings will be used.\n";
  110. //Try to create file
  111. tlog2 << "VCMI will try to save default system options...\n";
  112. GDefaultOptions.settingsChanged();
  113. }
  114. }
  115. THC tlog0<<"\tLoading default system settings: "<<pomtime.getDif()<<std::endl;
  116. //initializing audio
  117. // Note: because of interface button range, volume can only be a
  118. // multiple of 11, from 0 to 99.
  119. CGI->soundh = new CSoundHandler;
  120. CGI->soundh->init();
  121. CGI->soundh->setVolume(GDefaultOptions.soundVolume);
  122. CGI->musich = new CMusicHandler;
  123. //CGI->musich->init();
  124. //CGI->musich->setVolume(GDefaultOptions.musicVolume);
  125. tlog0<<"\tInitializing sound: "<<pomtime.getDif()<<std::endl;
  126. tlog0<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  127. initDLL(::console,logfile);
  128. CGI->setFromLib();
  129. CGI->soundh->initCreaturesSounds(CGI->creh->creatures);
  130. CGI->soundh->initSpellsSounds(CGI->spellh->spells);
  131. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  132. pomtime.getDif();
  133. CGI->curh = new CCursorHandler;
  134. CGI->curh->initCursor();
  135. CGI->curh->show();
  136. tlog0<<"Screen handler: "<<pomtime.getDif()<<std::endl;
  137. pomtime.getDif();
  138. graphics = new Graphics();
  139. graphics->loadHeroAnims();
  140. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  141. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  142. CMessage::init();
  143. tlog0<<"Message handler: "<<tmh.getDif()<<std::endl;
  144. //CPG = new CPreGame(); //main menu and submenus
  145. //tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  146. }
  147. #ifndef __GNUC__
  148. int _tmain(int argc, _TCHAR* argv[])
  149. #else
  150. int main(int argc, char** argv)
  151. #endif
  152. {
  153. putenv("SDL_VIDEO_WINDOW_POS");
  154. putenv("SDL_VIDEO_CENTERED=1");
  155. tlog0 << "Starting... " << std::endl;
  156. timeHandler total, pomtime;
  157. std::cout.flags(std::ios::unitbuf);
  158. logfile = new std::ofstream("VCMI_Client_log.txt");
  159. console = new CConsoleHandler;
  160. *console->cb = boost::bind(&processCommand, _1);
  161. console->start();
  162. atexit(dispose);
  163. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  164. conf.init();
  165. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  166. tlog0 << NAME << std::endl;
  167. srand ( time(NULL) );
  168. //CPG=NULL;
  169. CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  170. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO))
  171. {
  172. tlog1<<"Something was wrong: "<< SDL_GetError() << std::endl;
  173. exit(-1);
  174. }
  175. atexit(SDL_Quit);
  176. setScreenRes(800,600,conf.cc.bpp,conf.cc.fullscreen);
  177. tlog0 <<"\tInitializing screen: "<<pomtime.getDif() << std::endl;
  178. // Initialize video
  179. CGI->videoh = new CVideoPlayer;
  180. tlog0<<"\tInitializing video: "<<pomtime.getDif()<<std::endl;
  181. //we can properly play intro only in the main thread, so we have to move loading to the separate thread
  182. boost::thread loading(init);
  183. playIntro();
  184. SDL_FillRect(screen,NULL,0);
  185. SDL_Flip(screen);
  186. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  187. loading.join();
  188. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  189. CGI->musich->playMusic(musicBase::mainMenu, -1);
  190. new CGPreGame; //will set CGP pointer to itself
  191. hhh = new boost::thread(&CGPreGame::run, CGP);
  192. listenForEvents();
  193. return 0;
  194. }
  195. void processCommand(const std::string &message)
  196. {
  197. std::istringstream readed;
  198. readed.str(message);
  199. std::string cn; //command name
  200. readed >> cn;
  201. int3 src, dst;
  202. // int heronum;//TODO use me
  203. int3 dest;
  204. if(LOCPLINT && LOCPLINT->cingconsole)
  205. LOCPLINT->cingconsole->print(message);
  206. if(message==std::string("die, fool"))
  207. exit(EXIT_SUCCESS);
  208. else if(cn==std::string("activate"))
  209. {
  210. int what;
  211. readed >> what;
  212. switch (what)
  213. {
  214. case 0:
  215. GH.topInt()->activate();
  216. break;
  217. case 1:
  218. LOCPLINT->adventureInt->activate();
  219. break;
  220. case 2:
  221. LOCPLINT->castleInt->activate();
  222. break;
  223. }
  224. }
  225. else if(cn=="redraw")
  226. {
  227. GH.totalRedraw();
  228. }
  229. else if(cn=="screen")
  230. {
  231. tlog0 << "Screenbuf points to ";
  232. if(screenBuf == screen)
  233. tlog1 << "screen";
  234. else if(screenBuf == screen2)
  235. tlog1 << "screen2";
  236. else
  237. tlog1 << "?!?";
  238. tlog1 << std::endl;
  239. SDL_SaveBMP(screen, "Screen_c.bmp");
  240. SDL_SaveBMP(screen2, "Screen2_c.bmp");
  241. }
  242. else if(cn=="save")
  243. {
  244. std::string fname;
  245. readed >> fname;
  246. client->save(fname);
  247. }
  248. //else if(cn=="list")
  249. //{
  250. // if(CPG)
  251. // for(int i = 0; i < CPG->ourScenSel->mapsel.ourGames.size(); i++)
  252. // tlog0 << i << ".\t" << CPG->ourScenSel->mapsel.ourGames[i]->filename << std::endl;
  253. //}
  254. else if(cn=="load")
  255. {
  256. // TODO: this code should end the running game and manage to call startGame instead
  257. std::string fname;
  258. readed >> fname;
  259. client->loadGame(fname);
  260. }
  261. //else if(cn=="ln")
  262. //{
  263. // int num;
  264. // readed >> num;
  265. // std::string &name = CPG->ourScenSel->mapsel.ourGames[num]->filename;
  266. // client->load(name.substr(0, name.size()-6));
  267. //}
  268. else if(cn=="resolution" || cn == "r")
  269. {
  270. if(LOCPLINT)
  271. {
  272. tlog1 << "Resolution can be set only before starting the game.\n";
  273. return;
  274. }
  275. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  276. int i=1, hlp=1;
  277. tlog4 << "Available screen resolutions:\n";
  278. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  279. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  280. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  281. std::cin >> i;
  282. if(i < 0 || i > conf.guiOptions.size() || std::cin.bad() || std::cin.fail())
  283. {
  284. std::cin.clear();
  285. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  286. }
  287. else if(!i)
  288. {
  289. return;
  290. }
  291. else
  292. {
  293. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  294. conf.cc.resx = j->first.first;
  295. conf.cc.resy = j->first.second;
  296. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  297. }
  298. }
  299. else if(message=="get txt")
  300. {
  301. boost::filesystem::create_directory("Extracted_txts");
  302. tlog0<<"Command accepted. Opening .lod file...\t";
  303. CLodHandler * txth = new CLodHandler;
  304. txth->init(std::string(DATA_DIR "/Data/H3bitmap.lod"),"");
  305. tlog0<<"done.\nScanning .lod file\n";
  306. int curp=0;
  307. std::string pattern = ".TXT", pom;
  308. for(int i=0;i<txth->entries.size(); i++)
  309. {
  310. pom = txth->entries[i].nameStr;
  311. if(boost::algorithm::find_last(pom,pattern))
  312. {
  313. txth->extractFile(std::string(DATA_DIR "/Extracted_txts/")+pom,pom);
  314. }
  315. if(i%8) continue;
  316. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  317. if(p2!=curp)
  318. {
  319. curp = p2;
  320. tlog0<<"\r"<<curp<<"%";
  321. }
  322. }
  323. tlog0<<"\rExtracting done :)\n";
  324. }
  325. else if(cn=="crash")
  326. {
  327. int *ptr = NULL;
  328. *ptr = 666;
  329. //disaster!
  330. }
  331. else if(cn == "onlyai")
  332. {
  333. gOnlyAI = true;
  334. }
  335. else if(client && client->serv && client->serv->connected) //send to server
  336. {
  337. PlayerMessage pm(LOCPLINT->playerID,message);
  338. *client->serv << &pm;
  339. }
  340. }
  341. //plays intro, ends when intro is over or button has been pressed (handles events)
  342. void playIntro()
  343. {
  344. if(CGI->videoh->openAndPlayVideo("3DOLOGO.SMK", 60, 40, screen, true))
  345. {
  346. CGI->videoh->openAndPlayVideo("AZVS.SMK", 60, 80, screen, true);
  347. }
  348. }
  349. void dispose()
  350. {
  351. delete logfile;
  352. if (console)
  353. delete console;
  354. }
  355. static void setScreenRes(int w, int h, int bpp, bool fullscreen)
  356. {
  357. // VCMI will only work with 3 or 4 bytes per pixel
  358. if (bpp < 24) bpp = 24;
  359. if (bpp > 32) bpp = 32;
  360. // Try to use the best screen depth for the display
  361. int suggestedBpp = SDL_VideoModeOK(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0));
  362. if(suggestedBpp == 0)
  363. {
  364. tlog1 << "Error: SDL says that " << w << "x" << h << " resolution is not available!\n";
  365. return;
  366. }
  367. bool bufOnScreen = (screenBuf == screen);
  368. if(suggestedBpp != bpp)
  369. {
  370. tlog2 << "Warning: SDL says that " << bpp << "bpp is wrong and suggests " << suggestedBpp << std::endl;
  371. }
  372. if(screen) //screen has been already initialized
  373. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  374. SDL_InitSubSystem(SDL_INIT_VIDEO);
  375. if((screen = SDL_SetVideoMode(w, h, suggestedBpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == NULL)
  376. {
  377. tlog1 << "Requested screen resolution is not available (" << w << "x" << h << "x" << suggestedBpp << "bpp)\n";
  378. throw "Requested screen resolution is not available\n";
  379. }
  380. tlog0 << "New screen flags: " << screen->flags << std::endl;
  381. if(screen2)
  382. SDL_FreeSurface(screen2);
  383. screen2 = CSDL_Ext::copySurface(screen);
  384. SDL_EnableUNICODE(1);
  385. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  386. SDL_ShowCursor(SDL_DISABLE);
  387. screenBuf = bufOnScreen ? screen : screen2;
  388. }
  389. static void listenForEvents()
  390. {
  391. while(1) //main SDL events loop
  392. {
  393. SDL_Event *ev = new SDL_Event();
  394. //tlog0 << "Waiting... ";
  395. int ret = SDL_WaitEvent(ev);
  396. //tlog0 << "got " << (int)ev->type;
  397. if (ret == 0 || (ev->type==SDL_QUIT) ||
  398. (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  399. {
  400. if (client)
  401. client->stop();
  402. if (hhh) {
  403. CGP->terminate = true;
  404. hhh->join();
  405. delete hhh;
  406. hhh = NULL;
  407. }
  408. delete console;
  409. console = NULL;
  410. SDL_Delay(750);
  411. SDL_Quit();
  412. tlog0 << "Ending...\n";
  413. break;
  414. }
  415. else if(LOCPLINT && ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  416. {
  417. boost::unique_lock<boost::recursive_mutex> lock(*LOCPLINT->pim);
  418. bool full = !(screen->flags&SDL_FULLSCREEN);
  419. setScreenRes(conf.cc.resx,conf.cc.resy,conf.cc.bpp,full);
  420. GH.totalRedraw();
  421. delete ev;
  422. continue;
  423. }
  424. else if(ev->type == SDL_USEREVENT && ev->user.code == 1)
  425. {
  426. setScreenRes(conf.cc.resx,conf.cc.resy,conf.cc.bpp,conf.cc.fullscreen);
  427. delete ev;
  428. continue;
  429. }
  430. else if (ev->type == SDL_USEREVENT && ev->user.code == 2) {
  431. client->stop();
  432. delete ev;
  433. continue;
  434. }
  435. //tlog0 << " pushing ";
  436. eventsM.lock();
  437. events.push(ev);
  438. eventsM.unlock();
  439. //tlog0 << " done\n";
  440. }
  441. }
  442. void startGame(StartInfo * options)
  443. {
  444. if(gOnlyAI)
  445. {
  446. for (size_t i =0; i < options->playerInfos.size(); i++)
  447. {
  448. options->playerInfos[i].human = false;
  449. }
  450. }
  451. if(screen->w != conf.cc.resx || screen->h != conf.cc.resy)
  452. {
  453. //push special event to order event reading thread to change resolution
  454. SDL_Event ev;
  455. ev.type = SDL_USEREVENT;
  456. ev.user.code = 1;
  457. SDL_PushEvent(&ev);
  458. }
  459. CClient cl;
  460. if(options->mode == 0) //new game
  461. {
  462. cl.newGame(NULL, options);
  463. }
  464. else //load game
  465. {
  466. std::string fname = options->mapname;
  467. boost::algorithm::erase_last(fname,".vlgm1");
  468. cl.loadGame(fname);
  469. }
  470. client = &cl;
  471. CGI->musich->stopMusic();
  472. client->run();
  473. LOCPLINT->terminate_cond.waitUntil(true);
  474. client->endGame();
  475. client = NULL;
  476. }