CMT.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. srand ( time(NULL) );
  179. SDL_Surface *screen, *temp;
  180. std::vector<SDL_Surface*> Sprites;
  181. float i;
  182. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER/*|SDL_INIT_EVENTTHREAD*/)==0)
  183. {
  184. screen = SDL_SetVideoMode(1024,768,24,SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  185. ekran = screen;
  186. //FILE * zr = fopen("mal.txt","r");
  187. //FILE * ko = fopen("wyn.txt","w");
  188. //FILE * kodd = fopen("kod.txt","r");
  189. //FILE * deko = fopen("dekod.txt","w");
  190. //def(zr,ko,1);
  191. //inf(kodd, deko);
  192. //fclose(ko);fclose(zr);
  193. //for (int i=0;i<=20;i++)
  194. //{
  195. // zr = fopen("kod2.txt","r");
  196. // char c [200];
  197. // sprintf(c,"wyn%d.txt",i);
  198. // ko = fopen(c,"w");
  199. // def(zr,ko,i);
  200. // fclose(ko);fclose(zr);
  201. //}
  202. THC timeHandler tmh;
  203. CAmbarCendamo * ac = new CAmbarCendamo("3gryf");
  204. THC std::cout<<"Wczytywanie pliku: "<<tmh.getDif()<<std::endl;
  205. ac->deh3m();
  206. THC std::cout<<"Rozpoznawianie pliku lacznie: "<<tmh.getDif()<<std::endl;
  207. ac->loadDefs();
  208. THC std::cout<<"Wczytywanie defow: "<<tmh.getDif()<<std::endl;
  209. mapHandler * mh = new mapHandler();
  210. mh->reader = ac;
  211. THC std::cout<<"Stworzenie mapHandlera: "<<tmh.getDif()<<std::endl;
  212. mh->init();
  213. THC std::cout<<"Inicjalizacja mapHandlera: "<<tmh.getDif()<<std::endl;
  214. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  215. SDL_Surface * teren = mh->terrainRect(0,0,32,32);
  216. THC std::cout<<"Przygotowanie terenu do wyswietlenia: "<<tmh.getDif()<<std::endl;
  217. SDL_BlitSurface(teren,NULL,ekran,NULL);
  218. SDL_Flip(ekran);
  219. THC std::cout<<"Wyswietlenie terenu: "<<tmh.getDif()<<std::endl;
  220. //SDL_Surface * ss = ac->defs[0]->ourImages[0].bitmap;
  221. //SDL_BlitSurface(ss, NULL, ekran, NULL);
  222. SDL_Delay(5000);
  223. SDL_Quit();
  224. return 0;
  225. }
  226. else
  227. {
  228. printf("Coœ posz³o nie tak: %s/n",SDL_GetError());
  229. return -1;
  230. }
  231. }