Commit f5983768 authored by Ian Wadham's avatar Ian Wadham

Add a test-target and test-code for testing the KGrRenderer class and SVG file-locator code.

svn path=/branches/work/kgoldrunner-qgv/; revision=1298067
parent c9def411
......@@ -27,9 +27,6 @@ kde4_add_executable(kgoldrunner ${kgoldrunner_SRCS})
target_link_libraries(kgoldrunner ${KDE4_KDEUI_LIBS} kdegames kdegamesprivate)
install(TARGETS kgoldrunner ${INSTALL_TARGETS_DEFAULT_ARGS} )
########### install files ###############
install( PROGRAMS KGoldrunner.desktop DESTINATION ${XDG_APPS_INSTALL_DIR} )
......@@ -37,6 +34,22 @@ install( FILES kgoldrunnerui.rc DESTINATION ${DATA_INSTALL_DIR}/kgoldrunner )
kde4_install_icons( ${ICON_INSTALL_DIR} )
########### next target ###############
set(kgrtest_SRCS # NOTE: This target (kgrtest) is for testing purposes only.
g.cpp
kgrthemetypes.cpp
kgrrenderer.cpp
testmain.cpp
)
kde4_add_executable(kgrtest ${kgrtest_SRCS})
target_link_libraries(kgrtest ${KDE4_KDEUI_LIBS} kdegames kdegamesprivate)
install(TARGETS kgoldrunner kgrtest ${INSTALL_TARGETS_DEFAULT_ARGS} )
......
#include "kgrglobals.h"
#include "kgrrenderer.h"
#include "g.h"
#include <QBrush>
#include <QGraphicsRectItem>
#include <KGameRenderedItem>
#include <QDebug>
struct Tile {const int i; const int j; const char type; };
static const Tile test[] = {
{2, 2, BRICK}, // One brick at each corner of the level-layout.
{2, 21, BRICK},
{29, 2, BRICK},
{29, 21, BRICK},
{10, 10, BRICK}, // A block of 12 bricks.
{10, 11, BRICK},
{10, 12, BRICK},
{10, 13, BRICK},
{11, 10, BRICK},
{11, 11, BRICK},
{11, 12, BRICK},
{11, 13, BRICK},
{12, 10, BRICK},
{12, 11, BRICK},
{12, 12, BRICK},
{12, 13, BRICK},
{14, 10, CONCRETE}, // A block of 12 pieces of concrete.
{14, 11, CONCRETE},
{14, 12, CONCRETE},
{14, 13, CONCRETE},
{15, 10, CONCRETE},
{15, 11, CONCRETE},
{15, 12, CONCRETE},
{15, 13, CONCRETE},
{16, 10, CONCRETE},
{16, 11, CONCRETE},
{16, 12, CONCRETE},
{16, 13, CONCRETE},
{18, 11, BAR}, // A row of 6 bars.
{19, 11, BAR},
{20, 11, BAR},
{21, 11, BAR},
{22, 11, BAR},
{23, 11, BAR},
{20, 13, LADDER}, // A 6-piece ladder.
{20, 14, LADDER},
{20, 15, LADDER},
{20, 16, LADDER},
{20, 17, LADDER},
{20, 18, LADDER},
{14, 20, NUGGET}, // Some gold pieces.
{15, 20, NUGGET},
{16, 20, NUGGET},
{17, 20, NUGGET},
{20, 20, HERO}, // Extra tiles for the editor.
{21, 20, ENEMY},
{22, 20, FBRICK},
{23, 20, HLADDER},
{0, 0, FREE}
};
GS::GS (QObject * parent)
:
QGraphicsScene (parent),
m_tilesWide (32),
m_tilesHigh (24),
m_tileSize (10)
{
m_renderer = new KGrRenderer (this);
m_renderSet = m_renderer->getSetRenderer();
m_renderActors = m_renderer->getActorsRenderer();
// IDW test.
m_grid = new QGraphicsRectItem(0, 0, m_tilesWide * m_tileSize,
m_tilesHigh * m_tileSize);
addItem (m_grid);
for (int lev = 10; lev <= 13; lev++) {
qDebug() << "Background" << lev << m_renderer->getBackgroundKey(lev);
}
}
GS::~GS()
{
}
void GS::redrawScene (QSize size)
{
// Centre the tile grid in the scene.
int tileSize = qMin (size.width()/m_tilesWide, size.height()/m_tilesHigh);
qDebug() << "View size" << size << "tile size" << tileSize << size.width() << "/" << m_tilesWide << "=" << size.width()/m_tilesWide << "|" << size.height() << "/" << m_tilesHigh << "=" << size.height()/m_tilesHigh;
setSceneRect (0, 0, size.width(), size.height());
m_gridTopLeft = QPoint ((size.width() - m_tilesWide * tileSize)/2,
(size.height() - m_tilesHigh * tileSize)/2);
qDebug() << "Top left is" << m_gridTopLeft;
m_grid->setRect ((size.width() - m_tilesWide * tileSize)/2,
(size.height() - m_tilesHigh * tileSize)/2,
m_tilesWide * tileSize, m_tilesHigh * tileSize);
// NOTE: The background picture can be the same size as the level-layout, as
// in this example (Egypt theme), OR the same size as the entire viewport.
if (m_tiles.count() == 0) {
QString pixmapKey = m_renderer->getPixmapKey (BACKDROP);
m_background = new KGameRenderedItem (m_renderSet, pixmapKey);
addItem (m_background);
qDebug() << "BACKGROUND pixmap key" << pixmapKey;
}
if (tileSize != m_tileSize) {
m_background->setRenderSize (QSize ((m_tilesWide - 4) * tileSize,
(m_tilesHigh - 4) * tileSize));
}
m_background->setPos (m_gridTopLeft.x() + 2 * tileSize,
m_gridTopLeft.y() + 2 * tileSize);
if (m_tiles.count() == 0) {
m_tileSize = tileSize;
loadTestItems();
return;
}
redrawTestItems (tileSize);
m_tileSize = tileSize;
}
void GS::loadTestItems()
{
m_tiles.fill (0, m_tilesWide * m_tilesHigh);
int index = 0;
while (test[index].type != FREE) {
qDebug() << "Tile" << index << test[index].type << "at" <<
test[index].i << test[index].j;
paintCell (test[index].i, test[index].j, test[index].type);
index++;
}
m_hero = new KGameRenderedItem (m_renderActors, "hero");
addItem (m_hero);
m_hero->setRenderSize (QSize (m_tileSize, m_tileSize));
m_hero->setPos (m_gridTopLeft.x() + 8 * m_tileSize,
m_gridTopLeft.y() + 3 * m_tileSize);
m_hero->setFrame (22);
m_enemy1 = new KGameRenderedItem (m_renderActors, "enemy");
addItem (m_enemy1);
m_enemy1->setRenderSize (QSize (m_tileSize, m_tileSize));
m_enemy1->setPos (m_gridTopLeft.x() + 8 * m_tileSize,
m_gridTopLeft.y() + 4 * m_tileSize);
m_enemy1->setFrame (22);
m_enemy2 = new KGameRenderedItem (m_renderActors, "gold_enemy");
addItem (m_enemy2);
m_enemy2->setRenderSize (QSize (m_tileSize, m_tileSize));
m_enemy2->setPos (m_gridTopLeft.x() + 8 * m_tileSize,
m_gridTopLeft.y() + 5 * m_tileSize);
m_enemy2->setFrame (22);
m_brick = new KGameRenderedItem (m_renderSet, "brick");
addItem (m_brick);
m_brick->setRenderSize (QSize (m_tileSize, m_tileSize));
m_brick->setPos (m_gridTopLeft.x() + 8 * m_tileSize,
m_gridTopLeft.y() + 6 * m_tileSize);
m_brick->setFrame (1);
}
void GS::redrawTestItems (const int tileSize)
{
// Re-size and re-position tiles.
for (int i = 0; i < m_tilesWide; i++) {
for (int j = 0; j < m_tilesHigh; j++) {
int t = i * m_tilesHigh + j;
if (m_tiles.at(t) != 0) {
if (tileSize != m_tileSize) {
m_tiles.at(t)->setRenderSize (QSize (tileSize, tileSize));
}
m_tiles.at(t)->setPos (m_gridTopLeft.x() + i * tileSize,
m_gridTopLeft.y() + j * tileSize);
qDebug() << "Tile" << m_tiles.at(t)->pixmap().size() <<
"at" << m_tiles.at(t)->pos();
}
}
}
// Re-size, re-position and test-animate sprites.
if (tileSize != m_tileSize) {
m_hero->setRenderSize (QSize (tileSize, tileSize));
m_enemy1->setRenderSize (QSize (tileSize, tileSize));
m_enemy2->setRenderSize (QSize (tileSize, tileSize));
m_brick->setRenderSize (QSize (tileSize, tileSize));
}
m_hero->setPos (m_gridTopLeft.x() + 8 * tileSize,
m_gridTopLeft.y() + 3 * tileSize);
m_hero->setFrame (m_hero->frame() + 1);
m_enemy1->setPos (m_gridTopLeft.x() + 8 * tileSize,
m_gridTopLeft.y() + 4 * tileSize);
m_enemy1->setFrame (m_enemy1->frame() + 1);
m_enemy2->setPos (m_gridTopLeft.x() + 8 * tileSize,
m_gridTopLeft.y() + 5 * tileSize);
m_enemy2->setFrame (m_enemy2->frame() + 1);
m_brick->setPos (m_gridTopLeft.x() + 8 * tileSize,
m_gridTopLeft.y() + 6 * tileSize);
m_brick->setFrame (m_brick->frame() + 1);
}
void GS::paintCell (const int i, const int j, const char type)
{
int offset = i * m_tilesHigh + j;
qDebug() << "Offset" << offset << "i,j" << i << j;
if (m_tiles.at(offset) != 0) {
// TODO: Delete this tile? Replace it with another type?
}
QString pixmapKey = m_renderer->getPixmapKey (type);
if ((type == HERO) || (type == ENEMY)) {
// Stationary pixmaps from the Actors file, used only by the editor.
m_tiles[offset] = new KGameRenderedItem (m_renderActors, pixmapKey);
}
else {
// Pixmaps from the Set file, used in level-layouts and in the editor.
m_tiles[offset] = new KGameRenderedItem (m_renderSet, pixmapKey);
}
addItem (m_tiles.at(offset));
m_tiles.at(offset)->setRenderSize (QSize (m_tileSize, m_tileSize));
m_tiles.at(offset)->setPos (m_gridTopLeft.x() + (i + 1) * m_tileSize,
m_gridTopLeft.y() + (j + 1) * m_tileSize);
qDebug() << "Tile" << type << "i,j" << i << j << "size" << m_tileSize <<
"at" << m_tiles.at(offset)->pos();
}
// Minimal QGraphicsView code.
GV::GV (QWidget * parent)
:
QGraphicsView (parent),
m_scene (new GS (parent))
{
setScene (m_scene);
}
GV::~GV()
{
}
#include "g.moc"
// These are rough QGraphicsScene and QGraphicsView classes for testing the
// KGrRenderer class, but they illustrate points about KGoldrunner graphics.
//
// 1. The scene has examples of every type of SVG element used in KGr.
// 2. There is a brick marking each corner of the level-layout.
// 3. There is an empty border, at least two bricks wide, around the layout.
// 4. The border usually holds the scores and titles and acts as a buffer zone
// for mouse moves.
// 5. The rectangle around the level-layout and border is there for testing
// purposes only and is not part of KGoldrunner.
// 6. Again, purely for testing purposes, the sprites at the top left change
// their animation frames as the view is resized.
// 7. The border and its contents have not been drawn.
// 8. Some borders (e.g. Egypt) contain border tiles (not drawn here).
// 9. Some (e.g. Default) contain display-tiles for scores (not drawn here).
//
#ifndef G_H
#define G_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QResizeEvent>
#include <QGraphicsRectItem>
#include <QVector>
#include <QPoint>
#include <KGameRenderedItem>
class KGrRenderer;
class KGameRenderer;
class GS : public QGraphicsScene
{
Q_OBJECT
public:
GS (QObject * parent = 0);
~GS();
void redrawScene (QSize size);
public slots:
void paintCell (const int i, const int j, const char type);
private:
KGrRenderer * m_renderer;
KGameRenderer * m_renderSet;
KGameRenderer * m_renderActors;
int m_tileSize;
int m_tilesWide;
int m_tilesHigh;
QVector<KGameRenderedItem *> m_tiles;
QPoint m_gridTopLeft;
QGraphicsRectItem * m_grid;
KGameRenderedItem * m_background;
KGameRenderedItem * m_hero;
KGameRenderedItem * m_enemy1;
KGameRenderedItem * m_enemy2;
KGameRenderedItem * m_brick;
void loadTestItems();
void redrawTestItems (const int tileSize);
};
class GV : public QGraphicsView
{
Q_OBJECT
public:
GV(QWidget * parent = 0);
~GV();
/* void setGridSize() {
if (scene() != 0) {
fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
}
}; */
protected:
void resizeEvent(QResizeEvent* event) {
if (scene() != 0) {
m_scene->redrawScene (event->size());
// fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
}
QGraphicsView::resizeEvent(event);
};
GS * m_scene;
};
#endif G_H
// This main program is only a vehicle for testing g.h, g.cpp, kgrrenderer.h,
// kgrrenderer.cpp, kgrthemetypes.h and kgrthemetypes.cpp. Testmain.cpp, g.h
// and g.cpp are not part of the final KGoldrunner.
#include <KAboutData>
#include <KCmdLineArgs>
#include <KLocale>
#include <KApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include "g.h"
static const char description[] =
I18N_NOOP ("KGoldrunner is a game of action and puzzle solving");
// The intention is to keep the KGoldrunner version number in line with KDE.
static const char version[] = "4.9";
int main (int argc, char **argv)
{
KAboutData about ("kgoldrunner", 0, ki18n ("KGoldrunner"),
version, ki18n (description),
KAboutData::License_GPL,
ki18n ("(C) 2003 Ian Wadham and Marco Krüger"),
KLocalizedString(), "http://games.kde.org/kgoldrunner" );
KCmdLineArgs::init (argc, argv, &about);
KApplication app;
QMainWindow * window = new QMainWindow();
GV * view = new GV (window);
view->setMinimumSize (512, 384);
window->setCentralWidget (view);
window->show();
return app.exec();
}
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