CMT.cpp 8.5 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, *GEORM;
  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. GEORM = TTF_OpenFont("Fonts\\georgia.ttf",10);
  84. //initializing audio
  85. CMusicHandler * mush = new CMusicHandler;
  86. mush->initMusics();
  87. //CSndHandler snd("Heroes3.snd");
  88. //snd.extract("AELMMOVE.wav","snddd.wav");
  89. //audio initialized
  90. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  91. {
  92. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  93. // well, there's no music, but most games don't break without music...
  94. }*/
  95. screen2 = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  96. screen = SDL_ConvertSurface(screen2, screen2->format, SDL_SWSURFACE);
  97. ekran = screen;
  98. SDL_WM_SetCaption(NAME,""); //set window title
  99. CGameInfo * cgi = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  100. CGameInfo::mainObj = cgi;
  101. cgi->consoleh = new CConsoleHandler;
  102. cgi->consoleh->runConsole();
  103. cgi->mush = mush;
  104. cgi->curh = new CCursorHandler;
  105. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  106. cgi->spriteh = new CLodHandler;
  107. cgi->spriteh->init(std::string("Data\\H3sprite.lod"));
  108. cgi->bitmaph = new CLodHandler;
  109. cgi->bitmaph->init(std::string("Data\\H3bitmap.lod"));
  110. THC std::cout<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  111. cgi->curh->initCursor();
  112. cgi->curh->showGraphicCursor();
  113. cgi->screenh = new CScreenHandler;
  114. cgi->screenh->initScreen();
  115. //colors initialization
  116. SDL_Color p;
  117. p.unused = 0;
  118. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  119. cgi->playerColors.push_back(p); //red
  120. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  121. cgi->playerColors.push_back(p); //blue
  122. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  123. cgi->playerColors.push_back(p);//tan
  124. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  125. cgi->playerColors.push_back(p); //green
  126. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  127. cgi->playerColors.push_back(p); //orange
  128. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  129. cgi->playerColors.push_back(p); //purple
  130. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  131. cgi->playerColors.push_back(p);//teal
  132. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  133. cgi->playerColors.push_back(p);//pink
  134. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  135. cgi->neutralColor = p;//gray
  136. //colors initialized
  137. cgi->townh = new CTownHandler;
  138. cgi->townh->loadNames();
  139. CAbilityHandler * abilh = new CAbilityHandler;
  140. abilh->loadAbilities();
  141. cgi->abilh = abilh;
  142. CHeroHandler * heroh = new CHeroHandler;
  143. heroh->loadHeroes();
  144. heroh->loadPortraits();
  145. cgi->heroh = heroh;
  146. cgi->generaltexth = new CGeneralTextHandler;
  147. cgi->generaltexth->load();
  148. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  149. CPreGame * cpg = new CPreGame(); //main menu and submenus
  150. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  151. cpg->mush = mush;
  152. cgi->scenarioOps = cpg->runLoop();
  153. THC tmh.getDif();
  154. cgi->sspriteh = new CSemiLodHandler();
  155. cgi->sspriteh->openLod("H3sprite.lod");
  156. CArtHandler * arth = new CArtHandler;
  157. arth->loadArtifacts();
  158. cgi->arth = arth;
  159. CCreatureHandler * creh = new CCreatureHandler;
  160. creh->loadCreatures();
  161. cgi->creh = creh;
  162. CSpellHandler * spellh = new CSpellHandler;
  163. spellh->loadSpells();
  164. cgi->spellh = spellh;
  165. CBuildingHandler * buildh = new CBuildingHandler;
  166. buildh->loadBuildings();
  167. cgi->buildh = buildh;
  168. CObjectHandler * objh = new CObjectHandler;
  169. objh->loadObjects();
  170. cgi->objh = objh;
  171. cgi->dobjinfo = new CDefObjInfoHandler;
  172. cgi->dobjinfo->load();
  173. cgi->state = new CGameState();
  174. cgi->pathf = new CPathfinder();
  175. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  176. std::string mapname;
  177. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  178. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  179. gzFile map = gzopen(mapname.c_str(),"rb");
  180. std::string mapstr;int pom;
  181. while((pom=gzgetc(map))>=0)
  182. {
  183. mapstr+=pom;
  184. }
  185. gzclose(map);
  186. unsigned char *initTable = new unsigned char[mapstr.size()];
  187. for(int ss=0; ss<mapstr.size(); ++ss)
  188. {
  189. initTable[ss] = mapstr[ss];
  190. }
  191. #define CHOOSE
  192. #ifdef CHOOSE
  193. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  194. #else
  195. CAmbarCendamo * ac = new CAmbarCendamo("RoEtest"); //4gryf
  196. #endif
  197. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  198. cgi->ac = ac;
  199. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  200. ac->deh3m();
  201. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  202. ac->loadDefs();
  203. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  204. CMapHandler * mh = new CMapHandler();
  205. cgi->mh = mh;
  206. mh->reader = ac;
  207. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  208. mh->init();
  209. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  210. //SDL_Rect * sr = new SDL_Rect(); sr->h=64;sr->w=64;sr->x=0;sr->y=0;
  211. //SDL_Surface * teren = mh->terrainRect(xx,yy,25,19);
  212. //THC std::cout<<"Preparing terrain to display: "<<tmh.getDif()<<std::endl;
  213. //SDL_BlitSurface(teren,NULL,ekran,NULL);
  214. //SDL_FreeSurface(teren);
  215. //SDL_UpdateRect(ekran, 0, 0, ekran->w, ekran->h);
  216. //THC std::cout<<"Displaying terrain: "<<tmh.getDif()<<std::endl;
  217. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  218. {
  219. if(cgi->scenarioOps.playerInfos[i].name=="AI")
  220. cgi->playerint.push_back(new CGlobalAI());
  221. else
  222. {
  223. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  224. ((CPlayerInterface*)(cgi->playerint[i]))->init();
  225. }
  226. }
  227. while(1) //main game loop, one execution per turn
  228. {
  229. for (int i=0;i<cgi->playerint.size();i++)
  230. {
  231. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  232. cgi->playerint[i]->yourTurn();
  233. }
  234. }
  235. }
  236. else
  237. {
  238. printf("Something was wrong: %s/n", SDL_GetError());
  239. return -1;
  240. }
  241. }