#include <QtGui>
#include <QtCore>
const int N = 1000;
const int S = 500;
const int DT = 10;
const double R = 7;
{
public:
MyGraphicsItem(): _vel((double(qrand())/RAND_MAX-0.5)*S/10,(double(qrand())/RAND_MAX-0.5)*S/10),
_rect
(-R,
-R,
2*R,
2*R
), _color
(QColor::fromRgba(qrand
())) { setPos(double(qrand())/RAND_MAX*S,double(qrand())/RAND_MAX*S);
}
painter
->setRenderHint
(QPainter::Antialiasing,
true);
painter->setPen(Qt::NoPen);
painter->setBrush(_color);
painter->drawEllipse(_rect);
}
QRectF boundingRect
() const { return _rect;
} void step() {
setPos(pos() + _vel*double(DT)/1000);
if(pos().x() < 0 || pos().x() > S) _vel.setX(-_vel.x());
if(pos().y() < 0 || pos().y() > S) _vel.setY(-_vel.y());
}
protected:
};
{
public:
MyGraphicsScene
(): _startTime
(QTime::currentTime()), _framesCount
(0) { for(int i=0; i<N; ++i) addItem(new MyGraphicsItem);
startTimer(0);
}
protected:
static_cast<MyGraphicsItem*>(item)->step();
++_framesCount;
if(_framesCount == 50) {
qDebug
() <<
"FPS:" <<
double(_framesCount
) / _startTime.
msecsTo(QTime::currentTime()) * 1000;
_startTime
= QTime::currentTime();
_framesCount = 0;
}
}
int _framesCount;
};
int main(int argc, char *argv[])
{
MyGraphicsScene scene;
view.resize(S,S);
view.scale(0.95,0.95);
view.show();
return app.exec();
}
#include <QtGui>
#include <QtCore>
const int N = 1000;
const int S = 500;
const int DT = 10;
const double R = 7;
class MyGraphicsItem: public QGraphicsItem
{
public:
MyGraphicsItem(): _vel((double(qrand())/RAND_MAX-0.5)*S/10,(double(qrand())/RAND_MAX-0.5)*S/10),
_rect(-R,-R,2*R,2*R), _color(QColor::fromRgba(qrand())) {
setPos(double(qrand())/RAND_MAX*S,double(qrand())/RAND_MAX*S);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(Qt::NoPen);
painter->setBrush(_color);
painter->drawEllipse(_rect);
}
QRectF boundingRect() const { return _rect; }
void step() {
setPos(pos() + _vel*double(DT)/1000);
if(pos().x() < 0 || pos().x() > S) _vel.setX(-_vel.x());
if(pos().y() < 0 || pos().y() > S) _vel.setY(-_vel.y());
}
protected:
QPointF _vel;
QRectF _rect;
QColor _color;
};
class MyGraphicsScene: public QGraphicsScene
{
public:
MyGraphicsScene(): _startTime(QTime::currentTime()), _framesCount(0) {
for(int i=0; i<N; ++i) addItem(new MyGraphicsItem);
startTimer(0);
}
protected:
void timerEvent(QTimerEvent* event) {
foreach(QGraphicsItem* item, items())
static_cast<MyGraphicsItem*>(item)->step();
QApplication::processEvents();
++_framesCount;
if(_framesCount == 50) {
qDebug() << "FPS:" << double(_framesCount) / _startTime.msecsTo(QTime::currentTime()) * 1000;
_startTime = QTime::currentTime();
_framesCount = 0;
}
}
QTime _startTime;
int _framesCount;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyGraphicsScene scene;
QGraphicsView view(&scene);
view.setOptimizationFlags(QGraphicsView::DontClipPainter);
view.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
view.resize(S,S);
view.scale(0.95,0.95);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks