From c97d3f98f912c1dbaadf8309a983915d5c5d721e Mon Sep 17 00:00:00 2001 From: giersch Date: Wed, 21 Nov 2007 11:53:09 +0000 Subject: [PATCH] . --- DrawingWindow.cpp | 96 +++++++++++++++++++++++++++++++++++-------- DrawingWindow.h | 22 ++++++---- chateaux/chateaux.cpp | 20 +++++++-- chateaux/chateaux.pro | 2 + test/hello.pro | 4 +- 5 files changed, 115 insertions(+), 29 deletions(-) diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index c68b7b8..02df2eb 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -1,7 +1,6 @@ #include "DrawingWindow.h" #include #include -#include #include #include @@ -56,32 +55,40 @@ DrawingWindow::~DrawingWindow() 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); } @@ -116,7 +123,7 @@ void DrawingWindow::drawRect(int x1, int y1, int x2, int y2) 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); } @@ -134,21 +141,58 @@ void DrawingWindow::drawCircle(int x, int y, int r) 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; @@ -289,13 +333,31 @@ void DrawingWindow::initialize(DrawingWindow::ThreadFunction f) } 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) { diff --git a/DrawingWindow.h b/DrawingWindow.h index 17253ba..17e9d0d 100644 --- a/DrawingWindow.h +++ b/DrawingWindow.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -34,10 +36,13 @@ public: 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(); @@ -48,7 +53,10 @@ public: 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); @@ -79,9 +87,6 @@ private: QImage *image; QPainter *painter; - QColor fgColor; - QColor bgColor; - bool dirtyFlag; QRect dirtyRect; @@ -89,7 +94,10 @@ private: 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); diff --git a/chateaux/chateaux.cpp b/chateaux/chateaux.cpp index 44e22d4..c5bb60f 100644 --- a/chateaux/chateaux.cpp +++ b/chateaux/chateaux.cpp @@ -4,6 +4,7 @@ #include #include #include +#include /* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et * de -10 à +140 en ordonnée @@ -200,10 +201,16 @@ void initialise(DrawingWindow& w) 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); } @@ -296,12 +303,17 @@ int jeu1(DrawingWindow& w) 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; } diff --git a/chateaux/chateaux.pro b/chateaux/chateaux.pro index 689b24a..bd9b921 100644 --- a/chateaux/chateaux.pro +++ b/chateaux/chateaux.pro @@ -12,6 +12,8 @@ profile { } INCLUDEPATH += ../ +DEPENDPATH += ../ + HEADERS += ../DrawingWindow.h SOURCES += ../DrawingWindow.cpp \ chateaux.cpp diff --git a/test/hello.pro b/test/hello.pro index e5f0db2..11492d8 100644 --- a/test/hello.pro +++ b/test/hello.pro @@ -3,7 +3,7 @@ TARGET = hello CONFIG += qt CONFIG += debug -#CONFIG += profile +CONFIG += profile profile { QMAKE_CFLAGS += -pg @@ -12,6 +12,8 @@ profile { } INCLUDEPATH += ../ +DEPENDPATH += ../ + HEADERS += ../DrawingWindow.h SOURCES += ../DrawingWindow.cpp \ hello.cpp -- 2.20.1