CMT.cpp 7.9 KB

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