CMT.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <cstdlib>
  44. #include "../lib/NetPacks.h"
  45. #if __MINGW32__
  46. #undef main
  47. #endif
  48. /*
  49. * CMT.cpp, part of VCMI engine
  50. *
  51. * Authors: listed in file AUTHORS in main folder
  52. *
  53. * License: GNU General Public License v2.0 or later
  54. * Full text of license available in license.txt file, in main folder
  55. *
  56. */
  57. std::string NAME = NAME_VER + std::string(" (client)"); //application name
  58. SDL_Surface *screen = NULL, //main screen surface
  59. *screen2 = NULL,//and hlp surface (used to store not-active interfaces layer)
  60. *screenBuf = screen; //points to screen (if only advmapint is present) or screen2 (else) - should be used when updating controls which are not regularly redrawed
  61. SystemOptions GDefaultOptions;
  62. std::queue<SDL_Event*> events;
  63. boost::mutex eventsM;
  64. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  65. void processCommand(const std::string &message, CClient *&client);
  66. static void setScreenRes(int w, int h, int bpp, bool fullscreen);
  67. void dispose();
  68. void playIntro();
  69. void init()
  70. {
  71. timeHandler tmh, pomtime;
  72. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  73. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  74. #else
  75. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  76. #endif
  77. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  78. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  79. TTF_Init();
  80. atexit(TTF_Quit);
  81. TNRB16 = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",16);
  82. GEOR13 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",13);
  83. GEOR16 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",16);
  84. GEORXX = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",22);
  85. GEORM = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",10);
  86. if(! (TNRB16 && GEOR16 && GEORXX && GEORM))
  87. {
  88. tlog1 << "One of the fonts couldn't be loaded!\n";
  89. exit(-1);
  90. }
  91. THC tlog0<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  92. {
  93. //read system options
  94. CLoadFile settings("config" PATHSEPARATOR "sysopts.bin");
  95. if(settings.sfile)
  96. {
  97. settings >> GDefaultOptions;
  98. }
  99. else //file not found (probably, may be also some kind of access problem
  100. {
  101. tlog2 << "Warning: Cannot read system options, default settings will be used.\n";
  102. //Try to create file
  103. tlog2 << "VCMI will try to save default system options...\n";
  104. GDefaultOptions.settingsChanged();
  105. }
  106. }
  107. THC tlog0<<"\tLoading default system settings: "<<pomtime.getDif()<<std::endl;
  108. //initializing audio
  109. // Note: because of interface button range, volume can only be a
  110. // multiple of 11, from 0 to 99.
  111. CGI->soundh = new CSoundHandler;
  112. CGI->soundh->init();
  113. CGI->soundh->setVolume(GDefaultOptions.soundVolume);
  114. CGI->musich = new CMusicHandler;
  115. CGI->musich->init();
  116. CGI->musich->setVolume(GDefaultOptions.musicVolume);
  117. tlog0<<"\tInitializing sound: "<<pomtime.getDif()<<std::endl;
  118. tlog0<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  119. initDLL(::console,logfile);
  120. CGI->setFromLib();
  121. CGI->soundh->initCreaturesSounds(CGI->creh->creatures);
  122. CGI->soundh->initSpellsSounds(CGI->spellh->spells);
  123. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  124. pomtime.getDif();
  125. CGI->curh = new CCursorHandler;
  126. CGI->curh->initCursor();
  127. CGI->curh->show();
  128. tlog0<<"Screen handler: "<<pomtime.getDif()<<std::endl;
  129. pomtime.getDif();
  130. graphics = new Graphics();
  131. graphics->loadHeroAnims();
  132. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  133. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  134. CMessage::init();
  135. tlog0<<"Message handler: "<<tmh.getDif()<<std::endl;
  136. CPG = new CPreGame(); //main menu and submenus
  137. tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  138. }
  139. #ifndef __GNUC__
  140. int _tmain(int argc, _TCHAR* argv[])
  141. #else
  142. int main(int argc, char** argv)
  143. #endif
  144. {
  145. tlog0 << "Starting... " << std::endl;
  146. timeHandler total, pomtime;
  147. CClient *client = NULL;
  148. std::cout.flags(std::ios::unitbuf);
  149. logfile = new std::ofstream("VCMI_Client_log.txt");
  150. console = new CConsoleHandler;
  151. *console->cb = boost::bind(&processCommand, _1, boost::ref(client));
  152. console->start();
  153. atexit(dispose);
  154. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  155. conf.init();
  156. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  157. tlog0 << NAME << std::endl;
  158. srand ( time(NULL) );
  159. CPG=NULL;
  160. atexit(SDL_Quit);
  161. CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  162. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO)==0)
  163. {
  164. setScreenRes(800,600,conf.cc.bpp,conf.cc.fullscreen);
  165. tlog0 <<"\tInitializing screen: "<<pomtime.getDif() << std::endl;
  166. // Initialize video
  167. CGI->videoh = new CVideoPlayer;
  168. tlog0<<"\tInitializing video: "<<pomtime.getDif()<<std::endl;
  169. //we can properly play intro only in the main thread, so we have to move loading to the separate thread
  170. boost::thread loading(init);
  171. playIntro();
  172. SDL_FillRect(screen,NULL,0);
  173. SDL_Flip(screen);
  174. loading.join();
  175. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  176. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  177. CGI->musich->playMusic(musicBase::mainMenu, -1);
  178. CPG->showMainMenu();
  179. StartInfo *options = new StartInfo(CPG->runLoop());
  180. if(screen->w != conf.cc.resx || screen->h != conf.cc.resy)
  181. {
  182. setScreenRes(conf.cc.resx,conf.cc.resy,conf.cc.bpp,conf.cc.fullscreen);
  183. }
  184. CClient cl;
  185. if(options->mode == 0) //new game
  186. {
  187. pomtime.getDif();
  188. char portc[10];
  189. SDL_itoa(conf.cc.port,portc,10);
  190. CClient::runServer(portc);
  191. tlog0<<"Preparing shared memory and starting server: "<<pomtime.getDif()<<std::endl;
  192. pomtime.getDif();//reset timers
  193. CConnection *c=NULL;
  194. //wait until server is ready
  195. tlog0<<"Waiting for server... ";
  196. cl.waitForServer();
  197. tlog0 << pomtime.getDif()<<std::endl;
  198. while(!c)
  199. {
  200. try
  201. {
  202. tlog0 << "Establishing connection...\n";
  203. c = new CConnection(conf.cc.server,portc,NAME);
  204. }
  205. catch(...)
  206. {
  207. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" <<std::endl;
  208. SDL_Delay(2000);
  209. }
  210. }
  211. THC tlog0<<"\tConnecting to the server: "<<pomtime.getDif()<<std::endl;
  212. cl.newGame(c,options);
  213. client = &cl;
  214. CGI->musich->stopMusic();
  215. boost::thread t(boost::bind(&CClient::run,&cl));
  216. }
  217. else //load game
  218. {
  219. std::string fname = options->mapname;
  220. boost::algorithm::erase_last(fname,".vlgm1");
  221. cl.load(fname);
  222. client = &cl;
  223. CGI->musich->stopMusic();
  224. boost::thread t(boost::bind(&CClient::run,&cl));
  225. }
  226. SDL_Event *ev = NULL;
  227. while(1) //main SDL events loop
  228. {
  229. ev = new SDL_Event();
  230. int ret = SDL_WaitEvent(ev);
  231. if(ret == 0 || (ev->type==SDL_QUIT) || (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  232. {
  233. LOCPLINT->pim->lock();
  234. cl.close();
  235. console->end();
  236. SDL_Delay(750);
  237. tlog0 << "Ending...\n";
  238. exit(EXIT_SUCCESS);
  239. }
  240. else if(ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  241. {
  242. boost::unique_lock<boost::recursive_mutex> lock(*LOCPLINT->pim);
  243. bool full = !(screen->flags&SDL_FULLSCREEN);
  244. setScreenRes(conf.cc.resx,conf.cc.resy,conf.cc.bpp,full);
  245. LOCPLINT->totalRedraw();
  246. }
  247. eventsM.lock();
  248. events.push(ev);
  249. eventsM.unlock();
  250. }
  251. }
  252. else
  253. {
  254. tlog1<<"Something was wrong: "<<SDL_GetError()<<std::endl;
  255. return -1;
  256. }
  257. }
  258. void processCommand(const std::string &message, CClient *&client)
  259. {
  260. std::istringstream readed;
  261. readed.str(message);
  262. std::string cn; //command name
  263. readed >> cn;
  264. int3 src, dst;
  265. // int heronum;//TODO use me
  266. int3 dest;
  267. if(LOCPLINT && LOCPLINT->cingconsole)
  268. LOCPLINT->cingconsole->print(message);
  269. if(message==std::string("die, fool"))
  270. exit(EXIT_SUCCESS);
  271. else if(cn==std::string("activate"))
  272. {
  273. int what;
  274. readed >> what;
  275. switch (what)
  276. {
  277. case 0:
  278. LOCPLINT->topInt()->activate();
  279. break;
  280. case 1:
  281. LOCPLINT->adventureInt->activate();
  282. break;
  283. case 2:
  284. LOCPLINT->castleInt->activate();
  285. break;
  286. }
  287. }
  288. else if(cn=="save")
  289. {
  290. std::string fname;
  291. readed >> fname;
  292. client->save(fname);
  293. }
  294. else if(cn=="load")
  295. {
  296. std::string fname;
  297. readed >> fname;
  298. client->load(fname);
  299. }
  300. else if(cn=="resolution")
  301. {
  302. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  303. int i=1, hlp=1;
  304. tlog4 << "Available screen resolutions:\n";
  305. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  306. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  307. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  308. std::cin >> i;
  309. if(i < 0 || i > conf.guiOptions.size() || std::cin.bad() || std::cin.fail())
  310. {
  311. std::cin.clear();
  312. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  313. }
  314. else if(!i)
  315. {
  316. return;
  317. }
  318. else
  319. {
  320. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  321. conf.cc.resx = j->first.first;
  322. conf.cc.resy = j->first.second;
  323. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  324. }
  325. }
  326. else if(message=="get txt")
  327. {
  328. boost::filesystem::create_directory("Extracted_txts");
  329. tlog0<<"Command accepted. Opening .lod file...\t";
  330. CLodHandler * txth = new CLodHandler;
  331. txth->init(std::string(DATA_DIR "Data" PATHSEPARATOR "H3bitmap.lod"),"");
  332. tlog0<<"done.\nScanning .lod file\n";
  333. int curp=0;
  334. std::string pattern = ".TXT", pom;
  335. for(int i=0;i<txth->entries.size(); i++)
  336. {
  337. pom = txth->entries[i].nameStr;
  338. if(boost::algorithm::find_last(pom,pattern))
  339. {
  340. txth->extractFile(std::string("Extracted_txts" PATHSEPARATOR)+pom,pom);
  341. }
  342. if(i%8) continue;
  343. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  344. if(p2!=curp)
  345. {
  346. curp = p2;
  347. tlog0<<"\r"<<curp<<"%";
  348. }
  349. }
  350. tlog0<<"\rExtracting done :)\n";
  351. }
  352. else if(client && client->serv && client->serv->connected) //send to server
  353. {
  354. PlayerMessage pm(LOCPLINT->playerID,message);
  355. *client->serv << &pm;
  356. }
  357. }
  358. //plays intro, ends when intro is over or button has been pressed (handles events)
  359. void playIntro()
  360. {
  361. if(CGI->videoh->openAndPlayVideo("3DOLOGO.SMK", 60, 40, screen, true))
  362. {
  363. CGI->videoh->openAndPlayVideo("AZVS.SMK", 60, 80, screen, true);
  364. }
  365. }
  366. void dispose()
  367. {
  368. delete logfile;
  369. delete console;
  370. }
  371. static void setScreenRes(int w, int h, int bpp, bool fullscreen)
  372. {
  373. if(screen) //screen has been already initialized
  374. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  375. SDL_InitSubSystem(SDL_INIT_VIDEO);
  376. // VCMI will only work with 3 or 4 bytes per pixel
  377. if (bpp < 24) bpp = 24;
  378. if (bpp > 32) bpp = 32;
  379. // Try to use the best screen depth for the display
  380. int suggestedBpp = SDL_VideoModeOK(w, h, bpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0));
  381. if(suggestedBpp == 0)
  382. {
  383. tlog2 << "Warning: SDL says that " << w << "x" << h << " resolution is not available!\n";
  384. suggestedBpp = bpp;
  385. }
  386. else if(suggestedBpp != bpp)
  387. {
  388. tlog2 << "Warning: SDL says that " << bpp << "bpp is wrong and suggests " << suggestedBpp << std::endl;
  389. }
  390. if((screen = SDL_SetVideoMode(w, h, suggestedBpp, SDL_SWSURFACE|(fullscreen?SDL_FULLSCREEN:0))) == NULL)
  391. {
  392. tlog1 << "Requested screen resolution is not available (" << w << "x" << h << "x" << suggestedBpp << "bpp)\n";
  393. throw "Requested screen resolution is not available\n";
  394. }
  395. if(screen2)
  396. SDL_FreeSurface(screen2);
  397. screen2 = CSDL_Ext::copySurface(screen);
  398. SDL_EnableUNICODE(1);
  399. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  400. SDL_ShowCursor(SDL_DISABLE);
  401. }