]> git.treefish.org Git - seamulator.git/commitdiff
Replaced sea and surface refs with shared pointers
authorAlexander Schmidt <alex@treefish.org>
Fri, 15 Jul 2016 10:58:31 +0000 (12:58 +0200)
committerAlexander Schmidt <alex@treefish.org>
Fri, 15 Jul 2016 10:58:31 +0000 (12:58 +0200)
sea.cpp
sea.h
seafwd.h [new file with mode: 0644]
seamulator.cpp
watersurface.h
watersurfacefwd.h [new file with mode: 0644]

diff --git a/sea.cpp b/sea.cpp
index 37a6cd67b04fe63e616f48d19d53be1d56f00018..42772d4d5534ba23e27bfbb2020c0c883ef766e9 100644 (file)
--- a/sea.cpp
+++ b/sea.cpp
@@ -2,15 +2,17 @@
 
 #include <cstdlib>
 
-Sea::Sea(WaterSurface& surface) : m_surface{surface}
+#include "watersurface.h"
+
+Sea::Sea(WaterSurfacePtr surface) : m_surface{surface}
 {
 }
 
 void Sea::update()
 {
-  for (int y = 0; y < m_surface.size(); ++y) {
-    for (int x = 0; x < m_surface.size(); ++x) {
-      m_surface.at(x, y)
+  for (int y = 0; y < m_surface->size(); ++y) {
+    for (int x = 0; x < m_surface->size(); ++x) {
+      m_surface->at(x, y)
        .setHeight(((double)std::rand()/(double)RAND_MAX));
     }
   }
diff --git a/sea.h b/sea.h
index 6a9cd0ad79b804a6d43383c553fa62407577e347..48307ee540e68741669cd75fb67a0725bb6ffcd3 100644 (file)
--- a/sea.h
+++ b/sea.h
@@ -1,13 +1,15 @@
 #pragma once
 
-#include "watersurface.h"
+#include "seafwd.h"
+
+#include "watersurfacefwd.h"
 
 class Sea
 {
  public:
-  Sea(WaterSurface& surface);
+  Sea(WaterSurfacePtr surface);
   void update();
 
  private:
-  WaterSurface& m_surface;
+  WaterSurfacePtr m_surface;
 };
diff --git a/seafwd.h b/seafwd.h
new file mode 100644 (file)
index 0000000..22977d7
--- /dev/null
+++ b/seafwd.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <memory>
+
+class Sea;
+using SeaPtr = std::shared_ptr<Sea>;
index 6bb51c9460df936cb0da508eadd6279ccecdc85c..b6739b5641f1a3332f6f2ddf63f8d2aa931a1e7d 100644 (file)
@@ -8,20 +8,20 @@
 const int LATTICE_SIZE = 10;
 const int LATTICE_UNIT = 1;
 
-WaterSurface surface(LATTICE_SIZE);
-Sea sea(surface);
+SeaPtr sea;
+WaterSurfacePtr surface;
 
 void drawSingleTile(int x, int y)
 {
   glBegin(GL_TRIANGLES);
 
-  glVertex3f(x, y, surface.at(x, y).getHeight());
-  glVertex3f(x+1, y, surface.at(x+1, y).getHeight());
-  glVertex3f(x+1, y+1, surface.at(x+1, y+1).getHeight());
+  glVertex3f(x, y, surface->at(x, y).getHeight());
+  glVertex3f(x+1, y, surface->at(x+1, y).getHeight());
+  glVertex3f(x+1, y+1, surface->at(x+1, y+1).getHeight());
 
-  glVertex3f(x, y, surface.at(x, y).getHeight());
-  glVertex3f(x, y+1, surface.at(x, y+1).getHeight());
-  glVertex3f(x+1, y+1, surface.at(x+1, y+1).getHeight());
+  glVertex3f(x, y, surface->at(x, y).getHeight());
+  glVertex3f(x, y+1, surface->at(x, y+1).getHeight());
+  glVertex3f(x+1, y+1, surface->at(x+1, y+1).getHeight());
 
   glEnd();
 }
@@ -37,7 +37,7 @@ void displayMe(void)
 
   glClear(GL_COLOR_BUFFER_BIT);
 
-  sea.update();
+  sea->update();
 
   glScalef(LATTICE_UNIT, LATTICE_UNIT, 1.0f);
   glTranslatef(-(float)(LATTICE_SIZE-1)/2, -(float)(LATTICE_SIZE-1)/2, 0);
@@ -56,6 +56,9 @@ int main(int argc, char** argv)
 {
   std::srand(std::time(0));
 
+  surface = std::make_shared<WaterSurface>(LATTICE_SIZE);
+  sea = std::make_shared<Sea>(surface);
+
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
   glEnable(GL_DEPTH_TEST);
index bb0c77acddd2d7571c56d4c39c338f3cc701f1ba..cf5829e02555e29651ea41b36798245e3ccb8807 100644 (file)
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "watersurfacefwd.h"
+
 #include <vector>
 
 #include "surfacepoint.h"
diff --git a/watersurfacefwd.h b/watersurfacefwd.h
new file mode 100644 (file)
index 0000000..efb7fe3
--- /dev/null
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <memory>
+
+class WaterSurface;
+using WaterSurfacePtr = std::shared_ptr<WaterSurface>;