QTfrontend/ui/widget/bgwidget.cpp
author sheepluva
Sun, 30 Oct 2011 15:31:30 +0100
changeset 6241 e2c7771162ae
parent 6060 fdfc01419815
child 6242 27297c421bbc
permissions -rw-r--r--
frontend flakes: * good-bye clipping * also "respawn" flakes when leaving window to the right (instead just the botton) + flakes rotate in 4 degree steps, so let's only generate 90 animation frames instead of 90
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     1
/*
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Kristian Lehmann <email@thexception.net>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4560
diff changeset
     4
 * Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com>
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     5
 *
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
     9
 *
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    13
 * GNU General Public License for more details.
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    14
 *
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    18
 */
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    19
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    20
#include "bgwidget.h"
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    21
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    22
SpritePosition::SpritePosition(QWidget * parent, int sh)
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    23
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    24
    wParent = parent;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    25
    iSpriteHeight = sh;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    26
    reset();
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    27
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    28
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    29
SpritePosition::~SpritePosition()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    30
{
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    31
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    32
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    33
void SpritePosition::move()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    34
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    35
    fX += fXMov;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    36
    fY += fYMov;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    37
    iAngle += 4;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    38
    if (iAngle >= 360) iAngle = 0;
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
    39
    if ((fX - fXMov) > wParent->width()) reset();
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
    40
    else if ((fY - fYMov) > wParent->height()) reset();
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    41
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    42
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    43
void SpritePosition::reset()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    44
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    45
    fY = -1 * iSpriteHeight;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    46
    fX = (qrand() % ((int)(wParent->width() * 1.5))) - wParent->width()/2;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    47
    fYMov = ((qrand() % 400)+300) / 100.0f;
5113
d92b58d100ae background stars won't go all in the same direction anymore
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
    48
    fXMov = fYMov * 0.2f+((qrand()%100)/100.0f * 0.6f); //so between 0.2 and 0.6, or 0.5 +/- 0.3
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    49
    iAngle = qrand() % 360;
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    50
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    51
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    52
QPoint SpritePosition::pos()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    53
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    54
    return QPoint((int)fX,(int)fY);
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    55
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    56
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    57
int SpritePosition::getAngle()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    58
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    59
    return iAngle;
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    60
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    61
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    62
void SpritePosition::init()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    63
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    64
    fY = qrand() % (wParent->height() + 1);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    65
    fX = qrand() % (wParent->width() + 1);
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    66
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    67
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    68
BGWidget::BGWidget(QWidget * parent) : QWidget(parent)
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    69
{
2071
0faa147c47df Still trying to improve perf of stars - this seems to help a little bit
nemo
parents: 2069
diff changeset
    70
    setAttribute(Qt::WA_NoSystemBackground, true);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    71
    sprite.load(":/res/Star.png");
2377
f3fab2b09e0c And in frontend
nemo
parents: 2071
diff changeset
    72
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    73
    setAutoFillBackground(false);
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    74
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    75
    for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height());
2377
f3fab2b09e0c And in frontend
nemo
parents: 2071
diff changeset
    76
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
    77
    for (int i = 0; i < 90; i++)
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    78
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    79
        rotatedSprites[i] = new QImage(sprite.width(), sprite.height(), QImage::Format_ARGB32);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    80
        rotatedSprites[i]->fill(0);
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    81
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    82
        QPoint translate(sprite.width()/2, sprite.height()/2);
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    83
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    84
        QPainter p;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    85
        p.begin(rotatedSprites[i]);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    86
    //  p.setRenderHint(QPainter::Antialiasing);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    87
        p.setRenderHint(QPainter::SmoothPixmapTransform);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    88
        p.translate(translate.x(), translate.y());
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
    89
        p.rotate(4 * i);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    90
        p.translate(-1*translate.x(), -1*translate.y());
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    91
        p.drawImage(0, 0, sprite);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    92
    }
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    93
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    94
    timerAnimation = new QTimer();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    95
    connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate()));
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
    96
    timerAnimation->setInterval(ANIMATION_INTERVAL);
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    97
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    98
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
    99
BGWidget::~BGWidget()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   100
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   101
    for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i];
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   102
    for (int i = 0; i < 90; i++) delete rotatedSprites[i];
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   103
    delete timerAnimation;
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   104
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   105
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   106
void BGWidget::paintEvent(QPaintEvent *event)
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   107
{
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 2948
diff changeset
   108
    Q_UNUSED(event);
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 2948
diff changeset
   109
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   110
    QPainter p;
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   111
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   112
    p.begin(this);
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   113
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   114
    for (int i = 0; i < SPRITE_MAX; i++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   115
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   116
        QPoint point = spritePositions[i]->pos();
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   117
        p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()/4]);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   118
    }
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   119
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   120
    p.end();
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   121
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   122
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   123
void BGWidget::animate()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   124
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   125
    for (int i = 0; i < SPRITE_MAX; i++)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   126
    {
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   127
        QPoint oldPos = spritePositions[i]->pos();
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   128
        spritePositions[i]->move();
6241
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   129
        QPoint newPos = spritePositions[i]->pos();
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   130
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   131
        int xdiff = newPos.x() - oldPos.x();
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   132
        int ydiff = newPos.y() - oldPos.y();
e2c7771162ae frontend flakes:
sheepluva
parents: 6060
diff changeset
   133
        update(oldPos.x(), oldPos.y(), xdiff+sprite.width(), ydiff+sprite.height());
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   134
    }
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   135
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   136
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   137
void BGWidget::startAnimation()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   138
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   139
    timerAnimation->start();
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   140
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   141
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   142
void BGWidget::stopAnimation()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   143
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   144
    timerAnimation->stop();
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   145
}
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   146
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   147
void BGWidget::init()
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   148
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2377
diff changeset
   149
    for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init();
2012
76fff564246b TheXception's patch:
unc0rr
parents:
diff changeset
   150
}