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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID20 Demo Source File                            */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. //  Polymorphism with a collection of graphics objects
  10.  
  11. #include <stdlib.h>
  12. #include <graphics.h>
  13. #include <stdio.h>
  14.  
  15. #define Uses_TObject
  16. #define Uses_TPoint
  17. #define Uses_TRect
  18. #define Uses_TCollection
  19. #include <tv.h>
  20.  
  21. class TGraphObject : public TObject
  22. {
  23.  
  24. public:
  25.  
  26.     int x, y;
  27.     TGraphObject();
  28.     // for this example, the constructor assigns random values to x, y
  29.     virtual void draw() = 0;
  30.     // pure virtual function--must be defined in derived classes
  31.  
  32. };
  33.  
  34. class TGraphPoint : public TGraphObject
  35. {
  36.  
  37. public:
  38.  
  39.     TGraphPoint();
  40.     // for this example, the constructor assigns random values to x, y
  41.     virtual void draw();
  42.  
  43. };
  44.  
  45. class TGraphCircle : public TGraphObject
  46. {
  47.  
  48. public:
  49.  
  50.     int radius;
  51.     TGraphCircle();
  52.     // for this example, the constructor assigns random values to x, y,
  53.     // and radius
  54.     virtual void draw();
  55. };
  56.  
  57. class TGraphRect : public TGraphObject
  58. {
  59.  
  60. public:
  61.  
  62.     int width, height;
  63.     TGraphRect();
  64.     // for this example, the constructor assigns random values to x, y, w,
  65.     // and h
  66.     virtual void draw();
  67. };
  68.  
  69. TGraphObject::TGraphObject()
  70. {
  71.     x = random( getmaxx() );
  72.     y = random( getmaxy() );
  73. };
  74.  
  75. TGraphPoint::TGraphPoint() : TGraphObject()
  76. {
  77. };
  78.  
  79. void TGraphPoint::draw()
  80. {
  81.  
  82.     // make a nice fat, visible point
  83.     for( int dx = x-2; dx < x+2; dx++)
  84.         for( int dy = y-2; dy < y+2; dy++)
  85.             putpixel( dx, dy, 1);
  86. }
  87.  
  88. TGraphCircle::TGraphCircle() : TGraphObject()
  89. {
  90.     radius = 20 + random( 20 );
  91. }
  92.  
  93. void TGraphCircle::draw()
  94. {
  95.     circle( x, y, radius);
  96. }
  97.  
  98. TGraphRect::TGraphRect() : TGraphObject()
  99. {
  100.     width  = 10 + random( 20 ) + x;
  101.     height =  6 + random( 15 ) + y;
  102. }
  103.  
  104. void TGraphRect::draw()
  105. {
  106.     rectangle( x, y, x + width, y + height );
  107. }
  108.  
  109. void startGraphics()
  110. {
  111.     int gdriver = DETECT, gmode, errorcode;
  112.     initgraph( &gdriver, &gmode, "..\\..\\bgi" );
  113.  
  114.     errorcode = graphresult();
  115.     if( errorcode != grOk )
  116.         {
  117.         printf( "Graphics init error: %s\n", grapherrormsg(errorcode));
  118.         getchar();
  119.         exit(1);
  120.         }
  121. }
  122.  
  123. void callDraw( void *p, void * )
  124. {
  125.     ((TGraphObject *)p)->draw();
  126.     // Call the appropriate draw member function
  127. }
  128.  
  129. void drawAll( TNSCollection *c, void * )
  130. {
  131.     c->forEach( &callDraw, 0 );   // Draw each object
  132. }
  133.  
  134. int main()
  135. {
  136.     startGraphics();
  137.     TNSCollection *list = new TNSCollection(10, 5);   // Create collection
  138.  
  139.     for( int i = 0; i < 20; i++ )
  140.         {
  141.         switch ( i % 3 )
  142.             {
  143.             case 0:
  144.                 TGraphPoint *gp = new TGraphPoint;
  145.                 list->insert( gp );
  146.                 break;
  147.             case 1:
  148.                 TGraphCircle *gc = new TGraphCircle;
  149.                 list->insert( gc );
  150.                 break;
  151.             case 2:
  152.                 TGraphRect *gr = new TGraphRect;
  153.                 list->insert( gr );
  154.                 break;
  155.             }
  156.         }
  157.  
  158.     drawAll( list, 0 );
  159.     getchar();
  160.     // pause to admire?
  161.  
  162.     closegraph();
  163.     return 0;
  164. }
  165.  
  166.