Skip to content

Commit aa261f3

Browse files
Karl Phillip BuhrKarl Phillip Buhr
Karl Phillip Buhr
authored and
Karl Phillip Buhr
committed
Added QtFPSvsTIMEanimation to the project
1 parent 3c936e2 commit aa261f3

13 files changed

+863
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
QT += widgets
2+
3+
SOURCES += \
4+
main.cpp \
5+
time_anim_improved.cpp \
6+
fps_canvas.cpp \
7+
fps_anim.cpp \
8+
time_anim.cpp \
9+
time_canvas.cpp
10+
11+
HEADERS += \
12+
time_anim_improved.h \
13+
time_anim.h \
14+
fps_canvas.h \
15+
fps_anim.h \
16+
time_canvas.h

QtFPSvsTIMEAnimation/aula15_animation.pro.user

Lines changed: 248 additions & 0 deletions
Large diffs are not rendered by default.

QtFPSvsTIMEAnimation/fps_anim.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "fps_anim.h"
2+
3+
#include <QTimer>
4+
5+
6+
FrameAnimation::FrameAnimation(QWidget* parent)
7+
: QWidget(parent)
8+
{
9+
_max_fps = 60;
10+
_counter = 0;
11+
12+
_black_canvas = new FpsCanvas((QFrame*)this);
13+
_blue_canvas = new FpsCanvas((QFrame*)this, Qt::blue);
14+
_red_canvas = new FpsCanvas((QFrame*)this, Qt::red);
15+
16+
_black_label = new QLabel(this);
17+
_black_label->setText("60 FPS");
18+
_blue_label = new QLabel(this);
19+
_blue_label->setText("30 FPS");
20+
_red_label = new QLabel(this);
21+
_red_label->setText("10 FPS");
22+
23+
_h_layout = new QGridLayout(this);
24+
_h_layout->addWidget(_black_canvas, 0, 0, 1, 1);
25+
_h_layout->addWidget(_blue_canvas, 0, 1, 1, 1);
26+
_h_layout->addWidget(_red_canvas, 0, 2, 1, 1);
27+
_h_layout->addWidget(_black_label, 3, 0, 1, 1);
28+
_h_layout->addWidget(_blue_label, 3, 1, 1, 1);
29+
_h_layout->addWidget(_red_label, 3, 2, 1, 1);
30+
31+
_group_box = new QGroupBox(this);
32+
_group_box->setTitle("Frame-based Animation");
33+
_group_box->setLayout(_h_layout);
34+
35+
_main_layout = new QVBoxLayout(this);
36+
_main_layout->addWidget(_group_box);
37+
setLayout(_main_layout);
38+
39+
// Start timer
40+
QTimer::singleShot(1000/_max_fps, this, SLOT(_tick()));
41+
}
42+
43+
void FrameAnimation::_tick()
44+
{
45+
/* Decide which canvas should be updated */
46+
47+
// 30 FPS canvas must be draw once every 2x
48+
if (_counter % 2 == 0)
49+
_blue_canvas->draw();
50+
51+
// 10 FPS canvas must be draw once every 6x
52+
if (_counter % 10 == 0)
53+
_red_canvas->draw();
54+
55+
_counter++;
56+
_counter = _counter % 60; // Ranges from 0 to 59
57+
58+
// 60 FPS canvas must be draw every time, so we ignore _counter
59+
_black_canvas->draw();
60+
61+
// Reset the timer
62+
QTimer::singleShot(1000/_max_fps, this, SLOT(_tick()));
63+
}
64+
65+
void FrameAnimation::reset()
66+
{
67+
_black_canvas->reset();
68+
_blue_canvas->reset();
69+
_red_canvas->reset();
70+
}

QtFPSvsTIMEAnimation/fps_anim.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include "fps_canvas.h"
3+
4+
#include <QHBoxLayout>
5+
#include <QWidget>
6+
#include <QGroupBox>
7+
#include <QLabel>
8+
9+
10+
class FrameAnimation : public QWidget
11+
{
12+
Q_OBJECT
13+
public:
14+
FrameAnimation(QWidget* parent = NULL);
15+
16+
public slots:
17+
void reset();
18+
19+
private slots:
20+
void _tick();
21+
22+
private:
23+
QGroupBox* _group_box;
24+
QVBoxLayout* _main_layout;
25+
QGridLayout* _h_layout;
26+
FpsCanvas* _black_canvas, *_blue_canvas, *_red_canvas;
27+
QLabel* _black_label, *_blue_label, *_red_label;
28+
int _max_fps;
29+
int _counter;
30+
};

