The next incarnation of the Pong game written in C++ is called QPong because it uses Qt toolkit to provide its UI. The source code is available on Github as Xcode project. All base classes were copied without any modifications but integration with Qt required some fine adjustments in a way the project is built.
Since Qt is written in C++ there is no need to mix any languages as it was the case with Cocoa Pong application. Nonetheless I still have to create a custom implementation of the game's drawing context class which is called QDrawingContext:
#include <QtGui/QtGui>
#include "DrawingContext.h"
class QDrawingContext : public Pong::DrawingContext {
public:
QDrawingContext(QPainter *painter);
virtual ~QDrawingContext();
virtual void setFillColor(Pong::RGBColor const &color);
virtual void fillRectangle(Pong::Rectangle bounds);
virtual void fillOval(Pong::Rectangle bounds);
private:
QPainter *_painter;
QColor _qFillColor;
};
Note that I left #include statements and they reference 'QtGui/QtGui' instead of just 'QtGui' because that's how you should do it in Xcode. Its implementation uses QPainter to do the actual drawing:
QDrawingContext::QDrawingContext(QPainter *painter) : _qFillColor(0, 0, 0) {
_painter = painter;
}
QDrawingContext::~QDrawingContext() {
}
void QDrawingContext::setFillColor(Pong::RGBColor const &color) {
DrawingContext::setFillColor(color);
_qFillColor = QColor(color.red * 255, color.green * 255, color.blue * 255);
}
void QDrawingContext::fillRectangle(Pong::Rectangle bounds) {
_painter->setBrush(_qFillColor);
QRectF qbounds = QRectFromRectangle(bounds);
_painter->drawRect(qbounds);
}
void QDrawingContext::fillOval(Pong::Rectangle bounds) {
_painter->setBrush(_qFillColor);
QRectF qbounds = QRectFromRectangle(bounds);
_painter->drawEllipse(qbounds);
}
Although integration was easy for the source code it was not so for the IDE. It turns out that Qt has its own compiler called 'moc' which generates some additional C++ code for classes with Q_OBJECT macro. It is required to provide certain meta-information about the object and should be a part of the build process. Hopefully I was not the first person to cope with this so I found this handy post that explains how to make Xcode to create mocables. My version is roughly the same:
cd "$PROJECT_DIR"/QPong rm -f QPong.pro qmake -project -o QPong.pro qmake -spec macx-g++ QPong.pro make mocables
Note that you have to call this script before compilation phase. You should actually run it once first to generate moc_* files and add them to the project so they got compiled with the rest of the project files.