Friendly Robot Class You will create a Robot class which will be able to draw a little robot icon at a particular place on the screen. Your robot will alternate drawing from two possible icons to create a small animation. We've provided main.cc that uses your Robot class to draw an animated robot that follows your cursor around. You will be implementing four member functions we've declared for you in the Robot class in robot.h. Robot needs a constructor, and getter and setter functions (aka accessors and mutators) for pixel location. Constructor We've declared the constructor in robot.h: Robot (const std::string& filenamel, const std::string& filename2); You must implement the Robot Class constructor in robot.cc. It takes two string parameters, the filename for the robot icon to show first, and the filename for the robot icon to show second. Your constructor should save these filenames in the filename1_ and filename2_ member variables but should not construct the images yet. Why not load the images in the constructor? It's not good practice to do work in constructors because if there is an error it leaves the program in a bad state. Don't forget: when you implement a member function, you must specify the class name before the function name, so the compiler knows that the function is a member of the class e.g.: int MyClassName::MyFunctionName() { }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

main.cc file

#include "robotutils/robotclicklistener.h"

//
// You do not need to edit this file.
//

// Helper function to create robot*.bmp. Feel free to make your own
// icons and use this for inspiration.
/*
void CreateRobotIcon() {
  graphics::Image image(31, 31);
  // Arms
  image.DrawLine(0, 10, 10, 15, 109, 131, 161, 6);
  image.DrawLine(30, 10, 10, 15, 109, 131, 161, 6);
  // Legs
  image.DrawLine(10, 15, 10, 30, 109, 131, 161, 6);
  image.DrawLine(20, 15, 20, 30, 109, 131, 161, 6);
  // Body
  image.DrawRectangle(5, 0, 20, 22, 130, 151, 179);
  // Eyes
  image.DrawCircle(10, 8, 2, 255, 255, 255);
  image.DrawCircle(20, 8, 2, 255, 255, 255);
  image.DrawCircle(9, 8, 2, 62, 66, 71);
  image.DrawCircle(19, 8, 2, 62, 66, 71);
  image.SaveImageBmp("robot.bmp");
}
*/

int main() {
  RobotClickListener listener{};
  listener.Start();
  return 0;
}

robot.cc file

#include "robot.h"

#include <string>

#include "cpputils/graphics/image.h"

// ========================= YOUR CODE HERE =========================
// This implementation file (robot.cc) should hold the
// implementation of member functions declared in the header (robot.h).
//
// Implement the following member functions, declared in robot.h:
//   1. Robot constructor
//   2. SetPosition
//   3. GetX
//   4. GetY
//
// Remember to specify the name of the class with :: in this format:
//     <return type> MyClassName::MyFunction() {
//        ...
//     }
// to tell the compiler that each function belongs to the Robot class.
// ===================================================================

// You don't need to modify these. These are helper functions
// used to load the robot icons and draw them on the screen.
void Robot::Draw(graphics::Image& image) {
  // Load the image into the icon if needed.
  if (icon1_.GetWidth() <= 0) {
    icon1_.Load(filename1_);
  }
  if (icon2_.GetWidth() <= 0) {
    icon2_.Load(filename2_);
  }
  mod_ = (mod_ + 1) % 2;
  if (mod_ == 1) {
    DrawIconOnImage(icon1_, image);
  } else {
    DrawIconOnImage(icon2_, image);
  }
}

void Robot::DrawIconOnImage(graphics::Image& icon,
                            graphics::Image& image) const {
  int width = icon.GetWidth();
  int height = icon.GetHeight();
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      int x = x_ + i - width / 2;
      int y = y_ + j - height / 2;
      if (y >= 0 && x >= 0 && x < image.GetWidth() && y < image.GetHeight()) {
        image.SetColor(x, y, icon.GetColor(i, j));
      }
    }
  }
}

robot.h file

#include <string>

#include "cpputils/graphics/image.h"

class Robot {
 public:
  // You don't need to change this file, but you will be
  // implementing some of these member functions in robot.cc.
  Robot(const std::string& filename1, const std::string& filename2);
  void SetPosition(int x, int y);
  int GetX() const;
  int GetY() const;
  void Draw(graphics::Image& image);

 private:
  void DrawIconOnImage(graphics::Image& icon, graphics::Image& image) const;
  std::string filename1_;
  std::string filename2_;
  graphics::Image icon1_;
  graphics::Image icon2_;
  graphics::Color color_;
  int x_ = 0;
  int y_ = 0;
  int mod_ = 0;
};

