Commit ba62d22e authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

Render background

svn path=/trunk/KDE/kdegames/klines/; revision=657186
parent 0beda631
......@@ -103,6 +103,18 @@ QPixmap KLinesRenderer::backgroundTilePixmap() const
return m_pixHash.value( "field_cell" );
}
QPixmap KLinesRenderer::backgroundPixmap( const QSize& sz ) const
{
if ( m_bkgnd.size() != sz )
{
m_bkgnd = QPixmap( sz );
m_bkgnd.fill( Qt::transparent );
QPainter p( &m_bkgnd );
m_renderer->render( &p, "background" );
}
return m_bkgnd;
}
void KLinesRenderer::rerenderPixmaps()
{
// this should be in sync with svg
......
......@@ -41,6 +41,7 @@ public:
QPixmap ballPixmap( BallColor c ) const;
QPixmap animationFrame( AnimationType type, BallColor c, int frame ) const;
QPixmap backgroundPixmap( const QSize& size ) const;
QPixmap backgroundTilePixmap() const;
void setCellSize(int size) { m_cellSize = size; rerenderPixmaps(); }
......@@ -67,6 +68,10 @@ private:
* All rendered pixmaps will have this size
*/
int m_cellSize;
/**
* Background. Cached until requested size doesn't change
*/
mutable QPixmap m_bkgnd;
KSvgRenderer *m_renderer;
QHash<QString, QPixmap> m_pixHash;
};
......
......@@ -46,7 +46,9 @@ void KLinesView::resizeEvent( QResizeEvent* ev )
// =============== KLinesScene =======================
KLinesScene::KLinesScene( QObject* parent )
: QGraphicsScene(parent), m_numFreeCells(FIELD_SIZE*FIELD_SIZE), m_score(0), m_bonusScore(0), m_cellSize(32)
: QGraphicsScene(parent),
m_playFieldOrigin(0, 0 ), m_numFreeCells(FIELD_SIZE*FIELD_SIZE),
m_score(0), m_bonusScore(0), m_cellSize(32)
{
m_animator = new KLinesAnimator(this);
connect( m_animator, SIGNAL(moveFinished()), SLOT(moveAnimFinished() ) );
......@@ -111,7 +113,10 @@ void KLinesScene::resizeScene(int width,int height)
// store focus item field pos (calculated using old cellSize)
FieldPos focusRectFieldPos = pixToField( m_focusItem->pos() );
m_cellSize = qMin(width, height)/FIELD_SIZE;
int minDim = qMin( width, height );
m_cellSize = minDim/FIELD_SIZE;
m_playFieldOrigin = QPoint( width/2 - minDim/2, height/2 - minDim/2 );
setSceneRect( 0, 0, width, height );
KLinesRenderer::self()->setCellSize( m_cellSize );
......@@ -191,6 +196,10 @@ BallItem* KLinesScene::randomlyPlaceBall(BallColor c)
void KLinesScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
{
if ( !QRectF( m_playFieldOrigin.x(), m_playFieldOrigin.y(),
m_cellSize*FIELD_SIZE, m_cellSize*FIELD_SIZE ).contains( ev->scenePos() ) )
return;
selectOrMove( pixToField(ev->scenePos()) );
}
......@@ -573,9 +582,9 @@ void KLinesScene::undo()
void KLinesScene::drawBackground(QPainter *p, const QRectF&)
{
// FIXME dimsuz: temp
for(int x=0; x<m_cellSize*FIELD_SIZE;x+=m_cellSize)
for(int y=0; y<m_cellSize*FIELD_SIZE;y+=m_cellSize)
p->drawPixmap( 0,0, KLinesRenderer::self()->backgroundPixmap( sceneRect().size().toSize() ) );
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() );
}
......
......@@ -76,14 +76,19 @@ public:
/**
* Field coords to pixel coords
*/
inline QPointF fieldToPix(const FieldPos& fpos) const {
return QPointF( fpos.x*m_cellSize + m_cellSize * 0.05 , fpos.y*m_cellSize + m_cellSize * 0.05 );
inline QPointF fieldToPix(const FieldPos& fpos) const
{
return QPointF( m_playFieldOrigin.x()+fpos.x*m_cellSize + m_cellSize * 0.05 ,
m_playFieldOrigin.y()+fpos.y*m_cellSize + m_cellSize * 0.05 );
}
/**
* Pixel coords to field coords
*/
inline FieldPos pixToField( const QPointF& p ) const {
return FieldPos(static_cast<int>(p.x()/m_cellSize), static_cast<int>(p.y()/m_cellSize)); }
inline FieldPos pixToField( const QPointF& p ) const
{
return FieldPos(static_cast<int>(( p.x()-m_playFieldOrigin.x() )/m_cellSize),
static_cast<int>(( p.y()-m_playFieldOrigin.y() )/m_cellSize));
}
public slots:
/**
* Starts new game
......@@ -168,6 +173,10 @@ private:
* We need random numbers in this game
*/
KRandomSequence m_randomSeq;
/**
* Origin of playfield
*/
QPoint m_playFieldOrigin;
/**
* Position of selected ball (-1,-1) if none
*/
......
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