Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. // ############################################################
  2. // YOUR IMPLEMENTATION
  3. // ############################################################
  4.  
  5. namespace tui {
  6.  
  7.  
  8.  
  9. class color {
  10. private:
  11. byte code = 0;
  12. color(byte code) //constructor
  13. {
  14. this->code = code;
  15. }
  16. public:
  17. byte getCode() const
  18. {
  19. return code;
  20. }
  21. static const color black;
  22. static const color red;
  23. static const color green;
  24. static const color yellow;
  25. static const color blue;
  26. static const color magenta;
  27. static const color white;
  28. static const color system_default;
  29.  
  30. };
  31. /* predefine some colors */
  32. const color color::black(0);
  33. const color color::red(1);
  34. const color color::green(2);
  35. const color color::yellow(3);
  36. const color color::blue(4);
  37. const color color::magenta(5);
  38. const color color::white(7);
  39. const color color::system_default(9);
  40.  
  41.  
  42. class pen {
  43. private:
  44. color _foreground, _background;
  45. char _c;
  46. bool _bright;
  47. public:
  48. pen(color foreground_code, color background_code, char ch, bool bright) //constructor
  49. :_foreground(foreground_code),_background(background_code),_c(ch),_bright(bright)
  50. {}
  51.  
  52. point pen_to_point() const //convert a pen to a point
  53. {
  54. point pt;
  55. pt._ch = _c;
  56. pt._color_code = _foreground.getCode();
  57. pt._bg_color_code = _background.getCode();
  58. pt._bright = _bright;
  59. return pt;
  60. }
  61. };
  62.  
  63. class shape {
  64.  
  65. public:
  66. virtual ~shape() {}; // the virtual destructor of base class
  67. virtual void draw(screen &scr, const pen &p) const = 0;
  68. };
  69.  
  70.  
  71.  
  72. class rectangle : public shape {
  73. private:
  74. int height;
  75. int width;
  76. int top_left_x;
  77. int top_left_y;
  78. int bottom_x ;
  79. int bottom_y ;
  80.  
  81. public:
  82. rectangle(int x, int y, int height, int width) // the constructor of rectangle
  83. :top_left_x(x),top_left_y(y),height(height),width(width),bottom_x(top_left_x + height),bottom_y(top_left_y + width)
  84. {}
  85.  
  86. ~rectangle() {}; //the destructor of the derived class ( 1) delete the rectangle(Derived class) 2) delete the shape (base class))
  87.  
  88. /* Public Helpers for private members*/
  89. int getHeight()
  90. {
  91. return height;
  92. }
  93. int getWidth()
  94. {
  95. return width;
  96. }
  97. int getTop_left_corner_x()
  98. {
  99. return top_left_x;
  100. }
  101. int getTop_left_corner_y()
  102. {
  103. return top_left_y;
  104. }
  105.  
  106. void draw(screen &scr, const pen &p) const
  107. {
  108. scr.set_rect(top_left_x, top_left_y, bottom_x, bottom_y, p.pen_to_point());
  109. }
  110.  
  111. };
  112.  
  113. class circle : public shape {
  114. private:
  115. int center_x;
  116. int center_y;
  117. int radius;
  118.  
  119. public:
  120. circle(int x, int y, int rad) //constructor of the circle
  121. :center_x(x), center_y(y), radius(rad)
  122. {}
  123. ~circle() {}; ////the destructor of the derived class ( 1) delete the circle(Derived class) 2) delete the shape (base class))
  124.  
  125.  
  126. /* Public Helpers for private members*/
  127. int getCenter_x()
  128. {
  129. return center_x;
  130. }
  131. int getCenter_y()
  132. {
  133. return center_y;
  134. }
  135. int getRadius()
  136. {
  137. return radius;
  138. }
  139.  
  140.  
  141. void draw(screen &scr, const pen &p) const
  142. {
  143. scr.set_circle(center_x, center_y, radius, p.pen_to_point());
  144. }
  145. };
  146.  
  147. class canvas {
  148. private:
  149. screen& _scr; //an screen obj
  150. std::vector<std::pair < shape*, pen > > list;
  151. public:
  152. canvas(screen &scr) //constructor
  153. :_scr(scr)
  154. {}
  155.  
  156.  
  157.  
  158. /* The 3 methods of the class*/
  159.  
  160. void add(shape* obj, pen obj1)
  161. {
  162. list.push_back(std::make_pair(obj, obj1));
  163. }
  164. void clear()
  165. {
  166.  
  167. list.clear();
  168. _scr.clear();
  169. }
  170. void show()
  171. {
  172. _scr.clear();
  173.  
  174. int i;
  175.  
  176. for (i = 0; i < list.size(); i++)
  177. list[i].first->draw(_scr, list[i].second);
  178.  
  179. _scr.render();
  180.  
  181. }
  182. };
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement