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