QtFPSvsTIMEAnimation/fps_canvas.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "fps_canvas.h"
2+
3+
#include <QPainter>
4+
#include <QDebug>
5+
6+
7+
FpsCanvas::FpsCanvas(QFrame* parent, const QColor color)
8+
: QFrame(parent), _color(color)
9+
{
10+
// Initialize class members
11+
_x = _y = 50;
12+
_dx = 2;
13+
_dy = 1;
14+
_sq_sz = 10;
15+
16+
// Set size of the canvas
17+
setMinimumSize(175, 175);
18+
}
19+
20+
void FpsCanvas::paintEvent(QPaintEvent* event)
21+
{
22+
QFrame::paintEvent(event);
23+
24+
QPainter painter(this);
25+
26+
// Paint white background
27+
painter.fillRect(0, 0, width(), height(), Qt::white);
28+
29+
// Paint black frame around the canvas
30+
painter.setPen(Qt::black);
31+
painter.drawRect(0, 0, width()-1, height()-1);
32+
33+
/* Move square */
34+
35+
_x += _dx;
36+
_y += _dy;
37+
38+
if ( _x <= 0 || (_x >= (width()-1) - _sq_sz) )
39+
_dx = -_dx;
40+
41+
if ( _y <= 0 || (_y >= (height()-1) - _sq_sz) )
42+
_dy = -_dy;
43+
44+
// Draw square
45+
painter.fillRect(_x, _y, _sq_sz, _sq_sz, _color);
46+
}
47+
48+
void FpsCanvas::draw()
49+
{
50+
// Trigger paintEvent()
51+
repaint();
52+
}
53+
54+
void FpsCanvas::reset()
55+
{
56+
_x = _y = 50;
57+
_dx = 2;
58+
_dy = 1;
59+
}

QtFPSvsTIMEAnimation/fps_canvas.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include <QFrame>
3+
4+
5+
class FpsCanvas : public QFrame
6+
{
7+
Q_OBJECT
8+
public:
9+
FpsCanvas(QFrame* parent = NULL, const QColor color = Qt::black);
10+
11+
void draw();
12+
void reset();
13+
14+
void paintEvent(QPaintEvent* event);
15+
16+
private:
17+
QColor _color;
18+
int _sq_sz;
19+
int _x, _y;
20+
int _dx, _dy;
21+
};

QtFPSvsTIMEAnimation/main.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* This application is based on the following tutorial by Steven Lambert:
2+
* "Why You Should be Using Time-based Animation and How to Implement it"
3+
* http://blog.sklambert.com/using-time-based-animation-implement/
4+
*/
5+
#include "fps_anim.h"
6+
#include "time_anim.h"
7+
#include "time_anim_improved.h"
8+
9+
#include <QApplication>
10+
#include <QMainWindow>
11+
#include <QPushButton>
12+
#include <QVBoxLayout>
13+
14+
15+
int main(int argc, char* argv[])
16+
{
17+
QApplication app(argc, argv);
18+
19+
QWidget window;
20+
window.setWindowTitle("Frame-based VS Time-based Animation");
21+
22+
QPushButton reset_bt(&window);
23+
reset_bt.setText("Reset");
24+
25+
FrameAnimation fps_widget(&window);
26+
QObject::connect(&reset_bt, SIGNAL(clicked()), &fps_widget, SLOT(reset()));
27+
28+
TimeAnimation time_widget(&window);
29+
QObject::connect(&reset_bt, SIGNAL(clicked()), &time_widget, SLOT(reset()));
30+
31+
TimeAnimationImproved time_imp_widget(&window);
32+
QObject::connect(&reset_bt, SIGNAL(clicked()), &time_imp_widget, SLOT(reset()));
33+
34+
QVBoxLayout main_layout(&window);
35+
main_layout.setAlignment(Qt::AlignCenter);
36+
main_layout.addWidget(&reset_bt, 0, Qt::AlignRight);
37+
main_layout.addWidget(&fps_widget);
38+
main_layout.addWidget(&time_widget);
39+
main_layout.addWidget(&time_imp_widget);
40+
41+
window.setLayout(&main_layout);
42+
window.show();
43+
44+
return app.exec();
45+
}

