Lots of oving 7

master
Øyvind Skaaden 2019-02-26 15:01:05 +01:00
parent 11672f1f5c
commit 55ddeb17f9
8 changed files with 196 additions and 49 deletions

View File

@ -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);
}

View File

@ -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<Circle> eyes;
};

View File

@ -1,12 +0,0 @@
#include "Face.h"
Face::Face()
{
}
Face::~Face()
{
}

View File

@ -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

View File

@ -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

View File

@ -20,12 +20,10 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="Emoji.cpp" />
<ClCompile Include="Face.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Emoji.h" />
<ClInclude Include="Face.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>

View File

@ -21,16 +21,10 @@
<ClCompile Include="Emoji.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Face.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Emoji.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Face.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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();
}