Commit ed73c6e3 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Added source code

parent 4e85903a
Loading
Loading
Loading
Loading
+13 −0
Original line number Original line Diff line number Diff line
#include "Color.h"

// various predefined colors
const Color Color::Red(255,0,0);
const Color Color::Green(0,255,0);
const Color Color::Blue(0,0,255);
const Color Color::Purple(255,0,255);
const Color Color::Yellow(255,255,0);
const Color Color::White(255,255,255);
const Color Color::Gray(100,100,100);
const Color Color::Black(0,0,0);
const Color Color::Brown(102, 51, 0);
const Color Color::Orange(255,127,80);
 No newline at end of file

DataStructures/Color.h

0 → 100644
+35 −0
Original line number Original line Diff line number Diff line
//
// Created by Ari on 3/28/23.
//

#ifndef HW6_COLOR_H
#define HW6_COLOR_H
/**
 * Represents a Color
 */
class Color {
public:
    /**
     * Denotes one color
     * @param theRed The amount of red in the color, expressed as an integer from 0 to 255 inclusive.
     * @param theGreen The amount of green in the color, expressed as an integer from 0 to 255 inclusive.
     * @param theBlue The amount of blue in the color, expressed as an integer from 0 to 255 inclusive.
     */
    Color(int theRed, int theGreen, int theBlue) : red(theRed), green(theGreen), blue(theBlue) {}

    // accessors/setters
    int getRed() const { return red; }

    int getGreen() const { return green; }

    int getBlue() const { return blue; }

    // known colors
    static const Color Red, Green, Blue, Purple, Yellow, White, Gray, Black, Brown, Orange;

private:
    int red, green, blue;
};


#endif //HW6_COLOR_H

DataStructures/Point.h

0 → 100644
+19 −0
Original line number Original line Diff line number Diff line
//
// Created by Ari on 3/31/23.
//

#ifndef HW6_POINT_H
#define HW6_POINT_H

/**
 * A Point in the Window, whose coordinates are provided in pixels.
 */
class Point {
 public:
  Point(int theXX, int theYY) : xx(theXX), yy(theYY) {}

  // pixel locations for the point
  int xx, yy;
};

#endif //HW6_POINT_H

GUI/Window.cpp

0 → 100644
+42 −0
Original line number Original line Diff line number Diff line
#include "Window.h"
// VARIABLE settings

// Window instance starts of as the null pointer
draw::Window *draw::Window::instance = nullptr;

// frames per second
const double draw::Window::FRAME_RATE = 10;

bool draw::Window::ANIMATE = true;

bool draw::Window::ALL_DONE = false;

// METHOD IMPLEMENTATIONS
void draw::Window::_drawShapes() {
    do {
        addShape(new AllDoneSensor());

        // animation
        while (!ALL_DONE && ANIMATE) {
            char userInput='\0'; // default value is nothing

            // check for user input
            if (gfx_event_waiting()) {
                userInput = gfx_wait();
            }

            // draw shapes
            for (Shape *theShape: shapeList) {
                theShape->onInput(userInput);
                theShape->draw();
            }

            // transition to the next frame
            gfx_flush();
            this_thread::sleep_for(chrono::milliseconds((int) (1000.0 / FRAME_RATE)));
            gfx_clear(); // clear the screen
        }
        // animation paused
        this_thread::sleep_for(chrono::milliseconds((int) (1000.0 / FRAME_RATE)));
    } while (!ALL_DONE && !ANIMATE);
}
 No newline at end of file

GUI/Window.h

0 → 100644
+173 −0
Original line number Original line Diff line number Diff line
//
// Created by Ari Trachtenberg on 3/30/23.
//

#ifndef HW6_WINDOW_H
#define HW6_WINDOW_H

//
// Created by Ari on 3/28/23.
//

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include "gfx.h"
#include "../DataStructures/Color.h"
#include "../Shapes/Shape.h"
#include "../Shapes/Rect/Rect.h"
#include "../Shapes/AllDoneSensor.h"

using namespace std;

namespace draw {
/**
 * A class that represents the window on which shapes are drawn.
 * This is a Singleton class, meaning that there can be only one object
 * of this class in the system.
 */
    class Window {
    public:
        // METHODS
        static Window &getInstance() {
            if (instance == nullptr)
                instance = new Window();
            return *instance;
        }

        /**
         * Adds a shape to the list of shapes that are displayed on this Window.
         * @param theShape The shape to add to those that are displayed on this Window.
         */
        void addShape(Shape *theShape) { shapeList.push_back(theShape); }

        /**
         * @param index
         * @return The [index]th Shape being displayed by this Window
         */
        Shape *getShape(int index) {
            return shapeList[index];
        }

        /**
         * Signals that the Window should close down.
         */
        static void setAllDone() {
            ALL_DONE = true;
        }

        static void WaitForAllDone() {
            cout << "Press 'x' in the drawing window to exit" << endl;
            while (!ALL_DONE) {
                this_thread::sleep_for(chrono::milliseconds((int) (1000.0 / FRAME_RATE)));
            }
        }

        /**
         * @return The number of shapes being displayed by this Window
         */
        int numShapes() {
            return shapeList.size();
        }

        // ... Accessors/Setters
        Color getBgColor() {
            return bgColor;
        }

        void setBgColor(Color theBgColor) {
            bgColor = theBgColor;
        }

        // FIELDS
        /**
         * The width of the Window.
         */
        const int width;

        /**
       * The height of the Window.
       */
        const int height;

        /**
       * The title of the Window.
       */
        const string title;

        /**
         * @return A rectangle encapsulating the current screen bounds
         */
        const Rect bounds;

        /**
 * The number of frames of animation to display per second, on average.
 */
        static double const FRAME_RATE;

    private:
        // METHODS
        /*
        * This constructor can only be called internally.
         * Creates the window of the given width, height, and with the given title.
        * @param theWidth The width of the window.
        * @param theHeight The height of the window.
        * @param theTitle The title of the window.
        */
        explicit Window(int theWidth = 600,
               int theHeight = 400,
               string theTitle = "Homework Six")
                : width(theWidth),
                  height(theHeight),
                  title(theTitle),
                  bgColor(Color::White),
                  bounds(Color::White, 0, 0, width, height) {
            // set up the window
            gfx_open(width, height, title.c_str());

            // set up the drawing loop
            drawThread = thread(&Window::_drawShapes, this);
            drawThread.detach();
        }

        /**
         * A loop that keeps drawing the shapes that are part of this Window, but on a separate thread
         */
        void _drawShapes();


        // FIELDS
        /**
         * The background color of the Window.
         */
        Color bgColor;

        /**
         * A list of the shapes that are displayed on this Window.
         */
        vector<Shape *> shapeList;

        /**
         * The Thread used for drawing shapes.
         */
        thread drawThread;

        /**
         * The sole Window instance.
         */
        static Window *instance;

        /**
         * When true, the window closes.
         */
        static bool ALL_DONE;

        /**
       * Animation (i.e., contiuous viewing of window shapes) proceeds while ANIMATE is true, and pauses when it is false.
       */
        static bool ANIMATE;

    };
}
#endif //HW6_WINDOW_H
Loading