#include "DrawingWindow.h"
#include <QApplication>
#include <QPaintEvent>
-#include <QPainter>
#include <QThread>
#include <QTimerEvent>
delete image;
}
-void DrawingWindow::setColor(float red, float green, float blue)
+void DrawingWindow::setColor(unsigned int color)
{
- fgColor.setRgbF(red, green, blue);
- applyColor();
+ setColor(QColor::fromRgb(color));
}
void DrawingWindow::setColor(const char *name)
{
- fgColor.setNamedColor(name);
- applyColor();
+ setColor(QColor(name));
}
-void DrawingWindow::setBgColor(float red, float green, float blue)
+void DrawingWindow::setColor(float red, float green, float blue)
{
- bgColor.setRgbF(red, green, blue);
+ setColor(QColor::fromRgbF(red, green, blue));
+}
+
+void DrawingWindow::setBgColor(unsigned int color)
+{
+ setBgColor(QColor::fromRgb(color));
}
void DrawingWindow::setBgColor(const char *name)
{
- bgColor.setNamedColor(name);
+ setBgColor(QColor(name));
+}
+
+void DrawingWindow::setBgColor(float red, float green, float blue)
+{
+ setBgColor(QColor::fromRgbF(red, green, blue));
}
void DrawingWindow::clearGraph()
{
safeLock(imageMutex);
- painter->fillRect(image->rect(), bgColor);
+ painter->fillRect(image->rect(), getBgColor());
dirty();
safeUnlock(imageMutex);
}
void DrawingWindow::fillRect(int x1, int y1, int x2, int y2)
{
- painter->setBrush(fgColor);
+ painter->setBrush(getColor());
drawRect(x1, y1, x2, y2);
painter->setBrush(Qt::NoBrush);
}
void DrawingWindow::fillCircle(int x, int y, int r)
{
- painter->setBrush(fgColor);
+ painter->setBrush(getColor());
drawCircle(x, y, r);
painter->setBrush(Qt::NoBrush);
}
-void DrawingWindow::drawText(int x, int y, const char *text)
+void DrawingWindow::drawText(int x, int y, const char *text, int flags)
{
QRect r(image->rect());
- r.moveTo(x, y);
+ switch (flags & Qt::AlignHorizontal_Mask) {
+ case Qt::AlignRight:
+ r.setRight(x);
+ break;
+ case Qt::AlignHCenter:
+ if (x < width / 2)
+ r.setLeft(2 * x - width + 1);
+ else
+ r.setRight(2 * x);
+ break;
+ default:
+ r.setLeft(x);
+ }
+ switch (flags & Qt::AlignVertical_Mask) {
+ case Qt::AlignBottom:
+ r.setBottom(y);
+ break;
+ case Qt::AlignVCenter:
+ if (y < height / 2)
+ r.setTop(2 * y - height + 1);
+ else
+ r.setBottom(2 * y);
+ break;
+ default:
+ r.setTop(y);
+ }
safeLock(imageMutex);
- painter->drawText(r, 0, text, &r);
+ painter->drawText(r, flags, text, &r);
dirty(r);
safeUnlock(imageMutex);
}
+void DrawingWindow::drawTextBg(int x, int y, const char *text, int flags)
+{
+ painter->setBackgroundMode(Qt::OpaqueMode);
+ drawText(x, y, text, flags);
+ painter->setBackgroundMode(Qt::TransparentMode);
+}
+
+unsigned int DrawingWindow::getPointColor(int x, int y)
+{
+ return image->pixel(x, y);
+}
+
bool DrawingWindow::sync(unsigned long time)
{
bool synced;
}
inline
-void DrawingWindow::applyColor()
+void DrawingWindow::setColor(const QColor& color)
{
QPen pen(painter->pen());
- pen.setColor(fgColor);
+ pen.setColor(color);
painter->setPen(pen);
}
+inline
+void DrawingWindow::setBgColor(const QColor& color)
+{
+ painter->setBackground(color);
+}
+
+inline
+QColor DrawingWindow::getColor()
+{
+ return painter->pen().color();
+}
+
+inline
+QColor DrawingWindow::getBgColor()
+{
+ return painter->background().color();
+}
+
inline
void DrawingWindow::safeLock(QMutex &mutex)
{
#include <QColor>
#include <QImage>
#include <QMutex>
+#include <QPainter>
+#include <QPen>
#include <QRect>
#include <QWaitCondition>
#include <QWidget>
const int height;
// http://www.w3.org/TR/SVG/types.html#ColorKeywords
- void setColor(float red, float green, float blue);
+ void setColor(unsigned int color);
void setColor(const char *name);
- void setBgColor(float red, float green, float blue);
+ void setColor(float red, float green, float blue);
+
+ void setBgColor(unsigned int color);
void setBgColor(const char *name);
+ void setBgColor(float red, float green, float blue);
void clearGraph();
void drawCircle(int x, int y, int r);
void fillCircle(int x, int y, int r);
- void drawText(int x, int y, const char *text);
+ void drawText(int x, int y, const char *text, int flags = 0);
+ void drawTextBg(int x, int y, const char *text, int flags = 0);
+
+ unsigned int getPointColor(int x, int y);
bool sync(unsigned long time = ULONG_MAX);
QImage *image;
QPainter *painter;
- QColor fgColor;
- QColor bgColor;
-
bool dirtyFlag;
QRect dirtyRect;
void initialize(ThreadFunction f);
- void applyColor();
+ void setColor(const QColor& color);
+ void setBgColor(const QColor& color);
+ QColor getColor();
+ QColor getBgColor();
void safeLock(QMutex &mutex);
void safeUnlock(QMutex &mutex);
#include <iostream>
#include <ctime>
#include <cstdlib>
+#include <sstream>
/* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et
* de -10 à +140 en ordonnée
largeurMont = frand(largeurMin, largeurMax);
hauteurMont = frand(hauteurMin, hauteurMax);
wnd = frand(-30, 30);
+ w.setBgColor("white");
w.clearGraph();
dessineTerrain(w, largeurMont, hauteurMont);
dessineChateau(w, positionChateau1);
dessineChateau(w, positionChateau2);
+ w.setColor("wheat");
+ w.drawText(rtowX(w, positionChateau1), rtowY(w, 0) + 8, "Joueur 1",
+ Qt::AlignHCenter);
+ w.drawText(rtowX(w, positionChateau2), rtowY(w, 0) + 8, "Joueur 2",
+ Qt::AlignHCenter);
dessineVent(w, wnd);
}
dessineVent(w, wnd);
} while (!perdant);
dessineFlammes(w, x, y);
- std::cout << "Joueur " << perdant;
+ std::stringstream msg;
+ msg << "Joueur " << perdant;
if (perdant == joueur)
- std::cout << " s'est suicidé";
+ msg << " s'est suicidé !";
else
- std::cout << " a perdu";
- std::cout << " !\n";
+ msg << " a perdu !";
+ w.setColor("darkred");
+ w.setBgColor("white");
+ w.drawTextBg(w.width / 2, w.height / 3, msg.str().c_str(),
+ Qt::AlignCenter);
+ std::cout << msg.str() << "\n";
return perdant;
}