home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / FIGURES.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  3KB  |  126 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // FIGURES.CPP: This file contains the definitions for the Point
  4. // class (declared in figures.h). Member functions for the
  5. // Location class appear as inline functions in figures.h.
  6.  
  7. #include "figures.h"
  8. #include <graphics.h>
  9. #include <conio.h>
  10.  
  11. // member functions for the Point class
  12.  
  13. //constructor
  14. Point::Point(int InitX, int InitY) : Location (InitX, InitY)
  15. {
  16.    Visible = false;    // make invisible by default
  17. }
  18.  
  19. void Point::Show()
  20. {
  21.    Visible = true;
  22.    putpixel(X, Y, getcolor()); // uses default color
  23. }
  24.  
  25. void Point::Hide()
  26. {
  27.    Visible = false;
  28.    putpixel(X, Y, getbkcolor()); // uses background color to erase
  29. }
  30.  
  31. void Point::MoveTo(int NewX, int NewY)
  32. {
  33.    Hide();          // make current point invisible
  34.    X = NewX;        // change X and Y coordinates to new location
  35.    Y = NewY;
  36.    Show();          // show point at new location
  37. }
  38.  
  39. // a general-purpose function for getting keyboard
  40. // cursor movement keys (not a member function)
  41.  
  42. Boolean GetDelta(int& DeltaX, int& DeltaY)
  43. {
  44.    char KeyChar;
  45.    Boolean Quit;
  46.    DeltaX = 0;
  47.    DeltaY = 0;
  48.  
  49.    do
  50. {
  51.       KeyChar = getch();     // read the keystroke
  52.       if (KeyChar == 13)     // carriage return
  53.          return(false);
  54.       if (KeyChar == 0)      // an extended keycode
  55.          {
  56.           Quit = true;       // assume it is usable
  57.           KeyChar = getch(); // get rest of keycode
  58.               switch (KeyChar) {
  59.                  case 72: DeltaY = -1; break; // down arrow
  60.                  case 80: DeltaY =  1; break; // up arrow
  61.                  case 75: DeltaX = -1; break; // left arrow
  62.                  case 77: DeltaX =  1; break; // right arrow
  63.                  default: Quit = false; // bad key
  64.                  };
  65.          };
  66.    } while (!Quit);
  67.    return(true);
  68. }
  69.  
  70. void Point::Drag(int DragBy)
  71. {
  72.    int DeltaX, DeltaY;
  73.    int FigureX, FigureY;
  74.  
  75.    Show(); // display figure to be dragged
  76.    FigureX = GetX(); // get initial position of figure
  77.    FigureY = GetY();
  78.  
  79.    // This is the drag loop
  80.    while (GetDelta(DeltaX, DeltaY))
  81. {
  82.       // Apply delta to figure at X, Y
  83.       FigureX += (DeltaX * DragBy);
  84.       FigureY += (DeltaY * DragBy);
  85.       MoveTo(FigureX, FigureY); // tell figure to move
  86.       };
  87. }
  88. // Member functions for the Circle class
  89.  
  90. //constructor
  91. Circle::Circle(int InitX, int InitY, int InitRadius) : Point (InitX, InitY)
  92. {
  93.    Radius = InitRadius;
  94. }
  95.  
  96. void Circle::Show()
  97. {
  98.    Visible = true;
  99.    circle(X, Y, Radius);     // draw the circle
  100. }
  101.  
  102. void Circle::Hide()
  103. {
  104.    unsigned int TempColor;   // to save current color
  105.    TempColor = getcolor();   // set to current color
  106.    setcolor(getbkcolor());   // set drawing color to background
  107.    Visible = false;
  108.    circle(X, Y, Radius);     // draw in background color to
  109.    setcolor(TempColor);      // set color back to current color
  110. }
  111.  
  112. void Circle::Expand(int ExpandBy)
  113. {
  114.    Hide();                       // erase old circle
  115.    Radius += ExpandBy;           // expand radius
  116.    if (Radius < 0)               // avoid negative radius
  117.       Radius = 0;
  118.    Show();                       // draw new circle
  119. }
  120.  
  121. void Circle::Contract(int ContractBy)
  122. {
  123.    Expand(-ContractBy);      // redraws with (Radius-ContractBy)
  124. }
  125.  
  126.