1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// Osman Zakir
// 8 / 7 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 16 Exercise 6
// chapter16ex6.cpp
// Exercise Specifications:
/**
* Make an “analog clock,” that is, a clock with hands that move. You get the
* time of day from the operating system through a library call. A major part
* of this exercise is to find the functions that give you the time of day and
* a way of waiting for a short period of time (e.g., a second for a clock tick)
* and to learn to use them based on the documentation you found. Hint:
* clock(), sleep().
*/
#include "../../GUI.h"
#include "../../Graph.h"
#include "../../Window.h"
#include <ctime>
#include <string>
#include <iostream>
#include <stdexcept>
namespace Graph_lib
{
struct TimerC : Widget
{
private:
double time_interval;
public:
TimerC(double time, Callback cb)
: Widget{ Point{ 0,0 }, 0, 0, "", cb },
time_interval{ time }
{
}
static void cb_timer(void *clock)
{
static_cast<TimerC *>(clock)->do_it(nullptr, static_cast<TimerC*>(clock)->own);
Fl::repeat_timeout(*static_cast<TimerC *>(clock), cb_timer, clock);
}
void attach(Window &win)
{
own = &win;
Fl::add_timeout(time_interval, cb_timer, this);
}
operator double()
{
return time_interval;
}
};
struct window_timer : Window
{
public:
window_timer(Point xy, int w, int h, const std::string &title,
Point xy2, int radius, Point p1_1, Point p2_1, Point p1_2, Point p2_2, Point p1_3, Point p2_3);
private:
TimerC timerx;
Clock clockx;
void timer_go();
static void cb_timer(Address, Address addr) {
reference_to<window_timer>(addr).timer_go();
}
};
struct Clock : Shape
{
public:
Clock(Point xy, int radius, Point p1_1, Point p2_1, Point p1_2, Point p2_2, Point p1_3, Point p2_3);
void draw_lines() const;
void redraw();
void refresh();
int time_state(); // function to change the time state; supposed to be called on each clock tick
private:
Circle m_circle;
Line m_second_hand;
Line m_minute_hand;
Line m_hour_hand;
};
}
int main()
{
}
Graph_lib::window_timer::window_timer(Point xy, int w, int h, const std::string &title,
Point xy2, int radius, Point p1_1, Point p2_1, Point p1_2, Point p2_2, Point p1_3, Point p2_3)
: Window{ xy, w, h, title }, timerx{ 1.0, cb_timer },
clockx{ xy2, radius, Line{p1_1, p1_2}, Line{p1_2, p2_2}, Line{p1_3, p2_3} }
{
attach(timerx);
attach(clockx);
}
Graph_lib::Clock::Clock(Point xy, int radius, Point p1_1, Point p2_1, Point p1_2, Point p2_2, Point p1_3, Point p2_3)
: m_circle{ xy, radius }, m_second_hand{ p1_1, p2_1 }, m_minute_hand{ p1_2, p2_2 }, m_hour_hand{ p1_3, p2_3 }
{
draw_lines();
}
void Graph_lib::window_timer::timer_go()
{
using namespace std;
cout << "Clock Tick" << endl;
}
int Graph_lib::Clock::time_state()
{
// TODO
}
void Graph_lib::Clock::draw_lines() const
{
m_circle.draw_lines();
m_hour_hand.draw();
m_minute_hand.draw();
m_second_hand.draw();
}
|