Commit ae5b560a authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

Moved frame loading code to KReversiChipFrameSet class.

It fits better there then in KReversiScene.

svn path=/trunk/KDE/kdegames/kreversi/; revision=588887
parent 3b558988
......@@ -34,7 +34,7 @@
</entry>
<entry name="UseColoredChips" type="Bool">
<label>Whether to use colored chips instead of black and white ones.</label>
<default>true</default>
<default>false</default>
</entry>
</group>
</kcfg>
......@@ -21,8 +21,10 @@
*
********************************************************************/
#include "kreversichip.h"
#include <ksvgrenderer.h>
#include <QPainter>
#include <QPixmap>
#include <QImage>
#include <kdebug.h>
......@@ -70,8 +72,30 @@ void KReversiChip::showLastMoveMarker(bool show)
// -------------------------------------------------------------------------------
KReversiChipFrameSet::KReversiChipFrameSet( const QPixmap& allFrames, int frameSize )
KReversiChipFrameSet::KReversiChipFrameSet()
{
}
void KReversiChipFrameSet::loadFrames( const QString& chipsPath )
{
m_frames.clear();
QImage baseImg;
//Use the new addition to kdelib/kdecore, KSvgRenderer, so we can use .svgz
KSvgRenderer chips( chipsPath );
//TODO Return meaningful error?
if (!chips.isValid()) return;
//Construct an image object to render the contents of the .svgz file
baseImg = QImage(chips.defaultSize(),QImage::Format_ARGB32_Premultiplied);
//Fill the buffer, it is unitialised by default
baseImg.fill(0);
QPainter p(&baseImg);
chips.render(&p);
QPixmap allFrames = QPixmap::fromImage(baseImg);
int frameSize = allFrames.width() / 4;
for(int y=0; y < allFrames.height(); y += frameSize )
for(int x=0; x < allFrames.width(); x += frameSize )
{
......
......@@ -61,35 +61,34 @@ private:
};
/**
* This class will hold a chip animation frameset.
* This class will load and hold a chip animation frameset.
* As all chips share the same frames it's good to
* put them in a single storage class like this,
* so any chip can retrieve any frame any time :).
*
* This class is based on a number of assumptions which
* reflect the current allchips.png pixmap format.
* reflect the current chips.svgz pixmap format.
* Here they are:
* 1. Animation sequence goes from black to white colored chips
* 2a. as a consequence of 1: frame(0) is black chip
* 2b. as a consequence of 1: frame(frameCount()) is white chip
* But 2a and 2b shouldn't matter, because there's chipPixmap()
*
* These are subject to changing/rethinking ;)
* 3. The svg image contains 4 cols and 3 rows of chip pixmaps, i.e 12 frames
*/
class KReversiChipFrameSet
{
public:
KReversiChipFrameSet();
/**
* Initializes this class with 'allFrames' QPixmap, which
* Loads a chips (svg) pixmap found in path chipsPath, which
* contains chip's animation sequence.
* The chips frames are extracted from it and put into
* m_frames QList
* Supposes that allFrames represents an animation sequence
* Supposes that this pixmap represents an animation sequence
* going from black to white.
* @param allFrames a pixmap containing whole animation sequence
* @param frameSize size of each frame's pixmap. Frames are squares.
* @param chipsPath a path to svg pixmap containing whole animation sequence
*/
KReversiChipFrameSet( const QPixmap& allFrames, int frameSize );
void loadFrames( const QString& chipsPath );
/**
* Retruns a pixmap which corresponds to frame with number frameNo.
* It takes the chip color into account. This means that
......@@ -109,6 +108,10 @@ public:
* Returns number of frames in animation
*/
int frameCount() const { return m_frames.count(); }
/**
* Returns default chip size
*/
int defaultChipSize() const { return m_frames.at(0).width(); }
private:
QList<QPixmap> m_frames;
};
......
......@@ -23,11 +23,8 @@
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QTimer>
#include <QImage>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <ksvgrenderer.h>
#include "kreversiscene.h"
#include "kreversigame.h"
......@@ -43,6 +40,8 @@ KReversiScene::KReversiScene( KReversiGame* game , const QString& chipsPath )
{
setBackgroundBrush( Qt::lightGray );
setChipsPixmap(chipsPath);
QFont font; // it'll be initialised to default application font
font.setBold(true);
// NOTE we assume that fontMetrics in drawBackground() will be the same as here
......@@ -52,8 +51,6 @@ KReversiScene::KReversiScene( KReversiGame* game , const QString& chipsPath )
setSceneRect( 0, 0, m_boardRect.width()+2*fontHeight, m_boardRect.height()+2*fontHeight);
setChipsPixmap(chipsPath);
m_animTimer = new QTimer(this);
connect(m_animTimer, SIGNAL(timeout()), SLOT(slotAnimationStep()));
......@@ -67,21 +64,10 @@ KReversiScene::~KReversiScene()
void KReversiScene::setChipsPixmap( const QString& chipsPath )
{
delete m_frameSet;
QImage baseImg;
//Use the new addition to kdelib/kdecore, KSvgRenderer, so we can use .svgz
KSvgRenderer chips(KStandardDirs::locate("appdata", chipsPath));
//TODO Return meaningful error?
if (!chips.isValid()) return;
//Construct an image object to render the contents of the .svgz file
baseImg = QImage(chips.defaultSize(),QImage::Format_ARGB32_Premultiplied);
//Fill the buffer, it is unitialised by default
baseImg.fill(0);
QPainter p(&baseImg);
chips.render(&p);
m_frameSet = new KReversiChipFrameSet( QPixmap::fromImage(baseImg), CHIP_SIZE );
if(!m_frameSet)
m_frameSet = new KReversiChipFrameSet();
m_frameSet->loadFrames( chipsPath );
if(m_game)
{
// FIXME: Qt rc1 bug? there was items( m_boardRect ) here
......
......@@ -21,12 +21,14 @@
*
********************************************************************/
#include "kreversiview.h"
#include "kreversiscene.h"
#include <QGraphicsScene>
#include <QResizeEvent>
#include <kdebug.h>
KReversiView::KReversiView( QGraphicsScene* scene, QWidget *parent )
KReversiView::KReversiView( KReversiScene* scene, QWidget *parent )
: QGraphicsView(scene, parent)
{
setCacheMode( QGraphicsView::CacheBackground );
......
......@@ -25,10 +25,12 @@
#include <QGraphicsView>
class KReversiScene;
class KReversiView : public QGraphicsView
{
public:
KReversiView( QGraphicsScene* scene, QWidget *parent );
KReversiView( KReversiScene* scene, QWidget *parent );
private:
virtual QSize sizeHint() const;
};
......
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