CMT.cpp 15 KB

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