• Homework 9 - Shapes Part 3


    After you complete Homework 8 and email it in, delete the function main() and paste the following code at the end of your file. Then implement the sections marked "// IMPLEMENT..."

    //============================================================================
    // Class: TextMsg
    // Description:
    // Make a new class called TextMsg which inherits the behavior
    // of the Shape class and overrides the draw() function to
    // draw the text (next to the origin).

    class TextMsg : public Shape
    {
    private: // Private Data - for TextMsg use only!
     string msg;

    public: // Public Member Functions - anyone can call them
     //------------------------------------------------------------------------
     // CONSTRUCTORS and DESTRUCTORS
     //------------------------------------------------------------------------

     //++++++++++++++++++++++++++++++++
     // Function: TextMsg()     (a Constructor)
     TextMsg(uint newX, uint newY, string newMsg)
      : Shape(newX,newY) // Call the Shape constructor passing in newX,newY
     {
      msg = newMsg;
     }

     //++++++++++++++++++++++++++++++++
     // Function: ~TextMsg()    (a Destructor)
     // Doesn't do anything
     ~TextMsg() {}

     //------------------------------------------------------------------------
     // ACCESSORS
     //------------------------------------------------------------------------

     //------------------------------------------------------------------------
     // OTHER MEMBER FUNCTIONS
     //------------------------------------------------------------------------

     //++++++++++++++++++++++++++++++++
     // Function: draw()
     // Draw the text onto the Screen given by reference in 's'.
     // Each character is printed across the line starting one space
     // after the origin (so that the origin can be drawn).
     virtual void draw(Screen &s)
     {
      for (uint cnum = 0; cnum < msg.length(); cnum++) {
       s.poke(getX()+cnum+1,getY(),msg[cnum]);
      }
      Shape::draw(s);
     }
    };

    //============================================================================
    // Main Command Loop
    //============================================================================

    int main()
    {
     // Make a new "list" object that holds a list of "Shape"s that we
     // will construct
     list myShapes;
     // Make a new "Screen" object to draw into
     Screen myScreen(70,24);
     // Declare a variable to hold the "command" character from the user
     char cmd;
     // Possible Commands
     //   q - Exit
     //   r - Add a new Rectangle
     //   d - Add a new Dot
     //   l - Add a new Line
     //   t - Add a new Text message
     //   c - Add a new Circle
     //   f - Add a new FilledRectangle
     //   x - Erase screen (delete all of the shapes from the list and erase the screen)

     // Put some stuff into the screen as a start screen demo
     myShapes.push_back(new Line(1,1,3,1)); // vert
     myShapes.push_back(new Line(3,2,4,10)); // horiz
     myShapes.push_back(new Line(0,11,5,4)); // diag
     myShapes.push_back(new Line(30,19,6,37)); // rev diag
     myShapes.push_back(new TextMsg(7,0,"Graphics Demo"));
     myShapes.push_back(new Rect(35,1,4,5));
     myShapes.push_back(new Circle(20,10,8));
     myShapes.push_back(new FilledRect(4,16,7,4,'z'));
     myShapes.push_back(new Dot(8,12));
     myShapes.push_back(new TextMsg(40,10,"Commands:"));
     myShapes.push_back(new TextMsg(40,11,"   r - Add Rectangle"));
     myShapes.push_back(new TextMsg(40,12,"   f - Add Filled Rectangle"));
     myShapes.push_back(new TextMsg(40,13,"   d - Add Dot"));
     myShapes.push_back(new TextMsg(40,14,"   c - Add Circle"));
     myShapes.push_back(new TextMsg(40,15,"   t - Add Text Msg"));
     myShapes.push_back(new TextMsg(40,16,"   l - Add Line"));
     myShapes.push_back(new TextMsg(40,17,"   x - Erase All"));
     myShapes.push_back(new TextMsg(40,18,"   q - Quit"));

     while (true) {
      // FIRST, draw the current state of the screen
      myScreen.erase(); // Erase the screen
      list::iterator it; // walk the list of shapes and have them display themselves in the buffer
      for (it = myShapes.begin(); it != myShapes.end(); it++) {
       (*it)->draw(myScreen);
      }
      myScreen.display(); // Now make the screen display itself

      // SECOND, prompt the user to enter a 1 letter command
      cout << "Command: ";  // prompt message
      cin >> cmd; // get the command character

      // THIRD, depending on the command character entered, do the right thing...
      switch(cmd)
      {
      case 'q':
       {
        return 0; // exit (not so gracefully, but it works)
        break;
       }
      case 'x':
       {
        list::iterator it; // walk the list of shapes and delete them
        for (it = myShapes.begin(); it != myShapes.end(); it++) {
         delete (*it);
        }
        myShapes.clear(); // then empty the list
        break;
       }
      case 'c':
       {
        int tmpX, tmpY, tmpR;
        cout << "Enter X: "; cin >> tmpX;
        cout << "Enter Y: "; cin >> tmpY;
        cout << "Enter Radius: "; cin >> tmpR;
        myShapes.push_back(new Circle(tmpX,tmpY,tmpR));
        break;
       }
      case 'd':
       {
        int tmpX, tmpY;
    // IMPLEMENT THE PROMPT AND GET VALUES INTO tmpX AND tmpY
        myShapes.push_back(new Dot(tmpX,tmpY));
        break;
       }
      case 't':
       {
        int tmpX, tmpY;
        string tmpString;
    // IMPLEMENT THE PROMPT AND GET VALUES INTO tmpX AND tmpY
        getline(cin,tmpString); // reset the input
        cout << "Enter Text: "; getline(cin,tmpString);
        myShapes.push_back(new TextMsg(tmpX,tmpY,tmpString));
        break;
       }
      case 'l':
       {
        int tmpX, tmpY, tmpEndX, tmpEndY;
    // IMPLEMENT THE PROMPT AND GET VALUES INTO tmpX AND tmpY AND tmpEndX AND tmpEndY
        myShapes.push_back(new Line(tmpX,tmpY,tmpEndX,tmpEndY));
        break;
       }
      case 'r':
       {
        int tmpX, tmpY, tmpWidth, tmpHeight;
    // IMPLEMENT THE PROMPT AND GET VALUES INTO tmpX AND tmpY AND tmpWidth AND tmpHeight
        myShapes.push_back(new Rect(tmpX,tmpY,tmpWidth,tmpHeight));
        break;
       }
      case 'f':
       {
        int tmpX, tmpY, tmpWidth, tmpHeight;
        char tmpFill;
    // IMPLEMENT THE PROMPT AND GET VALUES INTO tmpX AND tmpY AND tmpWidth AND tmpHeight
        cout << "Fill character: "; cin >> tmpFill;
        myShapes.push_back(new FilledRect(tmpX,tmpY,tmpWidth,tmpHeight,tmpFill));
        break;
       }
      default:
       {
        cout << "Unrecognized command character " << cmd << "." << endl;
       }
      }
     };
     return 0;
    }