Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
authorgiersch <giersch>
Wed, 21 Nov 2007 11:53:09 +0000 (11:53 +0000)
committergiersch <giersch>
Wed, 21 Nov 2007 11:53:09 +0000 (11:53 +0000)
DrawingWindow.cpp
DrawingWindow.h
chateaux/chateaux.cpp
chateaux/chateaux.pro
test/hello.pro

index c68b7b8..02df2eb 100644 (file)
@@ -1,7 +1,6 @@
 #include "DrawingWindow.h"
 #include <QApplication>
 #include <QPaintEvent>
 #include "DrawingWindow.h"
 #include <QApplication>
 #include <QPaintEvent>
-#include <QPainter>
 #include <QThread>
 #include <QTimerEvent>
 
 #include <QThread>
 #include <QTimerEvent>
 
@@ -56,32 +55,40 @@ DrawingWindow::~DrawingWindow()
     delete image;
 }
 
     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)
 {
 }
 
 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)
 {
 }
 
 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);
 }
 
 void DrawingWindow::clearGraph()
 {
     safeLock(imageMutex);
-    painter->fillRect(image->rect(), bgColor);    
+    painter->fillRect(image->rect(), getBgColor());
     dirty();
     safeUnlock(imageMutex);
 }
     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)
 {
 
 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);
 }
     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)
 {
 
 void DrawingWindow::fillCircle(int x, int y, int r)
 {
-    painter->setBrush(fgColor);
+    painter->setBrush(getColor());
     drawCircle(x, y, r);
     painter->setBrush(Qt::NoBrush);
 }
 
     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());
 {
     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);
     safeLock(imageMutex);
-    painter->drawText(r, 0, text, &r);
+    painter->drawText(r, flags, text, &r);
     dirty(r);
     safeUnlock(imageMutex);
 }
 
     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;
 bool DrawingWindow::sync(unsigned long time)
 {
     bool synced;
@@ -289,13 +333,31 @@ void DrawingWindow::initialize(DrawingWindow::ThreadFunction f)
 }
 
 inline
 }
 
 inline
-void DrawingWindow::applyColor()
+void DrawingWindow::setColor(const QColor& color)
 {
     QPen pen(painter->pen());
 {
     QPen pen(painter->pen());
-    pen.setColor(fgColor);
+    pen.setColor(color);
     painter->setPen(pen);
 }
 
     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)
 {
 inline
 void DrawingWindow::safeLock(QMutex &mutex)
 {
index 17253ba..17e9d0d 100644 (file)
@@ -5,6 +5,8 @@
 #include <QColor>
 #include <QImage>
 #include <QMutex>
 #include <QColor>
 #include <QImage>
 #include <QMutex>
+#include <QPainter>
+#include <QPen>
 #include <QRect>
 #include <QWaitCondition>
 #include <QWidget>
 #include <QRect>
 #include <QWaitCondition>
 #include <QWidget>
@@ -34,10 +36,13 @@ public:
     const int height;
 
     // http://www.w3.org/TR/SVG/types.html#ColorKeywords
     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 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(const char *name);
+    void setBgColor(float red, float green, float blue);
 
     void clearGraph();
 
 
     void clearGraph();
 
@@ -48,7 +53,10 @@ public:
     void drawCircle(int x, int y, int r);
     void fillCircle(int x, int y, int r);
 
     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);
 
 
     bool sync(unsigned long time = ULONG_MAX);
 
@@ -79,9 +87,6 @@ private:
     QImage *image;
     QPainter *painter;
 
     QImage *image;
     QPainter *painter;
 
-    QColor fgColor;
-    QColor bgColor;
-
     bool dirtyFlag;
     QRect dirtyRect;
 
     bool dirtyFlag;
     QRect dirtyRect;
 
@@ -89,7 +94,10 @@ private:
 
     void initialize(ThreadFunction f);
 
 
     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);
 
     void safeLock(QMutex &mutex);
     void safeUnlock(QMutex &mutex);
index 44e22d4..c5bb60f 100644 (file)
@@ -4,6 +4,7 @@
 #include <iostream>
 #include <ctime>
 #include <cstdlib>
 #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
 
 /* 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);
     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.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);
 }
 
@@ -296,12 +303,17 @@ int jeu1(DrawingWindow& w)
         dessineVent(w, wnd);
     } while (!perdant);
     dessineFlammes(w, x, y);
         dessineVent(w, wnd);
     } while (!perdant);
     dessineFlammes(w, x, y);
-    std::cout << "Joueur " << perdant;
+    std::stringstream msg;
+    msg << "Joueur " << perdant;
     if (perdant == joueur)
     if (perdant == joueur)
-        std::cout << " s'est suicidé";
+        msg << " s'est suicidé !";
     else
     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;
 }
 
     return perdant;
 }
 
index 689b24a..bd9b921 100644 (file)
@@ -12,6 +12,8 @@ profile {
 }
 
 INCLUDEPATH += ../
 }
 
 INCLUDEPATH += ../
+DEPENDPATH += ../
+
 HEADERS += ../DrawingWindow.h
 SOURCES += ../DrawingWindow.cpp \
            chateaux.cpp
 HEADERS += ../DrawingWindow.h
 SOURCES += ../DrawingWindow.cpp \
            chateaux.cpp
index e5f0db2..11492d8 100644 (file)
@@ -3,7 +3,7 @@ TARGET = hello
 
 CONFIG += qt
 CONFIG += debug
 
 CONFIG += qt
 CONFIG += debug
-#CONFIG += profile
+CONFIG += profile
 
 profile {
        QMAKE_CFLAGS += -pg
 
 profile {
        QMAKE_CFLAGS += -pg
@@ -12,6 +12,8 @@ profile {
 }
 
 INCLUDEPATH += ../
 }
 
 INCLUDEPATH += ../
+DEPENDPATH += ../
+
 HEADERS += ../DrawingWindow.h
 SOURCES += ../DrawingWindow.cpp \
            hello.cpp
 HEADERS += ../DrawingWindow.h
 SOURCES += ../DrawingWindow.cpp \
            hello.cpp