CMT.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  29. # include <fcntl.h>
  30. # include <io.h>
  31. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  32. #else
  33. # define SET_BINARY_MODE(file)
  34. #endif
  35. #define CHUNK 16384
  36. #define pi 3.14159
  37. const char * NAME = "VCMI 0.2";
  38. #include "CAmbarCendamo.h"
  39. #include "mapHandler.h"
  40. #include "global.h"
  41. #include "CPreGame.h"
  42. /* Compress from file source to file dest until EOF on source.
  43. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  44. allocated for processing, Z_STREAM_ERROR if an invalid compression
  45. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  46. version of the library linked do not match, or Z_ERRNO if there is
  47. an error reading or writing the files. */
  48. SDL_Surface * ekran;
  49. TTF_Font * TNRB16, *TNR, *GEOR13;
  50. int def(FILE *source, FILE *dest, int level, int winBits=15, int memLevel =8)
  51. {
  52. int ret, flush;
  53. unsigned have;
  54. z_stream strm;
  55. unsigned char in[CHUNK];
  56. unsigned char out[CHUNK];
  57. /* allocate deflate state */
  58. strm.zalloc = Z_NULL;
  59. strm.zfree = Z_NULL;
  60. strm.opaque = Z_NULL;
  61. ret = deflateInit2(&strm, level,Z_DEFLATED,winBits,memLevel,0);//8-15, 1-9, 0-2
  62. if (ret != Z_OK)
  63. return ret;
  64. /* compress until end of file */
  65. do {
  66. strm.avail_in = fread(in, 1, CHUNK, source);
  67. if (ferror(source)) {
  68. (void)deflateEnd(&strm);
  69. return Z_ERRNO;
  70. }
  71. flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  72. strm.next_in = in;
  73. /* run deflate() on input until output buffer not full, finish
  74. compression if all of source has been read in */
  75. do {
  76. strm.avail_out = CHUNK;
  77. strm.next_out = out;
  78. ret = deflate(&strm, flush); /* no bad return value */
  79. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  80. have = CHUNK - strm.avail_out;
  81. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  82. (void)deflateEnd(&strm);
  83. return Z_ERRNO;
  84. }
  85. } while (strm.avail_out == 0);
  86. assert(strm.avail_in == 0); /* all input will be used */
  87. /* done when last data in file processed */
  88. } while (flush != Z_FINISH);
  89. assert(ret == Z_STREAM_END); /* stream will be complete */
  90. /* clean up and return */
  91. (void)deflateEnd(&strm);
  92. return Z_OK;
  93. }
  94. /* Decompress from file source to file dest until stream ends or EOF.
  95. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  96. allocated for processing, Z_DATA_ERROR if the deflate data is
  97. invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  98. the version of the library linked do not match, or Z_ERRNO if there
  99. is an error reading or writing the files. */
  100. int inf(FILE *source, FILE *dest, int wBits = 15)
  101. {
  102. int ret;
  103. unsigned have;
  104. z_stream strm;
  105. unsigned char in[CHUNK];
  106. unsigned char out[CHUNK];
  107. /* allocate inflate state */
  108. strm.zalloc = Z_NULL;
  109. strm.zfree = Z_NULL;
  110. strm.opaque = Z_NULL;
  111. strm.avail_in = 0;
  112. strm.next_in = Z_NULL;
  113. ret = inflateInit2(&strm, wBits);
  114. if (ret != Z_OK)
  115. return ret;
  116. /* decompress until deflate stream ends or end of file */
  117. do {
  118. strm.avail_in = fread(in, 1, CHUNK, source);
  119. if (ferror(source)) {
  120. (void)inflateEnd(&strm);
  121. return Z_ERRNO;
  122. }
  123. if (strm.avail_in == 0)
  124. break;
  125. strm.next_in = in;
  126. /* run inflate() on input until output buffer not full */
  127. do {
  128. strm.avail_out = CHUNK;
  129. strm.next_out = out;
  130. ret = inflate(&strm, Z_NO_FLUSH);
  131. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  132. switch (ret) {
  133. case Z_NEED_DICT:
  134. ret = Z_DATA_ERROR; /* and fall through */
  135. case Z_DATA_ERROR:
  136. case Z_MEM_ERROR:
  137. (void)inflateEnd(&strm);
  138. return ret;
  139. }
  140. have = CHUNK - strm.avail_out;
  141. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  142. (void)inflateEnd(&strm);
  143. return Z_ERRNO;
  144. }
  145. } while (strm.avail_out == 0);
  146. /* done when inflate() says it's done */
  147. } while (ret != Z_STREAM_END);
  148. /* clean up and return */
  149. (void)inflateEnd(&strm);
  150. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  151. }
  152. /* report a zlib or i/o error */
  153. void zerr(int ret)
  154. {
  155. fputs("zpipe: ", stderr);
  156. switch (ret) {
  157. case Z_ERRNO:
  158. if (ferror(stdin))
  159. fputs("error reading stdin\n", stderr);
  160. if (ferror(stdout))
  161. fputs("error writing stdout\n", stderr);
  162. break;
  163. case Z_STREAM_ERROR:
  164. fputs("invalid compression level\n", stderr);
  165. break;
  166. case Z_DATA_ERROR:
  167. fputs("invalid or incomplete deflate data\n", stderr);
  168. break;
  169. case Z_MEM_ERROR:
  170. fputs("out of memory\n", stderr);
  171. break;
  172. case Z_VERSION_ERROR:
  173. fputs("zlib version mismatch!\n", stderr);
  174. }
  175. }
  176. /*void SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B)
  177. {
  178. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  179. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  180. {
  181. p[0] = R;
  182. p[1] = G;
  183. p[2] = B;
  184. }
  185. else
  186. {
  187. p[0] = B;
  188. p[1] = G;
  189. p[2] = R;
  190. }
  191. SDL_UpdateRect(ekran, x, y, 1, 1);
  192. }*/
  193. int _tmain(int argc, _TCHAR* argv[])
  194. {
  195. int xx=0, yy=0, zz=0;
  196. SDL_Event sEvent;
  197. srand ( time(NULL) );
  198. SDL_Surface *screen, *temp;
  199. std::vector<SDL_Surface*> Sprites;
  200. float i;
  201. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  202. {
  203. TTF_Init();
  204. atexit(TTF_Quit);
  205. atexit(SDL_Quit);
  206. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  207. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  208. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  209. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  210. //initializing audio
  211. CMusicHandler * mush = new CMusicHandler;
  212. mush->initMusics();
  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_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  220. ekran = screen;
  221. //FILE * zr = fopen("mal.txt","r");
  222. //FILE * ko = fopen("wyn.txt","w");
  223. //FILE * kodd = fopen("kod.txt","r");
  224. //FILE * deko = fopen("dekod.txt","w");
  225. //def(zr,ko,1);
  226. //inf(kodd, deko);
  227. //fclose(ko);fclose(zr);
  228. //for (int i=0;i<=20;i++)
  229. //{
  230. // zr = fopen("kod2.txt","r");
  231. // char c [200];
  232. // sprintf(c,"wyn%d.txt",i);
  233. // ko = fopen(c,"w");
  234. // def(zr,ko,i);
  235. // fclose(ko);fclose(zr);
  236. //}
  237. SDL_WM_SetCaption(NAME,""); //set window title
  238. CPreGame * cpg = new CPreGame();
  239. cpg->mush = mush;
  240. cpg->runLoop();
  241. THC timeHandler tmh;
  242. CGameInfo * cgi = new CGameInfo;
  243. CGameInfo::mainObj = cgi;
  244. cgi->mush = mush;
  245. cgi->sspriteh = new CSemiLodHandler();
  246. cgi->sspriteh->openLod("H3sprite.lod");
  247. CArtHandler * arth = new CArtHandler;
  248. arth->loadArtifacts();
  249. cgi->arth = arth;
  250. CCreatureHandler * creh = new CCreatureHandler;
  251. creh->loadCreatures();
  252. cgi->creh = creh;
  253. CAbilityHandler * abilh = new CAbilityHandler;
  254. abilh->loadAbilities();
  255. cgi->abilh = abilh;
  256. CHeroHandler * heroh = new CHeroHandler;
  257. heroh->loadHeroes();
  258. cgi->heroh = heroh;
  259. CSpellHandler * spellh = new CSpellHandler;
  260. spellh->loadSpells();
  261. cgi->spellh = spellh;
  262. CBuildingHandler * buildh = new CBuildingHandler;
  263. buildh->loadBuildings();
  264. cgi->buildh = buildh;
  265. CObjectHandler * objh = new CObjectHandler;
  266. objh->loadObjects();
  267. cgi->objh = objh;
  268. CAmbarCendamo * ac = new CAmbarCendamo("4gryf"); //4gryf
  269. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  270. cgi->ac = ac;
  271. THC std::cout<<"Wczytywanie pliku: "<<tmh.getDif()<<std::endl;
  272. ac->deh3m();
  273. THC std::cout<<"Rozpoznawianie pliku lacznie: "<<tmh.getDif()<<std::endl;
  274. ac->loadDefs();
  275. THC std::cout<<"Wczytywanie defow: "<<tmh.getDif()<<std::endl;
  276. CMapHandler * mh = new CMapHandler();
  277. mh->reader = ac;
  278. THC std::cout<<"Stworzenie mapHandlera: "<<tmh.getDif()<<std::endl;
  279. mh->init();
  280. THC std::cout<<"Inicjalizacja mapHandlera: "<<tmh.getDif()<<std::endl;
  281. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  282. SDL_Surface * teren = mh->terrainRect(xx,yy,25,19);
  283. THC std::cout<<"Przygotowanie terenu do wyswietlenia: "<<tmh.getDif()<<std::endl;
  284. SDL_BlitSurface(teren,NULL,ekran,NULL);
  285. SDL_FreeSurface(teren);
  286. SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  287. THC std::cout<<"Wyswietlenie terenu: "<<tmh.getDif()<<std::endl;
  288. //SDL_Surface * ss = ac->defs[0]->ourImages[0].bitmap;
  289. //SDL_BlitSurface(ss, NULL, ekran, NULL);
  290. bool scrollingLeft = false;
  291. bool scrollingRight = false;
  292. bool scrollingUp = false;
  293. bool scrollingDown = false;
  294. bool updateScreen = false;
  295. for(;;) // main loop
  296. {
  297. try
  298. {
  299. if(SDL_PollEvent(&sEvent)) //wait for event...
  300. {
  301. if(sEvent.type==SDL_QUIT)
  302. return 0;
  303. else if (sEvent.type==SDL_KEYDOWN)
  304. {
  305. switch (sEvent.key.keysym.sym)
  306. {
  307. case SDLK_LEFT:
  308. {
  309. scrollingLeft = true;
  310. break;
  311. }
  312. case (SDLK_RIGHT):
  313. {
  314. scrollingRight = true;
  315. break;
  316. }
  317. case (SDLK_UP):
  318. {
  319. scrollingUp = true;
  320. break;
  321. }
  322. case (SDLK_DOWN):
  323. {
  324. scrollingDown = true;
  325. break;
  326. }
  327. case (SDLK_q):
  328. {
  329. return 0;
  330. break;
  331. }
  332. case (SDLK_u):
  333. {
  334. if(!ac->map.twoLevel)
  335. break;
  336. if (zz)
  337. zz--;
  338. else zz++;
  339. updateScreen = true;
  340. break;
  341. }
  342. }
  343. } //keydown end
  344. else if(sEvent.type==SDL_KEYUP)
  345. {
  346. switch (sEvent.key.keysym.sym)
  347. {
  348. case SDLK_LEFT:
  349. {
  350. scrollingLeft = false;
  351. break;
  352. }
  353. case (SDLK_RIGHT):
  354. {
  355. scrollingRight = false;
  356. break;
  357. }
  358. case (SDLK_UP):
  359. {
  360. scrollingUp = false;
  361. break;
  362. }
  363. case (SDLK_DOWN):
  364. {
  365. scrollingDown = false;
  366. break;
  367. }
  368. }
  369. }//keyup end
  370. } //event end
  371. /////////////// scrolling terrain
  372. if(scrollingLeft)
  373. {
  374. if(xx>0)
  375. {
  376. xx--;
  377. updateScreen = true;
  378. }
  379. }
  380. if(scrollingRight)
  381. {
  382. if(xx<ac->map.width-25+8)
  383. {
  384. xx++;
  385. updateScreen = true;
  386. }
  387. }
  388. if(scrollingUp)
  389. {
  390. if(yy>0)
  391. {
  392. yy--;
  393. updateScreen = true;
  394. }
  395. }
  396. if(scrollingDown)
  397. {
  398. if(yy<ac->map.height-18+8)
  399. {
  400. yy++;
  401. updateScreen = true;
  402. }
  403. }
  404. if(updateScreen)
  405. {
  406. SDL_FillRect(ekran, NULL, SDL_MapRGB(ekran->format, 0, 0, 0));
  407. SDL_Surface * help = mh->terrainRect(xx,yy,25,18,zz);
  408. SDL_BlitSurface(help,NULL,ekran,NULL);
  409. SDL_FreeSurface(help);
  410. SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  411. updateScreen = false;
  412. }
  413. /////////
  414. }
  415. catch(...)
  416. { continue; }
  417. SDL_Delay(25); //give time for other apps
  418. }
  419. return 0;
  420. }
  421. else
  422. {
  423. printf("Coœ posz³o nie tak: %s/n",SDL_GetError());
  424. return -1;
  425. }
  426. }