CMT.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  33. # include <fcntl.h>
  34. # include <io.h>
  35. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  36. #else
  37. # define SET_BINARY_MODE(file)
  38. #endif
  39. #define CHUNK 16384
  40. #define pi 3.14159
  41. const char * NAME = "VCMI 0.2";
  42. #include "CAmbarCendamo.h"
  43. #include "mapHandler.h"
  44. #include "global.h"
  45. #include "CPreGame.h"
  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. /* allocate inflate state */
  112. strm.zalloc = Z_NULL;
  113. strm.zfree = Z_NULL;
  114. strm.opaque = Z_NULL;
  115. strm.avail_in = 0;
  116. strm.next_in = Z_NULL;
  117. ret = inflateInit2(&strm, wBits);
  118. if (ret != Z_OK)
  119. return ret;
  120. /* decompress until deflate stream ends or end of file */
  121. do {
  122. strm.avail_in = fread(in, 1, CHUNK, source);
  123. if (ferror(source)) {
  124. (void)inflateEnd(&strm);
  125. return Z_ERRNO;
  126. }
  127. if (strm.avail_in == 0)
  128. break;
  129. strm.next_in = in;
  130. /* run inflate() on input until output buffer not full */
  131. do {
  132. strm.avail_out = CHUNK;
  133. strm.next_out = out;
  134. ret = inflate(&strm, Z_NO_FLUSH);
  135. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  136. switch (ret) {
  137. case Z_NEED_DICT:
  138. ret = Z_DATA_ERROR; /* and fall through */
  139. case Z_DATA_ERROR:
  140. case Z_MEM_ERROR:
  141. (void)inflateEnd(&strm);
  142. return ret;
  143. }
  144. have = CHUNK - strm.avail_out;
  145. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  146. (void)inflateEnd(&strm);
  147. return Z_ERRNO;
  148. }
  149. } while (strm.avail_out == 0);
  150. /* done when inflate() says it's done */
  151. } while (ret != Z_STREAM_END);
  152. /* clean up and return */
  153. (void)inflateEnd(&strm);
  154. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  155. }
  156. /* report a zlib or i/o error */
  157. void zerr(int ret)
  158. {
  159. fputs("zpipe: ", stderr);
  160. switch (ret) {
  161. case Z_ERRNO:
  162. if (ferror(stdin))
  163. fputs("error reading stdin\n", stderr);
  164. if (ferror(stdout))
  165. fputs("error writing stdout\n", stderr);
  166. break;
  167. case Z_STREAM_ERROR:
  168. fputs("invalid compression level\n", stderr);
  169. break;
  170. case Z_DATA_ERROR:
  171. fputs("invalid or incomplete deflate data\n", stderr);
  172. break;
  173. case Z_MEM_ERROR:
  174. fputs("out of memory\n", stderr);
  175. break;
  176. case Z_VERSION_ERROR:
  177. fputs("zlib version mismatch!\n", stderr);
  178. }
  179. }
  180. /*void SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B)
  181. {
  182. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  183. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  184. {
  185. p[0] = R;
  186. p[1] = G;
  187. p[2] = B;
  188. }
  189. else
  190. {
  191. p[0] = B;
  192. p[1] = G;
  193. p[2] = R;
  194. }
  195. SDL_UpdateRect(ekran, x, y, 1, 1);
  196. }*/
  197. int _tmain(int argc, _TCHAR* argv[])
  198. {
  199. THC timeHandler tmh;
  200. THC tmh.getDif();
  201. int xx=0, yy=0, zz=0;
  202. SDL_Event sEvent;
  203. srand ( time(NULL) );
  204. SDL_Surface *screen, *temp;
  205. std::vector<SDL_Surface*> Sprites;
  206. float i;
  207. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  208. {
  209. CPG=NULL;
  210. TTF_Init();
  211. atexit(TTF_Quit);
  212. atexit(SDL_Quit);
  213. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  214. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  215. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  216. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  217. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  218. //initializing audio
  219. CMusicHandler * mush = new CMusicHandler;
  220. mush->initMusics();
  221. //CSndHandler snd("Heroes3.snd");
  222. //snd.extract("AELMMOVE.wav","snddd.wav");
  223. //audio initialized
  224. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  225. {
  226. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  227. // well, there's no music, but most games don't break without music...
  228. }*/
  229. screen = SDL_SetVideoMode(800,600,24,SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  230. ekran = screen;
  231. //FILE * zr = fopen("mal.txt","r");
  232. //FILE * ko = fopen("wyn.txt","w");
  233. //FILE * kodd = fopen("kod.txt","r");
  234. //FILE * deko = fopen("dekod.txt","w");
  235. //def(zr,ko,1);
  236. //inf(kodd, deko);
  237. //fclose(ko);fclose(zr);
  238. //for (int i=0;i<=20;i++)
  239. //{
  240. // zr = fopen("kod2.txt","r");
  241. // char c [200];
  242. // sprintf(c,"wyn%d.txt",i);
  243. // ko = fopen(c,"w");
  244. // def(zr,ko,i);
  245. // fclose(ko);fclose(zr);
  246. //}
  247. SDL_WM_SetCaption(NAME,""); //set window title
  248. THC std::cout<<"Inicjalizacja ekranu, czcionek, obslugi dzwieku: "<<tmh.getDif()<<std::endl;
  249. CPreGame * cpg = new CPreGame();
  250. THC std::cout<<"Razem inicjalizacja CPreGame: "<<tmh.getDif()<<std::endl;
  251. cpg->mush = mush;
  252. cpg->runLoop();
  253. THC tmh.getDif();
  254. CGameInfo * cgi = new CGameInfo;
  255. CGameInfo::mainObj = cgi;
  256. cgi->mush = mush;
  257. //////////////////////////////////////////////////////////////////////////////// lod testing
  258. //CLodHandler * clod = new CLodHandler;
  259. //clod->loadLod("h3abp_bm.lod");
  260. //CLodHandler * test = new CLodHandler;
  261. //test->init(std::string("h3abp_bm.lod"));
  262. CDefHandler * tdef = new CDefHandler;
  263. tdef->openDef(std::string("newh3sprite\\AVLSPTR3.DEF"));
  264. //tdef->getSprite(0);
  265. //CLodHandler * bitmapLod = new CLodHandler;
  266. //bitmapLod->init(std::string("newH3bitmap.lod"));
  267. //CPCXConv * tconv = new CPCXConv;
  268. //tconv->fromFile(std::string("newh3bitmap\\ADOPBPNL.PCX"));
  269. //tconv->convert();
  270. //tconv->saveBMP(std::string("tesciczekConva.bmp"));
  271. CSemiDefHandler * semek = new CSemiDefHandler;
  272. semek->openDef(std::string("EDG.DEF"), std::string("H3sprite.lod"));
  273. //////////////////////////////////////////////////////////////////////////////// lod testing end
  274. cgi->sspriteh = new CSemiLodHandler();
  275. cgi->sspriteh->openLod("H3sprite.lod");
  276. CArtHandler * arth = new CArtHandler;
  277. arth->loadArtifacts();
  278. cgi->arth = arth;
  279. CCreatureHandler * creh = new CCreatureHandler;
  280. creh->loadCreatures();
  281. cgi->creh = creh;
  282. CAbilityHandler * abilh = new CAbilityHandler;
  283. abilh->loadAbilities();
  284. cgi->abilh = abilh;
  285. CHeroHandler * heroh = new CHeroHandler;
  286. heroh->loadHeroes();
  287. cgi->heroh = heroh;
  288. CSpellHandler * spellh = new CSpellHandler;
  289. spellh->loadSpells();
  290. cgi->spellh = spellh;
  291. CBuildingHandler * buildh = new CBuildingHandler;
  292. buildh->loadBuildings();
  293. cgi->buildh = buildh;
  294. CObjectHandler * objh = new CObjectHandler;
  295. objh->loadObjects();
  296. cgi->objh = objh;
  297. cgi->dobjinfo = new CDefObjInfoHandler;
  298. cgi->dobjinfo->load();
  299. THC std::cout<<"Inicjalizacja wszelakich handlerow: "<<tmh.getDif()<<std::endl;
  300. std::string mapname;
  301. //if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  302. //else mapname = "4gryf";
  303. mapname = "4gryf";
  304. CAmbarCendamo * ac = new CAmbarCendamo(mapname.c_str()); //4gryf
  305. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  306. cgi->ac = ac;
  307. THC std::cout<<"Wczytywanie pliku: "<<tmh.getDif()<<std::endl;
  308. ac->deh3m();
  309. THC std::cout<<"Rozpoznawianie pliku lacznie: "<<tmh.getDif()<<std::endl;
  310. ac->loadDefs();
  311. THC std::cout<<"Wczytywanie defow: "<<tmh.getDif()<<std::endl;
  312. CMapHandler * mh = new CMapHandler();
  313. mh->reader = ac;
  314. THC std::cout<<"Stworzenie mapHandlera: "<<tmh.getDif()<<std::endl;
  315. mh->init();
  316. THC std::cout<<"Inicjalizacja mapHandlera: "<<tmh.getDif()<<std::endl;
  317. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  318. SDL_Surface * teren = mh->terrainRect(xx,yy,25,19);
  319. THC std::cout<<"Przygotowanie terenu do wyswietlenia: "<<tmh.getDif()<<std::endl;
  320. SDL_BlitSurface(teren,NULL,ekran,NULL);
  321. SDL_FreeSurface(teren);
  322. SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  323. THC std::cout<<"Wyswietlenie terenu: "<<tmh.getDif()<<std::endl;
  324. //SDL_Surface * ss = ac->defs[0]->ourImages[0].bitmap;
  325. //SDL_BlitSurface(ss, NULL, ekran, NULL);
  326. bool scrollingLeft = false;
  327. bool scrollingRight = false;
  328. bool scrollingUp = false;
  329. bool scrollingDown = false;
  330. bool updateScreen = false;
  331. unsigned char animVal=0; //for animations handling
  332. for(;;) // main loop
  333. {
  334. try
  335. {
  336. if(SDL_PollEvent(&sEvent)) //wait for event...
  337. {
  338. if(sEvent.type==SDL_QUIT)
  339. return 0;
  340. else if (sEvent.type==SDL_KEYDOWN)
  341. {
  342. switch (sEvent.key.keysym.sym)
  343. {
  344. case SDLK_LEFT:
  345. {
  346. scrollingLeft = true;
  347. break;
  348. }
  349. case (SDLK_RIGHT):
  350. {
  351. scrollingRight = true;
  352. break;
  353. }
  354. case (SDLK_UP):
  355. {
  356. scrollingUp = true;
  357. break;
  358. }
  359. case (SDLK_DOWN):
  360. {
  361. scrollingDown = true;
  362. break;
  363. }
  364. case (SDLK_q):
  365. {
  366. return 0;
  367. break;
  368. }
  369. case (SDLK_u):
  370. {
  371. if(!ac->map.twoLevel)
  372. break;
  373. if (zz)
  374. zz--;
  375. else zz++;
  376. updateScreen = true;
  377. break;
  378. }
  379. }
  380. } //keydown end
  381. else if(sEvent.type==SDL_KEYUP)
  382. {
  383. switch (sEvent.key.keysym.sym)
  384. {
  385. case SDLK_LEFT:
  386. {
  387. scrollingLeft = false;
  388. break;
  389. }
  390. case (SDLK_RIGHT):
  391. {
  392. scrollingRight = false;
  393. break;
  394. }
  395. case (SDLK_UP):
  396. {
  397. scrollingUp = false;
  398. break;
  399. }
  400. case (SDLK_DOWN):
  401. {
  402. scrollingDown = false;
  403. break;
  404. }
  405. }
  406. }//keyup end
  407. } //event end
  408. /////////////// scrolling terrain
  409. if(scrollingLeft)
  410. {
  411. if(xx>0)
  412. {
  413. xx--;
  414. updateScreen = true;
  415. }
  416. }
  417. if(scrollingRight)
  418. {
  419. if(xx<ac->map.width-25+8)
  420. {
  421. xx++;
  422. updateScreen = true;
  423. }
  424. }
  425. if(scrollingUp)
  426. {
  427. if(yy>0)
  428. {
  429. yy--;
  430. updateScreen = true;
  431. }
  432. }
  433. if(scrollingDown)
  434. {
  435. if(yy<ac->map.height-18+8)
  436. {
  437. yy++;
  438. updateScreen = true;
  439. }
  440. }
  441. if(updateScreen)
  442. {
  443. SDL_FillRect(ekran, NULL, SDL_MapRGB(ekran->format, 0, 0, 0));
  444. SDL_Surface * help = mh->terrainRect(xx,yy,25,19,zz,animVal);
  445. SDL_BlitSurface(help,NULL,ekran,NULL);
  446. SDL_FreeSurface(help);
  447. SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  448. updateScreen = false;
  449. }
  450. /////////
  451. }
  452. catch(...)
  453. { continue; }
  454. updateScreen = true;
  455. ++animVal; //for animations
  456. SDL_Delay(30); //give time for other apps
  457. }
  458. return 0;
  459. }
  460. else
  461. {
  462. printf("Coœ posz³o nie tak: %s/n",SDL_GetError());
  463. return -1;
  464. }
  465. }