CMT.cpp 11 KB

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