pic.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* This file is part of Invaders.
  2. *
  3. * Copyright (C) 2020 LCM.
  4. * You may use, distribute and modify Invaders under the terms of the
  5. * GPLv3 license, available at <https://www.gnu.org/licenses/\>.
  6. */
  7. #include "pic.hpp"
  8. //char** picture;
  9. pic::pic(int w, int h) : body(w,h){
  10. picture = new char* [height];
  11. for(int i=0; i<height; ++i)
  12. picture[i] = new char[w];
  13. clear();
  14. }
  15. pic::~pic()
  16. {
  17. delete [] picture;
  18. }
  19. void pic::loadpicture(const char* nome)
  20. {
  21. ifstream input(nome);
  22. char c;
  23. if(!nome){
  24. input.close();
  25. }
  26. else{
  27. for(int i=0; i<height; i++) //rows
  28. for(int j=0; j<width; j++){ //columns
  29. c = input.get(); // get character from file
  30. if(c!='\n') picture[i][j]=c;
  31. if(c=='\n') j--;
  32. }
  33. input.close();
  34. }
  35. }
  36. void pic::clear(){
  37. for(int i=0; i<height; i++)
  38. for(int j=0; j<width; j++)
  39. picture[i][j]=' ';
  40. }
  41. void pic::set_values(int W, int H){
  42. width=W;
  43. height=H;
  44. }
  45. void pic::print(){
  46. for(int i=0; i<height; i++){
  47. for(int j=0; j<width; j++)
  48. cout << picture[i][j];
  49. cout << endl;
  50. }
  51. }