CMT.cpp 9.4 KB

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