forked from karlphillip/GraphicsProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_canvas.cpp
More file actions
91 lines (73 loc) · 2.41 KB
/
time_canvas.cpp
File metadata and controls
91 lines (73 loc) · 2.41 KB
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
/* Copyright (C) 2014 Karl Phillip Buhr <karlphillip@gmail.com>
*
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
* To view a copy of this license, visit:
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
*
* Or to read the human-readable summary of the license:
* https://creativecommons.org/licenses/by-sa/2.5/
*/
#include "time_canvas.h"
#include <cmath>
#include <QPainter>
#include <QDebug>
TimeCanvas::TimeCanvas(QFrame* parent, const QColor color)
: QFrame(parent), _color(color)
{
// Initialize class members
_x = _y = 50;
_dx = 2;
_dy = 1;
_sq_sz = 10;
// Set canvas size
setMinimumSize(175, 175);
}
void TimeCanvas::move(float dt)
{
/* Update position of square */
/* Rounding _dx up/down can affect the horizontal movement of the square.
* Rounding down when _dx is negative is essential to fix the slowness
* that happens when going from right to left.
*/
if (_dx > 0)
_x += std::ceil((_dx * dt * 60) / 1000); // (2 * 16 * 60) / 1000 = 1.92 = 2
else
_x += std::floor((_dx * dt * 60) / 1000); // (-2 * 16 * 60) / 1000 = -1.92 = -2
/* Rounding _dy up/down can also affect the vertical move of the square.
* If _dy is positive, round up, else the square will move sideways only.
* If _dy is negative, round down, else the square won't go up after colliding.
*/
if (_dy > 0)
_y += std::ceil((_dy * dt * 60) / 1000); // (1 * 16 * 60) / 1000 = 0.96 = 1
else
_y += std::floor((_dy * dt * 60) / 1000); // (-1 * 16 * 60) / 1000 = -0.96 = -1
if ( _x <= 0 || (_x >= (width()-1) - _sq_sz) ) {
_dx *= -1;
}
if ( _y <= 0 || (_y >= (height()-1) - _sq_sz) ) {
_dy *= -1;
}
}
void TimeCanvas::draw()
{
// Trigger paintEvent()
repaint();
}
void TimeCanvas::paintEvent(QPaintEvent* event)
{
QFrame::paintEvent(event);
/* Paint white background */
QPainter painter(this);
painter.fillRect(0, 0, width(), height(), Qt::white);
/* Paint black frame around the canvas */
painter.setPen(Qt::black);
painter.drawRect(0, 0, width()-1, height()-1);
/* Draw square */
painter.fillRect(_x, _y, _sq_sz, _sq_sz, _color);
}
void TimeCanvas::reset()
{
_x = _y = 50;
_dx = 2;
_dy = 1;
}