changes in arhitecture, some improvments, much optimising and simplify,

many bagfixes and new graphics engine
This commit is contained in:
blizer
2011-08-20 07:34:19 +04:00
committed by unknown
parent 0a9679fd99
commit f3d0ca7101
33 changed files with 1444 additions and 897 deletions

View File

@@ -1,21 +1,23 @@
#include "ad_graphics.h"
#include <QGLWidget>
#include <QGraphicsItem>
AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(parent)
{
data = adcore->addata;
core = adcore;
//scale(cellSize, cellSize);
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
setAlignment(Qt::AlignLeft | Qt::AlignTop);
scene = new QGraphicsScene();
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
core->setScene(scene);
setScene(scene);
//setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
//setCacheMode();
//setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
mapimg = new QImage(data->map->rect().size()*cellSize,QImage::Format_ARGB32);
//setDragMode(RubberBandDrag);
//gsw = new QGraphicsWidget();
//scene->addWidget(new QPushButton());
QPainter p(mapimg);
for (int i=0; i<data->map->cells().size(); i++) {
for (int j=0; j<data->map->cells().at(i).size(); j++) {
@@ -30,14 +32,6 @@ AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(paren
pen.setColor(Qt::black);
brush.setColor(Qt::black);
}
// if (cel <= Map::PlayerTower && cel !=Map::Wall) {
// pen.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255));
// brush.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255));
// }
// if (cel >= Map::PlayerAlien) {
// pen.setColor(Qt::yellow);
// brush.setColor(Qt::yellow);
// }
if (cel >= Map::Free || (cel <= Map::PlayerTower && cel !=Map::Wall))
{
pen.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,150));
@@ -48,152 +42,82 @@ AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(paren
p.drawRect(i*cellSize,j*cellSize,cellSize,cellSize);
}
}
startTimer(50);
show();
p.end();
scene->setSceneRect(mapimg->rect());
scene->setBackgroundBrush(*mapimg);
setCacheMode(QGraphicsView::CacheBackground);
resize(mapimg->size());
// startTimer(50);
}
AD_Graphics::~AD_Graphics()
{
//delete gsw;
delete scene;
}
void AD_Graphics::mouseDoubleClickEvent(QMouseEvent *)
{
core->nextWave();
}
void AD_Graphics::mousePressEvent(QMouseEvent *event)
{
scene->clearSelection();
if (event->button() == Qt::LeftButton)
core->adtowers->addTower(0,1,(QPointF(event->pos())/cellSize-QPointF(0.5,0.5)).toPoint());
{
if (m_building) emit add_tow((QPointF(event->pos())/cellSize).toPoint());
else
{
QList<QGraphicsItem*> list = scene->items(event->pos(), Qt::IntersectsItemBoundingRect, Qt::AscendingOrder);
ADItem * al = 0;
foreach(QGraphicsItem* x, list)
{
ADItem * i = qgraphicsitem_cast<ADItem*>(x);
if (i->itemType() == ADItem::Tower)
{
i->setSelected(true);
emit tower_select(i->tid());
return;
}
if (i->itemType() == ADItem::Alien)
{
al = i;
}
}
if (al != 0)
{
al->setSelected(true);
emit alien_select(al->id());
return;
}
}
}
if (event->button() == Qt::RightButton)
core->adtowers->addTower(0,0,(QPointF(event->pos())/cellSize-QPointF(0.5,0.5)).toPoint());
if (event->button() == Qt::MidButton)
core->adtowers->delTower((QPointF(event->pos())/cellSize-QPointF(0.5,0.5)).toPoint());
emit cancel();
}
void AD_Graphics::timerEvent(QTimerEvent * )
{
//qDebug() << data->curAliens.size();
draw();
}
//void AD_Graphics::timerEvent(QTimerEvent * )
//{
// scene->update();
//}
void AD_Graphics::drawBackground(QPainter * p, const QRectF & )
{
p->drawImage(0,0,*mapimg);
// for (int i=0; i<data->map->cells().size(); i++) {
// for (int j=0; j<data->map->cells().at(i).size(); j++) {
// QPen pen;
// QBrush brush;
// pen.setColor(Qt::white);
// brush.setColor(Qt::white);
// brush.setStyle(Qt::SolidPattern);
// int cel = data->map->cells().at(i).at(j);
// if (cel == Map::Wall)
// {
// pen.setColor(Qt::black);
// brush.setColor(Qt::black);
// }
// // if (cel <= Map::PlayerTower && cel !=Map::Wall) {
// // pen.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255));
// // brush.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255));
// // }
// // if (cel >= Map::PlayerAlien) {
// // pen.setColor(Qt::yellow);
// // brush.setColor(Qt::yellow);
// // }
// if (cel >= Map::Free || (cel <= Map::PlayerTower && cel !=Map::Wall))
// {
// pen.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,50));
// brush.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,50));
// }
// p->setPen(pen);
// p->setBrush(brush);
// p->drawRect(i*cellSize,j*cellSize,cellSize,cellSize);
// }
// }
//qDebug() << "t:" << data->curTowers.size();
for (QHash<QPoint, TowerType>::iterator i = data->curTowers.begin(); i != data->curTowers.end(); ++i)
{
//qDebug() << "draw alien";
//p->rotate(-90);
//p->scale(cellSize, cellSize);
//p->translate(rec.width()/2, rec.height()/2);
//p->rotate(-90);
//p->translate(-rec.height()/2, -rec.width()/2);
p->translate((*i).pos * cellSize+QPointF(20,20));
p->rotate((*i).angle);
//p->setPen(QColor(0,0,0,0));
//p->setBrush(brush);
//p->drawRect(QRect(-10,-10,20,20));
if ((*i).build < data->players.at((*i).PlayerId)->srcTowers.at((*i).src).buildTime)
{
QBrush brush;
brush.setColor(Qt::darkRed);
brush.setStyle(Qt::DiagCrossPattern);
p->setBrush(brush);
p->drawRect(-20,-20,40,40);
} else {
p->drawImage(QRect(-20,-20,40,40), *data->curAnimations.at((*i).imgType).images.at((*i).animIndex));
}
p->resetTransform();
}
//qDebug() << "a:" << data->curAliens.size();
for (QHash<int, AlienType>::iterator i = data->curAliens.begin(); i != data->curAliens.end(); ++i)
{
//qDebug() << "draw alien";
//p->rotate(-90);
//p->scale(cellSize, cellSize);
//p->translate(rec.width()/2, rec.height()/2);
//p->rotate(-90);
//p->translate(-rec.height()/2, -rec.width()/2);
p->translate((*i).pos * cellSize+QPointF(10,10));
p->rotate((*i).angle);
p->drawImage(QRect(-20,-20,40,40), *data->curAnimations.at((*i).imgType).images.at((*i).animIndex));
(*i).animIndex++;
if ((*i).animIndex >= data->curAnimations.at((*i).imgType).images.size())
(*i).animIndex = 0;
p->resetTransform();
}
//qDebug() << "s:" << data->curSplashes.size();
for (QHash<int, SplashType>::iterator i = data->curSplashes.begin(); i != data->curSplashes.end(); ++i)
{
//qDebug() << "draw alien";
//p->rotate(-90);
//p->scale(cellSize, cellSize);
//p->translate(rec.width()/2, rec.height()/2);
//p->rotate(-90);
//p->translate(-rec.height()/2, -rec.width()/2);
//QBrush brush;
//brush.setColor(Qt::red);
//brush.setStyle(Qt::SolidPattern);
p->translate((*i).pos * cellSize);
p->rotate((*i).angle);
//p->setBrush(brush);
//p->setPen(QColor(0,0,0,0));
//p->drawEllipse(QPoint(),5,5);
QImage img;
img = *data->curAnimations.at((*i).imgType).images.at((*i).animIndex);
p->drawImage(QPoint(-(img.size().width()/2),-(img.size().height()/2)), img);
(*i).animIndex++;
if ((*i).animIndex >= data->curAnimations.at((*i).imgType).images.size())
(*i).animIndex = 0;
p->resetTransform();
}
p->drawImage(0,0,*mapimg);
}
void AD_Graphics::draw()
void AD_Graphics::drawForeground(QPainter *p, const QRectF &rect)
{
scene->update();
//resetCachedContent();
//repaint();
QList <QGraphicsItem *> l = scene->items();
foreach(QGraphicsItem * g,l)
{
ADItem * i = qgraphicsitem_cast<ADItem *>(g);
if (i->isBarVisible())
{
p->setPen(Qt::black);
p->setBrush(QBrush(QColor(qRound(255*(1-i->value())),qRound(255*i->value()),0)));
p->drawRect(i->pos().x()-cellSize,i->pos().y()-cellSize*1.25,cellSize*2*i->value(),cellSize/4);
}
}
}