|
@@ -0,0 +1,192 @@
|
|
|
|
+#include "Level.h"
|
|
|
|
+
|
|
|
|
+Level::Level(std::string nom, int numero, int tableau[NB_BLOC_VERTI][NB_BLOC_HORIZON])
|
|
|
|
+:m_nom(nom),m_numero(numero),m_reussi(false)
|
|
|
|
+{
|
|
|
|
+ // [1] Remplissage du tableau
|
|
|
|
+ std::cout <<"Le niveau '"<<nom<<"' numero "<<numero<<" fait "<<
|
|
|
|
+ NB_BLOC_HORIZON<<" par "<<NB_BLOC_VERTI<<" blocs."<<std::endl<<std::endl;
|
|
|
|
+ for (int i(0); i<NB_BLOC_HORIZON; i++)
|
|
|
|
+ {
|
|
|
|
+ for (int j(0); j<NB_BLOC_VERTI; j++)
|
|
|
|
+ {
|
|
|
|
+ m_tableau[j][i]=tableau[j][i];
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // [2] Création des surfaces
|
|
|
|
+ m_bloc[0] = SDL_CreateRGBSurface(SDL_HWSURFACE, LG_BLOC, LG_BLOC, 16, 0, 0, 0, 0);
|
|
|
|
+ m_bloc[1] = SDL_CreateRGBSurface(SDL_HWSURFACE, LG_BLOC, LG_BLOC, 16, 0, 0, 0, 0);
|
|
|
|
+ m_bloc[2] = SDL_CreateRGBSurface(SDL_HWSURFACE, LG_BLOC, LG_BLOC, 16, 0, 0, 0, 0);
|
|
|
|
+
|
|
|
|
+ // [3] Création du titre
|
|
|
|
+ std::string nomNiveau("Level N ");
|
|
|
|
+ nomNiveau += convertirEntierEnCaracteres(numero);
|
|
|
|
+ nomNiveau = nomNiveau + " : " + nom;
|
|
|
|
+ std::cout << nomNiveau <<" chargé !" << std::endl;
|
|
|
|
+
|
|
|
|
+ // [4] Remplissage des surfaces
|
|
|
|
+ SDL_FillRect(m_bloc[0],NULL,SDL_MapRGB(m_bloc[0]->format,255,255,255));
|
|
|
|
+ SDL_FillRect(m_bloc[1],NULL,SDL_MapRGB(m_bloc[1]->format,255,0,0));
|
|
|
|
+ SDL_FillRect(m_bloc[2],NULL,SDL_MapRGB(m_bloc[2]->format,0,128,0));
|
|
|
|
+ m_titre = transform(nomNiveau);
|
|
|
|
+
|
|
|
|
+ // [5] Création de l'arrivéeet du spawn
|
|
|
|
+ m_posBut.x=m_posBut.y=-1000;
|
|
|
|
+ m_but = createCircle(LG_BLOC/2,SDL_MapRGB(m_bloc[0]->format,0,255,0),5);//m_bloc[0] juste pour récuperer le format 16 bits
|
|
|
|
+ m_posSpawn.x=m_posSpawn.y=-1000;
|
|
|
|
+ m_spawn = createCircle(LG_BLOC/2,SDL_MapRGB(m_bloc[0]->format,0,255,255),7);
|
|
|
|
+
|
|
|
|
+}///Constructeur
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Level::~Level()
|
|
|
|
+{
|
|
|
|
+ for (int i(0); i<NB_BLOCS; i++)
|
|
|
|
+ {
|
|
|
|
+ SDL_FreeSurface(m_bloc[i]);
|
|
|
|
+ }
|
|
|
|
+ SDL_FreeSurface(m_titre);
|
|
|
|
+ SDL_FreeSurface(m_but);
|
|
|
|
+ SDL_FreeSurface(m_spawn);
|
|
|
|
+}///Destructeur
|
|
|
|
+
|
|
|
|
+void Level::setBloc(int x, int y, int typeBloc)
|
|
|
|
+{
|
|
|
|
+ if ((typeBloc == RIEN || typeBloc == BLOC || typeBloc == BUMPER || typeBloc == PLANCHE) &&
|
|
|
|
+ 0<=x && 0<=y && x<NB_BLOC_HORIZON && y<NB_BLOC_VERTI)
|
|
|
|
+ m_tableau[y][x]=typeBloc;
|
|
|
|
+}///setBloc
|
|
|
|
+
|
|
|
|
+void Level::save(int xSpawn, int ySpawn, int xBut, int yBut)
|
|
|
|
+{
|
|
|
|
+ //à implémenter
|
|
|
|
+}///save
|
|
|
|
+
|
|
|
|
+int Level::getBloc(int x, int y)
|
|
|
|
+{
|
|
|
|
+ if (x<0 || x>=NB_BLOC_HORIZON || y>=NB_BLOC_VERTI)
|
|
|
|
+ return BLOC;
|
|
|
|
+ else if (y<0)
|
|
|
|
+ return RIEN;
|
|
|
|
+ else
|
|
|
|
+ return m_tableau[y][x];
|
|
|
|
+}///getBloc
|
|
|
|
+
|
|
|
|
+void Level::afficher(SDL_Surface *screen)
|
|
|
|
+{
|
|
|
|
+ // [1] Création de la position
|
|
|
|
+ SDL_Rect position;
|
|
|
|
+
|
|
|
|
+ // [2] Collage du niveau
|
|
|
|
+ for (int i(0); i<NB_BLOC_HORIZON; i++)
|
|
|
|
+ {
|
|
|
|
+ for (int j(0); j<NB_BLOC_VERTI; j++)
|
|
|
|
+ {
|
|
|
|
+ position.x = i*LG_BLOC;
|
|
|
|
+ position.y = j*LG_BLOC;
|
|
|
|
+ if (m_tableau[j][i]==RIEN)
|
|
|
|
+ /*Rien faire*/;
|
|
|
|
+ else if (m_tableau[j][i]==BLOC)
|
|
|
|
+ SDL_BlitSurface(m_bloc[0], 0, screen, &position);
|
|
|
|
+ else if (m_tableau[j][i]==BUMPER)
|
|
|
|
+ SDL_BlitSurface(m_bloc[1], 0, screen, &position);
|
|
|
|
+ else if (m_tableau[j][i]==PLANCHE)
|
|
|
|
+ SDL_BlitSurface(m_bloc[2], 0, screen, &position);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ SDL_BlitSurface(m_but, 0, screen, &m_posBut);
|
|
|
|
+ SDL_BlitSurface(m_spawn,0,screen, &m_posSpawn);
|
|
|
|
+
|
|
|
|
+ // [3] Collage du titre
|
|
|
|
+ position.x=20;
|
|
|
|
+ position.y=20;
|
|
|
|
+ SDL_BlitSurface(m_titre, 0, screen, &position);
|
|
|
|
+
|
|
|
|
+}///afficher
|
|
|
|
+
|
|
|
|
+std::string const Level::getNom()
|
|
|
|
+{
|
|
|
|
+ return m_nom;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Level::lireParametres(int &xSpawn,int &ySpawn,float &resistance,float &gravite,int &xBut,int &yBut)
|
|
|
|
+{
|
|
|
|
+ // [1] ouverture du flux
|
|
|
|
+ std::string fichier("Niveaux/level "+convertirEntierEnCaracteres(m_numero)+".txt");
|
|
|
|
+ std::cout << fichier << std::endl;//Introduction
|
|
|
|
+ std::ifstream fluxIn(fichier.c_str());
|
|
|
|
+ if (fluxIn)
|
|
|
|
+ {
|
|
|
|
+ // [2] On saute ce qui nous plaît pas
|
|
|
|
+ for (int i(0);i<NB_BLOC_VERTI+2;i++)
|
|
|
|
+ getline(fluxIn,fichier);//Là, le string fichier sert de poubelle
|
|
|
|
+
|
|
|
|
+ // [3] Renseignement des paramètres
|
|
|
|
+ fluxIn >> xSpawn;
|
|
|
|
+ fluxIn >> ySpawn;
|
|
|
|
+ fluxIn >> resistance;
|
|
|
|
+ fluxIn >> gravite;
|
|
|
|
+ fluxIn >> xBut;
|
|
|
|
+ fluxIn >> yBut;
|
|
|
|
+
|
|
|
|
+ m_posSpawn.x=xSpawn*LG_BLOC;
|
|
|
|
+ m_posSpawn.y=ySpawn*LG_BLOC;
|
|
|
|
+ m_posBut.x=xBut*LG_BLOC;
|
|
|
|
+ m_posBut.y=yBut*LG_BLOC;
|
|
|
|
+
|
|
|
|
+ // [4] Affichage infos
|
|
|
|
+ std::cout << "Spawn("<<xSpawn<<";"<<ySpawn<<")."<<std::endl
|
|
|
|
+ <<"Resistance: "<<resistance<<std::endl
|
|
|
|
+ <<"Gravité: "<<gravite<<std::endl
|
|
|
|
+ <<"But("<<xBut<<";"<<yBut<<")."<<std::endl<<std::endl;
|
|
|
|
+
|
|
|
|
+ }//Fin de l'ouverture du fichier possible
|
|
|
|
+
|
|
|
|
+ // [5] On previent s'il y a pépin
|
|
|
|
+ else
|
|
|
|
+ std::cout << "Echec de l'ouverture du fichier lors de la lecture des paramètres."<<std::endl;
|
|
|
|
+}///lireParametres
|
|
|
|
+
|
|
|
|
+/***===================={ Fonctions indépendantes }====================***/
|
|
|
|
+Level* haveLevel(std::string nomFichier)
|
|
|
|
+{
|
|
|
|
+ // [1] Mise au poin du nom du fichier
|
|
|
|
+ nomFichier="Niveaux/"+nomFichier;
|
|
|
|
+
|
|
|
|
+ // [2] Ouverture du flux
|
|
|
|
+ std::ifstream fluxIn(nomFichier.c_str());
|
|
|
|
+ if (fluxIn)
|
|
|
|
+ std::cout << "Ouverture du fichier "<<nomFichier<<" réussie."<<std::endl;
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ std::cout << "Echec de l'ouverture du fichier "<<nomFichier<<"."<<std::endl;
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // [3] Création des composants du niveau
|
|
|
|
+ std::string nomNiveau;
|
|
|
|
+ int numeroNiveau;
|
|
|
|
+ int tableauNiveau[NB_BLOC_VERTI][NB_BLOC_HORIZON];
|
|
|
|
+
|
|
|
|
+ // [4] Remplissage du niveau
|
|
|
|
+ getline(fluxIn,nomNiveau);
|
|
|
|
+ fluxIn >> numeroNiveau;
|
|
|
|
+ for (int y(0); y<NB_BLOC_VERTI; y++)
|
|
|
|
+ {
|
|
|
|
+ for (int x(0); x<NB_BLOC_HORIZON; x++)
|
|
|
|
+ {
|
|
|
|
+ fluxIn >> tableauNiveau[y][x];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // [5] Création de l'objet
|
|
|
|
+ Level* niveau(0);
|
|
|
|
+ niveau = new Level(nomNiveau,numeroNiveau,tableauNiveau);
|
|
|
|
+
|
|
|
|
+ // [6] Renvoi du niveau
|
|
|
|
+ return niveau;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|