From 55ddeb17f9e1f3b92051f5ed5377d6d3c80d8fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Skaaden?= Date: Tue, 26 Feb 2019 15:01:05 +0100 Subject: [PATCH] Lots of oving 7 --- Oving 7/Oving 7/Emoji.cpp | 56 +++++++++++++++-- Oving 7/Oving 7/Emoji.h | 81 +++++++++++++++++++++---- Oving 7/Oving 7/Face.cpp | 12 ---- Oving 7/Oving 7/Graph_lib.cpp | 14 +++++ Oving 7/Oving 7/Graph_lib.h | 39 ++++++++++++ Oving 7/Oving 7/Oving 7.vcxproj | 2 - Oving 7/Oving 7/Oving 7.vcxproj.filters | 6 -- Oving 7/Oving 7/main.cpp | 35 ++++++----- 8 files changed, 196 insertions(+), 49 deletions(-) delete mode 100644 Oving 7/Oving 7/Face.cpp create mode 100644 Oving 7/Oving 7/Graph_lib.cpp create mode 100644 Oving 7/Oving 7/Graph_lib.h diff --git a/Oving 7/Oving 7/Emoji.cpp b/Oving 7/Oving 7/Emoji.cpp index 7054782..8935128 100644 --- a/Oving 7/Oving 7/Emoji.cpp +++ b/Oving 7/Oving 7/Emoji.cpp @@ -1,12 +1,60 @@ #include "Emoji.h" +/* + * Implement your emojis in this file. + **/ - -Emoji::Emoji() +// A yellow, empty face. +Face::Face(Point c, int r) : faceMask{c, r} { + faceMask.set_fill_color(Color::yellow); + faceMask.set_color(Color::black); + /* TODO: + * - add member initializer list + * - implement the constructor. I.e. fill color + **/ + + cout << "Not yet implemented\n"; } - -Emoji::~Emoji() +void Face::attach_to(Graph_lib::Window& win) { + win.attach(faceMask); +} + +/* TODO: + * - define more emojis. + **/ + +EmptyFace::EmptyFace(Point p, int r, bool upSideDown) : Face( p, r ) +{ + int offset = int(1 / 4 * r * sqrt(2)); + if (upSideDown) + offset *= -1; + + int eyeR = r / 4; + + Point left { p.x - offset, p.y - offset }; + Point right { p.x + offset, p.y - offset }; + + //Circle leftEye { left, eyeR }; + //Circle rightEye { right, eyeR }; + + //leftEye.set_fill_color(Color::black); + //rightEye.set_fill_color(Color::black); + + //eyes.push_back(leftEye); + //eyes.push_back(rightEye); + + +} + +void EmptyFace::attach_to(Graph_lib::Window & win) +{ + Face::attach_to(win); + //win.attach(eyes[0]); + Circle leftE{ left, eyeR }; + Circle rightE{ right, eyeR }; + win.attach(leftE); + win.attach(rightE); } diff --git a/Oving 7/Oving 7/Emoji.h b/Oving 7/Oving 7/Emoji.h index 893ce5d..a54d361 100644 --- a/Oving 7/Oving 7/Emoji.h +++ b/Oving 7/Oving 7/Emoji.h @@ -1,11 +1,70 @@ -#pragma once -class Emoji -{ -public: - virtual void Emoji::attach_to(Graph_lib::Window&) = 0; - Emoji(const Emoji&) = delete; - Emoji& operator=(const Emoji&) = delete; - Emoji() {} - virtual ~Emoji(); -}; - +#pragma once + +// Include Graph_lib library files that holds declarations needed for Window, +// and Shape derivatives. +#include "Graph.h" +#include "GUI.h" + +// This part is only relevant for non-Windows users in 2019. +// Windows users has Graph_lib::Arc, Mac don't. +#ifndef WIN32 +#include "Graph_lib.h" +#endif + +using namespace Graph_lib; + +// An abstract class. Concrete classes derived from this base class +// has to implement the member function attach_to(). +class Emoji +{ +public: + // Disable copying. Disable slicing, etc. + Emoji(const Emoji&) = delete; + Emoji& operator=(const Emoji&) = delete; + + // Deleting the copy constructor also deletes the default constructor. + // Emoji needs a default constructor. + Emoji() {} + // Emoji() = default; // is an alternative way of achieving the same. + + // The pure virtual function that has to be overriden for a deriving class + // to be instantiable. Every class deriving from Emoji is supposed to + // attach all its Shapes to a window. This makes the class abstract. + virtual void attach_to(Graph_lib::Window&) = 0; + + // Relevant because Vector_ref can own Emojis and automatically cleans up. + // Subject will be visited later in the course. + virtual ~Emoji() {} +}; + +// A yellow, empty face. +// An abstract class. +class Face : public Emoji +{ + /* TODO: + * - add shapes (private) + * - make the class abstract + **/ + + Circle faceMask; + +public: + Face(Point c, int r); + void attach_to(Graph_lib::Window& win) override; +}; + +/* TODO: + * - declare more emojis. + **/ + +class EmptyFace : public Face +{ +public: + EmptyFace(Point p, int r, bool upSideDown = false); + void attach_to(Graph_lib::Window& win) override; +private: + int offset; + int eyeR; + Point left, right; + Vector_ref eyes; +}; diff --git a/Oving 7/Oving 7/Face.cpp b/Oving 7/Oving 7/Face.cpp deleted file mode 100644 index 56071e4..0000000 --- a/Oving 7/Oving 7/Face.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "Face.h" - - - -Face::Face() -{ -} - - -Face::~Face() -{ -} diff --git a/Oving 7/Oving 7/Graph_lib.cpp b/Oving 7/Oving 7/Graph_lib.cpp new file mode 100644 index 0000000..cdae09e --- /dev/null +++ b/Oving 7/Oving 7/Graph_lib.cpp @@ -0,0 +1,14 @@ +#ifndef WIN32 +// If this is not a Windows computer, read this file. + +#include "Graph_lib.h" + +void Graph_lib::Arc::draw_lines() const +{ + if (color().visibility()) { + fl_color(color().as_int()); + fl_arc(point(0).x, point(0).y, w, h, start_deg, end_deg); + } +} + +#endif diff --git a/Oving 7/Oving 7/Graph_lib.h b/Oving 7/Oving 7/Graph_lib.h new file mode 100644 index 0000000..faed588 --- /dev/null +++ b/Oving 7/Oving 7/Graph_lib.h @@ -0,0 +1,39 @@ +#pragma once + +#ifndef WIN32 +// If this is not a Windows computer, read this file. + +// Code that extends Graph_lib for exercise 7. +// Not relevant for Windows users. + +#include "Graph.h" + +namespace Graph_lib { + +// For a description of this class, refer to the exercise text. +class Arc : public Graph_lib::Shape +{ +public: + Arc(Point center, int w, int h, int sd, int ed) + : w{w}, h{h}, start_deg{sd}, end_deg{ed} + { + add(Point{center.x - w / 2, center.y - h / 2}); + } + + void draw_lines() const override; + + void set_start(int d) { start_deg = d; } + void set_end(int d) { end_deg = d; } + void setw(int w) { this->w = w; } + void seth(int h) { this->h = h; } + +private: + int w; + int h; + int start_deg; + int end_deg; +}; + +} // namespace Graph_lib + +#endif diff --git a/Oving 7/Oving 7/Oving 7.vcxproj b/Oving 7/Oving 7/Oving 7.vcxproj index ae3349e..7031f5d 100644 --- a/Oving 7/Oving 7/Oving 7.vcxproj +++ b/Oving 7/Oving 7/Oving 7.vcxproj @@ -20,12 +20,10 @@ - - 15.0 diff --git a/Oving 7/Oving 7/Oving 7.vcxproj.filters b/Oving 7/Oving 7/Oving 7.vcxproj.filters index 066e783..52d5b4d 100644 --- a/Oving 7/Oving 7/Oving 7.vcxproj.filters +++ b/Oving 7/Oving 7/Oving 7.vcxproj.filters @@ -21,16 +21,10 @@ Source Files - - Source Files - Header Files - - Header Files - \ No newline at end of file diff --git a/Oving 7/Oving 7/main.cpp b/Oving 7/Oving 7/main.cpp index c39cede..34d2281 100644 --- a/Oving 7/Oving 7/main.cpp +++ b/Oving 7/Oving 7/main.cpp @@ -1,19 +1,26 @@ -// Example program in TDT4102_Graphics template, from PPP page 415 -#include "Graph.h" -#include "Simple_window.h" -int main() { +#include "Simple_window.h" +#include "Emoji.h" + +// Size of window and emoji radius +constexpr int xmax = 1000; +constexpr int ymax = 600; +constexpr int emojiRadius = 50; + +int main() +{ using namespace Graph_lib; - cout << "The New \"Hello, Graphical World!\" message\n"; - Point tl{ 100, 100 }; - Simple_window win{ tl, 600, 400, "Canvas" }; - Polygon poly; - poly.add(Point{ 300, 200 }); - poly.add(Point{ 350, 100 }); - poly.add(Point{ 400, 200 }); - poly.set_color(Color::red); + const Point tl{100, 100}; + const string win_label{"Emoji factory"}; + Simple_window win{tl, xmax, ymax, win_label}; + EmptyFace eFace(Point{ 100, 100 }, emojiRadius); + + eFace.attach_to(win); + + /* TODO: + * - initialize emojis + * - connect emojis to window + **/ - win.attach(poly); win.wait_for_button(); } -