Commit 01da2cf5 authored by Roney Gomes's avatar Roney Gomes

Backgrounds and their stylized borders are properly rendered and resized.

svn path=/branches/work/kgoldrunner-qgv/; revision=1306879
parent 904bced8
......@@ -2,6 +2,7 @@
########### next target ###############
set(kgoldrunner_SRCS
kgrscene.cpp
kgrplayfield.cpp
kgrsprite.cpp
kgoldrunner.cpp
......@@ -37,7 +38,8 @@ kde4_install_icons( ${ICON_INSTALL_DIR} )
########### next target ###############
set(kgrtest_SRCS # NOTE: This target (kgrtest) is for testing purposes only.
g.cpp
kgrscene.cpp
kgrview.cpp
kgrthemetypes.cpp
kgrrenderer.cpp
testmain.cpp
......
#!/usr/bin/env xdg-open
# KDE Config File
[Desktop Entry]
Name=KGoldrunner
......
......@@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#include "kgrscene.h"
#include "kgoldrunner.h"
#include "kgrrenderer.h" // IDW test
......@@ -36,6 +37,7 @@
#include <kconfiggroup.h>
#include <kdebug.h>
#include <QDebug>
#include <ktoolbar.h>
#include <kmenubar.h>
......@@ -124,7 +126,7 @@ KGoldrunner::KGoldrunner()
// Tell the KMainWindow that the KGrCanvas object is the main widget.
setCentralWidget (view);
m_scene = new QGraphicsScene (this); //IDW test.
m_scene = new KGrScene; //IDW test.
m_renderer = new KGrRenderer (m_scene); // IDW test.
// Set up our actions (menu, toolbar and keystrokes) ...
......@@ -185,6 +187,7 @@ void KGoldrunner::KGoldrunner_2()
KGoldrunner::~KGoldrunner()
{
delete m_scene;
}
void KGoldrunner::setupActions()
......
......@@ -35,8 +35,6 @@ const int L_LEVEL = 15;
#include <KXmlGuiWindow>
#include <QGraphicsScene> // IDW test.
class QSignalMapper;
class KAction;
class KToggleAction;
......@@ -44,6 +42,7 @@ class KToggleAction;
class KGrGame;
class KGrCanvas;
class KGrRenderer;
class KGrScene; // RNY test.
/**
* This class serves as the main window for KGoldrunner. It handles the
......@@ -79,7 +78,7 @@ protected:
void keyReleaseEvent (QKeyEvent * event);
private:
QGraphicsScene * m_scene; // IDW test.
KGrScene * m_scene; // IDW test.
bool identifyMoveAction (QKeyEvent * event, bool pressed);
......
......@@ -822,18 +822,26 @@ void KGrCanvas::makeBorder ()
if (!l.isEmpty()) {
kDebug() << "drawing the border tiles...";
// Draw fancy border
// Top left corner.
borderElements.append(makeBorderElement(l, tlX - imgW, tlY - imgW, 0));
// Top right corner.
borderElements.append(makeBorderElement(l, tlX + pw, tlY - imgW, 2));
// Bottom left corner.
borderElements.append(makeBorderElement(l, tlX - imgW, tlY + ph, 6));
// Bottom right corner.
borderElements.append(makeBorderElement(l, tlX + pw, tlY + ph, 8));
for (int i = 0; i < nCellsW * imgW; i += imgW) {
// Top borderline.
borderElements.append(makeBorderElement(l, tlX + i, tlY - imgW, 1));
// Bottom borderline.
borderElements.append(makeBorderElement(l, tlX + i, tlY + ph, 7));
}
for (int i = 0; i < nCellsH * imgH; i += imgH) {
// Left borderline.
borderElements.append(makeBorderElement(l, tlX - imgW, tlY + i, 3));
// Right borderline.
borderElements.append(makeBorderElement(l, tlX + pw, tlY + i, 5));
}
}
......
......@@ -52,6 +52,20 @@ const int STEP = 4;
const int gameCycle = 4; // Animation frames per playfield tile.
const int graphicsCycle = 8; // Animation frames per running cycle.
// Indexes for the first and last tiles for each side of the background border
// in the list generated by KGrRenderer::borderTiles().
const int TOP_BORDER_BEGIN = 4;
const int TOP_BORDER_END = 31;
const int BOTTOM_BORDER_BEGIN = 32;
const int BOTTOM_BORDER_END = 59;
const int LEFT_BORDER_BEGIN = 60;
const int LEFT_BORDER_END = 79;
const int RIGHT_BORDER_BEGIN = 80;
const int RIGHT_BORDER_END = 99;
// Keyboard action codes
enum KBAction {KB_UP, KB_DOWN, KB_LEFT, KB_RIGHT,
KB_DIGLEFT, KB_DIGRIGHT, KB_STOP};
......
......@@ -26,21 +26,19 @@
#include <QDebug>
#include <QString>
#include <QGraphicsScene>
#include "kgrglobals.h"
#include "kgrthemetypes.h"
#include "kgrrenderer.h"
#include "kgrscene.h"
#include <cmath>
KGrRenderer::KGrRenderer (QGraphicsScene * scene)
KGrRenderer::KGrRenderer (KGrScene * scene)
:
QObject (scene),
m_scene (scene)
{
qDebug() << "KGrRenderer called";
// Set up two theme providers: for the Set and the Actors.
m_setProvider = new KgThemeProvider("Theme", this); // Save config.
m_actorsProvider = new KgThemeProvider("", this); // Do not save.
......@@ -76,6 +74,9 @@ KGrRenderer::KGrRenderer (QGraphicsScene * scene)
connect (m_setProvider, SIGNAL(currentThemeChanged(const KgTheme*)),
this, SLOT(currentThemeChanged(const KgTheme*)));
connect (m_setProvider, SIGNAL(currentThemeChanged(const KgTheme *)),
m_scene, SLOT(currentThemeChanged(const KgTheme *)));
// Match the starting SVG theme for the Actors to the one for the Set.
currentThemeChanged (m_setProvider->currentTheme());
}
......@@ -87,11 +88,13 @@ KGrRenderer::~KGrRenderer()
void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme)
{
qDebug() << "KGrRenderer::currentThemeChanged()";
// Start of game or change of theme: initialise the counts of pixmap keys.
initPixmapKeys();
// Make the Actors theme (hero, etc.) match the Set theme (bricks, etc.).
qDebug() << "KGrRenderer::currentThemeChanged()" << currentSetTheme->name();
// qDebug() << "KGrRenderer::currentThemeChanged()" << currentSetTheme->name();
foreach (const KgTheme * actorsTheme, m_actorsProvider->themes()) {
if (actorsTheme->customData("Set") ==
currentSetTheme->customData("Set")) {
......@@ -107,11 +110,6 @@ void KGrRenderer::currentThemeChanged (const KgTheme* currentSetTheme)
m_borderColor = QColor (s);
s = currentSetTheme->customData("TextColor", "#FFFFFF");
m_textColor = QColor (s);
// Fill the scene (and view) with the new background color. Do this even if
// the background has no border, to avoid ugly white rectangles appearing
// if rendering and painting is momentarily a bit slow.
m_scene->setBackgroundBrush (m_borderColor);
}
void KGrRenderer::selectTheme()
......@@ -167,6 +165,52 @@ KGameRenderedItem * KGrRenderer::getTileItem
return tile;
}
KGameRenderedItem * KGrRenderer::getBackground
(const int level, KGameRenderedItem * currentBackground)
{
if (currentBackground) {
m_scene->removeItem (currentBackground);
delete currentBackground;
}
QString key = getBackgroundKey (level);
KGameRenderedItem * background = new KGameRenderedItem (m_setRenderer, key);
return background;
}
QList <KGameRenderedItem *> KGrRenderer::borderTiles() const
{
qDebug() << "KGrRenderer::borderTiles()";
qDebug() << "m_setRenderer:" << m_setRenderer;
QList <KGameRenderedItem *> list;
// Corners.
list.append (new KGameRenderedItem (m_setRenderer, "frame-topleft"));
list.append (new KGameRenderedItem (m_setRenderer, "frame-topright"));
list.append (new KGameRenderedItem (m_setRenderer, "frame-bottomleft"));
list.append (new KGameRenderedItem (m_setRenderer, "frame-bottomright"));
// Upper side.
for (int i = 0; i < FIELDWIDTH; i++)
list.append (new KGameRenderedItem (m_setRenderer, "frame-top"));
// Lower side.
for (int i = 0; i < FIELDWIDTH; i++)
list.append (new KGameRenderedItem (m_setRenderer, "frame-bottom"));
// Left side.
for (int i = 0; i < FIELDHEIGHT; i++)
list.append (new KGameRenderedItem (m_setRenderer, "frame-left"));
// Right side.
for (int i = 0; i < FIELDHEIGHT; i++)
list.append (new KGameRenderedItem (m_setRenderer, "frame-right"));
return list;
}
QString KGrRenderer::getPixmapKey (const char picType, const int index)
{
QString pixmapKey = "";
......@@ -198,6 +242,8 @@ QString KGrRenderer::getBackgroundKey (const int level)
pixmapKey = pixmapKey.arg (level % frameCount);
}
}
qDebug() << "BACKGROUND pixmap key" << pixmapKey;
return pixmapKey;
}
......
......@@ -22,7 +22,7 @@
#include <QString>
#include <KGameRenderer>
class QGraphicsScene;
class KGrScene;
class KgThemeProvider;
class KgThemeSelector;
class KGameRenderedItem;
......@@ -59,7 +59,7 @@ class KGrRenderer : public QObject
{
Q_OBJECT
public:
KGrRenderer (QGraphicsScene * scene);
KGrRenderer (KGrScene * scene);
virtual ~KGrRenderer();
/*
......@@ -85,22 +85,22 @@ public:
KGameRenderedItem * currentTile);
/*
* Get the SVG element name for a KGoldrunner tile type. If the theme has
* more than one tile of that type (e.g. BRICK), make a random selection.
* Create the QGraphicsScene item for the background corresponding to the
* current level.
*
* @param picType The internal KGoldrunner type of a tile or background.
* @param level The current level in a KGoldrunner game.
* @param currentBackground The pre-existing background that is to be
* replaced, or zero if theres'no background yet.
*/
QString getPixmapKey (const char picType, const int index);
KGameRenderedItem * getBackground (const int level,
KGameRenderedItem * currentBackground);
/*
* Get the SVG element name for a KGoldrunner background. If the theme has
* more than one background, cycle though the choices as the KGoldrunner
* game's level changes.
*
* @param level The current level in a KGoldrunner game.
*/
QString getBackgroundKey (const int level);
bool hasBorder() const { return m_hasBorder; }
QColor borderColor() const { return m_borderColor; }
QColor textColor() const { return m_textColor; }
// TODO: Describe this method.
QList <KGameRenderedItem * > borderTiles() const;
public slots:
/*
* Show the theme-selector dialog. When the theme changes, KGrRenderer uses
......@@ -130,7 +130,7 @@ private:
// no suffix, >0 = number of variants.
};
QGraphicsScene * m_scene; // The scene to be rendered.
KGrScene * m_scene; // The scene to be rendered.
KgThemeProvider * m_setProvider; // Provider for Set themes.
KgThemeProvider * m_actorsProvider; // Provider for Actors themes.
......@@ -155,6 +155,23 @@ private:
// Count the number of variants of a tile or background.
int countFrames (const int index);
/*
* Get the SVG element name for a KGoldrunner tile type. If the theme has
* more than one tile of that type (e.g. BRICK), make a random selection.
*
* @param picType The internal KGoldrunner type of a tile or background.
*/
QString getPixmapKey (const char picType, const int index);
/*
* Get the SVG element name for a KGoldrunner background. If the theme has
* more than one background, cycle though the choices as the KGoldrunner
* game's level changes.
*
* @param level The current level in a KGoldrunner game.
*/
QString getBackgroundKey (const int level);
};
#endif // KGRRENDERER_H
......@@ -21,6 +21,7 @@
#include "kgrtheme.h"
#include <QDebug>
#include <QPainter>
#include <QFileInfo>
......@@ -266,9 +267,10 @@ QList< QPixmap > KGrTheme::namedTiles (QStringList names, unsigned int size)
{
QList< QPixmap > list;
QPixmap pix;
foreach (const QString &name, names) {
if (loadPixmap (QSize (size, size), name, pix, Set)) {
qDebug() << name;
list.append (pix);
}
else {
......@@ -296,6 +298,7 @@ QList< QPixmap > KGrTheme::frameTiles (unsigned int size)
{
QStringList tileNames;
KConfigGroup themeGroup (KGlobal::config(), "Theme_" + themeName);
qDebug() << themeName;
tileNames = themeGroup.readEntry ("BorderTiles", QStringList());
return namedTiles (tileNames, size);
......
......@@ -27,7 +27,7 @@
#include <QMainWindow>
#include <QGraphicsView>
#include "g.h"
#include "kgrview.h"
static const char description[] =
I18N_NOOP ("KGoldrunner is a game of action and puzzle solving");
......@@ -46,7 +46,7 @@ int main (int argc, char **argv)
KApplication app;
QMainWindow * window = new QMainWindow();
GV * view = new GV (window);
KGrView * view = new KGrView (window);
view->setMinimumSize (512, 384);
window->setCentralWidget (view);
......
......@@ -102,7 +102,7 @@ Description[zh_TW]=簡單又乾淨的 KDE 主題
Author=Mauricio Piacentini
Set=default/set.svg
Actors=default/actors.svg
DrawCanvasBorder=0
DrawCanvasBorder=1
BorderColor=#2C89A0
TextColor=#FFFFFF
Preview=default/default.png
......@@ -99,7 +99,7 @@ Description[zh_TW]=英雄被困在電腦裡面。
Author=Eugene Trounev and Mauricio Piacentini
Set=kgr_geek/set.svgz
Actors=kgr_geek/actors.svgz
DrawCanvasBorder=1
DrawCanvasBorder=0
BorderColor=#687D54
TextColor=#FFFFFF
Preview=kgr_geek/kgr_geek.png
......@@ -100,7 +100,7 @@ Description[zh_TW]=KGoldrunner 可調整的主題,就像是八位元時代的
Author=Luciano Montanaro
Set=nostalgia/set.svg
Actors=nostalgia/actors.svg
DrawCanvasBorder=0
DrawCanvasBorder=1
BorderColor=#000000
TextColor=#FFFFFF
Preview=nostalgia/nostalgia.png
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