From 31e7aa23c4e95b5e37f33938e9bb47ea82a3f4d9 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Fri, 15 Jul 2016 12:58:31 +0200 Subject: [PATCH] Replaced sea and surface refs with shared pointers --- sea.cpp | 10 ++++++---- sea.h | 8 +++++--- seafwd.h | 6 ++++++ seamulator.cpp | 21 ++++++++++++--------- watersurface.h | 2 ++ watersurfacefwd.h | 6 ++++++ 6 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 seafwd.h create mode 100644 watersurfacefwd.h diff --git a/sea.cpp b/sea.cpp index 37a6cd6..42772d4 100644 --- a/sea.cpp +++ b/sea.cpp @@ -2,15 +2,17 @@ #include -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 6a9cd0a..48307ee 100644 --- 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 index 0000000..22977d7 --- /dev/null +++ b/seafwd.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +class Sea; +using SeaPtr = std::shared_ptr; diff --git a/seamulator.cpp b/seamulator.cpp index 6bb51c9..b6739b5 100644 --- a/seamulator.cpp +++ b/seamulator.cpp @@ -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(LATTICE_SIZE); + sea = std::make_shared(surface); + glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glEnable(GL_DEPTH_TEST); diff --git a/watersurface.h b/watersurface.h index bb0c77a..cf5829e 100644 --- a/watersurface.h +++ b/watersurface.h @@ -1,5 +1,7 @@ #pragma once +#include "watersurfacefwd.h" + #include #include "surfacepoint.h" diff --git a/watersurfacefwd.h b/watersurfacefwd.h new file mode 100644 index 0000000..efb7fe3 --- /dev/null +++ b/watersurfacefwd.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +class WaterSurface; +using WaterSurfacePtr = std::shared_ptr; -- 2.39.5