123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- #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;
- }
|