From 2b31ab37a9b9d7d47ed5aad5a58516ec091a558b Mon Sep 17 00:00:00 2001
From: Nick Gasson <nick@nickg.me.uk>
Date: Sat, 23 May 2009 15:10:05 +0100
Subject: [PATCH] First attempt at water

---
 include/IMap.hpp      |  5 +++++
 include/IQuadTree.hpp |  3 +--
 src/Editor.cpp        |  2 ++
 src/Map.cpp           | 45 +++++++++++++++++++++++--------------------
 src/QuadTree.cpp      |  2 +-
 5 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/include/IMap.hpp b/include/IMap.hpp
index bdec9ae..7ecc0dd 100644
--- a/include/IMap.hpp
+++ b/include/IMap.hpp
@@ -72,6 +72,11 @@ public:
    // Toggle display of grid lines
    virtual void setGrid(bool onOff) = 0;
 
+   // Toggle pick mode on and off
+   // This turns of display of everything but the terrain
+   // and things that can be clicked on
+   virtual void setPickMode(bool onOff) = 0;
+
    // Make a hill or valley in the given area
    virtual void raiseArea(const Point<int>& aStartPos,
                           const Point<int>& aFinishPos) = 0;
diff --git a/include/IQuadTree.hpp b/include/IQuadTree.hpp
index 9473566..6af0dd2 100644
--- a/include/IQuadTree.hpp
+++ b/include/IQuadTree.hpp
@@ -28,8 +28,7 @@ struct ISectorRenderable {
    virtual ~ISectorRenderable() {}
 
    virtual void renderSector(IGraphicsPtr aContext,
-                             Point<int> botLeft, Point<int> topRight,
-                             bool pickMode) = 0;
+                             Point<int> botLeft, Point<int> topRight) = 0;
 };
 
 typedef std::shared_ptr<ISectorRenderable> ISectorRenderablePtr;
