home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 15.ddi / TVDOCDEM.ZIP / TVGUID21.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  8.5 KB  |  385 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID021 Demo Source File                            */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. //  Polymorphism with a collection of graphics objects
  10. //  writing objects to a stream
  11.  
  12. #include <stdlib.h>
  13. #include <graphics.h>
  14. #include <stdio.h>
  15.  
  16. #define Uses_TObject
  17. #define Uses_TPoint
  18. #define Uses_TRect
  19. #define Uses_TCollection
  20. #define Uses_ofpstream
  21. #define Uses_ifpstream
  22. #define Uses_TStreamableClass
  23. #include <tv.h>
  24.  
  25. class TGraphObject : public TObject, public TStreamable
  26. {
  27.  
  28. public:
  29.  
  30.     int x, y;
  31.     TGraphObject();
  32.     // for this example, the constructor assigns random values to x, y
  33.     virtual void draw() = 0;
  34.     // pure virtual function--must be defined in derived classes
  35.     virtual void write( opstream& os);
  36.     virtual void *read( ipstream& );
  37.  
  38. protected:
  39.  
  40.     TGraphObject( StreamableInit ) {}
  41.  
  42. private:
  43.  
  44.     virtual const char *streamableName() const = 0;
  45.  
  46. };
  47.  
  48. class TGraphPoint : public TGraphObject
  49. {
  50.  
  51. public:
  52.  
  53.     TGraphPoint();
  54.     // for this example, the constructor assigns random values to x, y
  55.     virtual void draw();
  56.  
  57.     static const char * const name;
  58.     static TStreamable *build();
  59.  
  60. protected:
  61.  
  62.     TGraphPoint( StreamableInit ) : TGraphObject( streamableInit ) {}
  63.  
  64. private:
  65.  
  66.     virtual const char *streamableName() const
  67.         { return name; }
  68.  
  69. };
  70.  
  71. class TGraphCircle : public TGraphObject
  72. {
  73.  
  74. public:
  75.  
  76.     int radius;
  77.     TGraphCircle();
  78.         // for this example, the constructor assigns random values to x, y,
  79.         // and radius
  80.     virtual void draw();
  81.     virtual void write( opstream& os);
  82.     virtual void *read( ipstream& );
  83.  
  84.     static const char * const name;
  85.     static TStreamable *build();
  86.  
  87. protected:
  88.  
  89.     TGraphCircle( StreamableInit ) : TGraphObject( streamableInit ) {}
  90.  
  91. private:
  92.  
  93.     virtual const char *streamableName() const
  94.         { return name; }
  95.  
  96. };
  97.  
  98. class TGraphRect : public TGraphObject
  99. {
  100.  
  101. public:
  102.  
  103.     int width, height;
  104.     TGraphRect();
  105.     // for this example, the constructor assigns random values to x, y, w,
  106.     // and h
  107.     virtual void draw();
  108.     virtual void write( opstream& os);
  109.     virtual void *read( ipstream& );
  110.  
  111.     static const char * const name;
  112.     static TStreamable *build();
  113.  
  114. protected:
  115.  
  116.     TGraphRect( StreamableInit ) : TGraphObject( streamableInit ) {}
  117.  
  118. private:
  119.  
  120.     virtual const char *streamableName() const
  121.         { return name; }
  122.  
  123. };
  124.  
  125. class TGraphCollection : public TCollection
  126. {
  127.  
  128. public:
  129.  
  130.     TGraphCollection() : TCollection( 12, 5 ) {}
  131.     void drawAll();
  132.     static const char * const name;
  133.     static TStreamable *build();
  134.  
  135. private:
  136.  
  137.     virtual void freeItem( void *item );
  138.  
  139.     virtual const char *streamableName() const
  140.         { return name; }
  141.  
  142.     virtual void *readItem( ipstream& );
  143.     virtual void writeItem( void *, opstream& );
  144.  
  145. protected:
  146.  
  147.     TGraphCollection( StreamableInit ) : TCollection( streamableInit ) {}
  148. };
  149.  
  150. TGraphObject::TGraphObject()
  151. {
  152.     x = random( getmaxx() );
  153.     y = random( getmaxy() );
  154. };
  155.  
  156. void TGraphObject::write( opstream& os )
  157. {
  158.     os << x << y;
  159. }
  160.  
  161. void *TGraphObject::read( ipstream& is )
  162. {
  163.     is >> x >> y;
  164.     return this;
  165. }
  166.  
  167. inline ipstream& operator >> ( ipstream& is, TGraphObject& cl )
  168.     { return is >> (TStreamable&)cl; }
  169. inline ipstream& operator >> ( ipstream& is, TGraphObject*& cl )
  170.     { return is >> (void *&)cl; }
  171.  
  172. inline opstream& operator << ( opstream& os, TGraphObject& cl )
  173.     { return os << (TStreamable&)cl; }
  174. inline opstream& operator << ( opstream& os, TGraphObject* cl )
  175.     { return os << (TStreamable *)cl; }
  176.  
  177. TGraphPoint::TGraphPoint() : TGraphObject()
  178. {
  179. };
  180.  
  181. void TGraphPoint::draw()
  182. {
  183.     // make a nice fat, visible point
  184.     for( int dx = x-2; dx < x+2; dx++ )
  185.         for( int dy = y-2; dy < y+2; dy++ )
  186.             putpixel( dx, dy, 1);
  187. }
  188.  
  189. TStreamable *TGraphPoint::build()
  190. {
  191.     return new TGraphPoint( streamableInit );
  192. }
  193.  
  194. const char * const TGraphPoint::name = "TGraphPoint";
  195.  
  196. TGraphCircle::TGraphCircle() : TGraphObject()
  197. {
  198.     radius = 20 + random( 20 );
  199. }
  200.  
  201. void TGraphCircle::draw()
  202. {
  203.     circle( x, y, radius);
  204. }
  205.  
  206. void TGraphCircle::write( opstream& os )
  207. {
  208.     TGraphObject::write( os );
  209.     os << radius;
  210. }
  211.  
  212. void *TGraphCircle::read( ipstream& is )
  213. {
  214.     TGraphObject::read( is );
  215.     is >> radius;
  216.     return this;
  217. }
  218.  
  219. TStreamable *TGraphCircle::build()
  220. {
  221.     return new TGraphCircle( streamableInit );
  222. }
  223.  
  224. const char * const TGraphCircle::name = "TGraphCircle";
  225.  
  226. TGraphRect::TGraphRect() : TGraphObject()
  227. {
  228.     width  = 10 + random( 20 ) + x;
  229.     height =  6 + random( 15 ) + y;
  230. }
  231.  
  232. void TGraphRect::draw()
  233. {
  234.     rectangle( x, y, x + width, y + height );
  235. }
  236.  
  237. void TGraphRect::write( opstream& os )
  238. {
  239.     TGraphObject::write( os );
  240.     os << width << height;
  241. }
  242.  
  243. void *TGraphRect::read( ipstream& is )
  244. {
  245.     TGraphObject::read( is );
  246.     is >> width >> height;
  247.     return this;
  248. }
  249.  
  250. TStreamable *TGraphRect::build()
  251. {
  252.     return new TGraphRect( streamableInit );
  253. }
  254.  
  255. const char * const TGraphRect::name = "TGraphRect";
  256.  
  257. void startGraphics()
  258. {
  259.     int gdriver = DETECT, gmode, errorcode;
  260.     initgraph( &gdriver, &gmode, "..\\..\\bgi" );
  261.  
  262.     errorcode = graphresult();
  263.     if( errorcode != grOk )
  264.         {
  265.         printf( "Graphics init error: %s\n", grapherrormsg(errorcode));
  266.         getchar();
  267.         exit(1);
  268.         }
  269. }
  270.  
  271. static void callDraw( void *p, void * )
  272. {
  273.     ((TGraphObject *)p)->draw();
  274.     // Call the appriate draw member function
  275. }
  276.  
  277. void TGraphCollection::drawAll()
  278. {
  279.     forEach( &callDraw, 0 );    // Draw each object
  280. }
  281.  
  282. void TGraphCollection::freeItem( void *item )
  283. {
  284.     delete (TGraphObject *)item;
  285. }
  286.  
  287. void *TGraphCollection::readItem( ipstream& is )
  288. {
  289.     TGraphObject *ptr;
  290.     is >> ptr;
  291.     return ptr;
  292. }
  293.  
  294. void TGraphCollection::writeItem( void *ptr, opstream& os )
  295. {
  296.     os << (TGraphObject *)ptr;
  297. }
  298.  
  299. const char * const TGraphCollection::name = "TGraphCollection";
  300.  
  301. TStreamable *TGraphCollection::build()
  302. {
  303.     return new TGraphCollection( streamableInit );
  304. }
  305.  
  306. inline ipstream& operator >> ( ipstream& is, TGraphCollection& cl )
  307.     { return is >> (TStreamable&)cl; }
  308. inline ipstream& operator >> ( ipstream& is, TGraphCollection*& cl )
  309.     { return is >> (void *&)cl; }
  310.  
  311. inline opstream& operator << ( opstream& os, TGraphCollection& cl )
  312.     { return os << (TStreamable&)cl; }
  313. inline opstream& operator << ( opstream& os, TGraphCollection* cl )
  314.     { return os << (TStreamable *)cl; }
  315.  
  316. TStreamableClass RGraphPoint( TGraphPoint::name,
  317.                               TGraphPoint::build,
  318.                               __DELTA(TGraphPoint) 
  319.                             );
  320.  
  321. TStreamableClass RGraphCircle( TGraphCircle::name,
  322.                                TGraphCircle::build,
  323.                                __DELTA(TGraphCircle) 
  324.                              );
  325.  
  326. TStreamableClass RGraphRect( TGraphRect::name,
  327.                              TGraphRect::build,
  328.                              __DELTA(TGraphRect) 
  329.                            );
  330.  
  331. TStreamableClass RGraphCollection( TGraphCollection::name,
  332.                                    TGraphCollection::build,
  333.                                    __DELTA(TGraphCollection) 
  334.                                  );
  335.  
  336. int main()
  337. {
  338.     startGraphics();
  339.  
  340.     TGraphCollection *list = new TGraphCollection;  // Create collection
  341.  
  342.     for( int i = 0; i < 12; i++ )
  343.         {
  344.         switch ( i % 3 )
  345.             {
  346.             case 0:
  347.                 TGraphPoint *gp = new TGraphPoint;
  348.                 list->insert( gp );
  349.                 break;
  350.             case 1:
  351.                 TGraphCircle *gc = new TGraphCircle;
  352.                 list->insert( gc );
  353.                 break;
  354.             case 2:
  355.                 TGraphRect *gr = new TGraphRect;
  356.                 list->insert( gr );
  357.                 break;
  358.             }
  359.         }
  360.  
  361.     list->drawAll();
  362.     getchar();
  363.     // pause to admire?
  364.  
  365.     ofpstream of( "graph.sav" );
  366.     of << list;
  367.     of.close();
  368.  
  369.     list->removeAll();
  370.     cleardevice();
  371.     list->drawAll();
  372.     getchar();
  373.  
  374.     ifpstream in( "graph.sav" );
  375.     in >> list;
  376.     in.close();
  377.  
  378.     list->drawAll();
  379.     getchar();
  380.  
  381.     closegraph();
  382.     return 0;
  383. }
  384.  
  385.