Friendly Robot Class
You will create a Robot class which will be able to draw a little robot icon at a
particular place on the screen.
Your robot will alternate drawing from two possible icons to create a small animation.
We've provided main.cc that uses your Robot class to draw an animated robot that
follows your cursor around.
DHI
You will be implementing four member functions we've declared for you in the Robot
class in robot.h.
Robot needs a constructor, and getter and setter functions (aka accessors and
mutators) for pixel location.
Constructor
We've declared the constructor in robot.h:
Robot (const std::string& filenamel, const std::string& filename2);
You must implement the Robot Class constructor in robot.cc. It takes two string
parameters, the filename for the robot icon to show first, and the filename for the robot
icon to show second. Your constructor should save these filenames in the filename1_
and filename2_ member variables but should not construct the images yet.
Why not load the images in the constructor? It's not good practice to do work in
constructors because if there is an error it leaves the program in a bad state.
Don't forget: when you implement a member function, you must specify the class
name before the function name, so the compiler knows that the function is a
member of the class e.g.:
int MyClassName::MyFunctionName() {
}
Transcribed Image Text:Friendly Robot Class You will create a Robot class which will be able to draw a little robot icon at a particular place on the screen. Your robot will alternate drawing from two possible icons to create a small animation. We've provided main.cc that uses your Robot class to draw an animated robot that follows your cursor around. DHI You will be implementing four member functions we've declared for you in the Robot class in robot.h. Robot needs a constructor, and getter and setter functions (aka accessors and mutators) for pixel location. Constructor We've declared the constructor in robot.h: Robot (const std::string& filenamel, const std::string& filename2); You must implement the Robot Class constructor in robot.cc. It takes two string parameters, the filename for the robot icon to show first, and the filename for the robot icon to show second. Your constructor should save these filenames in the filename1_ and filename2_ member variables but should not construct the images yet. Why not load the images in the constructor? It's not good practice to do work in constructors because if there is an error it leaves the program in a bad state. Don't forget: when you implement a member function, you must specify the class name before the function name, so the compiler knows that the function is a member of the class e.g.: int MyClassName::MyFunctionName() { }
Accessors (Getter) Functions
We declared the accessors (getter) functions for you in robot.h:
int GetX();
int GetY();
The Robot class tracks its (x, y) pixel location with two private integer member
variables. It must provide public accessor functions for x and y, GetX() and GetY()
which both return int s. Implement these accessor functions in robot.cc, following
the appropriate syntax for member function definitions (don't forget the class name!)
Mutator (Setter) Function
To set the (x, y) location, Robot provides a public function Set Position which takes
two arguments, the int x position and int y position.
void SetPosition(int x, int y);
Implement this mutator function in robot.cc, following the appropriate syntax for
member function definitions (don't forget the class name!)
How the Robot icon is Drawn
If you're curious how the Robot icon is drawn on the screen:
Robot must implement a function, Draw, that takes a reference to a
graphics::Image, and draws the robot icon on that image.
The Draw function will be called repeatedly by main.cc. Each time Draw is called
Robot should draw an icon centered at (x, y). Robot must alternate which icon is
drawn from the the two filenames in the constructor: The first time Draw is called it
shows the first icon, and the second time the second icon, etc. The Draw function
loads the image assets passed in the filenames that you should have stored in the
constructor. Draw uses this method from graphics::Image :
/*
* Loads an image from a file. Returns false if the image could
* not be loaded. Note: this clears any current state, including
* pixel values, width and height.
*/
bool Load (const std::string& filename);
Transcribed Image Text:Accessors (Getter) Functions We declared the accessors (getter) functions for you in robot.h: int GetX(); int GetY(); The Robot class tracks its (x, y) pixel location with two private integer member variables. It must provide public accessor functions for x and y, GetX() and GetY() which both return int s. Implement these accessor functions in robot.cc, following the appropriate syntax for member function definitions (don't forget the class name!) Mutator (Setter) Function To set the (x, y) location, Robot provides a public function Set Position which takes two arguments, the int x position and int y position. void SetPosition(int x, int y); Implement this mutator function in robot.cc, following the appropriate syntax for member function definitions (don't forget the class name!) How the Robot icon is Drawn If you're curious how the Robot icon is drawn on the screen: Robot must implement a function, Draw, that takes a reference to a graphics::Image, and draws the robot icon on that image. The Draw function will be called repeatedly by main.cc. Each time Draw is called Robot should draw an icon centered at (x, y). Robot must alternate which icon is drawn from the the two filenames in the constructor: The first time Draw is called it shows the first icon, and the second time the second icon, etc. The Draw function loads the image assets passed in the filenames that you should have stored in the constructor. Draw uses this method from graphics::Image : /* * Loads an image from a file. Returns false if the image could * not be loaded. Note: this clears any current state, including * pixel values, width and height. */ bool Load (const std::string& filename);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education