diff --git a/src/Editor.cpp b/src/Editor.cpp
index d8f4c85..4f8b272 100644
--- a/src/Editor.cpp
+++ b/src/Editor.cpp
@@ -399,9 +399,11 @@ void Editor::onMouseMove(IPickBufferPtr aPickBuffer, int x, int y)
 {
    if (amDragging) {
       // Extend the selection rectangle
+      myMap->setPickMode(true);
       IGraphicsPtr pickContext = aPickBuffer->beginPick(x, y);
       display(pickContext);
       int id = aPickBuffer->endPick();
+      myMap->setPickMode(false);
 
       if (id > 0)
          myDragEnd = myMap->pickPosition(id);
diff --git a/src/Map.cpp b/src/Map.cpp
index db9e89e..b51c15e 100644
--- a/src/Map.cpp
+++ b/src/Map.cpp
@@ -75,6 +75,7 @@ public:
 
    void setStart(int x, int y) { myStartLocation = makePoint(x, y); }
    void setGrid(bool onOff);
+   void setPickMode(bool onOff) { inPickMode = onOff; }
    
    Track::Connection startLocation() const;
    ITrackSegmentPtr trackAt(const Point<int>& aPoint) const;
@@ -93,7 +94,7 @@ public:
    
    // ISectorRenderable interface
    void renderSector(IGraphicsPtr aContext,
-                     Point<int> botLeft, Point<int> topRight, bool pickMode);
+                     Point<int> botLeft, Point<int> topRight);
 private:
    // Tiles on the map
    struct Tile {
@@ -161,13 +162,13 @@ private:
    Point<int> myStartLocation;
    IQuadTreePtr myQuadTree;
    IFogPtr myFog;
-   bool shouldDrawGridLines;
+   bool shouldDrawGridLines, inPickMode;
 };
 
 Map::Map()
    : myTiles(NULL), myHeightMap(NULL), myWidth(0), myDepth(0),
      myStartLocation(makePoint(1, 1)),
-     shouldDrawGridLines(false)
+     shouldDrawGridLines(false), inPickMode(false)
 {
    myFog = makeFog(0.6, 0.7, 0.8,  // Colour
                    0.25,           // Density
@@ -332,7 +333,7 @@ void Map::highlightTile(IGraphicsPtr aContext, const Point<int>& aPoint) const
 
 // Render a small part of the map as directed by the quad tree
 void Map::renderSector(IGraphicsPtr aContext,
-                       Point<int> botLeft, Point<int> topRight, bool pickMode)
+                       Point<int> botLeft, Point<int> topRight)
 {
 #define RGB(r, g, b) r/255.0f, g/255.0f, b/255.0f
    
@@ -343,21 +344,6 @@ void Map::renderSector(IGraphicsPtr aContext,
       make_tuple(    0.0f,      RGB(133, 204, 98) ),
       make_tuple(   -1e10f,     RGB(178, 247, 220) )
    };
-
-   debug() << botLeft << topRight;
-
-   // Draw the water
-   if (!pickMode) {
-      static const float seaLevel = -1.0f;
-      glColor3f(0.0f, 0.0f, 1.0f);
-      glNormal3f(0.0f, 1.0f, 0.0f);
-      glBegin(GL_QUADS);
-      glVertex3f(botLeft.x, seaLevel, botLeft.y);
-      glVertex3f(botLeft.x, seaLevel, topRight.y);
-      glVertex3f(topRight.x, seaLevel, topRight.y);
-      glVertex3f(topRight.x, seaLevel, botLeft.y);
-      glEnd();
-   }
       
    for (int x = topRight.x-1; x >= botLeft.x; x--) {
       for (int y = botLeft.y; y < topRight.y; y++) {
@@ -392,7 +378,7 @@ void Map::renderSector(IGraphicsPtr aContext,
          //   drawNormal(v.pos, v.normal);
          //}
          
-         if (shouldDrawGridLines && !pickMode) {
+         if (shouldDrawGridLines && !inPickMode) {
             // Render grid lines
             glColor3f(0.0f, 0.0f, 0.0f);
             glBegin(GL_LINE_LOOP);
@@ -407,7 +393,7 @@ void Map::renderSector(IGraphicsPtr aContext,
             glEnd();
          }
 
-         if (!pickMode) {
+         if (!inPickMode) {
             // Draw the track, if any
             Tile& tile = tileAt(x, y);
             if (tile.track && !tile.track->marked()) {
@@ -424,6 +410,23 @@ void Map::renderSector(IGraphicsPtr aContext,
          glPopName();
       }			
    }
+
+   // Draw the water
+   if (!inPickMode) {
+      glPushName(NULL_OBJECT);
+      
+      static const float seaLevel = -0.1f;
+      glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
+      glNormal3f(0.0f, 1.0f, 0.0f);
+      glBegin(GL_QUADS);
+      glVertex3f(botLeft.x - 0.5f, seaLevel, botLeft.y - 0.5f);
+      glVertex3f(botLeft.x - 0.5f, seaLevel, topRight.y - 0.5f);
+      glVertex3f(topRight.x - 0.5f, seaLevel, topRight.y - 0.5f);
+      glVertex3f(topRight.x - 0.5f, seaLevel, botLeft.y - 0.5f);
+      glEnd();
+
+      glPopName();
+   }
 }
 
 // Called when we've changed the height of part of a tile
diff --git a/src/QuadTree.cpp b/src/QuadTree.cpp
index 976e57f..c439a2e 100644
--- a/src/QuadTree.cpp
+++ b/src/QuadTree.cpp
@@ -148,7 +148,7 @@ void QuadTree::displaySector(IGraphicsPtr aContext, int aSector)
    // See if it's a leaf
    if (mySectors[aSector].type == QT_LEAF)
       myRenderer->renderSector
-         (aContext, mySectors[aSector].botLeft, mySectors[aSector].topRight, true);
+         (aContext, mySectors[aSector].botLeft, mySectors[aSector].topRight);
    else {
       // Loop through each sector
       for (int i = 3; i >= 0; i--) {
-- 
2.39.5