QtFPSvsTIMEAnimation/time_anim.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "time_anim.h"
2+
3+
#include <qDebug>
4+
5+
6+
TimeAnimation::TimeAnimation(QWidget* parent)
7+
: QWidget(parent)
8+
{
9+
_max_fps = 60;
10+
_counter = 0;
11+
_last_time_60fps = _last_time_30fps = _last_time_10fps = QDateTime::currentMSecsSinceEpoch();
12+
13+
_black_canvas = new TimeCanvas((QFrame*)this, Qt::black);
14+
_blue_canvas = new TimeCanvas((QFrame*)this, Qt::blue);
15+
_red_canvas = new TimeCanvas((QFrame*)this, Qt::red);
16+
17+
_black_label = new QLabel(this);
18+
_black_label->setText("60 FPS");
19+
_blue_label = new QLabel(this);
20+
_blue_label->setText("30 FPS");
21+
_red_label = new QLabel(this);
22+
_red_label->setText("10 FPS");
23+
24+
_h_layout = new QGridLayout(this);
25+
_h_layout->addWidget(_black_canvas, 0, 0, 1, 1);
26+
_h_layout->addWidget(_blue_canvas, 0, 1, 1, 1);
27+
_h_layout->addWidget(_red_canvas, 0, 2, 1, 1);
28+
_h_layout->addWidget(_black_label, 3, 0, 1, 1);
29+
_h_layout->addWidget(_blue_label, 3, 1, 1, 1);
30+
_h_layout->addWidget(_red_label, 3, 2, 1, 1);
31+
32+
_group_box = new QGroupBox(this);
33+
_group_box->setTitle("Time-based Animation");
34+
_group_box->setLayout(_h_layout);
35+
36+
_main_layout = new QVBoxLayout(this);
37+
_main_layout->addWidget(_group_box);
38+
setLayout(_main_layout);
39+
40+
// Start timer
41+
QTimer::singleShot(1000/_max_fps, this, SLOT(_tick()));
42+
}
43+
44+
// TODO: study how to remove cur_time_30fps
45+
void TimeAnimation::_tick()
46+
{
47+
/* Decide which canvas should be updated */
48+
49+
qint64 now = QDateTime::currentMSecsSinceEpoch();
50+
51+
// 30 FPS canvas must be draw once every 2x
52+
if (_counter % 2 == 0)
53+
{
54+
// Compute how much time passed since the last update
55+
qint64 passed = now - _last_time_30fps;
56+
_last_time_30fps = now;
57+
58+
_blue_canvas->move(passed);
59+
_blue_canvas->draw();
60+
}
61+
62+
// 10 FPS canvas must be draw once every 6x
63+
if (_counter % 10 == 0)
64+
{
65+
// Compute how much time passed since the last update
66+
qint64 passed = now - _last_time_10fps;
67+
_last_time_10fps = now;
68+
69+
_red_canvas->move(passed);
70+
_red_canvas->draw();
71+
}
72+
73+
// 60 FPS canvas must be draw every time, so we ignore _counter
74+
{
75+
// Compute how much time passed since the last update
76+
qint64 passed = now - _last_time_60fps;
77+
_last_time_60fps = now;
78+
79+
_black_canvas->move(passed);
80+
_black_canvas->draw();
81+
}
82+
83+
++_counter;
84+
_counter = _counter % 60; // Ranges from 0 to 59
85+
86+
// Reset the timer
87+
QTimer::singleShot(1000/_max_fps, this, SLOT(_tick()));
88+
}
89+
90+
void TimeAnimation::reset()
91+
{
92+
_black_canvas->reset();
93+
_blue_canvas->reset();
94+
_red_canvas->reset();
95+
}

QtFPSvsTIMEAnimation/time_anim.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
#include "time_canvas.h"
3+
4+
#include <QHBoxLayout>
5+
#include <QWidget>
6+
#include <QGroupBox>
7+
#include <QLabel>
8+
9+
10+
class TimeAnimation : public QWidget
11+
{
12+
Q_OBJECT
13+
public:
14+
TimeAnimation(QWidget* parent = NULL);
15+
16+
public slots:
17+
void reset();
18+
19+
private slots:
20+
void _tick();
21+
22+
private:
23+
QGroupBox* _group_box;
24+
QVBoxLayout* _main_layout;
25+
QGridLayout* _h_layout;
26+
TimeCanvas* _black_canvas, *_blue_canvas, *_red_canvas;
27+
QLabel* _black_label, *_blue_label, *_red_label;
28+
qint64 _last_time_60fps, _last_time_30fps, _last_time_10fps;
29+
int _max_fps;
30+
int _counter;
31+
};

0 commit comments

Comments
 (0)