diff --git a/src/main.cpp b/src/main.cpp index c1340be..44c8f17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,9 +21,16 @@ #include +#include "testgame.h" + +using namespace toruschess; + int main(int argc, char *argv[]) { - QApplication app(argc, argv); - qDebug("Hello from Qt 4!"); - return 0; + QApplication app(argc, argv); + Q_INIT_RESOURCE(media); + + TestGame testGame; + testGame.show(); + return app.exec(); } diff --git a/src/media.qrc b/src/media.qrc index 225a0fc..1021410 100644 --- a/src/media.qrc +++ b/src/media.qrc @@ -1,5 +1,16 @@ - ../media/Chess_bdt45.svg + ../media/Chess_plt45.svg + ../media/Chess_nlt45.svg + ../media/Chess_blt45.svg + ../media/Chess_rlt45.svg + ../media/Chess_qlt45.svg + ../media/Chess_klt45.svg + ../media/Chess_pdt45.svg + ../media/Chess_ndt45.svg + ../media/Chess_bdt45.svg + ../media/Chess_rdt45.svg + ../media/Chess_qdt45.svg + ../media/Chess_kdt45.svg diff --git a/src/testgame.cpp b/src/testgame.cpp index 68faa92..e665c84 100644 --- a/src/testgame.cpp +++ b/src/testgame.cpp @@ -19,9 +19,91 @@ ***************************************************************************/ #include "testgame.h" +#include +#include +#include + namespace toruschess { -TestGame::TestGame() { -} + PieceLibrary::PieceLibrary(QObject *parent) + : QObject(parent), m_pieces(0), m_buffers(0) { + setSize(45, 45); + } + + PieceLibrary::~PieceLibrary() { + delete [] m_pieces; + delete [] m_buffers; + } + + void PieceLibrary::setSize(int width, int height) { + m_width = qMax(10, width); + m_height = qMax(10, height); + if (m_buffers) { + for (int i = 0; i < 12; i++) { + delete m_buffers[i]; + } + } else { + m_pieces = new QSvgRenderer* [12]; + m_buffers = new QImage* [12]; + for (int i = 0; i < 12; i++) { + m_pieces[i] = new QSvgRenderer(QString(":/media/chess_%1.svg").arg(i+1), this); + } + } + for (int i = 0; i < 12; i++) { + m_buffers[i] = new QImage(QSize(m_width, m_height), QImage::Format_ARGB32_Premultiplied); + m_buffers[i]->fill(0x0); + QPainter p(m_buffers[i]); + p.setViewport(1, 1, m_width-1, m_height-1); + m_pieces[i]->render(&p); + } + } + + void PieceLibrary::paint(QPainter &pt, int piece) const { + if (piece == 0) return; + if (piece < 0) piece = 6 - piece; + if (piece <= 0 || piece > 12) return; + pt.drawImage(0, 0, *m_buffers[piece-1]); + } + + TestGame::TestGame(QWidget *parent) : QMainWindow(parent), m_game(new Game()) { + PieceLibrary *lib = new PieceLibrary(this); + setCentralWidget(new TestField(this, lib, m_game)); + } + + TestField::TestField(QWidget *parent, PieceLibrary *lib, Game *game) : QWidget(parent), m_lib(lib), m_game(game) { + QGridLayout *layout = new QGridLayout(); + for (int y = 0; y < 8; y++) { + for (int x = 0; x < 8; x++) { + layout->addWidget(new TestPlace(this, m_lib, m_game->field(), Pos(x, y)), y, x); + } + } + layout->setSpacing(0); + setLayout(layout); + show(); + } + + void TestField::resizeEvent(QResizeEvent *event) { + m_lib->setSize(event->size().width()/8, event->size().height()/8); + } + + TestPlace::TestPlace(QWidget *parent, const PieceLibrary *lib, const Field *field, const Pos &p) + : QWidget(parent), m_field(field), m_lib(lib), m_pos(p) { + setMinimumSize(m_lib->minPieceSize()); + } + + QSize TestPlace::sizeHint() const { + return m_lib->pieceSize(); + } + void TestPlace::paintEvent(QPaintEvent *) { + QPainter pt(this); + if ((m_pos.x() + m_pos.y()) % 2) { + // #D18B47 + pt.fillRect(this->rect(), QBrush(QColor(0xD1, 0x8B, 0x47))); + } else { + // #FFCE9E + pt.fillRect(this->rect(), QBrush(QColor(0xFF, 0xCE, 0x9E))); + } + m_lib->paint(pt, m_field->place(m_pos)); + } } diff --git a/src/testgame.h b/src/testgame.h index 85c91b5..eecd619 100644 --- a/src/testgame.h +++ b/src/testgame.h @@ -21,35 +21,75 @@ #define TORUSCHESSTESTGAME_H #include +#include +#include +#include #include "toruschess.h" +/** + @author Stefan Bühler +*/ namespace toruschess { - class TestPlace : public QWidget { + class PieceLibrary : public QObject { + Q_OBJECT public: - TestPlace(QObject *parent, const Field *field, const Pos p); + PieceLibrary(QObject *parent); + virtual ~PieceLibrary(); + + QSize pieceSize() const { return QSize(m_width, m_height); } + QSize minPieceSize() const { return QSize(20, 20); } + void setSize(int width, int height); + + void paint(QPainter &pt, int piece) const; + + private: + QSvgRenderer **m_pieces; + QImage **m_buffers; + int m_width, m_height; + }; + + class TestPlace : public QWidget { + Q_OBJECT + public: + TestPlace(QWidget *parent, const PieceLibrary *lib, const Field *field, const Pos &p); const Pos& pos() const { return m_pos; } const Field* field() const { return m_field; } + virtual QSize sizeHint() const; + + protected: + virtual void paintEvent(QPaintEvent *event); + private: const Field *m_field; + const PieceLibrary *m_lib; Pos m_pos; }; class TestField : public QWidget { - }; - - /** - @author Stefan Bühler - */ - class TestGame{ + Q_OBJECT public: - TestGame(); + TestField(QWidget *parent, PieceLibrary *lib, Game *m_game); + protected: + virtual void resizeEvent(QResizeEvent *event); + + private: + PieceLibrary *m_lib; + Game *m_game; + }; + + class TestGame : public QMainWindow { + Q_OBJECT + public: + TestGame(QWidget *parent = 0); + + private: + Game *m_game; }; - } #endif