Advertisement
Guest User

aa

a guest
Jan 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.97 KB | None | 0 0
  1. #include "Dessin.h"
  2.  
  3. Dessin::Dessin(): largeur(0), hauteur(0){
  4. fond.setNom("None");
  5. }
  6. Dessin::Dessin(int l, int h, const Couleur &c) : largeur(l), hauteur(h), fond(c){
  7.  
  8. }
  9.  
  10. Dessin::~Dessin(){
  11. Iterateur<SmartPointer<Forme> > it(formes);
  12. while(!it.end()){
  13. SmartPointer<Forme> tmp = it;
  14. cout << "it << " << it <<" -> ";
  15. tmp.Delete();
  16. it++;
  17. }
  18. }
  19.  
  20. void Dessin::setFond(const char *nom){
  21. Couleur *tmp = recherchePalette(nom);
  22. if(tmp){
  23. fond = *tmp;
  24. }
  25. else throw BaseException("La couleur souhaitee n'est pas dans la palette.");
  26.  
  27. }
  28. Couleur Dessin::getFond() const{
  29. return fond;
  30. }
  31. void Dessin::ajouteCouleur(const Couleur &c){
  32. Couleur *tmp = recherchePalette(c.getNom());
  33. if(tmp) throw BaseException("Cette couleur est deja dans la palette.");
  34. //cout << "ajoute : " << &(c) << endl;
  35. palette.insere(c);
  36. }
  37. void Dessin::ajouteForme(Forme *pf, const char *nomCouleur){
  38. if(rechercheFormes(pf->getId()) == -1){
  39. Couleur *tmp = recherchePalette(nomCouleur);
  40. if(tmp){
  41. //cout << "it: " << *tmp << endl;
  42. cout << *tmp;
  43. pf->setCouleur(tmp);
  44. //cout << "pf: " << *pf->getCouleur();
  45. formes.insere(pf);
  46. }
  47. else throw BaseException("La couleur souhaitee n'est pas dans la palette.");
  48. }
  49. else throw BaseException("L'identifiant de cette forme existe deja.");
  50. }
  51.  
  52. void Dessin::setLargeur(int l){
  53. largeur = l;
  54. }
  55.  
  56. void Dessin::setHauteur(int h){
  57. hauteur = h;
  58. }
  59.  
  60. int Dessin::getLargeur() const{
  61. return largeur;
  62. }
  63.  
  64. int Dessin::getHauteur() const{
  65. return hauteur;
  66. }
  67. Couleur *Dessin::recherchePalette(const char *c){
  68. Iterateur<Couleur> it(palette);
  69. int i = 0;
  70.  
  71. while(!it.end()){
  72.  
  73. Couleur *tmp = &it;
  74. //cout << "couleur : "<< &it << endl;
  75. if(strcmp(tmp->getNom(), c) == 0) return &it;
  76. it++;
  77. i++;
  78. }
  79.  
  80. return NULL;
  81. }
  82.  
  83. int Dessin::changeCouleurForme(int n, const char *nomCouleur){
  84. Iterateur<SmartPointer<Forme> > it(formes);
  85. for(int i = 0; i < n; i++) it++;
  86. Couleur *c = recherchePalette(nomCouleur);
  87. if(c){
  88. (*it)->getVal()->setCouleur(c);
  89. return 1;
  90. }
  91. return -1;
  92. }
  93.  
  94. int Dessin::rechercheFormes(const char *c){
  95. Iterateur<SmartPointer<Forme> > it(formes);
  96. int i = 0;
  97. while(!it.end()){
  98. SmartPointer<Forme> tmp = (SmartPointer<Forme>) it;
  99. //cout << "tmp->getid(): " << tmp->getId() << " || c : " << c << endl;
  100. if(strcmp(tmp->getId(), c) == 0) return i;
  101. i++;
  102. it++;
  103. }
  104. return -1;
  105. }
  106.  
  107. void Dessin::afficheUneForme(int pos){
  108. Iterateur<SmartPointer<Forme> > it(formes);
  109. for(int i = 0; i < pos; i++) it++;
  110. cout << it;
  111. }
  112.  
  113.  
  114.  
  115. void Dessin::affichePalette(){
  116. Iterateur<Couleur> it(palette);
  117. cout << "Palette: (";
  118. while(!it.end()){
  119. Couleur tmp = (Couleur) it;
  120. cout << tmp.getNom();
  121. it++;
  122. if(!(it.end())) cout << ", ";
  123. }
  124. cout << ")" << endl;
  125.  
  126. }
  127.  
  128. void Dessin::afficheFormes(){
  129. Iterateur<SmartPointer<Forme> > it(formes);
  130. cout << "Formes: (";
  131. while(!it.end()){
  132. SmartPointer<Forme> tmp = (SmartPointer<Forme>) it;
  133. cout << tmp;
  134. it++;
  135. if(!it.end()) cout << ", ";
  136. }
  137. cout << ")" << endl;
  138.  
  139. }
  140.  
  141. void Dessin::Save(const char *nomFichier){
  142. ofstream fichier(nomFichier, ios::out);
  143. fichier << largeur << " " << hauteur << " ";
  144. fond.Save(fichier);
  145. fichier << " " << getNbCouleur() << " ";
  146. Iterateur<Couleur> it(palette);
  147. //Parcrours des couleurs pour les ecrire une par une
  148. while(!it.end()){
  149. //cout << "::::" << it;
  150. Couleur tmp = (Couleur)it;
  151. tmp.Save(fichier);
  152. fichier << " ";
  153. it++;
  154. }
  155. fichier << getNbFormes() << " ";
  156.  
  157. Iterateur<SmartPointer<Forme> > itFormes(formes);
  158.  
  159. //Parcours des formes pour les ecrire une par une
  160. while(!itFormes.end()){
  161. Pixel *p = dynamic_cast<Pixel *>((*itFormes)->getVal());
  162. Ligne *l = dynamic_cast<Ligne *>((*itFormes)->getVal());
  163. Rectangle *r = dynamic_cast<Rectangle *>((*itFormes)->getVal());
  164. if(p){
  165. fichier << "P ";
  166. p->Save(fichier);
  167. }
  168. if(l){
  169. fichier << "L ";
  170. l->Save(fichier);
  171. }
  172. if(r){
  173. fichier << "R ";
  174. r->Save(fichier);
  175. }
  176. itFormes++;
  177. }
  178. fichier.close();
  179. }
  180.  
  181. void Dessin::Load(const char *nomFichier){
  182. ifstream fichier(nomFichier, ios::in);
  183. fichier >> largeur >> hauteur;
  184. fond.Load(fichier);
  185. int nbCouleur;
  186. fichier >> nbCouleur;
  187. for(int i = 0; i < nbCouleur; i++){
  188. Couleur c;
  189. c.Load(fichier);
  190. ajouteCouleur(c);
  191. }
  192. int nbFormes;
  193. fichier >> nbFormes;
  194. //cout << "nbFormes: " << nbFormes;
  195. for(int i = 0; i < nbFormes; i++){
  196. char type;
  197. //cout << "slt";
  198. fichier >> type;
  199. //cout << "type :" <<type;
  200. if(type == 'P'){
  201.  
  202. SmartPointer<Forme> p = new Pixel();
  203. p->Load(fichier, palette);
  204. ajouteForme(p.getVal(), p.getVal()->getCouleur()->getNom());
  205. }
  206. if(type == 'L'){
  207. SmartPointer<Forme> l = new Ligne();
  208. l->Load(fichier, palette);
  209. //cout << "l.getVal()->getCouleur()->getNom(): " << *l.getVal();
  210. ajouteForme(l.getVal(), l.getVal()->getCouleur()->getNom());
  211.  
  212. }
  213. if(type == 'R'){
  214. SmartPointer<Forme> r = new Rectangle();
  215. r->Load(fichier, palette);
  216. ajouteForme(r.getVal(), r.getVal()->getCouleur()->getNom());
  217. }
  218. }
  219. }
  220.  
  221. int Dessin::getNbCouleur(){
  222. Iterateur<Couleur> it(palette);
  223. int i = 0;
  224. while(!it.end()){
  225. it++;
  226. i++;
  227.  
  228. }
  229. return i;
  230. }
  231.  
  232. int Dessin::getNbFormes(){
  233. Iterateur<SmartPointer<Forme> > it(formes);
  234. int i = 0;
  235. while(!it.end()){
  236. it++;
  237. i++;
  238. }
  239. return i;
  240. }
  241.  
  242. void Dessin::importCouleur(const char *nomFichier){
  243. ifstream fichier(nomFichier, ios::in | ios::binary);
  244. char *p;
  245. string ligne;
  246. bool entete = true;
  247. Couleur c;
  248. while(getline(fichier, ligne)){
  249. int i = 0;
  250. ligne.erase(ligne.end()-1, ligne.end());
  251. p = strtok((char *)ligne.c_str(), ";");
  252. while(p){
  253. if(!entete){
  254. switch(i){
  255. case 0: c.setRouge(atoi(p)); break;
  256. case 1: c.setVert(atoi(p)); break;
  257. case 2: c.setBleu(atoi(p)); break;
  258. case 3: c.setNom(p); break;
  259. default: ;
  260. }
  261. i++;
  262. }
  263. p = strtok(NULL, ";");
  264. }
  265. if(!entete) ajouteCouleur(c); // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la couleur et ne pas faire d'exception
  266. entete = false;
  267. }
  268. }
  269.  
  270.  
  271. void Dessin::importLigne(const char *nomFichier){
  272. ifstream fichier(nomFichier, ios::in | ios::binary);
  273. char *p, *tmpC;
  274. string ligne;
  275. bool entete = true;
  276.  
  277. Point pos, extremite;
  278. int id = 0;
  279. while(getline(fichier, ligne)){
  280. Ligne *l = new Ligne();
  281. int i = 0;
  282. //ligne.erase(ligne.end()-1, ligne.end());
  283. p = strtok((char *)ligne.c_str(), ";");
  284. //cout << endl << "Raw ligne: " << ligne << endl;
  285. while(p){
  286. //cout << p << endl; //cout << " aaa" ;
  287. if(!entete){
  288. //cout << i << ": pas l'entete -> ";
  289. switch(i){
  290. case 0: pos.setX(atoi(p)); break;
  291. case 1: pos.setY(atoi(p)); l->setPosition(pos); break;
  292. case 2: extremite.setX(atoi(p)); break;
  293. case 3: extremite.setY(atoi(p)); l->setExtremite(extremite); break;
  294. case 4: l->setProfondeur(atoi(p));
  295. case 5: tmpC = p; break; //-> p utilisé dans ajouteForme
  296. default: ;
  297. }
  298. i++;
  299. }
  300. p = strtok(NULL, ";");
  301. }
  302. if(!entete){ // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la forme et ne pas faire d'exception
  303. char buff[5];
  304. sprintf(buff, "L%d", id);
  305. id++;
  306. cout << buff << endl;
  307. l->setId(buff);
  308. //cout << endl << "Ligne: "<< *l << endl;
  309. ajouteForme(l, tmpC);
  310.  
  311. }
  312. entete = false;
  313.  
  314. }
  315. }
  316.  
  317. void Dessin::importRectangle(const char *nomFichier){
  318. ifstream fichier(nomFichier, ios::in | ios::binary);
  319. char *p, *tmpC;
  320. string ligne;
  321. bool entete = true;
  322.  
  323. Point pos;
  324. int id = 0;
  325. while(getline(fichier, ligne)){
  326. Rectangle *r = new Rectangle();
  327. int i = 0;
  328. //ligne.erase(ligne.end()-1, ligne.end());
  329. p = strtok((char *)ligne.c_str(), ";");
  330. //cout << endl << "Raw ligne: " << ligne << endl;
  331. while(p){
  332. //cout << p << endl; //cout << " aaa" ;
  333. if(!entete){
  334. //cout << i << ": pas l'entete -> ";
  335. switch(i){
  336. case 0: pos.setX(atoi(p)); break;
  337. case 1: pos.setY(atoi(p)); r->setPosition(pos); break;
  338. case 2: r->setDimX(atoi(p)); break;
  339. case 3: r->setDimY(atoi(p)); break;
  340. case 4: r->setRempli((bool) atoi(p)); break;
  341. case 5: r->setProfondeur(atoi(p)); break;
  342. case 6: tmpC = p; break; //-> p utilisé dans ajouteForme
  343. default: ;
  344. }
  345. i++;
  346. }
  347. p = strtok(NULL, ";");
  348. }
  349. if(!entete){ // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la forme et ne pas faire d'exception
  350. char buff[5];
  351. sprintf(buff, "R%d", id);
  352. id++;
  353. r->setId(buff);
  354. //cout << endl << "Rectangle: "<< *r << "Couleur = " << tmpC << endl;
  355. ajouteForme(r, tmpC);
  356.  
  357. }
  358. entete = false;
  359.  
  360. }
  361. }
  362.  
  363. void Dessin::importPixel(const char *nomFichier){
  364. ifstream fichier(nomFichier, ios::in | ios::binary);
  365. char *p, *tmpC;
  366. string ligne;
  367. bool entete = true;
  368.  
  369. Point pos;
  370. int id = 0;
  371. while(getline(fichier, ligne)){
  372. Pixel *pix = new Pixel();
  373. int i = 0;
  374. //ligne.erase(ligne.end()-1, ligne.end());
  375. p = strtok((char *)ligne.c_str(), ";");
  376. //cout << endl << "Raw ligne: " << ligne << endl;
  377. while(p){
  378. //cout << p << endl; //cout << " aaa" ;
  379. if(!entete){
  380. //cout << i << ": pas l'entete -> ";
  381. switch(i){
  382. case 0: pos.setX(atoi(p)); break;
  383. case 1: pos.setY(atoi(p)); pix->setPosition(pos); break;
  384. case 2: pix->setProfondeur(atoi(p)); break;
  385. case 3: tmpC = p; break; //-> p utilisé dans ajouteForme
  386. default: ;
  387. }
  388. i++;
  389. }
  390. p = strtok(NULL, ";");
  391. }
  392. if(!entete){ // Ajouter (&& recherchePalette(c.getNom()) == -1;) Si ignorer la forme et ne pas faire d'exception
  393. char buff[5];
  394. sprintf(buff, "P%d", id);
  395. id++;
  396. pix->setId(buff);
  397. cout << buff << endl;
  398. //cout << endl << "pixel: "<< *pix << "name = " << tmpC << endl;
  399. ajouteForme(pix, tmpC);
  400.  
  401. }
  402. entete = false;
  403.  
  404. }
  405. }
  406.  
  407. void Dessin::dessine(WindowSDL window){
  408. window.setBackground(fond.getRouge(), fond.getVert(), fond.getBleu());
  409. Iterateur<SmartPointer<Forme> > it(formes);
  410. while(!it.end()){
  411. SmartPointer<Forme> tmp = (SmartPointer<Forme>) it;
  412. //cout << *tmp;
  413. (*tmp).dessine(window);
  414. it++;
  415. }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement