Commit 19f841a0 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

No need to rerender whole background just to update a preview.

Invent PreviewItem for this case. Much faster now :).

/me is back from vacation and still catching up with email. Hi, guys!

svn path=/trunk/KDE/kdegames/klines/; revision=665266
parent 76b26d64
......@@ -11,6 +11,7 @@ set(klines_SRCS
scene.cpp
renderer.cpp
ballitem.cpp
previewitem.cpp
animator.cpp)
......
/*******************************************************************
*
* Copyright 2007 Dmitry Suzdalev <dimsuz@gmail.com>
*
* This file is part of the KDE project "KLines"
*
* KLines is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* KLines is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KLines; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#include "previewitem.h"
#include "renderer.h"
#include <QPainter>
PreviewItem::PreviewItem( QGraphicsScene* scene )
: QGraphicsItem( 0, scene )
{
}
void PreviewItem::setPreviewColors( const QList<BallColor>& colors )
{
if( colors.isEmpty() )
return;
m_pixmap = KLinesRenderer::self()->previewPixmap();
QPainter p( &m_pixmap );
int ballHeight = KLinesRenderer::self()->backgroundTilePixmap().height();
for(int i=0; i < 3; i++)
p.drawPixmap( 2, i*ballHeight,
KLinesRenderer::self()->ballPixmap( colors.at(i) ) );
p.end();
update();
}
void PreviewItem::paint( QPainter* p, const QStyleOptionGraphicsItem* option, QWidget* widget )
{
p->drawPixmap( 0, 0, m_pixmap );
}
QRectF PreviewItem::boundingRect() const
{
return m_pixmap.rect();
}
/*******************************************************************
*
* Copyright 2007 Dmitry Suzdalev <dimsuz@gmail.com>
*
* This file is part of the KDE project "KLines"
*
* KLines is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* KLines is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KLines; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
********************************************************************/
#ifndef PREVIEWITEM_H
#define PREVIEWITEM_H
#include <QGraphicsItem>
#include "commondefs.h"
class PreviewItem : public QGraphicsItem
{
public:
PreviewItem( QGraphicsScene* scene );
/**
* Sets colors to be displayed
*/
void setPreviewColors( const QList<BallColor>& colors );
// reimplemented functions
virtual void paint( QPainter* p, const QStyleOptionGraphicsItem* option, QWidget* widget = 0 );
virtual QRectF boundingRect() const;
private:
QPixmap m_pixmap;
};
#endif
......@@ -22,6 +22,7 @@
********************************************************************/
#include "scene.h"
#include "ballitem.h"
#include "previewitem.h"
#include "animator.h"
#include "renderer.h"
......@@ -60,6 +61,8 @@ KLinesScene::KLinesScene( QObject* parent )
m_focusItem->setZValue(1.0);
m_focusItem->setPen( Qt::DashLine );
m_previewItem = new PreviewItem(this);
m_previewItem->setPos( 0, 0 );
startNewGame();
}
......@@ -80,7 +83,7 @@ void KLinesScene::startNewGame()
QList<QGraphicsItem*> itemlist = items();
foreach( QGraphicsItem* item, itemlist )
{
if( item != m_focusItem )
if( item != m_focusItem && item != m_previewItem )
{
removeItem(item);
delete item;
......@@ -146,6 +149,10 @@ void KLinesScene::resizeScene(int width,int height)
m_focusItem->setRect( QRect(0,0, m_cellSize, m_cellSize) );
m_focusItem->setPos( fieldToPix( focusRectFieldPos ) );
int previewOriginY = height / 2 - (3 * m_cellSize) / 2;
m_previewItem->setPos( width-m_cellSize, previewOriginY );
m_previewItem->setPreviewColors( m_nextColors );
//kDebug() << "resize:" << width << "," << height << "; cellSize: " << m_cellSize << endl;
}
......@@ -178,7 +185,7 @@ void KLinesScene::nextThreeBalls()
m_nextColors[i] = c;
}
invalidate(sceneRect(), BackgroundLayer); // Refresh next colors preview zone
m_previewItem->setPreviewColors( m_nextColors );
m_animator->animateBorn( newItems );
}
......@@ -189,8 +196,9 @@ void KLinesScene::setPreviewZoneVisible( bool visible )
return;
m_previewZoneVisible = visible;
m_previewItem->setVisible( visible );
resizeScene((int) width(), (int) height());
invalidate(sceneRect());
invalidate( sceneRect() );
}
BallItem* KLinesScene::randomlyPlaceBall(BallColor c)
......@@ -233,7 +241,7 @@ void KLinesScene::selectOrMove( const FieldPos& fpos )
if( m_selPos.isValid() )
{
m_field[m_selPos.x][m_selPos.y]->stopAnimation();
if ( m_selPos == fpos )
{
m_selPos.x = m_selPos.y = -1; // invalidate position
......@@ -604,8 +612,9 @@ void KLinesScene::undo()
m_selPos = FieldPos();
emit scoreChanged(m_score);
invalidate(sceneRect(), BackgroundLayer); // Refresh next colors preview zone
m_previewItem->setPreviewColors( m_nextColors );
emit scoreChanged(m_score);
emit enableUndo(false);
}
......@@ -615,21 +624,6 @@ void KLinesScene::drawBackground(QPainter *p, const QRectF&)
for(int x=m_playFieldOrigin.x(); x<m_playFieldOrigin.x()+m_cellSize*FIELD_SIZE;x+=m_cellSize)
for(int y=m_playFieldOrigin.y(); y<m_playFieldOrigin.y()+m_cellSize*FIELD_SIZE;y+=m_cellSize)
p->drawPixmap( x, y, KLinesRenderer::self()->backgroundTilePixmap() );
// Preview zone
if (m_previewZoneVisible)
{
QPixmap pix = KLinesRenderer::self()->backgroundTilePixmap();
int ballHeight = pix.size().height();
int previewOriginY = (int) height() / 2 - (3 * m_cellSize) / 2;
p->drawPixmap((int) width() - m_cellSize, previewOriginY, KLinesRenderer::self()->previewPixmap());
for(int i=0; i < 3; i++)
if( !m_nextColors.isEmpty() )
p->drawPixmap( (int) width() - m_cellSize + 2, previewOriginY + i*ballHeight,
KLinesRenderer::self()->ballPixmap( m_nextColors.at(i) ) );
}
}
#include "scene.moc"
......@@ -33,6 +33,7 @@ static const int FIELD_SIZE=9;
class KLinesAnimator;
class BallItem;
class PreviewItem;
class QGraphicsRectItem;
/**
......@@ -222,6 +223,10 @@ private:
* Keyboard-playing focus indication
*/
QGraphicsRectItem *m_focusItem;
/**
* Item which displays next balls preview
*/
PreviewItem *m_previewItem;
/**
* Struct for holding game state - used on undos
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment