CMT.cpp 8.4 KB

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