From 254e94f9120cb3b79b5d430d90a071ead285c7bf Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 4 Nov 2019 14:30:28 +0100 Subject: [PATCH] Display list for the maze structure Objectives are not display-listed as they are removed when walked over. --- src/Maze.cpp | 71 ++++++++++++++++++++++++++++++++++------------------ src/Maze.h | 3 +++ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/Maze.cpp b/src/Maze.cpp index 7d8f679..95346ea 100644 --- a/src/Maze.cpp +++ b/src/Maze.cpp @@ -42,10 +42,56 @@ Maze::Maze(const std::string& maze_map, unsigned int scaling_factor, const Textu static_cast(y)}; } } + + preapre_display_list(); +} + +Maze::~Maze() +{ + if (display_list_index != -1) + { + glDeleteLists(display_list_index,1); + } } void Maze::draw() { + // Draw Maze + glCallList(display_list_index); + + // Draw objectives + for (const Vec3d& objective_pos: objective_positions) + { + glPushMatrix(); + glTranslatef((objective_pos.x+0.5)*scaling_factor,objective_pos.y,(objective_pos.z+0.5)*scaling_factor); + objective.draw_model(); + glPopMatrix(); + } +} + +bool Maze::remove_objective(const Vec2i& position) +{ + Vec3d objective{static_cast(position.x), + objective_altitude, + static_cast(position.y)}; + + for (auto it = objective_positions.begin();it!=objective_positions.end();++it) + { + if (objective == *it) + { + objective_positions.erase(it); + return true; + } + } + return false; +} + +void Maze::preapre_display_list() +{ + display_list_index = glGenLists(1); + + glNewList(display_list_index,GL_COMPILE); + // Draw ground glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); @@ -124,14 +170,6 @@ void Maze::draw() } glEnd(); - for (const Vec3d& objective_pos: objective_positions) - { - glPushMatrix(); - glTranslatef((objective_pos.x+0.5)*scaling_factor,objective_pos.y,(objective_pos.z+0.5)*scaling_factor); - objective.draw_model(); - glPopMatrix(); - } - // Draw outside glBindTexture(GL_TEXTURE_2D,out.opengl_id[0]); @@ -181,21 +219,6 @@ void Maze::draw() glVertex3f((outside_position.x+1)*scaling_factor,0,outside_position.z*scaling_factor); glEnd(); -} -bool Maze::remove_objective(const Vec2i& position) -{ - Vec3d objective{static_cast(position.x), - objective_altitude, - static_cast(position.y)}; - - for (auto it = objective_positions.begin();it!=objective_positions.end();++it) - { - if (objective == *it) - { - objective_positions.erase(it); - return true; - } - } - return false; + glEndList(); } diff --git a/src/Maze.h b/src/Maze.h index f434e72..69ad9f1 100644 --- a/src/Maze.h +++ b/src/Maze.h @@ -17,6 +17,7 @@ class Maze { public: Maze(const std::string& maze_map, unsigned int scaling_factor, const Texture& floor_texture, const Texture& wall_texture, const Texture& outside_texture, Model3D& objective); + ~Maze(); void draw(); @@ -31,6 +32,8 @@ private: const Texture& out; Model3D& objective; + GLuint display_list_index = -1; + const double objective_altitude = 0; const double wall_height = 2; unsigned corner_pos;