CMT.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // CMT.cpp : Defines the entry point for the console application.
  2. //
  3. #include "SDL.h"
  4. #include <cmath>
  5. #include "stdafx.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <vector>
  10. #include "zlib.h"
  11. #include <cmath>
  12. #include <ctime>
  13. #include "CArthandler.h"
  14. #include "CHeroHandler.h"
  15. #include "CCreatureHandler.h"
  16. #include "CAbilityHandler.h"
  17. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  18. # include <fcntl.h>
  19. # include <io.h>
  20. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  21. #else
  22. # define SET_BINARY_MODE(file)
  23. #endif
  24. #define CHUNK 16384
  25. #define pi 3.14159
  26. const char * NAME = "VCMI 0.1";
  27. #include "CAmbarCendamo.h"
  28. #include "mapHandler.h"
  29. #include "global.h"
  30. /* Compress from file source to file dest until EOF on source.
  31. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  32. allocated for processing, Z_STREAM_ERROR if an invalid compression
  33. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  34. version of the library linked do not match, or Z_ERRNO if there is
  35. an error reading or writing the files. */
  36. SDL_Surface * ekran;
  37. int def(FILE *source, FILE *dest, int level, int winBits=15, int memLevel =8)
  38. {
  39. int ret, flush;
  40. unsigned have;
  41. z_stream strm;
  42. unsigned char in[CHUNK];
  43. unsigned char out[CHUNK];
  44. /* allocate deflate state */
  45. strm.zalloc = Z_NULL;
  46. strm.zfree = Z_NULL;
  47. strm.opaque = Z_NULL;
  48. ret = deflateInit2(&strm, level,Z_DEFLATED,winBits,memLevel,0);//8-15, 1-9, 0-2
  49. if (ret != Z_OK)
  50. return ret;
  51. /* compress until end of file */
  52. do {
  53. strm.avail_in = fread(in, 1, CHUNK, source);
  54. if (ferror(source)) {
  55. (void)deflateEnd(&strm);
  56. return Z_ERRNO;
  57. }
  58. flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  59. strm.next_in = in;
  60. /* run deflate() on input until output buffer not full, finish
  61. compression if all of source has been read in */
  62. do {
  63. strm.avail_out = CHUNK;
  64. strm.next_out = out;
  65. ret = deflate(&strm, flush); /* no bad return value */
  66. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  67. have = CHUNK - strm.avail_out;
  68. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  69. (void)deflateEnd(&strm);
  70. return Z_ERRNO;
  71. }
  72. } while (strm.avail_out == 0);
  73. assert(strm.avail_in == 0); /* all input will be used */
  74. /* done when last data in file processed */
  75. } while (flush != Z_FINISH);
  76. assert(ret == Z_STREAM_END); /* stream will be complete */
  77. /* clean up and return */
  78. (void)deflateEnd(&strm);
  79. return Z_OK;
  80. }
  81. /* Decompress from file source to file dest until stream ends or EOF.
  82. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  83. allocated for processing, Z_DATA_ERROR if the deflate data is
  84. invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
  85. the version of the library linked do not match, or Z_ERRNO if there
  86. is an error reading or writing the files. */
  87. int inf(FILE *source, FILE *dest, int wBits = 15)
  88. {
  89. int ret;
  90. unsigned have;
  91. z_stream strm;
  92. unsigned char in[CHUNK];
  93. unsigned char out[CHUNK];
  94. /* allocate inflate state */
  95. strm.zalloc = Z_NULL;
  96. strm.zfree = Z_NULL;
  97. strm.opaque = Z_NULL;
  98. strm.avail_in = 0;
  99. strm.next_in = Z_NULL;
  100. ret = inflateInit2(&strm, wBits);
  101. if (ret != Z_OK)
  102. return ret;
  103. /* decompress until deflate stream ends or end of file */
  104. do {
  105. strm.avail_in = fread(in, 1, CHUNK, source);
  106. if (ferror(source)) {
  107. (void)inflateEnd(&strm);
  108. return Z_ERRNO;
  109. }
  110. if (strm.avail_in == 0)
  111. break;
  112. strm.next_in = in;
  113. /* run inflate() on input until output buffer not full */
  114. do {
  115. strm.avail_out = CHUNK;
  116. strm.next_out = out;
  117. ret = inflate(&strm, Z_NO_FLUSH);
  118. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  119. switch (ret) {
  120. case Z_NEED_DICT:
  121. ret = Z_DATA_ERROR; /* and fall through */
  122. case Z_DATA_ERROR:
  123. case Z_MEM_ERROR:
  124. (void)inflateEnd(&strm);
  125. return ret;
  126. }
  127. have = CHUNK - strm.avail_out;
  128. if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  129. (void)inflateEnd(&strm);
  130. return Z_ERRNO;
  131. }
  132. } while (strm.avail_out == 0);
  133. /* done when inflate() says it's done */
  134. } while (ret != Z_STREAM_END);
  135. /* clean up and return */
  136. (void)inflateEnd(&strm);
  137. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  138. }
  139. /* report a zlib or i/o error */
  140. void zerr(int ret)
  141. {
  142. fputs("zpipe: ", stderr);
  143. switch (ret) {
  144. case Z_ERRNO:
  145. if (ferror(stdin))
  146. fputs("error reading stdin\n", stderr);
  147. if (ferror(stdout))
  148. fputs("error writing stdout\n", stderr);
  149. break;
  150. case Z_STREAM_ERROR:
  151. fputs("invalid compression level\n", stderr);
  152. break;
  153. case Z_DATA_ERROR:
  154. fputs("invalid or incomplete deflate data\n", stderr);
  155. break;
  156. case Z_MEM_ERROR:
  157. fputs("out of memory\n", stderr);
  158. break;
  159. case Z_VERSION_ERROR:
  160. fputs("zlib version mismatch!\n", stderr);
  161. }
  162. }
  163. void SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B)
  164. {
  165. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  166. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  167. {
  168. p[0] = R;
  169. p[1] = G;
  170. p[2] = B;
  171. }
  172. else
  173. {
  174. p[0] = B;
  175. p[1] = G;
  176. p[2] = R;
  177. }
  178. SDL_UpdateRect(ekran, x, y, 1, 1);
  179. }
  180. int _tmain(int argc, _TCHAR* argv[])
  181. {
  182. int xx=0, yy=0, zz=0;
  183. SDL_Event sEvent;
  184. srand ( time(NULL) );
  185. SDL_Surface *screen, *temp;
  186. std::vector<SDL_Surface*> Sprites;
  187. float i;
  188. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER/*|SDL_INIT_EVENTTHREAD*/)==0)
  189. {
  190. screen = SDL_SetVideoMode(1024,768,24,SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  191. ekran = screen;
  192. //FILE * zr = fopen("mal.txt","r");
  193. //FILE * ko = fopen("wyn.txt","w");
  194. //FILE * kodd = fopen("kod.txt","r");
  195. //FILE * deko = fopen("dekod.txt","w");
  196. //def(zr,ko,1);
  197. //inf(kodd, deko);
  198. //fclose(ko);fclose(zr);
  199. //for (int i=0;i<=20;i++)
  200. //{
  201. // zr = fopen("kod2.txt","r");
  202. // char c [200];
  203. // sprintf(c,"wyn%d.txt",i);
  204. // ko = fopen(c,"w");
  205. // def(zr,ko,i);
  206. // fclose(ko);fclose(zr);
  207. //}
  208. SDL_WM_SetCaption(NAME,"");
  209. THC timeHandler tmh;
  210. CArtHandler * arth = new CArtHandler;
  211. arth->loadArtifacts();
  212. CHeroHandler * heroh = new CHeroHandler;
  213. heroh->loadHeroes();
  214. CCreatureHandler * creh = new CCreatureHandler;
  215. creh->loadCreatures();
  216. CAbilityHandler * abilh = new CAbilityHandler;
  217. abilh->loadAbilities();
  218. CAmbarCendamo * ac = new CAmbarCendamo("4gryf");
  219. THC std::cout<<"Wczytywanie pliku: "<<tmh.getDif()<<std::endl;
  220. ac->deh3m();
  221. THC std::cout<<"Rozpoznawianie pliku lacznie: "<<tmh.getDif()<<std::endl;
  222. ac->loadDefs();
  223. THC std::cout<<"Wczytywanie defow: "<<tmh.getDif()<<std::endl;
  224. mapHandler * mh = new mapHandler();
  225. mh->reader = ac;
  226. THC std::cout<<"Stworzenie mapHandlera: "<<tmh.getDif()<<std::endl;
  227. mh->init();
  228. THC std::cout<<"Inicjalizacja mapHandlera: "<<tmh.getDif()<<std::endl;
  229. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  230. SDL_Surface * teren = mh->terrainRect(xx,yy,32,24);
  231. THC std::cout<<"Przygotowanie terenu do wyswietlenia: "<<tmh.getDif()<<std::endl;
  232. SDL_BlitSurface(teren,NULL,ekran,NULL);
  233. SDL_FreeSurface(teren);
  234. SDL_Flip(ekran);
  235. THC std::cout<<"Wyswietlenie terenu: "<<tmh.getDif()<<std::endl;
  236. //SDL_Surface * ss = ac->defs[0]->ourImages[0].bitmap;
  237. //SDL_BlitSurface(ss, NULL, ekran, NULL);
  238. for(;;) // main loop
  239. {
  240. try
  241. {
  242. if(SDL_PollEvent(&sEvent)) //wait for event...
  243. {
  244. if(sEvent.type==SDL_QUIT)
  245. return 0;
  246. else if (sEvent.type==SDL_KEYDOWN)
  247. {
  248. switch (sEvent.key.keysym.sym)
  249. {
  250. case SDLK_LEFT:
  251. {
  252. if(xx>0)
  253. xx--;
  254. break;
  255. }
  256. case (SDLK_RIGHT):
  257. {
  258. if(xx<ac->map.width-33)
  259. xx++;
  260. break;
  261. }
  262. case (SDLK_UP):
  263. {
  264. if(yy>0)
  265. yy--;
  266. break;
  267. }
  268. case (SDLK_DOWN):
  269. {
  270. if(yy<ac->map.height-25)
  271. yy++;
  272. break;
  273. }
  274. case (SDLK_q):
  275. {
  276. return 0;
  277. break;
  278. }
  279. case (SDLK_u):
  280. {
  281. if (zz)
  282. zz--;
  283. else zz++;
  284. break;
  285. }
  286. }
  287. SDL_FillRect(ekran, NULL, SDL_MapRGB(ekran->format, 0, 0, 0));
  288. SDL_Surface * help = mh->terrainRect(xx,yy,32,24,zz);
  289. SDL_BlitSurface(help,NULL,ekran,NULL);
  290. SDL_FreeSurface(help);
  291. SDL_Flip(ekran);
  292. }
  293. }
  294. }
  295. catch(...)
  296. { continue; }
  297. SDL_Delay(30); //give time for other apps
  298. }
  299. return 0;
  300. }
  301. else
  302. {
  303. printf("Coœ posz³o nie tak: %s/n",SDL_GetError());
  304. return -1;
  305. }
  306. }