[Kde-games-devel] KDE/kdegames/kreversi
Mauricio Piacentini
mauricio at tabuleiro.com
Wed Jul 11 01:46:16 CEST 2007
SVN commit 686289 by piacentini:
Unify chips art in default_theme.svgz file. Also consolidate all
rendering in the KReversiRenderer singleton class, extending it as
necessary with a couple of new methods.
Also, due to a bug in Qt SVG rendering the bounds of the old move_hint
element were not being detected correctly. Changed the move_hint art to
an Oxygen-style checkmark. This is used to display possible moves.
CCMAIL: kde-games-devel at kde.org
M +11 -26 kreversichip.cpp
M +4 -6 kreversichip.h
M +13 -2 kreversirenderer.cpp
M +3 -1 kreversirenderer.h
M +6 -5 kreversiscene.cpp
M +2 -2 kreversiscene.h
M +4 -5 mainwindow.cpp
M +1 -1 pics/CMakeLists.txt
D pics/chips.svgz
D pics/chips_mono.svgz
M pics/default_theme.svgz
--- trunk/KDE/kdegames/kreversi/kreversichip.cpp #686288:686289
@@ -21,7 +21,7 @@
*
********************************************************************/
#include "kreversichip.h"
-#include <ksvgrenderer.h>
+#include "kreversirenderer.h"
#include <QPainter>
#include <QPixmap>
#include <QImage>
@@ -72,51 +72,36 @@
// -------------------------------------------------------------------------------
-// FIXME dimsuz: make member?
-static const int NUM_COLS_IN_PIX = 4;
-static const int NUM_ROWS_IN_PIX = 3;
-
KReversiChipFrameSet::KReversiChipFrameSet()
{
- m_renderer = new KSvgRenderer;
}
KReversiChipFrameSet::~KReversiChipFrameSet()
{
- delete m_renderer;
}
-void KReversiChipFrameSet::loadFrames( const QString& chipsPath, int chipSize )
+void KReversiChipFrameSet::switchChipSet( const QString& chipsPrefix, int chipSize )
{
- m_renderer->load( chipsPath );
- //TODO Return meaningful error?
- if (!m_renderer->isValid()) return;
- int size = chipSize == 0 ? m_renderer->defaultSize().width()/NUM_COLS_IN_PIX : chipSize;
+ m_currentChipsPrefix = chipsPrefix;
+ int size = chipSize == 0 ? KReversiRenderer::self()->defaultChipSize().width() : chipSize;
setChipSize( size );
}
void KReversiChipFrameSet::setChipSize( int newSize )
{
QImage baseImg;
- //TODO Return meaningful error?
- if (!m_renderer->isValid()) return;
+ m_frames.clear();
+ for (int i=1; i<=12; i++) {
//Construct an image object to render the contents of the .svgz file
- baseImg = QImage(newSize*NUM_COLS_IN_PIX, newSize*NUM_ROWS_IN_PIX, QImage::Format_ARGB32_Premultiplied);
+ baseImg = QImage(newSize, newSize, QImage::Format_ARGB32_Premultiplied);
//Fill the buffer, it is unitialised by default
baseImg.fill(0);
QPainter p(&baseImg);
- m_renderer->render(&p);
+ QString nextelement(m_currentChipsPrefix.arg(i));
+ KReversiRenderer::self()->renderElement(&p, nextelement);
p.end();
-
- QPixmap allFrames = QPixmap::fromImage(baseImg);
- int frameSize = allFrames.width() / NUM_COLS_IN_PIX;
-
- m_frames.clear();
- for(int y=0; y < allFrames.height(); y += frameSize )
- for(int x=0; x < allFrames.width(); x += frameSize )
- {
- m_frames.append( allFrames.copy(x,y, frameSize, frameSize) );
- }
+ m_frames.append( QPixmap::fromImage(baseImg) );
+ }
}
QPixmap KReversiChipFrameSet::frame( ChipColor color, int frameNo ) const
--- trunk/KDE/kdegames/kreversi/kreversichip.h #686288:686289
@@ -66,8 +66,6 @@
int m_col;
};
-class KSvgRenderer;
-
/**
* This class will load and hold a chip animation frameset.
* As all chips share the same frames it's good to
@@ -89,17 +87,17 @@
KReversiChipFrameSet();
~KReversiChipFrameSet();
/**
- * Loads a chips (svg) pixmap found in path chipsPath, which
+ * Prepare to render chips (from svg elements) pixmap with prefix chipsPrefix, which
* contains chip's animation sequence.
* The chips frames are extracted from it and put into
* m_frames QList
* Supposes that this pixmap represents an animation sequence
* going from black to white.
- * @param chipsPath a path to svg pixmap containing whole animation sequence
+ * @param chipsPrefixa svg prefix element ids containing whole animation sequence
* @param chipSize if not equal to 0 then chips will be scaled to chipSize x chipSize each
* else chips will be loaded unscaled
*/
- void loadFrames( const QString& chipsPath, int chipSize = 0 );
+ void switchChipSet( const QString& chipsPrefix, int chipSize = 0 );
/**
* Retruns a pixmap which corresponds to frame with number frameNo.
* It takes the chip color into account. This means that
@@ -131,6 +129,6 @@
int defaultChipSize() const { return m_frames.at(0).width(); }
private:
QList<QPixmap> m_frames;
- KSvgRenderer *m_renderer;
+ QString m_currentChipsPrefix;
};
#endif
--- trunk/KDE/kdegames/kreversi/kreversirenderer.cpp #686288:686289
@@ -53,11 +53,16 @@
m_renderer->render( p, "board_numbers", r );
}
-void KReversiRenderer::renderPossibleMove( QPainter *p )
+void KReversiRenderer::renderPossibleMove( QPainter *p, const QRectF& r )
{
- m_renderer->render( p, "move_hint" );
+ m_renderer->render( p, "move_hint", r );
}
+void KReversiRenderer::renderElement (QPainter *p, QString& elementid )
+{
+ m_renderer->render( p, elementid );
+}
+
KReversiRenderer::~KReversiRenderer()
{
delete m_renderer;
@@ -68,3 +73,9 @@
QRectF boardSize = m_renderer->boundsOnElement("board");
return QSize((int) boardSize.width(), (int) boardSize.height());
}
+
+QSize KReversiRenderer::defaultChipSize() const
+{
+ QRectF chipSize = m_renderer->boundsOnElement("chip_bw_1");
+ return QSize((int) chipSize.width(), (int) chipSize.height());
+}
--- trunk/KDE/kdegames/kreversi/kreversirenderer.h #686288:686289
@@ -35,8 +35,10 @@
void renderBackground( QPainter *p, const QRectF& r );
void renderBoard( QPainter *p, const QRectF& r );
void renderBoardLabels( QPainter *p, const QRectF& r );
- void renderPossibleMove( QPainter *p );
+ void renderPossibleMove( QPainter *p, const QRectF& r );
+ void renderElement (QPainter *p, QString& elementid );
QSize defaultBoardSize() const;
+ QSize defaultChipSize() const;
private:
// disable copy - it's singleton
KReversiRenderer();
--- trunk/KDE/kdegames/kreversi/kreversiscene.cpp #686288:686289
@@ -38,7 +38,7 @@
{
setBackgroundBrush( Qt::lightGray );
- setChipsPixmap(chipsPath);
+ setChipsPrefix(chipsPath);
m_animTimer = new QTimer(this);
connect(m_animTimer, SIGNAL(timeout()), SLOT(slotAnimationStep()));
@@ -86,9 +86,10 @@
// Render possible moves pixmap
QImage baseImg((int)m_curCellSize, (int)m_curCellSize, QImage::Format_ARGB32_Premultiplied);
+ QRectF moveRect(0,0,m_curCellSize,m_curCellSize);
baseImg.fill(0);
QPainter p(&baseImg);
- KReversiRenderer::self()->renderPossibleMove( &p );
+ KReversiRenderer::self()->renderPossibleMove( &p, moveRect );
p.end();
m_possMovePix = QPixmap::fromImage(baseImg);
@@ -99,18 +100,18 @@
displayLastAndPossibleMoves();
}
-void KReversiScene::setChipsPixmap( const QString& chipsPath )
+void KReversiScene::setChipsPrefix( const QString& chipsPrefix )
{
if(!m_frameSet) // this is a first invocation
{
m_frameSet = new KReversiChipFrameSet();
- m_frameSet->loadFrames( chipsPath );
+ m_frameSet->switchChipSet( chipsPrefix );
m_curCellSize = m_frameSet->defaultChipSize();
}
else // we're changing frameset's pixmap (monochrome-chips <-> color-chips transition)
{
// m_curCellSize is already defined in this case, so lets scale chips on load
- m_frameSet->loadFrames( chipsPath, (int)m_curCellSize );
+ m_frameSet->switchChipSet( chipsPrefix, (int)m_curCellSize );
}
if(m_game)
--- trunk/KDE/kdegames/kreversi/kreversiscene.h #686288:686289
@@ -59,10 +59,10 @@
*/
void setGame( KReversiGame* game );
/**
- * Sets the chips pixmap to be one found in chipsPath
+ * Sets the chips pixmap to be one found in chipsPrefix
* @see KReversiChipFrameSet
*/
- void setChipsPixmap( const QString& chipsPath );
+ void setChipsPrefix( const QString& chipsPrefix );
/**
* Sets whether to show board labels.
* You'll need to call
--- trunk/KDE/kdegames/kreversi/mainwindow.cpp #686288:686289
@@ -206,8 +206,8 @@
void KReversiMainWindow::slotUseColoredChips(bool toggled)
{
- QString chipsPath = m_coloredChipsAct->isChecked() ? "pics/chips.svgz" : "pics/chips_mono.svgz";
- m_scene->setChipsPixmap( KStandardDirs::locate("appdata", chipsPath) );
+ QString chipsPrefix = m_coloredChipsAct->isChecked() ? "chip_color_%1" : "chip_bw_%1";
+ m_scene->setChipsPrefix( chipsPrefix );
Preferences::setUseColoredChips(toggled);
Preferences::self()->writeConfig();
}
@@ -275,9 +275,8 @@
if(m_scene == 0) // if called first time
{
- // FIXME dimsuz: if chips[_mono].png not found give error end exit
- QString chipsPath = Preferences::useColoredChips() ? "pics/chips.svgz" : "pics/chips_mono.svgz";
- m_scene = new KReversiScene(m_game, KStandardDirs::locate("appdata", chipsPath));
+ QString chipsPrefix = Preferences::useColoredChips() ? "chip_color_%1" : "chip_bw_%1";
+ m_scene = new KReversiScene(m_game, chipsPrefix);
m_scene->setAnimationSpeed( Preferences::animationSpeed() );
connect( m_scene, SIGNAL(moveFinished()), SLOT(slotMoveFinished()) );
}
--- trunk/KDE/kdegames/kreversi/pics/CMakeLists.txt #686288:686289
@@ -1,3 +1,3 @@
########### install files ###############
-install( FILES chips.svgz chips_mono.svgz default_theme.svgz DESTINATION ${DATA_INSTALL_DIR}/kreversi/pics )
+install( FILES default_theme.svgz DESTINATION ${DATA_INSTALL_DIR}/kreversi/pics )
More information about the kde-games-devel
mailing list