author | koda |
Mon, 31 May 2010 19:33:42 +0000 | |
changeset 3487 | b1d00f1950c8 |
parent 2948 | 3f21a9dc93d0 |
child 4560 | 5d6c7f88db73 |
permissions | -rw-r--r-- |
2012 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2009 Kristian Lehmann <email@thexception.net> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include "bgwidget.h" |
|
20 |
||
21 |
SpritePosition::SpritePosition(QWidget * parent, int sh) |
|
22 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
23 |
wParent = parent; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
24 |
iSpriteHeight = sh; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
25 |
reset(); |
2012 | 26 |
} |
27 |
||
28 |
SpritePosition::~SpritePosition() |
|
29 |
{ |
|
30 |
} |
|
31 |
||
32 |
void SpritePosition::move() |
|
33 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
34 |
fX += fXMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
35 |
fY += fYMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
36 |
iAngle += 4; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
37 |
if (iAngle >= 360) iAngle = 0; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
38 |
if (fY > wParent->height()) reset(); |
2012 | 39 |
} |
40 |
||
41 |
void SpritePosition::reset() |
|
42 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
43 |
fY = -1 * iSpriteHeight; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
44 |
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
|
45 |
fYMov = ((qrand() % 400)+300) / 100.0f; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
46 |
fXMov = fYMov * 0.5f; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
47 |
iAngle = qrand() % 360; |
2012 | 48 |
} |
49 |
||
50 |
QPoint SpritePosition::pos() |
|
51 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
52 |
return QPoint((int)fX,(int)fY); |
2012 | 53 |
} |
54 |
||
55 |
int SpritePosition::getAngle() |
|
56 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
57 |
return iAngle; |
2012 | 58 |
} |
59 |
||
60 |
void SpritePosition::init() |
|
61 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
62 |
fY = qrand() % (wParent->height() + 1); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
63 |
fX = qrand() % (wParent->width() + 1); |
2012 | 64 |
} |
65 |
||
66 |
BGWidget::BGWidget(QWidget * parent) : QWidget(parent) |
|
67 |
{ |
|
2071
0faa147c47df
Still trying to improve perf of stars - this seems to help a little bit
nemo
parents:
2069
diff
changeset
|
68 |
setAttribute(Qt::WA_NoSystemBackground, true); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
69 |
sprite.load(":/res/Star.png"); |
2377 | 70 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
71 |
setAutoFillBackground(false); |
2012 | 72 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
73 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height()); |
2377 | 74 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
75 |
for (int i = 0; i < 360; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
76 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
77 |
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
|
78 |
rotatedSprites[i]->fill(0); |
2012 | 79 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
80 |
QPoint translate(sprite.width()/2, sprite.height()/2); |
2012 | 81 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
82 |
QPainter p; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
83 |
p.begin(rotatedSprites[i]); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
84 |
// p.setRenderHint(QPainter::Antialiasing); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
85 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
86 |
p.translate(translate.x(), translate.y()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
87 |
p.rotate(i); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
88 |
p.translate(-1*translate.x(), -1*translate.y()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
89 |
p.drawImage(0, 0, sprite); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
90 |
} |
2012 | 91 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
92 |
timerAnimation = new QTimer(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
93 |
connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate())); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
94 |
timerAnimation->setInterval(ANIMATION_INTERVAL); |
2012 | 95 |
} |
96 |
||
97 |
BGWidget::~BGWidget() |
|
98 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
99 |
for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i]; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
100 |
for (int i = 0; i < 360; i++) delete rotatedSprites[i]; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
101 |
delete timerAnimation; |
2012 | 102 |
} |
103 |
||
104 |
void BGWidget::paintEvent(QPaintEvent *event) |
|
105 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
106 |
QPainter p; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
107 |
p.begin(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
108 |
//p.setRenderHint(QPainter::Antialiasing); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
109 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
110 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
111 |
QPoint point = spritePositions[i]->pos(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
112 |
p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()]); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
113 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
114 |
p.end(); |
2012 | 115 |
} |
116 |
||
117 |
void BGWidget::animate() |
|
118 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
119 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
120 |
{ |
2069
f7fad4ad4455
update only the stars - cuts CPU useage in half on my machine
nemo
parents:
2012
diff
changeset
|
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 |
f7fad4ad4455
update only the stars - cuts CPU useage in half on my machine
nemo
parents:
2012
diff
changeset
|
122 |
update(spritePositions[i]->pos().x(),spritePositions[i]->pos().y(), sprite.width()+5, sprite.height()+5); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
123 |
spritePositions[i]->move(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
124 |
} |
2012 | 125 |
} |
126 |
||
127 |
void BGWidget::startAnimation() |
|
128 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
129 |
timerAnimation->start(); |
2012 | 130 |
} |
131 |
||
132 |
void BGWidget::stopAnimation() |
|
133 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
134 |
timerAnimation->stop(); |
2012 | 135 |
} |
136 |
||
137 |
void BGWidget::init() |
|
138 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
139 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init(); |
2012 | 140 |
} |