From ab4d6f1d93b913a81ff11a854faab2286362c561 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Fri, 5 Jun 2009 14:30:44 +0100 Subject: [PATCH] Use shared_ptr to manage asteroid display list --- src/Asteroid.cpp | 35 ++++++++++++++++++++++++++++++----- src/Asteroid.hpp | 7 ++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Asteroid.cpp b/src/Asteroid.cpp index bdd845e..f303333 100644 --- a/src/Asteroid.cpp +++ b/src/Asteroid.cpp @@ -23,8 +23,19 @@ Texture* Asteroid::surfTexture[Surface::NUM_SURF_TEX]; +namespace { + // Called by shared_ptr when no more references to a display + // list are held + void displayListDeleter(GLuint* u) + { + glDeleteLists(*u, 1); + delete u; + } +} + Asteroid::Asteroid(int x, int y, int width, int surftex) - : StaticObject(x, y, width, 4) + : StaticObject(x, y, width, 4), + displayList_(new GLuint, displayListDeleter) { LOAD_ONCE { surfTexture[0] = new Texture("images/dirt_surface2.png"); @@ -33,7 +44,7 @@ Asteroid::Asteroid(int x, int y, int width, int surftex) surfTexture[3] = new Texture("images/rock_surface2.png"); } - display_list = glGenLists(1); + *displayList_ = glGenLists(1); int change, texloop=0; @@ -111,14 +122,22 @@ Asteroid::Asteroid(int x, int y, int width, int surftex) GenerateDisplayList(surftex); } +Asteroid::Asteroid(const Asteroid& other) + : StaticObject(other), + displayList_(other.displayList_) +{ + copy(other.uppolys, other.uppolys + MAX_ASTEROID_WIDTH, uppolys); + copy(other.downpolys, other.downpolys + MAX_ASTEROID_WIDTH, downpolys); +} + Asteroid::~Asteroid() { - //glDeleteLists(display_list, 1); + } void Asteroid::GenerateDisplayList(int texidx) { - glNewList(display_list, GL_COMPILE); + glNewList(*displayList_, GL_COMPILE); glDisable(GL_BLEND); glEnable(GL_TEXTURE_2D); @@ -184,12 +203,18 @@ LineSegment Asteroid::GetDownBoundary(int poly) const void Asteroid::Draw(int viewadjust_x, int viewadjust_y) const { + if (!displayList_) { + // This asteroid has been copied and lost its display + // list reference + throw runtime_error("Asteroid::Draw called on invalid asteroid copy"); + } + double ix = xpos*OBJ_GRID_SIZE - viewadjust_x; double iy = ypos*OBJ_GRID_SIZE - viewadjust_y + OBJ_GRID_TOP; glLoadIdentity(); glTranslated(ix, iy, 0.0); - glCallList(display_list); + glCallList(*displayList_); } bool Asteroid::CheckCollision(const Ship& ship) const diff --git a/src/Asteroid.hpp b/src/Asteroid.hpp index 24eae05..c1db514 100644 --- a/src/Asteroid.hpp +++ b/src/Asteroid.hpp @@ -24,9 +24,14 @@ #include "Surface.hpp" #include "ObjectGrid.hpp" +#include + +using namespace std::tr1; + class Asteroid : public StaticObject { public: Asteroid(int x, int y, int width, int surftex); + Asteroid(const Asteroid& other); ~Asteroid(); void Draw(int viewadjust_x, int viewadjust_y) const; @@ -42,7 +47,7 @@ private: void GenerateDisplayList(int texidx); static Texture* surfTexture[Surface::NUM_SURF_TEX]; - GLuint display_list; + shared_ptr displayList_; struct AsteroidSection { double texX, texwidth; -- 2.39.2