QTfrontend/bgwidget.cpp
branchhedgeroid
changeset 6224 42b256eca362
parent 6055 88cfcd9161d3
parent 6223 cc3eb9b7230f
child 6226 3106add9a5bf
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2009 Kristian Lehmann <email@thexception.net>
       
     4  * Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 #include "bgwidget.h"
       
    21 
       
    22 SpritePosition::SpritePosition(QWidget * parent, int sh)
       
    23 {
       
    24     wParent = parent;
       
    25     iSpriteHeight = sh;
       
    26     reset();
       
    27 }
       
    28 
       
    29 SpritePosition::~SpritePosition()
       
    30 {
       
    31 }
       
    32 
       
    33 void SpritePosition::move()
       
    34 {
       
    35     fX += fXMov;
       
    36     fY += fYMov;
       
    37     iAngle += 4;
       
    38     if (iAngle >= 360) iAngle = 0;
       
    39     if (fY > wParent->height()) reset();
       
    40 }
       
    41 
       
    42 void SpritePosition::reset()
       
    43 {
       
    44     fY = -1 * iSpriteHeight;
       
    45     fX = (qrand() % ((int)(wParent->width() * 1.5))) - wParent->width()/2;
       
    46     fYMov = ((qrand() % 400)+300) / 100.0f;
       
    47     fXMov = fYMov * 0.2f+((qrand()%100)/100.0f * 0.6f); //so between 0.2 and 0.6, or 0.5 +/- 0.3
       
    48     iAngle = qrand() % 360;
       
    49 }
       
    50 
       
    51 QPoint SpritePosition::pos()
       
    52 {
       
    53     return QPoint((int)fX,(int)fY);
       
    54 }
       
    55 
       
    56 int SpritePosition::getAngle()
       
    57 {
       
    58     return iAngle;
       
    59 }
       
    60 
       
    61 void SpritePosition::init()
       
    62 {
       
    63     fY = qrand() % (wParent->height() + 1);
       
    64     fX = qrand() % (wParent->width() + 1);
       
    65 }
       
    66 
       
    67 BGWidget::BGWidget(QWidget * parent) : QWidget(parent)
       
    68 {
       
    69     setAttribute(Qt::WA_NoSystemBackground, true);
       
    70     sprite.load(":/res/Star.png");
       
    71 
       
    72     setAutoFillBackground(false);
       
    73 
       
    74     for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height());
       
    75 
       
    76     for (int i = 0; i < 360; i++)
       
    77     {
       
    78         rotatedSprites[i] = new QImage(sprite.width(), sprite.height(), QImage::Format_ARGB32);
       
    79         rotatedSprites[i]->fill(0);
       
    80 
       
    81         QPoint translate(sprite.width()/2, sprite.height()/2);
       
    82 
       
    83         QPainter p;
       
    84         p.begin(rotatedSprites[i]);
       
    85     //  p.setRenderHint(QPainter::Antialiasing);
       
    86         p.setRenderHint(QPainter::SmoothPixmapTransform);
       
    87         p.translate(translate.x(), translate.y());
       
    88         p.rotate(i);
       
    89         p.translate(-1*translate.x(), -1*translate.y());
       
    90         p.drawImage(0, 0, sprite);
       
    91     }
       
    92 
       
    93     timerAnimation = new QTimer();
       
    94     connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate()));
       
    95     timerAnimation->setInterval(ANIMATION_INTERVAL);
       
    96 }
       
    97 
       
    98 BGWidget::~BGWidget()
       
    99 {
       
   100     for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i];
       
   101     for (int i = 0; i < 360; i++) delete rotatedSprites[i];
       
   102     delete timerAnimation;
       
   103 }
       
   104 
       
   105 void BGWidget::paintEvent(QPaintEvent *event)
       
   106 {
       
   107     Q_UNUSED(event);
       
   108 
       
   109     QPainter p;
       
   110     p.begin(this);
       
   111     //p.setRenderHint(QPainter::Antialiasing);
       
   112     for (int i = 0; i < SPRITE_MAX; i++)
       
   113     {
       
   114         QPoint point = spritePositions[i]->pos();
       
   115         p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()]);
       
   116     }
       
   117     p.end();
       
   118 }
       
   119 
       
   120 void BGWidget::animate()
       
   121 {
       
   122     for (int i = 0; i < SPRITE_MAX; i++)
       
   123     {
       
   124         // 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
       
   125         update(spritePositions[i]->pos().x(),spritePositions[i]->pos().y(), sprite.width()+5, sprite.height()+5);
       
   126         spritePositions[i]->move();
       
   127     }
       
   128 }
       
   129 
       
   130 void BGWidget::startAnimation()
       
   131 {
       
   132     timerAnimation->start();
       
   133 }
       
   134 
       
   135 void BGWidget::stopAnimation()
       
   136 {
       
   137     timerAnimation->stop();
       
   138 }
       
   139 
       
   140 void BGWidget::init()
       
   141 {
       
   142     for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init();
       
   143 }