QTfrontend/bgwidget.cpp
changeset 2948 3f21a9dc93d0
parent 2377 f3fab2b09e0c
child 4560 5d6c7f88db73
equal deleted inserted replaced
2947:803b277e4894 2948:3f21a9dc93d0
    18 
    18 
    19 #include "bgwidget.h"
    19 #include "bgwidget.h"
    20 
    20 
    21 SpritePosition::SpritePosition(QWidget * parent, int sh)
    21 SpritePosition::SpritePosition(QWidget * parent, int sh)
    22 {
    22 {
    23 	wParent = parent;
    23     wParent = parent;
    24 	iSpriteHeight = sh;
    24     iSpriteHeight = sh;
    25 	reset();
    25     reset();
    26 }
    26 }
    27 
    27 
    28 SpritePosition::~SpritePosition()
    28 SpritePosition::~SpritePosition()
    29 {
    29 {
    30 }
    30 }
    31 
    31 
    32 void SpritePosition::move()
    32 void SpritePosition::move()
    33 {
    33 {
    34 	fX += fXMov;
    34     fX += fXMov;
    35 	fY += fYMov;
    35     fY += fYMov;
    36 	iAngle += 4;
    36     iAngle += 4;
    37 	if (iAngle >= 360) iAngle = 0;
    37     if (iAngle >= 360) iAngle = 0;
    38 	if (fY > wParent->height()) reset();
    38     if (fY > wParent->height()) reset();
    39 }
    39 }
    40 
    40 
    41 void SpritePosition::reset()
    41 void SpritePosition::reset()
    42 {
    42 {
    43 	fY = -1 * iSpriteHeight;
    43     fY = -1 * iSpriteHeight;
    44 	fX = (qrand() % ((int)(wParent->width() * 1.5))) - wParent->width()/2;
    44     fX = (qrand() % ((int)(wParent->width() * 1.5))) - wParent->width()/2;
    45 	fYMov = ((qrand() % 400)+300) / 100.0f;
    45     fYMov = ((qrand() % 400)+300) / 100.0f;
    46 	fXMov = fYMov * 0.5f;
    46     fXMov = fYMov * 0.5f;
    47 	iAngle = qrand() % 360;
    47     iAngle = qrand() % 360;
    48 }
    48 }
    49 
    49 
    50 QPoint SpritePosition::pos()
    50 QPoint SpritePosition::pos()
    51 {
    51 {
    52 	return QPoint((int)fX,(int)fY);
    52     return QPoint((int)fX,(int)fY);
    53 }
    53 }
    54 
    54 
    55 int SpritePosition::getAngle()
    55 int SpritePosition::getAngle()
    56 {
    56 {
    57 	return iAngle;
    57     return iAngle;
    58 }
    58 }
    59 
    59 
    60 void SpritePosition::init()
    60 void SpritePosition::init()
    61 {
    61 {
    62 	fY = qrand() % (wParent->height() + 1);
    62     fY = qrand() % (wParent->height() + 1);
    63 	fX = qrand() % (wParent->width() + 1);
    63     fX = qrand() % (wParent->width() + 1);
    64 }
    64 }
    65 
    65 
    66 BGWidget::BGWidget(QWidget * parent) : QWidget(parent)
    66 BGWidget::BGWidget(QWidget * parent) : QWidget(parent)
    67 {
    67 {
    68     setAttribute(Qt::WA_NoSystemBackground, true);
    68     setAttribute(Qt::WA_NoSystemBackground, true);
    69 	sprite.load(":/res/Star.png");
    69     sprite.load(":/res/Star.png");
    70 
    70 
    71 	setAutoFillBackground(false);
    71     setAutoFillBackground(false);
    72 
    72 
    73 	for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height());
    73     for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height());
    74 
    74 
    75 	for (int i = 0; i < 360; i++)
    75     for (int i = 0; i < 360; i++)
    76 	{
    76     {
    77 		rotatedSprites[i] = new QImage(sprite.width(), sprite.height(), QImage::Format_ARGB32);
    77         rotatedSprites[i] = new QImage(sprite.width(), sprite.height(), QImage::Format_ARGB32);
    78 		rotatedSprites[i]->fill(0);
    78         rotatedSprites[i]->fill(0);
    79 
    79 
    80 		QPoint translate(sprite.width()/2, sprite.height()/2);
    80         QPoint translate(sprite.width()/2, sprite.height()/2);
    81 
    81 
    82 		QPainter p;
    82         QPainter p;
    83 		p.begin(rotatedSprites[i]);
    83         p.begin(rotatedSprites[i]);
    84 	//	p.setRenderHint(QPainter::Antialiasing);
    84     //  p.setRenderHint(QPainter::Antialiasing);
    85 		p.setRenderHint(QPainter::SmoothPixmapTransform);
    85         p.setRenderHint(QPainter::SmoothPixmapTransform);
    86 		p.translate(translate.x(), translate.y());
    86         p.translate(translate.x(), translate.y());
    87 		p.rotate(i);
    87         p.rotate(i);
    88 		p.translate(-1*translate.x(), -1*translate.y());
    88         p.translate(-1*translate.x(), -1*translate.y());
    89 		p.drawImage(0, 0, sprite);
    89         p.drawImage(0, 0, sprite);
    90 	}
    90     }
    91 
    91 
    92 	timerAnimation = new QTimer();
    92     timerAnimation = new QTimer();
    93 	connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate()));
    93     connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate()));
    94 	timerAnimation->setInterval(ANIMATION_INTERVAL);
    94     timerAnimation->setInterval(ANIMATION_INTERVAL);
    95 }
    95 }
    96 
    96 
    97 BGWidget::~BGWidget()
    97 BGWidget::~BGWidget()
    98 {
    98 {
    99 	for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i];
    99     for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i];
   100 	for (int i = 0; i < 360; i++) delete rotatedSprites[i];
   100     for (int i = 0; i < 360; i++) delete rotatedSprites[i];
   101 	delete timerAnimation;
   101     delete timerAnimation;
   102 }
   102 }
   103 
   103 
   104 void BGWidget::paintEvent(QPaintEvent *event)
   104 void BGWidget::paintEvent(QPaintEvent *event)
   105 {
   105 {
   106 	QPainter p;
   106     QPainter p;
   107 	p.begin(this);
   107     p.begin(this);
   108 	//p.setRenderHint(QPainter::Antialiasing);
   108     //p.setRenderHint(QPainter::Antialiasing);
   109 	for (int i = 0; i < SPRITE_MAX; i++)
   109     for (int i = 0; i < SPRITE_MAX; i++)
   110 	{
   110     {
   111 		QPoint point = spritePositions[i]->pos();
   111         QPoint point = spritePositions[i]->pos();
   112 		p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()]);
   112         p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()]);
   113 	}
   113     }
   114 	p.end();
   114     p.end();
   115 }
   115 }
   116 
   116 
   117 void BGWidget::animate()
   117 void BGWidget::animate()
   118 {
   118 {
   119 	for (int i = 0; i < SPRITE_MAX; i++)
   119     for (int i = 0; i < SPRITE_MAX; i++)
   120 	{
   120     {
   121         // bottom edge of star *seems* clipped, but in fact, if I switch to just plain old repaint()/update() it is still clipped - artifact of transform?  As for 5, is arbitrary number. 4 was noticeably clipping, 5 seemed same as update() - I assume extra room is due to rotation and value really should be calculated proportional to width/height
   121         // bottom edge of star *seems* clipped, but in fact, if I switch to just plain old repaint()/update() it is still clipped - artifact of transform?  As for 5, is arbitrary number. 4 was noticeably clipping, 5 seemed same as update() - I assume extra room is due to rotation and value really should be calculated proportional to width/height
   122         update(spritePositions[i]->pos().x(),spritePositions[i]->pos().y(), sprite.width()+5, sprite.height()+5);
   122         update(spritePositions[i]->pos().x(),spritePositions[i]->pos().y(), sprite.width()+5, sprite.height()+5);
   123 		spritePositions[i]->move();
   123         spritePositions[i]->move();
   124 	}
   124     }
   125 }
   125 }
   126 
   126 
   127 void BGWidget::startAnimation()
   127 void BGWidget::startAnimation()
   128 {
   128 {
   129 	timerAnimation->start();
   129     timerAnimation->start();
   130 }
   130 }
   131 
   131 
   132 void BGWidget::stopAnimation()
   132 void BGWidget::stopAnimation()
   133 {
   133 {
   134 	timerAnimation->stop();
   134     timerAnimation->stop();
   135 }
   135 }
   136 
   136 
   137 void BGWidget::init()
   137 void BGWidget::init()
   138 {
   138 {
   139 	for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init();
   139     for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init();
   140 }
   140 }