Commit 4e377c07 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev
Browse files

- Make KLinesRenderer to be a singleton

- File->New now works

svn path=/trunk/KDE/kdegames/klines/; revision=619467
parent d3cd96b4
......@@ -64,14 +64,14 @@ KLinesAnimator::KLinesAnimator( KLinesScene* scene )
// we setup here one 'empty' frame at the end, because without it
// m_scene will delete 'burned' items in removeAnimFinished() slot so quickly
// that last frame won't get shown in the scene
m_removeTimeLine.setFrameRange(0, m_scene->renderer()->numFireFrames());
m_removeTimeLine.setFrameRange(0, KLinesRenderer::self()->numFireFrames());
connect(&m_removeTimeLine, SIGNAL(frameChanged(int)), SLOT(removeAnimationFrame(int)) );
connect(&m_removeTimeLine, SIGNAL(finished()), SIGNAL(removeFinished()));
m_bornTimeLine.setDuration(200);
m_bornTimeLine.setCurveShape(QTimeLine::LinearCurve);
m_bornTimeLine.setFrameRange(0, m_scene->renderer()->numBornFrames()-1);
m_bornTimeLine.setFrameRange(0, KLinesRenderer::self()->numBornFrames()-1);
connect(&m_bornTimeLine, SIGNAL(frameChanged(int)), SLOT(bornAnimationFrame(int)) );
connect(&m_bornTimeLine, SIGNAL(finished()), SIGNAL(bornFinished()));
......@@ -160,16 +160,16 @@ void KLinesAnimator::moveAnimationFrame(int frame)
void KLinesAnimator::removeAnimationFrame(int frame)
{
if(frame == m_scene->renderer()->numFireFrames())
if(frame == KLinesRenderer::self()->numFireFrames())
return;
foreach(BallItem* ball, m_removedBalls)
ball->setPixmap( m_scene->renderer()->firePixmap(frame) );
ball->setPixmap( KLinesRenderer::self()->firePixmap(frame) );
}
void KLinesAnimator::bornAnimationFrame(int frame)
{
foreach(BallItem* ball, m_bornBalls)
ball->setPixmap( m_scene->renderer()->bornPixmap(ball->color(), frame) );
ball->setPixmap( KLinesRenderer::self()->bornPixmap(ball->color(), frame) );
}
void KLinesAnimator::findPath( const FieldPos& from, const FieldPos& to )
......
......@@ -25,15 +25,15 @@
#include "ballitem.h"
#include "renderer.h"
BallItem::BallItem( QGraphicsScene* parent, const KLinesRenderer* renderer )
: QGraphicsPixmapItem( 0, parent ), m_renderer(renderer)
BallItem::BallItem( QGraphicsScene* parent )
: QGraphicsPixmapItem( 0, parent )
{
m_color = NumColors; // = uninitialized
m_timeLine.setCurveShape( QTimeLine::LinearCurve );
m_timeLine.setDuration(400);
m_timeLine.setLoopCount(0);
m_timeLine.setFrameRange(0, m_renderer->numSelectedFrames()-1);
m_timeLine.setFrameRange(0, KLinesRenderer::self()->numSelectedFrames()-1);
// starting by going lower
m_timeLine.setCurrentTime( m_timeLine.duration()/2 );
......@@ -55,12 +55,12 @@ void BallItem::startSelectedAnimation()
void BallItem::stopAnimation()
{
m_timeLine.stop();
setPixmap( m_renderer->ballPixmap( m_color ) );
setPixmap( KLinesRenderer::self()->ballPixmap( m_color ) );
}
void BallItem::animFrameChanged(int frame)
{
setPixmap(m_renderer->selectedPixmap( m_color, frame ));
setPixmap(KLinesRenderer::self()->selectedPixmap( m_color, frame ));
}
#include "ballitem.moc"
......@@ -28,8 +28,6 @@
#include "commondefs.h"
class KLinesRenderer;
/**
* QGraphicsItem for Ball
*/
......@@ -37,7 +35,7 @@ class BallItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
BallItem( QGraphicsScene* parent, const KLinesRenderer* r );
BallItem( QGraphicsScene* parent );
/**
* Sets ball's color
*/
......@@ -61,10 +59,6 @@ public:
private slots:
void animFrameChanged(int);
private:
/**
* Renderer used to render ball's pixmaps
*/
const KLinesRenderer* m_renderer;
/**
* Timeline for controlling animations
*/
......
......@@ -166,27 +166,9 @@ void KLines::gameOver(int score)
void KLines::startGame()
{
score = 0;
score_undo = 0;
bUndo = true;
bNewTurn = true;
if(bDemo)
stopDemo();
bFirst = true;
int level = levelAction->currentItem()-2;
setLevel(level);
lsb->setLevel(level);
lsb->setGameOver(false);
lsb->clearField();
generateRandomBalls();
placeBalls();
generateRandomBalls();
undoAction->setEnabled(false);
endTurnAction->setEnabled(true);
updateStatusBar();
updateScore(0);
mwidget->scene()->startNewGame();
mwidget->updateNextColors();
}
void KLines::setLevel(int level) {
......
......@@ -38,7 +38,7 @@ public:
void updatePix();
KLinesScene* scene() { return m_scene; }
private slots:
public slots:
void updateNextColors();
private:
LinesBoard * lsb;
......
......@@ -28,25 +28,24 @@
BallsPreview::BallsPreview( QWidget* parent )
: QWidget(parent)
{
m_renderer = new KLinesRenderer;
}
QSize BallsPreview::sizeHint() const
{
return QSize( m_renderer->backgroundTilePixmap().size().width()*3, m_renderer->backgroundTilePixmap().height() );
return QSize( KLinesRenderer::self()->backgroundTilePixmap().size().width()*3, KLinesRenderer::self()->backgroundTilePixmap().height() );
}
void BallsPreview::paintEvent( QPaintEvent* )
{
QPainter p(this);
QPixmap pix = m_renderer->backgroundTilePixmap();
// FIXME dimsuz: use smth like m_renderer->elementSize()
QPixmap pix = KLinesRenderer::self()->backgroundTilePixmap();
// FIXME dimsuz: use smth like KLinesRenderer::self()->elementSize()
int width = pix.size().width();
for(int i=0; i < 3; i++)
{
p.drawPixmap( i*width, 0, pix );
if( !m_colors.isEmpty() )
// FIXME dimsuz: "2" - hardcoded
p.drawPixmap( i*width+2, 2, m_renderer->ballPixmap( m_colors.at(i) ) );
p.drawPixmap( i*width+2, 2, KLinesRenderer::self()->ballPixmap( m_colors.at(i) ) );
}
}
......@@ -26,8 +26,6 @@
#include <QWidget>
#include "commondefs.h"
class KLinesRenderer;
/**
* Widget for displaying next-turn balls
*/
......@@ -45,7 +43,6 @@ public:
private:
void paintEvent( QPaintEvent* );
KLinesRenderer *m_renderer;
QList<BallColor> m_colors;
};
......
......@@ -24,6 +24,12 @@
#include "renderer.h"
KLinesRenderer* KLinesRenderer::self()
{
static KLinesRenderer instance;
return &instance;
}
KLinesRenderer::KLinesRenderer()
{
m_fieldPix = QPixmap( KStandardDirs::locate( "appdata", "field.jpg" ));
......
......@@ -27,12 +27,12 @@
#include "commondefs.h"
// FIXME dimsuz: make singleton - it's used in three classes BallItem, KLinesAnimator, KLinesScene
// FIXME dimsuz: give {fire,born,selected}Pixmap methods better names
class KLinesRenderer
{
public:
KLinesRenderer();
static KLinesRenderer* self();
QPixmap ballPixmap( BallColor c ) const;
QPixmap firePixmap( int frame ) const;
......@@ -45,6 +45,12 @@ public:
inline int numBornFrames() const { return 5; }
inline int numSelectedFrames() const { return 13; }
private:
// disable copy - it's singleton
KLinesRenderer();
KLinesRenderer( const KLinesRenderer& );
KLinesRenderer& operator=( const KLinesRenderer& );
~KLinesRenderer() { }
QPixmap m_ballsPix; // to be removed when SVG comes to us
QPixmap m_fieldPix; // to be removed when SVG comes to us
QPixmap m_firePix; // to be removed when SVG comes to us
......
......@@ -44,23 +44,47 @@ void KLinesView::resizeEvent( QResizeEvent* ev )
// =============== KLinesScene =======================
KLinesScene::KLinesScene( QObject* parent )
: QGraphicsScene(parent), m_numFreeCells(FIELD_SIZE*FIELD_SIZE), m_score(0), m_gameOver(false)
: QGraphicsScene(parent), m_numFreeCells(FIELD_SIZE*FIELD_SIZE), m_score(0)
{
m_renderer = new KLinesRenderer;
m_animator = new KLinesAnimator(this);
connect( m_animator, SIGNAL(moveFinished()), SLOT(moveAnimFinished() ) );
connect( m_animator, SIGNAL(removeFinished()), SLOT(removeAnimFinished() ) );
connect( m_animator, SIGNAL(bornFinished()), SLOT(bornAnimFinished() ) );
for(int x=0; x<FIELD_SIZE; ++x)
for(int y=0; y<FIELD_SIZE; ++y)
m_field[x][y] = 0;
// FIXME dimsuz: hardcoded size
m_focusItem = new QGraphicsRectItem( QRectF(0, 0, 32, 32), 0, this );
m_focusItem->setPen( Qt::DashLine );
startNewGame();
}
void KLinesScene::startNewGame()
{
// reset all vars
m_selPos = FieldPos();
m_numFreeCells = FIELD_SIZE*FIELD_SIZE;
m_score = 0;
m_placeBalls = true;
m_itemsToDelete.clear();
m_nextColors.clear();
m_focusItem->setPos(0, 0);
m_focusItem->hide();
// remove all ball items from the scene
QList<QGraphicsItem*> itemlist = items();
foreach( QGraphicsItem* item, itemlist )
{
if( item != m_focusItem )
{
removeItem(item);
delete item;
}
}
for(int x=0; x<FIELD_SIZE; ++x)
for(int y=0; y<FIELD_SIZE; ++y)
m_field[x][y] = 0;
// init m_nextColors
for(int i=0; i<3; i++)
{
......@@ -68,13 +92,12 @@ KLinesScene::KLinesScene( QObject* parent )
BallColor c = static_cast<BallColor>(m_randomSeq.getLong(static_cast<int>(NumColors)));
m_nextColors.append(c);
}
nextThreeBalls();
}
KLinesScene::~KLinesScene()
{
delete m_renderer;
delete m_animator;
}
......@@ -127,7 +150,7 @@ BallItem* KLinesScene::randomlyPlaceBall(BallColor c)
posy = m_randomSeq.getLong(FIELD_SIZE);
} while( m_field[posx][posy] != 0 );
BallItem* newBall = new BallItem( this, m_renderer );
BallItem* newBall = new BallItem( this );
newBall->setColor(c);
newBall->setPos( fieldToPix( FieldPos(posx,posy) ) );
m_field[posx][posy] = newBall;
......@@ -206,7 +229,11 @@ void KLinesScene::removeAnimFinished()
if(numBallsErased)
m_score += 2*numBallsErased*numBallsErased - 20*numBallsErased + 60 ;
qDeleteAll( m_itemsToDelete );
foreach( BallItem* item, m_itemsToDelete )
{
removeItem(item);
delete item;
}
m_itemsToDelete.clear();
// it is needed after qDeleteAll()
......@@ -448,7 +475,7 @@ void KLinesScene::drawBackground(QPainter *p, const QRectF&)
// FIXME dimsuz: temp
for(int x=0; x<32*FIELD_SIZE;x+=32)
for(int y=0; y<32*FIELD_SIZE;y+=32)
p->drawPixmap( x, y, m_renderer->backgroundTilePixmap() );
p->drawPixmap( x, y, KLinesRenderer::self()->backgroundTilePixmap() );
}
#include "scene.moc"
......@@ -31,7 +31,6 @@
static const int FIELD_SIZE=9;
class KLinesRenderer;
class KLinesAnimator;
class BallItem;
class QGraphicsRectItem;
......@@ -67,10 +66,6 @@ public:
* Overloaded above function
*/
BallItem* ballAt( int x, int y ) { return m_field[x][y]; }
/**
* Returns games' renderer
*/
const KLinesRenderer* renderer() const { return m_renderer; }
/**
* Field coords to pixel coords
*/
......@@ -83,6 +78,10 @@ public:
inline FieldPos pixToField( const QPointF& p ) const {
return FieldPos(static_cast<int>(p.x()/32), static_cast<int>(p.y()/32)); }
public slots:
/**
* Starts new game
*/
void startNewGame();
/**
* Moves keyboard-playing focus rect to the left
*/
......@@ -140,10 +139,6 @@ private:
* or 0 if there's no ball in that cell
*/
BallItem* m_field[FIELD_SIZE][FIELD_SIZE];
/**
* Renderer used to get all the game graphics
*/
const KLinesRenderer* m_renderer;
/**
* Used to start game animations
* This object knows how to do some ball animations
......@@ -170,10 +165,6 @@ private:
* Read more about it in removeAnimFinished() slot
*/
bool m_placeBalls;
/**
* Indicates game is over
*/
bool m_gameOver;
/**
* Items pending for removal after remove-anim finishes
*/
......
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