CMT.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // CMT.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "SDL.h"
  5. #include "SDL_TTF.h"
  6. #include "SDL_mixer.h"
  7. #include "CBuildingHandler.h"
  8. #include "SDL_Extensions.h"
  9. #include "SDL_framerate.h"
  10. #include <cmath>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <string>
  14. #include <assert.h>
  15. #include <vector>
  16. #include "zlib.h"
  17. #include <cmath>
  18. #include <ctime>
  19. #include "CArtHandler.h"
  20. #include "CHeroHandler.h"
  21. #include "CCreatureHandler.h"
  22. #include "CAbilityHandler.h"
  23. #include "CSpellHandler.h"
  24. #include "CBuildingHandler.h"
  25. #include "CObjectHandler.h"
  26. #include "CGameInfo.h"
  27. #include "CMusicHandler.h"
  28. #include "CSemiLodHandler.h"
  29. #include "CLodHandler.h"
  30. #include "CDefHandler.h"
  31. #include "CSndHandler.h"
  32. #include "CTownHandler.h"
  33. #include "CDefObjInfoHandler.h"
  34. #include "CAmbarCendamo.h"
  35. #include "mapHandler.h"
  36. #include "global.h"
  37. #include "CPreGame.h"
  38. #include "CGeneralTextHandler.h"
  39. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  40. # include <fcntl.h>
  41. # include <io.h>
  42. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  43. #else
  44. # define SET_BINARY_MODE(file)
  45. #endif
  46. #define CHUNK 16384
  47. const char * NAME = "VCMI 0.2";
  48. /* Compress from file source to file dest until EOF on source.
  49. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  50. allocated for processing, Z_STREAM_ERROR if an invalid compression
  51. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  52. version of the library linked do not match, or Z_ERRNO if there is
  53. an error reading or writing the files. */
  54. SDL_Surface * ekran;
  55. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX;
  56. int def(FILE *source, FILE *dest, int level, int winBits=15, int memLevel =8)
  57. {
  58. int ret, flush;
  59. unsigned have;
  60. z_stream strm;
  61. unsigned char in[CHUNK];
  62. unsigned char out[CHUNK];
  63. /* allocate deflate state */
  64. strm.zalloc = Z_NULL;
  65. strm.zfree = Z_NULL;
  66. strm.opaque = Z_NULL;
  67. ret = deflateInit2(&strm, level,Z_DEFLATED,winBits,memLevel,0);//8-15, 1-9, 0-2
  68. if (ret != Z_OK)
  69. return ret;
  70. /* compress until end of file */
  71. do {
  72. strm.avail_in = fread(in, 1, CHUNK, source);
  73. if (ferror(source)) {
  74. (void)deflateEnd(&strm);
  75. return Z_ERRNO;
  76. }
  77. flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  78. strm.next_in = in;
  79. /* run deflate() on input until output buffer not full, finish
  80. compression if all of source has been read in */
  81. do {
  82. strm.avail_out = CHUNK;
  83. strm.next_out = out;
  84. ret = deflate(&strm, flush); /* no bad return value */
  85. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  86. have = CHUNK - strm.avail_out;
  87. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  88. (void)deflateEnd(&strm);
  89. return Z_ERRNO;
  90. }
  91. } while (strm.avail_out == 0);
  92. assert(strm.avail_in == 0); /* all input will be used */
  93. /* done when last data in file processed */
  94. } while (flush != Z_FINISH);
  95. assert(ret == Z_STREAM_END); /* stream will be complete */
  96. /* clean up and return */
  97. (void)deflateEnd(&strm);
  98. return Z_OK;
  99. }
  100. /* Decompress from file source to file dest until stream ends or EOF.
  101. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  102. allocated for processing, Z_DATA_ERROR if the deflate data is
  103. invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  104. the version of the library linked do not match, or Z_ERRNO if there
  105. is an error reading or writing the files. */
  106. //int inf(FILE *source, FILE *dest, int wBits = 15)
  107. //{
  108. // int ret;
  109. // unsigned have;
  110. // z_stream strm;
  111. // unsigned char in[CHUNK];
  112. // unsigned char out[CHUNK];
  113. //
  114. // /* allocate inflate state */
  115. // strm.zalloc = Z_NULL;
  116. // strm.zfree = Z_NULL;
  117. // strm.opaque = Z_NULL;
  118. // strm.avail_in = 0;
  119. // strm.next_in = Z_NULL;
  120. // ret = inflateInit2(&strm, wBits);
  121. // if (ret != Z_OK)
  122. // return ret;
  123. //
  124. // /* decompress until deflate stream ends or end of file */
  125. // do {
  126. // strm.avail_in = fread(in, 1, CHUNK, source);
  127. // if (ferror(source)) {
  128. // (void)inflateEnd(&strm);
  129. // return Z_ERRNO;
  130. // }
  131. // if (strm.avail_in == 0)
  132. // break;
  133. // strm.next_in = in;
  134. //
  135. // /* run inflate() on input until output buffer not full */
  136. // do {
  137. // strm.avail_out = CHUNK;
  138. // strm.next_out = out;
  139. // ret = inflate(&strm, Z_NO_FLUSH);
  140. // assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  141. // switch (ret) {
  142. // case Z_NEED_DICT:
  143. // ret = Z_DATA_ERROR; /* and fall through */
  144. // case Z_DATA_ERROR:
  145. // case Z_MEM_ERROR:
  146. // (void)inflateEnd(&strm);
  147. // return ret;
  148. // }
  149. // have = CHUNK - strm.avail_out;
  150. // if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  151. // (void)inflateEnd(&strm);
  152. // return Z_ERRNO;
  153. // }
  154. // } while (strm.avail_out == 0);
  155. //
  156. // /* done when inflate() says it's done */
  157. // } while (ret != Z_STREAM_END);
  158. //
  159. // /* clean up and return */
  160. // (void)inflateEnd(&strm);
  161. // return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  162. //}
  163. /* report a zlib or i/o error */
  164. void zerr(int ret)
  165. {
  166. fputs("zpipe: ", stderr);
  167. switch (ret) {
  168. case Z_ERRNO:
  169. if (ferror(stdin))
  170. fputs("error reading stdin\n", stderr);
  171. if (ferror(stdout))
  172. fputs("error writing stdout\n", stderr);
  173. break;
  174. case Z_STREAM_ERROR:
  175. fputs("invalid compression level\n", stderr);
  176. break;
  177. case Z_DATA_ERROR:
  178. fputs("invalid or incomplete deflate data\n", stderr);
  179. break;
  180. case Z_MEM_ERROR:
  181. fputs("out of memory\n", stderr);
  182. break;
  183. case Z_VERSION_ERROR:
  184. fputs("zlib version mismatch!\n", stderr);
  185. }
  186. }
  187. int _tmain(int argc, _TCHAR* argv[])
  188. {
  189. THC timeHandler tmh;
  190. THC tmh.getDif();
  191. int xx=0, yy=0, zz=0;
  192. SDL_Event sEvent;
  193. srand ( time(NULL) );
  194. SDL_Surface *screen, *temp;
  195. std::vector<SDL_Surface*> Sprites;
  196. float i;
  197. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  198. {
  199. CPG=NULL;
  200. TTF_Init();
  201. atexit(TTF_Quit);
  202. atexit(SDL_Quit);
  203. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  204. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  205. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  206. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  207. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  208. //initializing audio
  209. CMusicHandler * mush = new CMusicHandler;
  210. mush->initMusics();
  211. //CSndHandler snd("Heroes3.snd");
  212. //snd.extract("AELMMOVE.wav","snddd.wav");
  213. //audio initialized
  214. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  215. {
  216. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  217. // well, there's no music, but most games don't break without music...
  218. }*/
  219. screen = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  220. ekran = screen;
  221. SDL_WM_SetCaption(NAME,""); //set window title
  222. CGameInfo * cgi = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  223. CGameInfo::mainObj = cgi;
  224. cgi->mush = mush;
  225. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  226. cgi->spriteh = new CLodHandler;
  227. cgi->spriteh->init(std::string("newH3sprite.lod"));
  228. cgi->bitmaph = new CLodHandler;
  229. cgi->bitmaph->init(std::string("newH3bitmap.lod"));
  230. //colors initialization
  231. SDL_Color p;
  232. p.unused = 0;
  233. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  234. cgi->playerColors.push_back(p); //red
  235. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  236. cgi->playerColors.push_back(p); //blue
  237. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  238. cgi->playerColors.push_back(p);//tan
  239. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  240. cgi->playerColors.push_back(p); //green
  241. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  242. cgi->playerColors.push_back(p); //orange
  243. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  244. cgi->playerColors.push_back(p); //purple
  245. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  246. cgi->playerColors.push_back(p);//teal
  247. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  248. cgi->playerColors.push_back(p);//pink
  249. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  250. cgi->neutralColor = p;//gray
  251. //colors initialized
  252. cgi->townh = new CTownHandler;
  253. cgi->townh->loadNames();
  254. THC std::cout<<"Loading .lods: "<<tmh.getDif()<<std::endl;
  255. CPreGame * cpg = new CPreGame(); //main menu and submenus
  256. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  257. //CMessage * ll = new CMessage;
  258. //CSDL_Ext::blueToPlayersAdv(ll->piecesOfBox[0].ourImages[0].bitmap, 0);
  259. //SDL_SaveBMP(ll->piecesOfBox[0].ourImages[0].bitmap, "test2.bmp");
  260. cpg->mush = mush;
  261. cpg->runLoop();
  262. THC tmh.getDif();
  263. //////////////////////////////////////////////////////////////////////////////// lod testing
  264. //CLodHandler * clod = new CLodHandler;
  265. //clod->loadLod("h3abp_bm.lod");
  266. //CLodHandler * test = new CLodHandler;
  267. //test->init(std::string("h3abp_bm.lod"));
  268. //CDefHandler * tdef = new CDefHandler;
  269. //tdef->openDef(std::string("newh3sprite\\AVLSPTR3.DEF"));
  270. //tdef->getSprite(0);
  271. //CLodHandler * bitmapLod = new CLodHandler;
  272. //bitmapLod->init(std::string("newH3bitmap.lod"));
  273. //CPCXConv * tconv = new CPCXConv;
  274. //tconv->fromFile(std::string("newh3bitmap\\ADOPBPNL.PCX"));
  275. //tconv->convert();
  276. //tconv->saveBMP(std::string("tesciczekConva.bmp"));
  277. //CSemiDefHandler * semek = new CSemiDefHandler;
  278. //semek->openDef(std::string("EDG.DEF"), std::string("H3sprite.lod"));
  279. //////////////////////////////////////////////////////////////////////////////// lod testing end
  280. cgi->sspriteh = new CSemiLodHandler();
  281. cgi->sspriteh->openLod("H3sprite.lod");
  282. CArtHandler * arth = new CArtHandler;
  283. arth->loadArtifacts();
  284. cgi->arth = arth;
  285. CCreatureHandler * creh = new CCreatureHandler;
  286. creh->loadCreatures();
  287. cgi->creh = creh;
  288. CAbilityHandler * abilh = new CAbilityHandler;
  289. abilh->loadAbilities();
  290. cgi->abilh = abilh;
  291. CHeroHandler * heroh = new CHeroHandler;
  292. heroh->loadHeroes();
  293. heroh->loadPortraits();
  294. cgi->heroh = heroh;
  295. CSpellHandler * spellh = new CSpellHandler;
  296. spellh->loadSpells();
  297. cgi->spellh = spellh;
  298. CBuildingHandler * buildh = new CBuildingHandler;
  299. buildh->loadBuildings();
  300. cgi->buildh = buildh;
  301. CObjectHandler * objh = new CObjectHandler;
  302. objh->loadObjects();
  303. cgi->objh = objh;
  304. cgi->generaltexth = new CGeneralTextHandler;
  305. cgi->generaltexth->load();
  306. cgi->dobjinfo = new CDefObjInfoHandler;
  307. cgi->dobjinfo->load();
  308. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  309. std::string mapname;
  310. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  311. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  312. gzFile map = gzopen(mapname.c_str(),"rb");
  313. std::string mapstr;int pom;
  314. while((pom=gzgetc(map))>=0)
  315. {
  316. mapstr+=pom;
  317. }
  318. gzclose(map);
  319. unsigned char *initTable = new unsigned char[mapstr.size()];
  320. for(int ss=0; ss<mapstr.size(); ++ss)
  321. {
  322. initTable[ss] = mapstr[ss];
  323. }
  324. #define CHOOSE
  325. #ifdef CHOOSE
  326. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  327. #else
  328. CAmbarCendamo * ac = new CAmbarCendamo("1smok"); //4gryf
  329. #endif
  330. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  331. cgi->ac = ac;
  332. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  333. ac->deh3m();
  334. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  335. ac->loadDefs();
  336. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  337. CMapHandler * mh = new CMapHandler();
  338. mh->reader = ac;
  339. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  340. mh->init();
  341. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  342. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  343. SDL_Surface * teren = mh->terrainRect(xx,yy,25,19);
  344. THC std::cout<<"Preparing terrain to display: "<<tmh.getDif()<<std::endl;
  345. SDL_BlitSurface(teren,NULL,ekran,NULL);
  346. SDL_FreeSurface(teren);
  347. SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  348. THC std::cout<<"Displaying terrain: "<<tmh.getDif()<<std::endl;
  349. //SDL_Surface * ss = ac->defs[0]->ourImages[0].bitmap;
  350. //SDL_BlitSurface(ss, NULL, ekran, NULL);
  351. bool scrollingLeft = false;
  352. bool scrollingRight = false;
  353. bool scrollingUp = false;
  354. bool scrollingDown = false;
  355. bool updateScreen = false;
  356. unsigned char animVal = 0; //for animations handling
  357. unsigned char animValHitCount = 0; //for slower animations
  358. //initializing framerate keeper
  359. FPSmanager * mainLoopFramerateKeeper = new FPSmanager;
  360. SDL_initFramerate(mainLoopFramerateKeeper);
  361. SDL_setFramerate(mainLoopFramerateKeeper, 30);
  362. //framerate keeper initialized
  363. for(;;) // main loop
  364. {
  365. try
  366. {
  367. if(SDL_PollEvent(&sEvent)) //wait for event...
  368. {
  369. if(sEvent.type==SDL_QUIT)
  370. return 0;
  371. else if (sEvent.type==SDL_KEYDOWN)
  372. {
  373. switch (sEvent.key.keysym.sym)
  374. {
  375. case SDLK_LEFT:
  376. {
  377. scrollingLeft = true;
  378. break;
  379. }
  380. case (SDLK_RIGHT):
  381. {
  382. scrollingRight = true;
  383. break;
  384. }
  385. case (SDLK_UP):
  386. {
  387. scrollingUp = true;
  388. break;
  389. }
  390. case (SDLK_DOWN):
  391. {
  392. scrollingDown = true;
  393. break;
  394. }
  395. case (SDLK_q):
  396. {
  397. return 0;
  398. break;
  399. }
  400. case (SDLK_u):
  401. {
  402. if(!ac->map.twoLevel)
  403. break;
  404. if (zz)
  405. zz--;
  406. else zz++;
  407. updateScreen = true;
  408. break;
  409. }
  410. }
  411. } //keydown end
  412. else if(sEvent.type==SDL_KEYUP)
  413. {
  414. switch (sEvent.key.keysym.sym)
  415. {
  416. case SDLK_LEFT:
  417. {
  418. scrollingLeft = false;
  419. break;
  420. }
  421. case (SDLK_RIGHT):
  422. {
  423. scrollingRight = false;
  424. break;
  425. }
  426. case (SDLK_UP):
  427. {
  428. scrollingUp = false;
  429. break;
  430. }
  431. case (SDLK_DOWN):
  432. {
  433. scrollingDown = false;
  434. break;
  435. }
  436. }
  437. }//keyup end
  438. else if(sEvent.type==SDL_MOUSEMOTION)
  439. {
  440. if(sEvent.motion.x<15)
  441. {
  442. scrollingLeft = true;
  443. }
  444. else
  445. {
  446. scrollingLeft = false;
  447. }
  448. if(sEvent.motion.x>screen->w-15)
  449. {
  450. scrollingRight = true;
  451. }
  452. else
  453. {
  454. scrollingRight = false;
  455. }
  456. if(sEvent.motion.y<15)
  457. {
  458. scrollingUp = true;
  459. }
  460. else
  461. {
  462. scrollingUp = false;
  463. }
  464. if(sEvent.motion.y>screen->h-15)
  465. {
  466. scrollingDown = true;
  467. }
  468. else
  469. {
  470. scrollingDown = false;
  471. }
  472. }
  473. } //event end
  474. /////////////// scrolling terrain
  475. if(scrollingLeft)
  476. {
  477. if(xx>0)
  478. {
  479. xx--;
  480. updateScreen = true;
  481. }
  482. }
  483. if(scrollingRight)
  484. {
  485. if(xx<ac->map.width-25+8)
  486. {
  487. xx++;
  488. updateScreen = true;
  489. }
  490. }
  491. if(scrollingUp)
  492. {
  493. if(yy>0)
  494. {
  495. yy--;
  496. updateScreen = true;
  497. }
  498. }
  499. if(scrollingDown)
  500. {
  501. if(yy<ac->map.height-19+8)
  502. {
  503. yy++;
  504. updateScreen = true;
  505. }
  506. }
  507. if(updateScreen)
  508. {
  509. SDL_FillRect(ekran, NULL, SDL_MapRGB(ekran->format, 0, 0, 0));
  510. SDL_Surface * help = mh->terrainRect(xx,yy,25,19,zz,animVal);
  511. SDL_BlitSurface(help,NULL,ekran,NULL);
  512. SDL_FreeSurface(help);
  513. SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  514. updateScreen = false;
  515. }
  516. /////////
  517. }
  518. catch(...)
  519. { continue; }
  520. updateScreen = true;
  521. ++animValHitCount; //for animations
  522. if(animValHitCount == 2)
  523. {
  524. animValHitCount = 0;
  525. ++animVal;
  526. }
  527. SDL_Delay(5); //give time for other apps
  528. SDL_framerateDelay(mainLoopFramerateKeeper);
  529. }
  530. return 0;
  531. }
  532. else
  533. {
  534. printf("Something was wrong: %s/n", SDL_GetError());
  535. return -1;
  536. }
  537. }