CMT.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "CVideoHandler.h"
  7. #include "SDL_mixer.h"
  8. #include "CBuildingHandler.h"
  9. #include "SDL_Extensions.h"
  10. #include "SDL_framerate.h"
  11. #include <cmath>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <string>
  15. #include <assert.h>
  16. #include <vector>
  17. #include "zlib.h"
  18. #include <cmath>
  19. #include <ctime>
  20. #include "CArtHandler.h"
  21. #include "CHeroHandler.h"
  22. #include "CCreatureHandler.h"
  23. #include "CAbilityHandler.h"
  24. #include "CSpellHandler.h"
  25. #include "CBuildingHandler.h"
  26. #include "CObjectHandler.h"
  27. #include "CGameInfo.h"
  28. #include "CMusicHandler.h"
  29. #include "CSemiLodHandler.h"
  30. #include "CLodHandler.h"
  31. #include "CDefHandler.h"
  32. #include "CSndHandler.h"
  33. #include "CTownHandler.h"
  34. #include "CDefObjInfoHandler.h"
  35. #include "CAmbarCendamo.h"
  36. #include "mapHandler.h"
  37. #include "global.h"
  38. #include "CPreGame.h"
  39. #include "CGeneralTextHandler.h"
  40. #include "CConsoleHandler.h"
  41. #include "CCursorHandler.h"
  42. #include "CScreenHandler.h"
  43. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  44. # include <fcntl.h>
  45. # include <io.h>
  46. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  47. #else
  48. # define SET_BINARY_MODE(file)
  49. #endif
  50. #define CHUNK 16384
  51. const char * NAME = "VCMI 0.3";
  52. /* Compress from file source to file dest until EOF on source.
  53. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  54. allocated for processing, Z_STREAM_ERROR if an invalid compression
  55. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  56. version of the library linked do not match, or Z_ERRNO if there is
  57. an error reading or writing the files. */
  58. SDL_Surface * ekran, * screen, * screen2;
  59. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX;
  60. int _tmain(int argc, _TCHAR* argv[])
  61. {
  62. //CBIKHandler cb;
  63. //cb.open("CSECRET.BIK");
  64. THC timeHandler tmh;
  65. THC tmh.getDif();
  66. int xx=0, yy=0, zz=0;
  67. SDL_Event sEvent;
  68. srand ( time(NULL) );
  69. SDL_Surface *temp;
  70. std::vector<SDL_Surface*> Sprites;
  71. float i;
  72. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  73. {
  74. CPG=NULL;
  75. TTF_Init();
  76. atexit(TTF_Quit);
  77. atexit(SDL_Quit);
  78. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  79. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  80. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  81. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  82. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  83. //initializing audio
  84. CMusicHandler * mush = new CMusicHandler;
  85. mush->initMusics();
  86. //CSndHandler snd("Heroes3.snd");
  87. //snd.extract("AELMMOVE.wav","snddd.wav");
  88. //audio initialized
  89. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  90. {
  91. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  92. // well, there's no music, but most games don't break without music...
  93. }*/
  94. screen2 = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  95. screen = SDL_ConvertSurface(screen2, screen2->format, SDL_SWSURFACE);
  96. ekran = screen;
  97. SDL_WM_SetCaption(NAME,""); //set window title
  98. CGameInfo * cgi = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  99. CGameInfo::mainObj = cgi;
  100. cgi->consoleh = new CConsoleHandler;
  101. cgi->consoleh->runConsole();
  102. cgi->mush = mush;
  103. cgi->curh = new CCursorHandler;
  104. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  105. cgi->spriteh = new CLodHandler;
  106. cgi->spriteh->init(std::string("Data\\H3sprite.lod"));
  107. cgi->bitmaph = new CLodHandler;
  108. cgi->bitmaph->init(std::string("Data\\H3bitmap.lod"));
  109. cgi->curh->initCursor();
  110. cgi->curh->showGraphicCursor();
  111. cgi->screenh = new CScreenHandler;
  112. cgi->screenh->initScreen();
  113. //colors initialization
  114. SDL_Color p;
  115. p.unused = 0;
  116. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  117. cgi->playerColors.push_back(p); //red
  118. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  119. cgi->playerColors.push_back(p); //blue
  120. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  121. cgi->playerColors.push_back(p);//tan
  122. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  123. cgi->playerColors.push_back(p); //green
  124. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  125. cgi->playerColors.push_back(p); //orange
  126. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  127. cgi->playerColors.push_back(p); //purple
  128. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  129. cgi->playerColors.push_back(p);//teal
  130. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  131. cgi->playerColors.push_back(p);//pink
  132. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  133. cgi->neutralColor = p;//gray
  134. //colors initialized
  135. cgi->townh = new CTownHandler;
  136. cgi->townh->loadNames();
  137. CAbilityHandler * abilh = new CAbilityHandler;
  138. abilh->loadAbilities();
  139. cgi->abilh = abilh;
  140. CHeroHandler * heroh = new CHeroHandler;
  141. heroh->loadHeroes();
  142. heroh->loadPortraits();
  143. cgi->heroh = heroh;
  144. THC std::cout<<"Loading .lods: "<<tmh.getDif()<<std::endl;
  145. CPreGame * cpg = new CPreGame(); //main menu and submenus
  146. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  147. cpg->mush = mush;
  148. cgi->scenarioOps = cpg->runLoop();
  149. THC tmh.getDif();
  150. cgi->sspriteh = new CSemiLodHandler();
  151. cgi->sspriteh->openLod("H3sprite.lod");
  152. CArtHandler * arth = new CArtHandler;
  153. arth->loadArtifacts();
  154. cgi->arth = arth;
  155. CCreatureHandler * creh = new CCreatureHandler;
  156. creh->loadCreatures();
  157. cgi->creh = creh;
  158. CSpellHandler * spellh = new CSpellHandler;
  159. spellh->loadSpells();
  160. cgi->spellh = spellh;
  161. CBuildingHandler * buildh = new CBuildingHandler;
  162. buildh->loadBuildings();
  163. cgi->buildh = buildh;
  164. CObjectHandler * objh = new CObjectHandler;
  165. objh->loadObjects();
  166. cgi->objh = objh;
  167. cgi->generaltexth = new CGeneralTextHandler;
  168. cgi->generaltexth->load();
  169. cgi->dobjinfo = new CDefObjInfoHandler;
  170. cgi->dobjinfo->load();
  171. cgi->state = new CGameState();
  172. cgi->pathf = new CPathfinder();
  173. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  174. std::string mapname;
  175. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  176. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  177. gzFile map = gzopen(mapname.c_str(),"rb");
  178. std::string mapstr;int pom;
  179. while((pom=gzgetc(map))>=0)
  180. {
  181. mapstr+=pom;
  182. }
  183. gzclose(map);
  184. unsigned char *initTable = new unsigned char[mapstr.size()];
  185. for(int ss=0; ss<mapstr.size(); ++ss)
  186. {
  187. initTable[ss] = mapstr[ss];
  188. }
  189. #define CHOOSE
  190. #ifdef CHOOSE
  191. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  192. #else
  193. CAmbarCendamo * ac = new CAmbarCendamo("1smok"); //4gryf
  194. #endif
  195. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  196. cgi->ac = ac;
  197. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  198. ac->deh3m();
  199. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  200. ac->loadDefs();
  201. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  202. CMapHandler * mh = new CMapHandler();
  203. cgi->mh = mh;
  204. mh->reader = ac;
  205. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  206. mh->init();
  207. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  208. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  209. //SDL_Surface * teren = mh->terrainRect(xx,yy,25,19);
  210. //THC std::cout<<"Preparing terrain to display: "<<tmh.getDif()<<std::endl;
  211. //SDL_BlitSurface(teren,NULL,ekran,NULL);
  212. //SDL_FreeSurface(teren);
  213. //SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  214. //THC std::cout<<"Displaying terrain: "<<tmh.getDif()<<std::endl;
  215. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  216. {
  217. if(cgi->scenarioOps.playerInfos[i].name=="AI")
  218. cgi->playerint.push_back(new CGlobalAI());
  219. else
  220. {
  221. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  222. ((CPlayerInterface*)(cgi->playerint[i]))->init();
  223. }
  224. }
  225. while(1) //main game loop, one execution per turn
  226. {
  227. for (int i=0;i<cgi->playerint.size();i++)
  228. {
  229. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  230. cgi->playerint[i]->yourTurn();
  231. }
  232. }
  233. }
  234. else
  235. {
  236. printf("Something was wrong: %s/n", SDL_GetError());
  237. return -1;
  238. }
  239. }