|
@@ -0,0 +1,438 @@
|
|
|
|
+#include "Niveau.h"
|
|
|
|
+
|
|
|
|
+Niveau::Niveau():m_font(0),m_items(0),m_nom("Sans Nom"),m_nomFichier(""),m_lvlNumber(0),m_effectifItems(0)
|
|
|
|
+{
|
|
|
|
+ m_font = SDL_CreateRGBSurface(SDL_HWSURFACE, 600, 600, 32, 0, 0, 0, 0);
|
|
|
|
+ m_items = SDL_CreateRGBSurface(SDL_HWSURFACE, 600, 600, 32, 0, 0, 0, 0);
|
|
|
|
+ reset();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Niveau::Niveau(std::string nomFichier, Uint16 fraction):m_font(0),m_items(0),m_nom("Sans Nom"),m_nomFichier(""),m_lvlNumber(0),m_effectifItems(0)
|
|
|
|
+{
|
|
|
|
+ m_font = SDL_CreateRGBSurface(SDL_HWSURFACE, 600, 600, 32, 0, 0, 0, 0);
|
|
|
|
+ m_items = SDL_CreateRGBSurface(SDL_HWSURFACE, 600, 600, 32, 0, 0, 0, 0);
|
|
|
|
+
|
|
|
|
+ if (!creer(nomFichier,fraction))
|
|
|
|
+ reset();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Niveau::~Niveau()
|
|
|
|
+{
|
|
|
|
+ SDL_FreeSurface(m_font);
|
|
|
|
+ SDL_FreeSurface(m_items);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int Niveau::peindreFont(Uint16 fraction)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Uint32 fontColor(8388608);
|
|
|
|
+ SDL_FillRect(m_font,NULL,fontColor);
|
|
|
|
+ Uint32 blanc(SDL_MapRGB(m_font->format,255,255,255));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_Surface* fontCopie = SDL_CreateRGBSurface(SDL_HWSURFACE, m_font->w, m_font->h, 32, 0, 0, 0, 0);
|
|
|
|
+ SDL_Surface* mur = SDL_CreateRGBSurface(SDL_HWSURFACE, LG_BLOC, LG_BLOC, 32, 0, 0, 0, 0);
|
|
|
|
+ SDL_FillRect(fontCopie,NULL,fontColor);
|
|
|
|
+ SDL_FillRect(mur,NULL,blanc);
|
|
|
|
+ SDL_Rect pos={0,0,0,0};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (fraction==0 || LG_BLOC%fraction!=0)
|
|
|
|
+ {
|
|
|
|
+ std::cout << "Objet Niveau: argument non renseigné ou non-multiple de " << LG_BLOC << std::endl;
|
|
|
|
+ fraction=5;
|
|
|
|
+ }
|
|
|
|
+ Uint16 coteGum(LG_BLOC/fraction);
|
|
|
|
+ SDL_Surface* gomme = SDL_CreateRGBSurface(SDL_HWSURFACE,coteGum, coteGum, 32, 0, 0, 0, 0);
|
|
|
|
+ SDL_FillRect(gomme,NULL,fontColor);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_Surface* solTexture = SDL_LoadBMP("Textures/planks.bmp");
|
|
|
|
+ SDL_Surface* murTexture = SDL_LoadBMP("Textures/cobblestone.bmp");
|
|
|
|
+ SDL_Surface* plaqueSprite = SDL_LoadBMP("Textures/plaque.bmp");
|
|
|
|
+ SDL_Surface* spawnAllieImg = SDL_LoadBMP("Textures/spawnAllie.bmp");
|
|
|
|
+ SDL_Surface* spawnFantomImg = SDL_LoadBMP("Textures/spawnFantom.bmp");
|
|
|
|
+ if (solTexture==0 || murTexture==0 || plaqueSprite==0 || spawnAllieImg==0 || spawnFantomImg==0)
|
|
|
|
+ {
|
|
|
|
+ std::cout << "Problème de chargement de texture dans l'objet Niveau, dans textures." << std::endl;
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ SDL_SetColorKey(plaqueSprite,SDL_SRCCOLORKEY,blanc);
|
|
|
|
+ SDL_SetColorKey(spawnAllieImg,SDL_SRCCOLORKEY,blanc);
|
|
|
|
+ SDL_SetColorKey(spawnFantomImg,SDL_SRCCOLORKEY,blanc);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_Surface* fullMur = SDL_CreateRGBSurface(SDL_HWSURFACE,600,600, 32, 0, 0, 0, 0);
|
|
|
|
+ for (int i(0); i<600; i++)
|
|
|
|
+ {
|
|
|
|
+ for (int j(0); j<600; j++)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ pos.x=i*solTexture->w;
|
|
|
|
+ pos.y=j*solTexture->h;
|
|
|
|
+ SDL_BlitSurface(solTexture, 0, m_font, &pos);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pos.x=i*murTexture->w;
|
|
|
|
+ pos.y=j*murTexture->h;
|
|
|
|
+ SDL_BlitSurface(murTexture, 0, fullMur, &pos);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ const int largeurTabl(LG_BLOC/coteGum);
|
|
|
|
+
|
|
|
|
+ for (int y(0); y<LG_TABL; y++)
|
|
|
|
+ {
|
|
|
|
+ for (int x(0); x<LG_TABL; x++)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ if (m_tabl[y][x]==PLAQUE)
|
|
|
|
+ {
|
|
|
|
+ pos.x=x*LG_BLOC+(LG_BLOC-plaqueSprite->w)/2;
|
|
|
|
+ pos.y=y*LG_BLOC+(LG_BLOC-plaqueSprite->h);
|
|
|
|
+ SDL_BlitSurface(plaqueSprite,0,m_font,&pos);
|
|
|
|
+ }
|
|
|
|
+ if (m_tabl[y][x]==SPAWN_ALLIE)
|
|
|
|
+ {
|
|
|
|
+ pos.x=x*LG_BLOC+(LG_BLOC-spawnAllieImg->w)/2;
|
|
|
|
+ pos.y=y*LG_BLOC+(LG_BLOC-spawnAllieImg->h)/2;
|
|
|
|
+ SDL_BlitSurface(spawnAllieImg,0,m_font,&pos);
|
|
|
|
+ }
|
|
|
|
+ if (m_tabl[y][x]==SPAWN_PHANTOM)
|
|
|
|
+ {
|
|
|
|
+ pos.x=x*LG_BLOC+(LG_BLOC-spawnFantomImg->w)/2;
|
|
|
|
+ pos.y=y*LG_BLOC+(LG_BLOC-spawnFantomImg->h)/2;
|
|
|
|
+ SDL_BlitSurface(spawnFantomImg,0,m_font,&pos);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (m_tabl[y][x]==MUR)
|
|
|
|
+ {
|
|
|
|
+ pos.x=x*LG_BLOC;
|
|
|
|
+ pos.y=y*LG_BLOC;
|
|
|
|
+ SDL_BlitSurface(mur, 0, fontCopie, &pos);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for (int i(0); i<largeurTabl; i++)
|
|
|
|
+ {
|
|
|
|
+ for (int j(0); j<largeurTabl; j++)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ short dirx,diry;
|
|
|
|
+ if (i==0)
|
|
|
|
+ dirx=-1;
|
|
|
|
+ else if (i==largeurTabl-1)
|
|
|
|
+ dirx=1;
|
|
|
|
+ else
|
|
|
|
+ dirx=0;
|
|
|
|
+ if (j==0)
|
|
|
|
+ diry=-1;
|
|
|
|
+ else if (j==largeurTabl-1)
|
|
|
|
+ diry=1;
|
|
|
|
+ else
|
|
|
|
+ diry=0;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (typeBloc(x+dirx,y+diry)!=MUR || typeBloc(x+dirx,y)!=MUR || typeBloc(x,y+diry)!=MUR)
|
|
|
|
+ {
|
|
|
|
+ pos.x=x*LG_BLOC+i*coteGum;
|
|
|
|
+ pos.y=y*LG_BLOC+j*coteGum;
|
|
|
|
+ SDL_BlitSurface(gomme, 0, fontCopie, &pos);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_SetColorKey(fontCopie, SDL_SRCCOLORKEY, blanc);
|
|
|
|
+ SDL_BlitSurface(fontCopie, 0, fullMur, 0);
|
|
|
|
+ SDL_SetColorKey(fullMur,SDL_SRCCOLORKEY,fontColor);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_BlitSurface(fullMur, 0, m_font, 0);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_FreeSurface(murTexture);
|
|
|
|
+ SDL_FreeSurface(solTexture);
|
|
|
|
+ SDL_FreeSurface(plaqueSprite);
|
|
|
|
+ SDL_FreeSurface(mur);
|
|
|
|
+ SDL_FreeSurface(fontCopie);
|
|
|
|
+ SDL_FreeSurface(gomme);
|
|
|
|
+ SDL_FreeSurface(fullMur);
|
|
|
|
+ SDL_FreeSurface(spawnAllieImg);
|
|
|
|
+ SDL_FreeSurface(spawnFantomImg);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool Niveau::creer(std::string nomFichier, Uint16 fraction)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ nomFichier="Niveaux/"+nomFichier;
|
|
|
|
+ m_nomFichier=nomFichier;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ std::ifstream fluxIn(nomFichier.c_str());
|
|
|
|
+ if (!fluxIn)
|
|
|
|
+ {
|
|
|
|
+ std::cout << "Echec de l'ouverture du fichier : "<<nomFichier<<"."<<std::endl;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ std::cout << "Ouverture du fichier "<<nomFichier<<" réussie."<<std::endl;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ getline(fluxIn,m_nom);
|
|
|
|
+ fluxIn >> m_lvlNumber;
|
|
|
|
+ for (int y(0); y<LG_TABL; y++)
|
|
|
|
+ {
|
|
|
|
+ for (int x(0); x<LG_TABL; x++)
|
|
|
|
+ {
|
|
|
|
+ fluxIn >> m_tabl[y][x];
|
|
|
|
+ if (m_tabl[y][x] == SPAWN_ALLIE)
|
|
|
|
+ {
|
|
|
|
+ m_spawnAllie[Y] = y;
|
|
|
|
+ m_spawnAllie[X] = x;
|
|
|
|
+ }
|
|
|
|
+ else if (m_tabl[y][x] < 0)
|
|
|
|
+ {
|
|
|
|
+ m_spawnPhantom[Y] = y;
|
|
|
|
+ m_spawnPhantom[X] = x;
|
|
|
|
+ m_effectifMobs = -m_tabl[y][x];
|
|
|
|
+ m_tabl[y][x] = SPAWN_PHANTOM;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (peindreFont(fraction)==1)
|
|
|
|
+ std::cout << "Problème dans la fonction peindreFont() dans niveau." << std::endl;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ remplirItems();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Niveau::afficher(SDL_Surface* support, bool hideItem)
|
|
|
|
+{
|
|
|
|
+ SDL_BlitSurface(m_font, 0, support, 0);
|
|
|
|
+ if (!hideItem)
|
|
|
|
+ SDL_BlitSurface(m_items, 0, support, 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Niveau::remplirItems()
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ SDL_Rect pos = {0,0,0,0};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_FillRect(m_items,0,0);
|
|
|
|
+ SDL_Surface* steackImg = SDL_LoadBMP("Textures/steak.bmp");
|
|
|
|
+ SDL_Surface* tonneauImg = SDL_LoadBMP("Textures/tonneau.bmp");
|
|
|
|
+ if (steackImg==0 || tonneauImg==0)
|
|
|
|
+ std::cout << "Problème de chargement de texture dans l'objet Niveau, dans items." << std::endl;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ m_effectifItems=0;
|
|
|
|
+ for (int x(0); x<LG_TABL; x++)
|
|
|
|
+ for (int y(0); y<LG_TABL; y++)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ if (m_tabl[y][x]==RIEN)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ m_itemTabl[y][x]=STEACK;
|
|
|
|
+ m_effectifItems++;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pos.x=x*LG_BLOC+(LG_BLOC-steackImg->w)/2;
|
|
|
|
+ pos.y=y*LG_BLOC+(LG_BLOC-steackImg->h)/2;
|
|
|
|
+ SDL_BlitSurface(steackImg, 0, m_items, &pos);
|
|
|
|
+ }
|
|
|
|
+ else if (m_tabl[y][x]==PLAQUE)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ m_itemTabl[y][x]=TONNEAU;
|
|
|
|
+ m_effectifItems++;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pos.x=x*LG_BLOC+(LG_BLOC-tonneauImg->w)/2;
|
|
|
|
+ pos.y=y*LG_BLOC+(LG_BLOC-tonneauImg->h)/2;
|
|
|
|
+ SDL_BlitSurface(tonneauImg, 0, m_items, &pos);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ m_itemTabl[y][x]=VIDE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_SetColorKey(m_items,SDL_SRCCOLORKEY,0);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_FreeSurface(steackImg);
|
|
|
|
+ SDL_FreeSurface(tonneauImg);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+short Niveau::typeBloc(int x, int y, bool coordonee)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ if(coordonee)
|
|
|
|
+ {
|
|
|
|
+ x /= LG_BLOC;
|
|
|
|
+ y /= LG_BLOC;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (x<0 || x>=LG_TABL || y>=LG_TABL || y<0)
|
|
|
|
+ return MUR;
|
|
|
|
+ else
|
|
|
|
+ return m_tabl[y][x];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+short Niveau::takeItem(int x, int y, bool coordonee)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ if(coordonee)
|
|
|
|
+ {
|
|
|
|
+ x /= LG_BLOC;
|
|
|
|
+ y /= LG_BLOC;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (x<0 || x>=LG_TABL || y>=LG_TABL || y<0)
|
|
|
|
+ {
|
|
|
|
+ std::cout << "Attention: demande d'une case de tableau inexistante avec typeItem()" << std::endl;
|
|
|
|
+ return VIDE;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ short butin(m_itemTabl[y][x]);
|
|
|
|
+ m_itemTabl[y][x]=VIDE;
|
|
|
|
+
|
|
|
|
+ if (butin!=VIDE)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ SDL_Surface* gomme = SDL_CreateRGBSurface(SDL_HWSURFACE, LG_BLOC, LG_BLOC, 32, 0, 0, 0, 0);
|
|
|
|
+ SDL_Rect pos = {0,0,0,0};
|
|
|
|
+ pos.x=x*LG_BLOC;
|
|
|
|
+ pos.y=y*LG_BLOC;
|
|
|
|
+ SDL_BlitSurface(gomme,0,m_items,&pos);
|
|
|
|
+ SDL_SetColorKey(m_items,SDL_SRCCOLORKEY,0);
|
|
|
|
+ SDL_FreeSurface(gomme);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ m_effectifItems--;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return butin;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int Niveau::itemsRestants()
|
|
|
|
+{
|
|
|
|
+ return m_effectifItems;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int Niveau::popFantom(axe choisi)
|
|
|
|
+{
|
|
|
|
+ if (choisi == X)
|
|
|
|
+ return m_spawnPhantom[X];
|
|
|
|
+ else if (choisi == Y)
|
|
|
|
+ return m_spawnPhantom[Y];
|
|
|
|
+ else
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int Niveau::popAllie(axe choisi)
|
|
|
|
+{
|
|
|
|
+ if (choisi == X)
|
|
|
|
+ return m_spawnAllie[X];
|
|
|
|
+ else if (choisi == Y)
|
|
|
|
+ return m_spawnAllie[Y];
|
|
|
|
+ else
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int Niveau::getEffectifMobs()
|
|
|
|
+{
|
|
|
|
+ return m_effectifMobs;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+std::string Niveau::getNom()
|
|
|
|
+{
|
|
|
|
+ return m_nom;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Niveau::save(int effectifPhantom)
|
|
|
|
+{
|
|
|
|
+ std::cout << "Demande de sauvegarde du niveau: " << m_nomFichier << std::endl;
|
|
|
|
+ std::ofstream ecriture(m_nomFichier.c_str());
|
|
|
|
+ if (ecriture)
|
|
|
|
+ {
|
|
|
|
+ ecriture << m_nom << std::endl;
|
|
|
|
+ ecriture << m_lvlNumber;
|
|
|
|
+ for (int y(0); y<LG_TABL; y++)
|
|
|
|
+ {
|
|
|
|
+ ecriture << std::endl;
|
|
|
|
+ for (int x(0); x<LG_TABL; x++)
|
|
|
|
+ {
|
|
|
|
+ if (m_tabl[y][x]==SPAWN_PHANTOM)
|
|
|
|
+ ecriture << -effectifPhantom;
|
|
|
|
+ else
|
|
|
|
+ ecriture << m_tabl[y][x];
|
|
|
|
+ ecriture << " ";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ std::cout << "La sauvegarde a échoué." << std::endl;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Niveau::placeBloc(short blocChoisi, int x, int y, Uint32 couleur, bool coordonee)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ if(coordonee)
|
|
|
|
+ {
|
|
|
|
+ x /= LG_BLOC;
|
|
|
|
+ y /= LG_BLOC;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (x>=0 && x<LG_TABL && y>=0 && y<LG_TABL)
|
|
|
|
+ m_tabl[y][x]=blocChoisi;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ SDL_Surface* carreColore = SDL_CreateRGBSurface(SDL_HWSURFACE, LG_BLOC, LG_BLOC, 32, 0, 0, 0, 0);
|
|
|
|
+ SDL_FillRect(carreColore,NULL,couleur);
|
|
|
|
+ SDL_Rect position;
|
|
|
|
+ position.x = x*LG_BLOC;
|
|
|
|
+ position.y = y*LG_BLOC;
|
|
|
|
+ SDL_BlitSurface(carreColore, 0, m_font, &position);
|
|
|
|
+ SDL_FreeSurface(carreColore);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Niveau::reset()
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ for (int y(0); y<LG_TABL; y++)
|
|
|
|
+ for (int x(0); x<LG_TABL; x++)
|
|
|
|
+ m_tabl[y][x]=MUR;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (peindreFont(5)==1)
|
|
|
|
+ std::cout << "Problème dans la fonction peindreFont() dans niveau." << std::endl;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|