From 4a76d2e2cf7874e54a4e6688ebf1fa8ca59ce8c1 Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Mon, 19 Jan 2009 22:57:36 -0500 Subject: [PATCH] hacks that make a polygon draw --- src/Entities/Polygon.cpp | 11 ++++++++--- src/Entities/Polygon.h | 2 +- src/entityCreator.cpp | 9 +++++++++ src/graphics/graphics.cpp | 28 ++++++++++++++++++++++++++++ src/graphics/graphics.h | 2 ++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Entities/Polygon.cpp b/src/Entities/Polygon.cpp index 42cfbdb..bf2fb00 100644 --- a/src/Entities/Polygon.cpp +++ b/src/Entities/Polygon.cpp @@ -16,15 +16,20 @@ */ #include "Polygon.h" +#include "debug.h" #include "Vector2.h" +#include "graphics/graphics.h" + /// ***** Constructors/Destructors ***** -Polygon::Polygon(const Vector2& pos, vector points) - : PhysicsEntity(pos), points(points) +Polygon::Polygon(const vector& points) + : PhysicsEntity(Vector2(0,0)), points(points) { + DASSERT(0 < points.size()); + createBindingBox(); } Polygon::~Polygon() @@ -36,7 +41,7 @@ Polygon::~Polygon() void Polygon::draw() const { - // TODO + graphics::drawPolygon(points); } /// ***** Private Class Methods ***** diff --git a/src/Entities/Polygon.h b/src/Entities/Polygon.h index 5d8d10d..b8c8493 100644 --- a/src/Entities/Polygon.h +++ b/src/Entities/Polygon.h @@ -30,7 +30,7 @@ using std::vector; class Polygon: public PhysicsEntity { public: - Polygon(const Vector2&, vector); + Polygon(const vector&); virtual ~Polygon(); virtual void draw() const; diff --git a/src/entityCreator.cpp b/src/entityCreator.cpp index 2df4457..1b4b6f7 100644 --- a/src/entityCreator.cpp +++ b/src/entityCreator.cpp @@ -23,6 +23,7 @@ #include "input/inputManager.h" #include "Entities/Ball.h" +#include "Entities/Polygon.h" #include "graphics/colors.h" @@ -78,6 +79,14 @@ void creator::init() { addBall(Vector2(200+i*2, 200+i*2), 10, cCyan); } + + // add a polygon into the mix (currently not cleaned up) + vector points; + points.push_back(Vector2(50,50)); + points.push_back(Vector2(50,100)); + points.push_back(Vector2(100,50)); + + manager::add(new Polygon(points)); } void creator::clean() { diff --git a/src/graphics/graphics.cpp b/src/graphics/graphics.cpp index 537cdc8..d89db8a 100644 --- a/src/graphics/graphics.cpp +++ b/src/graphics/graphics.cpp @@ -34,6 +34,7 @@ using std::endl; /// ***** Private Method Headers ***** void glDrawCircle(int); +void glDrawPolygon( const std::vector& points ); void sdlInit(); void glInit(); @@ -65,6 +66,21 @@ void graphics::drawCircle(float radius, const Vector2& pos, const float* color) glDrawCircle(32); } +void graphics::drawPolygon +( + const std::vector& points, + const float* color +) +{ + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + if(color != NULL) + glColor3fv(color); + + glDrawPolygon(points); +} + /// ***** Private Methods ***** void glDrawCircle(int pieces) @@ -81,6 +97,18 @@ void glDrawCircle(int pieces) glEnd(); } +void glDrawPolygon( const std::vector& points ) +{ + glBegin(GL_POLYGON); + for(unsigned int n = 0; n < points.size(); n++) + { + const Vector2& vec = points.at(n); + + glVertex3f(vec.x, vec.y, 0); + } + glEnd(); +} + void sdlInit() { if(SDL_Init(SDL_INIT_VIDEO) < 0) diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 1f23760..aa87e1b 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -19,6 +19,7 @@ #define GRAPHICS_H #include "Vector2.h" +#include /// ***** Header Methods ***** @@ -29,6 +30,7 @@ namespace graphics void clean(); void drawCircle(float radius, const Vector2&, const float* color = 0); + void drawPolygon(const std::vector& points, const float* color = 0); } #endif // GRAPHICS_H -- 2.10.2