home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / OOPWLD.ZIP / SHAPES / SHED.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-11  |  3.4 KB  |  95 lines

  1. // SHED.CPP: graphic shape editor
  2. // #define EXTEND  // un-comment to add a new feature to the system
  3. #include "polygon.hpp"
  4. #include "ellipse.hpp"
  5. #if defined(EXTEND)
  6. #include "circbox.hpp"  // An "extension" to the system
  7. #endif
  8. #include "doskey.hpp"
  9. #include "shlist.hpp"
  10. // Note the above contain standard C library includes.
  11.  
  12. // BGI initialization:
  13. char * bgipath = "d:\\t\\g\\"; // path with BGI files
  14. bgi_graphics BGI(bgipath);  // can also link directly; see manual
  15.  
  16. shape * makeshape() {
  17.   const x = bgi_graphics::xmax()/2, y = bgi_graphics::ymax()/2;
  18.   while(1) {
  19.     bgi_graphics::bottomprompt(
  20.       "c: circle, e: ellipse, r: rectangle, s: square, t: triangle"
  21. #if defined(EXTEND)
  22.       ", b: circlebox"  // must also extend the menu
  23. #endif
  24.     );
  25.     switch(get_doskey()) {
  26.       case CAP_C: case LC_C: return new Circle(x, y, 10);
  27.       case CAP_E: case LC_E: return new Ellipse(x, y, 10, 30);
  28.       case CAP_R: case LC_R: return new Rectangle(x,y);
  29.       case CAP_S: case LC_S: return new Square(x, y);
  30.       case CAP_T: case LC_T: return new Triangle(x, y);
  31.       // Must also extend the switch statement:
  32. #if defined(EXTEND)
  33.       case CAP_B: case LC_B: return new circlebox(x,y, 10);
  34. #endif
  35.       default: break;
  36.     }
  37.   }
  38. }
  39.  
  40. main() {
  41.   shape_list shapelist;
  42.   // make some initial shapes:
  43.   int verts1[] = { 0, 0, 0, 20, 50, 50, 75, 0 };
  44.   shapelist.add(new Polygon(sizeof(verts1)/(sizeof(verts1[0]) * 2),
  45.             verts1, 100,300, 20,20, WHITE, HATCH_FILL));
  46.   shapelist.add(new Ellipse(200,200, 50, 75, WHITE, HATCH_FILL));
  47.   shapelist.add(new Ellipse(100,200, 70, 75, WHITE, HATCH_FILL));
  48.   shapelist.add(new Square(300,300, 100));
  49.   shapelist.add(new Circle(350,350, 47));
  50.  
  51.   shape * sh = shapelist.top();
  52.   shapelist.drawlist();
  53.   while(1) {
  54.     bgi_graphics::bottomprompt(
  55.       "a: add shape, r: remove, n: next shape, "
  56.       "arrow keys: move, e: edit, esc to quit");
  57.     const move_factor = 20;
  58.     switch(get_doskey()) {
  59.       case CAP_A       : // Add a shape
  60.       case LC_A        : sh = shapelist.add(makeshape());
  61.                          sh->draw();
  62.                          break;
  63.       case CAP_R       :
  64.       case LC_R        : shapelist.remove();
  65.                          sh = shapelist.top();
  66.                          shapelist.drawlist();
  67.                          break;
  68.       case CAP_N       : // move to next valid shape in the list
  69.       case LC_N        : sh = shapelist.next();
  70.                          if(!sh) sh = shapelist.top();
  71.                          if(!sh) return 0;  // empty list
  72.                          sh->draw();
  73.                          break;
  74.       case LEFT_ARROW  : sh->moverelative(- move_factor, 0);
  75.                          shapelist.drawlist();
  76.                          break;
  77.       case RIGHT_ARROW : sh->moverelative(move_factor, 0);
  78.                          shapelist.drawlist();
  79.                          break;
  80.       case UP_ARROW    : sh->moverelative(0, - move_factor);
  81.                          shapelist.drawlist();
  82.                          break;
  83.       case DOWN_ARROW  : sh->moverelative(0,  move_factor);
  84.                          shapelist.drawlist();
  85.                          break;
  86.       case CAP_E       :
  87.       case LC_E        : sh->edit();
  88.                          shapelist.drawlist();
  89.                          break;
  90.       case ESCAPE      : return 0;
  91.       default          : break;
  92.     }
  93.   }
  94. }
  95.