CMT.cpp 8.2 KB

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