Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Dessin.h"
- Dessin::Dessin(): largeur(0), hauteur(0){
- fond.setNom("None");
- }
- Dessin::Dessin(int l, int h, const Couleur &c) : largeur(l), hauteur(h), fond(c){
- }
- Dessin::~Dessin(){
- Iterateur<SmartPointer<Forme> > it(formes);
- while(!it.end()){
- SmartPointer<Forme> tmp = it;
- cout << "it << " << it <<" -> ";
- tmp.Delete();
- it++;
- }
- }
- void Dessin::setFond(const char *nom){
- Couleur *tmp = recherchePalette(nom);
- if(tmp){
- fond = *tmp;
- }
- else throw BaseException("La couleur souhaitee n'est pas dans la palette.");
- }
- Couleur Dessin::getFond() const{
- return fond;
- }
- void Dessin::ajouteCouleur(const Couleur &c){
- Couleur *tmp = recherchePalette(c.getNom());
- if(tmp) throw BaseException("Cette couleur est deja dans la palette.");
- //cout << "ajoute : " << &(c) << endl;
- palette.insere(c);
- }
- void Dessin::ajouteForme(Forme *pf, const char *nomCouleur){
- if(rechercheFormes(pf->getId()) == -1){
- Couleur *tmp = recherchePalette(nomCouleur);
- if(tmp){
- //cout << "it: " << *tmp << endl;
- cout << *tmp;
- pf->setCouleur(tmp);
- //cout << "pf: " << *pf->getCouleur();
- formes.insere(pf);
- }
- else throw BaseException("La couleur souhaitee n'est pas dans la palette.");
- }
- else throw BaseException("L'identifiant de cette forme existe deja.");
- }
- void Dessin::setLargeur(int l){
- largeur = l;
- }
- void Dessin::setHauteur(int h){
- hauteur = h;
- }
- int Dessin::getLargeur() const{
- return largeur;
- }
- int Dessin::getHauteur() const{
- return hauteur;
- }
- Couleur *Dessin::recherchePalette(const char *c){
- Iterateur<Couleur> it(palette);
- int i = 0;
- while(!it.end()){
- Couleur *tmp = ⁢
- //cout << "couleur : "<< &it << endl;
- if(strcmp(tmp->getNom(), c) == 0) return ⁢
- it++;
- i++;
- }
- return NULL;
- }
- int Dessin::changeCouleurForme(int n, const char *nomCouleur){
- Iterateur<SmartPointer<Forme> > it(formes);
- for(int i = 0; i < n; i++) it++;
- Couleur *c = recherchePalette(nomCouleur);
- if(c){
- (*it)->getVal()->setCouleur(c);
- return 1;
- }
- return -1;
- }
- int Dessin::rechercheFormes(const char *c){
- Iterateur<SmartPointer<Forme> > it(formes);
- int i = 0;
- while(!it.end()){
- SmartPointer<Forme> tmp = (SmartPointer<Forme>) it;
- //cout << "tmp->getid(): " << tmp->getId() << " || c : " << c << endl;
- if(strcmp(tmp->getId(), c) == 0) return i;
- i++;
- it++;
- }
- return -1;
- }
- void Dessin::afficheUneForme(int pos){
- Iterateur<SmartPointer<Forme> > it(formes);
- for(int i = 0; i < pos; i++) it++;
- cout << it;
- }
- void Dessin::affichePalette(){
- Iterateur<Couleur> it(palette);
- cout << "Palette: (";
- while(!it.end()){
- Couleur tmp = (Couleur) it;
- cout << tmp.getNom();
- it++;
- if(!(it.end())) cout << ", ";
- }
- cout << ")" << endl;
- }
- void Dessin::afficheFormes(){
- Iterateur<SmartPointer<Forme> > it(formes);
- cout << "Formes: (";
- while(!it.end()){
- SmartPointer<Forme> tmp = (SmartPointer<Forme>) it;
- cout << tmp;
- it++;
- if(!it.end()) cout << ", ";
- }
- cout << ")" << endl;
- }
- void Dessin::Save(const char *nomFichier){
- ofstream fichier(nomFichier, ios::out);
- fichier << largeur << " " << hauteur << " ";
- fond.Save(fichier);
- fichier << " " << getNbCouleur() << " ";
- Iterateur<Couleur> it(palette);
- //Parcrours des couleurs pour les ecrire une par une
- while(!it.end()){
- //cout << "::::" << it;
- Couleur tmp = (Couleur)it;
- tmp.Save(fichier);
- fichier << " ";
- it++;
- }
- fichier << getNbFormes() << " ";
- Iterateur<SmartPointer<Forme> > itFormes(formes);
- //Parcours des formes pour les ecrire une par une
- while(!itFormes.end()){
- Pixel *p = dynamic_cast<Pixel *>((*itFormes)->getVal());
- Ligne *l = dynamic_cast<Ligne *>((*itFormes)->getVal());
- Rectangle *r = dynamic_cast<Rectangle *>((*itFormes)->getVal());
- if(p){
- fichier << "P ";
- p->Save(fichier);
- }
- if(l){
- fichier << "L ";
- l->Save(fichier);
- }
- if(r){
- fichier << "R ";
- r->Save(fichier);
- }
- itFormes++;
- }
- fichier.close();
- }
- void Dessin::Load(const char *nomFichier){
- ifstream fichier(nomFichier, ios::in);
- fichier >> largeur >> hauteur;
- fond.Load(fichier);
- int nbCouleur;
- fichier >> nbCouleur;
- for(int i = 0; i < nbCouleur; i++){
- Couleur c;
- c.Load(fichier);
- ajouteCouleur(c);
- }
- int nbFormes;
- fichier >> nbFormes;
- //cout << "nbFormes: " << nbFormes;
- for(int i = 0; i < nbFormes; i++){
- char type;
- //cout << "slt";
- fichier >> type;
- //cout << "type :" <<type;
- if(type == 'P'){
- SmartPointer<Forme> p = new Pixel();
- p->Load(fichier, palette);
- ajouteForme(p.getVal(), p.getVal()->getCouleur()->getNom());
- }
- if(type == 'L'){
- SmartPointer<Forme> l = new Ligne();
- l->Load(fichier, palette);
- //cout << "l.getVal()->getCouleur()->getNom(): " << *l.getVal();
- ajouteForme(l.getVal(), l.getVal()->getCouleur()->getNom());
- }
- if(type == 'R'){
- SmartPointer<Forme> r = new Rectangle();
- r->Load(fichier, palette);
- ajouteForme(r.getVal(), r.getVal()->getCouleur()->getNom());
- }
- }
- }
- int Dessin::getNbCouleur(){
- Iterateur<Couleur> it(palette);
- int i = 0;
- while(!it.end()){
- it++;
- i++;
- }
- return i;
- }
- int Dessin::getNbFormes(){
- Iterateur<SmartPointer<Forme> > it(formes);
- int i = 0;
- while(!it.end()){
- it++;
- i++;
- }
- return i;
- }
- void Dessin::importCouleur(const char *nomFichier){
- ifstream fichier(nomFichier, ios::in | ios::binary);
- char *p;
- string ligne;
- bool entete = true;
- Couleur c;
- while(getline(fichier, ligne)){
- int i = 0;
- ligne.erase(ligne.end()-1, ligne.end());
- p = strtok((char *)ligne.c_str(), ";");
- while(p){
- if(!entete){
- switch(i){
- case 0: c.setRouge(atoi(p)); break;
- case 1: c.setVert(atoi(p)); break;
- case 2: c.setBleu(atoi(p)); break;
- case 3: c.setNom(p); break;
- default: ;
- }
- i++;
- }
- p = strtok(NULL, ";");
- }
- if(!entete) ajouteCouleur(c); // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la couleur et ne pas faire d'exception
- entete = false;
- }
- }
- void Dessin::importLigne(const char *nomFichier){
- ifstream fichier(nomFichier, ios::in | ios::binary);
- char *p, *tmpC;
- string ligne;
- bool entete = true;
- Point pos, extremite;
- int id = 0;
- while(getline(fichier, ligne)){
- Ligne *l = new Ligne();
- int i = 0;
- //ligne.erase(ligne.end()-1, ligne.end());
- p = strtok((char *)ligne.c_str(), ";");
- //cout << endl << "Raw ligne: " << ligne << endl;
- while(p){
- //cout << p << endl; //cout << " aaa" ;
- if(!entete){
- //cout << i << ": pas l'entete -> ";
- switch(i){
- case 0: pos.setX(atoi(p)); break;
- case 1: pos.setY(atoi(p)); l->setPosition(pos); break;
- case 2: extremite.setX(atoi(p)); break;
- case 3: extremite.setY(atoi(p)); l->setExtremite(extremite); break;
- case 4: l->setProfondeur(atoi(p));
- case 5: tmpC = p; break; //-> p utilisé dans ajouteForme
- default: ;
- }
- i++;
- }
- p = strtok(NULL, ";");
- }
- if(!entete){ // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la forme et ne pas faire d'exception
- char buff[5];
- sprintf(buff, "L%d", id);
- id++;
- cout << buff << endl;
- l->setId(buff);
- //cout << endl << "Ligne: "<< *l << endl;
- ajouteForme(l, tmpC);
- }
- entete = false;
- }
- }
- void Dessin::importRectangle(const char *nomFichier){
- ifstream fichier(nomFichier, ios::in | ios::binary);
- char *p, *tmpC;
- string ligne;
- bool entete = true;
- Point pos;
- int id = 0;
- while(getline(fichier, ligne)){
- Rectangle *r = new Rectangle();
- int i = 0;
- //ligne.erase(ligne.end()-1, ligne.end());
- p = strtok((char *)ligne.c_str(), ";");
- //cout << endl << "Raw ligne: " << ligne << endl;
- while(p){
- //cout << p << endl; //cout << " aaa" ;
- if(!entete){
- //cout << i << ": pas l'entete -> ";
- switch(i){
- case 0: pos.setX(atoi(p)); break;
- case 1: pos.setY(atoi(p)); r->setPosition(pos); break;
- case 2: r->setDimX(atoi(p)); break;
- case 3: r->setDimY(atoi(p)); break;
- case 4: r->setRempli((bool) atoi(p)); break;
- case 5: r->setProfondeur(atoi(p)); break;
- case 6: tmpC = p; break; //-> p utilisé dans ajouteForme
- default: ;
- }
- i++;
- }
- p = strtok(NULL, ";");
- }
- if(!entete){ // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la forme et ne pas faire d'exception
- char buff[5];
- sprintf(buff, "R%d", id);
- id++;
- r->setId(buff);
- //cout << endl << "Rectangle: "<< *r << "Couleur = " << tmpC << endl;
- ajouteForme(r, tmpC);
- }
- entete = false;
- }
- }
- void Dessin::importPixel(const char *nomFichier){
- ifstream fichier(nomFichier, ios::in | ios::binary);
- char *p, *tmpC;
- string ligne;
- bool entete = true;
- Point pos;
- int id = 0;
- while(getline(fichier, ligne)){
- Pixel *pix = new Pixel();
- int i = 0;
- //ligne.erase(ligne.end()-1, ligne.end());
- p = strtok((char *)ligne.c_str(), ";");
- //cout << endl << "Raw ligne: " << ligne << endl;
- while(p){
- //cout << p << endl; //cout << " aaa" ;
- if(!entete){
- //cout << i << ": pas l'entete -> ";
- switch(i){
- case 0: pos.setX(atoi(p)); break;
- case 1: pos.setY(atoi(p)); pix->setPosition(pos); break;
- case 2: pix->setProfondeur(atoi(p)); break;
- case 3: tmpC = p; break; //-> p utilisé dans ajouteForme
- default: ;
- }
- i++;
- }
- p = strtok(NULL, ";");
- }
- if(!entete){ // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la forme et ne pas faire d'exception
- char buff[5];
- sprintf(buff, "P%d", id);
- id++;
- pix->setId(buff);
- cout << buff << endl;
- //cout << endl << "pixel: "<< *pix << "name = " << tmpC << endl;
- ajouteForme(pix, tmpC);
- }
- entete = false;
- }
- }
- void Dessin::dessine(WindowSDL window){
- window.setBackground(fond.getRouge(), fond.getVert(), fond.getBleu());
- Iterateur<SmartPointer<Forme> > it(formes);
- while(!it.end()){
- SmartPointer<Forme> tmp = (SmartPointer<Forme>) it;
- //cout << *tmp;
- (*tmp).dessine(window);
- it++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement