Fenetre.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "Fenetre.h"
  2. Fenetre::Fenetre(): m_ecran(0), m_vies(0), typeCo(LOCAL), m_ogre(&m_input, &m_terrain), m_menestrel(&m_input, &m_terrain)
  3. {
  4. if(init())
  5. {
  6. std::string nomNiveau;
  7. while((nomNiveau = menu(m_ecran)) != "quitter")
  8. {
  9. if(!chargerNiveau(nomNiveau))
  10. break;
  11. if(typeCo == LOCAL)
  12. jeulocal();
  13. if(typeCo == HOST)
  14. jeuhost();
  15. if(typeCo == CLIENT)
  16. jeuclient();
  17. if(!m_input.continuer())
  18. break;
  19. }
  20. }
  21. }
  22. Fenetre::~Fenetre()
  23. {
  24. SDL_FreeSurface(m_ecran);
  25. SDL_FreeSurface(m_vies);
  26. SDL_FreeSurface(m_grosBourin);
  27. SDL_Quit();
  28. TTF_Quit();
  29. }
  30. bool Fenetre::init()
  31. {
  32. m_vies = IMG_Load("Skins/vie.png");
  33. if(m_vies == 0)
  34. {
  35. std::cout << "Chargement de l'image vie impossible" << std::endl;
  36. return false;
  37. }
  38. m_grosBourin = IMG_Load("Skins/grosBourin.png");
  39. if(m_grosBourin == 0)
  40. {
  41. std::cout << "Chargement de l'image grosBourin impossible" << std::endl;
  42. return false;
  43. }
  44. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1 || TTF_Init() == -1)
  45. {
  46. std::cout << "Initialisation de la SDL impossible" << std::endl;
  47. SDL_Quit();
  48. TTF_Quit();
  49. return false;
  50. }
  51. SDL_WM_SetCaption("PacMen", 0);
  52. m_ecran = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  53. if(m_ecran == 0)
  54. {
  55. std::cout << "Creation de l'ecran impossible" << std::endl;
  56. SDL_Quit();
  57. TTF_Quit();
  58. return false;
  59. }
  60. return true;
  61. }
  62. void Fenetre::jeulocal()
  63. {
  64. int vie = 3;
  65. Uint32 tempsprecedent = SDL_GetTicks();
  66. SDL_Rect pos;
  67. m_input.reset();
  68. while(m_terrain.itemsRestants()!=0)
  69. {
  70. m_input.updateEvenement();
  71. if(m_input.getTouches(SDLK_ESCAPE) || !m_input.continuer())
  72. {
  73. std::cout << "quitter niveau" << std::endl;
  74. return;
  75. }
  76. m_terrain.afficher(m_ecran);
  77. m_ogre.deplacer();
  78. m_menestrel.deplacer();
  79. for(unsigned int i = 0;i<m_fantomes.size();i++)
  80. m_fantomes[i]->deplacer();
  81. for(unsigned int i = 0;i<m_fantomes.size();i++)
  82. {
  83. if(m_ogre.getPosition() == m_fantomes[i]->getPosition() && m_fantomes[i]->vivant())
  84. {
  85. if(m_ogre.getBerzerk())
  86. {
  87. m_fantomes[i]->positionner(m_posFantomes);
  88. m_fantomes[i]->tuer();
  89. }
  90. else
  91. {
  92. for(unsigned int i = 0;i<m_fantomes.size();i++)
  93. m_fantomes[i]->positionner(m_posFantomes);
  94. m_menestrel.positionner(m_posPersos);
  95. m_ogre.positionner(m_posPersos);
  96. vie--;
  97. }
  98. }
  99. if(m_menestrel.getPosition() == m_fantomes[i]->getPosition() && m_fantomes[i]->vivant())
  100. {
  101. for(unsigned int i = 0;i<m_fantomes.size();i++)
  102. m_fantomes[i]->positionner(m_posFantomes);
  103. m_ogre.positionner(m_posPersos);
  104. m_menestrel.positionner(m_posPersos);
  105. vie--;
  106. }
  107. }
  108. m_ogre.afficher(m_ecran);
  109. m_menestrel.afficher(m_ecran);
  110. for(unsigned int i = 0;i<m_fantomes.size();i++)
  111. m_fantomes[i]->afficher(m_ecran);
  112. pos.y = 0;
  113. for(int i = 0;i<vie;i++)
  114. {
  115. pos.x = i*30;
  116. SDL_BlitSurface(m_vies, 0, m_ecran, &pos);
  117. }
  118. if(m_fantomes[0]->getMultiKill(m_fantomes.size()))
  119. {
  120. vie++;
  121. pos.x = 44;
  122. pos.y = 247;
  123. SDL_BlitSurface(m_grosBourin, 0, m_ecran, &pos);
  124. SDL_Flip(m_ecran);
  125. SDL_Delay(2000);
  126. }
  127. if(vie <= 0)
  128. {
  129. std::cout << "mort" << std::endl;
  130. return;
  131. }
  132. SDL_Flip(m_ecran);
  133. while((SDL_GetTicks()-tempsprecedent)<1000/60);
  134. tempsprecedent = SDL_GetTicks();
  135. }
  136. std::cout << "plus de steaks" << std::endl;
  137. }
  138. void Fenetre::jeuhost()
  139. {
  140. }
  141. void Fenetre::jeuclient()
  142. {
  143. }
  144. bool Fenetre::chargerNiveau(std::string nomNiveau)
  145. {
  146. viderFantomes();
  147. if(!m_terrain.creer(nomNiveau))
  148. return false;
  149. m_posPersos.x = m_terrain.popAllie(X);
  150. m_posPersos.y = m_terrain.popAllie(Y);
  151. m_posFantomes.x = m_terrain.popFantom(X);
  152. m_posFantomes.y = m_terrain.popFantom(Y);
  153. m_ogre.positionner(m_posPersos);
  154. m_menestrel.positionner(m_posPersos);
  155. for(int i = 0;i<m_terrain.getEffectifMobs();i++)
  156. {
  157. m_fantomes.push_back(new Fantome(&m_terrain, &m_ogre, &m_menestrel));
  158. m_fantomes[i]->positionner(m_posFantomes);
  159. }
  160. return true;
  161. }
  162. void Fenetre::viderFantomes()
  163. {
  164. for(unsigned int i = 0;i<m_fantomes.size();i++)
  165. delete m_fantomes[i];
  166. m_fantomes.clear();
  167. }