home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CL187A.ZIP / EXAMP502.CPP < prev    next >
C/C++ Source or Header  |  1994-03-15  |  3KB  |  142 lines

  1. // examp502.cpp -- link with cl.obj pitem.obj graphics.lib
  2. // Container Lite (CL v 1.87a)
  3. // (C) Copyright 1994  John Webster Small
  4. // All rights reserved
  5.  
  6. #include "pitem.h"
  7. #define BGI_PATHNAME "\\bc4\\bgi"
  8. #include "gconsole.cpp"
  9.  
  10. #define ID_Shape 1U
  11.  
  12. class Shape : public Streamable  {
  13.  
  14.     int x, y;
  15.  
  16.     void init(
  17.         // Shape level initializers with
  18.         // defaults provided for each
  19.         int x = GETRANDX(),
  20.         int y = GETRANDY()
  21.     )
  22.     { this->x = x; this->y = y; }
  23.  
  24. protected:
  25.  
  26.     void assign(const Shape& s)
  27.         { Streamable::assign
  28.         (*(const Streamable *)&s);
  29.          x = s.x; y = s.y; }
  30.     Shape(defaultConstructor)
  31.         : Streamable(defaultConstruct)
  32.         { init(); }
  33.     virtual int put(ostream& os);
  34.     int    get(istream& is);
  35.     static    StreamablE extract(istream& is);
  36.  
  37. public:
  38.  
  39.     static  int register_Class()
  40.         { return clasSv.regClass
  41.         (Shape::extract,ID_Shape); }
  42.  
  43.     Shape(const Shape& s) : Streamable(defaultConstruct)
  44.         { init(); assign(s); }
  45.     Shape(int x = GETRANDX(),
  46.         int y = GETRANDY())
  47.         : Streamable()
  48.         { init(x,y); }
  49.     virtual    int operator=(const Streamable& s);
  50.     virtual    StreamablE clone() const
  51.         { return (StreamablE)
  52.         new Shape(*this); }
  53.     virtual    unsigned ID() const
  54.         { return ID_Shape; }
  55.     int getx() { return x; }
  56.     int gety() { return y; }
  57.     Shape& setxy(int x = GETRANDX(),
  58.         int y = GETRANDY())
  59.         { this->x = x; this->y = y;
  60.         return *this;}
  61.     virtual void show(int color = GETRANDCOLOR(),
  62.         int xxpose = 0, int yxpose = 0,
  63.         int scale = 1)
  64.         { PUTPIX(x,y,color); }
  65.     virtual char * name()
  66.         { return "shape (pixel)"; }
  67.     virtual    ~Shape()  {}
  68.  
  69. };    /*  class Shape  */
  70.  
  71. int Shape::put(ostream& os)
  72. {
  73. //    if (Streamable::put(os))  {
  74.         os << x << endm << y << endm;
  75.         if (os)
  76.             return 1;  // success
  77. //    }
  78.     return 0;
  79. }
  80.  
  81. int Shape::get(istream& is)
  82. {
  83. //    if (Streamable::get(is))  {
  84.         is >> x >> nextm >> y >> nextm;
  85.         if (is)
  86.             return 1;
  87. //    }
  88.     return 0;
  89. }
  90.  
  91. StreamablE Shape::extract(istream& is)
  92. {
  93.     Shape* S;
  94.     S = new Shape(defaultConstruct);
  95.     if (S) if (S->get(is))
  96.         return (StreamablE) S;
  97.     else
  98.         delete S;
  99.     return  StreamablE0;
  100. }
  101.  
  102. int Shape::operator=(const Streamable& s)
  103. {
  104.     if (this->ID() == s.ID())  {
  105.         assign(*(const Shape *)&s);
  106.         return 1;
  107.     }
  108.     return 0;
  109. }
  110.  
  111. #if defined(CL_NO_TEMPLATES)
  112.     #define CL_ALL_DEF
  113.     CL_PITEM(Shape,Streamable)
  114.     #define ITEM Shape
  115.     #define CL Shapes
  116.     #include "cl.hf"
  117. #else
  118.     CL_PITEM(Shape,Streamable)
  119.     #define Shapes CL<Shape>
  120. #endif
  121.  
  122.  
  123. #define  ShapesFile "shapes.tmp"
  124.  
  125. main()
  126. {
  127.     openGraphics();
  128.     Shapes sb(CL_NEW | CL_DEL |
  129.         CL_SAVE,1000);
  130.     Shape s;
  131.     while (sb.insQNew(&s.setxy()));
  132.     Register_Class(Shape);
  133.     sb.save(ShapesFile);
  134.     sb.allDel();
  135.     sb.load(ShapesFile);
  136.     while (++sb)  sb.get()->show();
  137.     cout << "Press <enter> to exit" << endl;
  138.     (void) cin.get();
  139.     closeGraphics();
  140.     return 0;
  141. }
  142.