2d graphics done

This commit is contained in:
Stefan Bühler 2009-01-06 17:02:15 +01:00
parent 25ba93da75
commit 77567f18c2
4 changed files with 156 additions and 16 deletions

View File

@ -21,9 +21,16 @@
#include <QApplication>
#include "testgame.h"
using namespace toruschess;
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
qDebug("Hello from Qt 4!");
return 0;
Q_INIT_RESOURCE(media);
TestGame testGame;
testGame.show();
return app.exec();
}

View File

@ -1,5 +1,16 @@
<RCC>
<qresource prefix="/media" >
<file alias="chess_1">../media/Chess_bdt45.svg</file>
<file alias="chess_1.svg">../media/Chess_plt45.svg</file>
<file alias="chess_2.svg">../media/Chess_nlt45.svg</file>
<file alias="chess_3.svg">../media/Chess_blt45.svg</file>
<file alias="chess_4.svg">../media/Chess_rlt45.svg</file>
<file alias="chess_5.svg">../media/Chess_qlt45.svg</file>
<file alias="chess_6.svg">../media/Chess_klt45.svg</file>
<file alias="chess_7.svg">../media/Chess_pdt45.svg</file>
<file alias="chess_8.svg">../media/Chess_ndt45.svg</file>
<file alias="chess_9.svg">../media/Chess_bdt45.svg</file>
<file alias="chess_10.svg">../media/Chess_rdt45.svg</file>
<file alias="chess_11.svg">../media/Chess_qdt45.svg</file>
<file alias="chess_12.svg">../media/Chess_kdt45.svg</file>
</qresource>
</RCC>

View File

@ -19,9 +19,91 @@
***************************************************************************/
#include "testgame.h"
#include <QGridLayout>
#include <QPainter>
#include <QResizeEvent>
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));
}
}

View File

@ -21,35 +21,75 @@
#define TORUSCHESSTESTGAME_H
#include <QWidget>
#include <QSvgRenderer>
#include <QMainWindow>
#include <QSize>
#include "toruschess.h"
namespace toruschess {
class TestPlace : public QWidget {
public:
TestPlace(QObject *parent, const Field *field, const Pos p);
const Pos& pos() const { return m_pos; }
const Field* field() const { return m_field; }
private:
const Field *m_field;
Pos m_pos;
};
class TestField : public QWidget {
};
/**
@author Stefan Bühler <stbuehler@web.de>
*/
class TestGame{
public:
TestGame();
namespace toruschess {
class PieceLibrary : public QObject {
Q_OBJECT
public:
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 {
Q_OBJECT
public:
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