2
0

CMT.cpp 9.0 KB

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