CMT.cpp 7.9 KB

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