From 016ae7f1a2f0f2fc41d8c4417e40e16851e3e9cf Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 18 Nov 2009 00:20:04 +0000 Subject: [PATCH] added DEBUG_GL_ERROR debug functionality git-svn-id: svn://anubis/anaglym/trunk@188 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 55 ++++++++++++++++++++++++++++++++++++++++++++--- Engine.h | 2 +- tests/bowling.lua | 1 + 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Engine.cc b/Engine.cc index 2be23b8..427ac1a 100644 --- a/Engine.cc +++ b/Engine.cc @@ -72,6 +72,24 @@ static void debug_hook(lua_State * L, lua_Debug * debug) g_engine->debug_hook(debug); } +#define DEBUG_GL_ERROR +#ifdef DEBUG_GL_ERROR +#define checkGLError() checkGLErrorLine(__FUNCTION__, __LINE__) +#else +#define checkGLError() +#endif +static void checkGLErrorLine(const char * function, int line) +{ + GLenum err = glGetError(); + if (err != 0) + { + cerr << "gl error in " << function + << ": " << err << " (0x" << hex << err << ") at line " + << dec << line << endl; + } +} + + /******** Engine functions ********/ Engine::Engine(const string & path, Video & video) @@ -300,12 +318,14 @@ Engine::Object * Engine::getObject(int id) void Engine::startFrame() { + checkGLError(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(m_eye[0], m_eye[1], m_eye[2], m_center[0], m_center[1], m_center[2], m_up[0], m_up[1], m_up[2]); + checkGLError(); } void Engine::endFrame() @@ -506,8 +526,9 @@ void Engine::getScreenSize(int * width, int * height) void Engine::drawText(const char * text, GLfloat r, GLfloat g, GLfloat b, int ptsize, float x, float y) { + checkGLError(); m_font->FaceSize(ptsize); - glPushAttrib(GL_ENABLE_BIT); + glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT); glDisable(GL_LIGHTING); glPushMatrix(); glTranslatef(x, y, 0); @@ -515,6 +536,7 @@ void Engine::drawText(const char * text, GLfloat r, GLfloat g, GLfloat b, m_font->Render(text); glPopMatrix(); glPopAttrib(); + checkGLError(); } void Engine::getTextSize(const char * text, int ptsize, @@ -529,6 +551,7 @@ void Engine::getTextSize(const char * text, int ptsize, void Engine::drawLine(float r, float g, float b, float x1, float y1, float x2, float y2, float width) { + checkGLError(); glPushAttrib(GL_LINE_BIT | GL_ENABLE_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); @@ -540,11 +563,13 @@ void Engine::drawLine(float r, float g, float b, glVertex2f(x2, y2); glEnd(); glPopAttrib(); + checkGLError(); } void Engine::drawRect(float r, float g, float b, float width, float height, float x, float y, float rot) { + checkGLError(); glPushAttrib(GL_ENABLE_BIT); glPushMatrix(); glTranslatef(x, y, 0); @@ -561,11 +586,13 @@ void Engine::drawRect(float r, float g, float b, glEnd(); glPopMatrix(); glPopAttrib(); + checkGLError(); } void Engine::fillRect(float r, float g, float b, float width, float height, float x, float y, float rot) { + checkGLError(); glPushAttrib(GL_ENABLE_BIT); glPushMatrix(); glTranslatef(x, y, 0); @@ -582,11 +609,13 @@ void Engine::fillRect(float r, float g, float b, glEnd(); glPopMatrix(); glPopAttrib(); + checkGLError(); } void Engine::drawImage(float width, float height, float x, float y, int tex, float rot) { + checkGLError(); glPushAttrib(GL_ENABLE_BIT); glPushMatrix(); glTranslatef(x, y, 0); @@ -608,6 +637,7 @@ void Engine::drawImage(float width, float height, float x, float y, glEnd(); glPopMatrix(); glPopAttrib(); + checkGLError(); } /* called by SDL when the update timer expires */ @@ -726,6 +756,7 @@ void Engine::update_overlay_event() { if (m_event_update_overlay_present) { + checkGLError(); glPushAttrib(GL_ENABLE_BIT); glDisable(GL_DEPTH_TEST); int width = m_video.getWidth(); @@ -749,6 +780,7 @@ void Engine::update_overlay_event() glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopAttrib(); + checkGLError(); } } @@ -926,18 +958,23 @@ Engine::Object::Object(const Engine::Object & orig) { m_is_visible = orig.m_is_visible; m_ode_object = new OdeWorld::Object(*orig.m_ode_object); - m_display_list = orig.m_display_list; + m_is_managed = orig.m_is_managed; + if (m_is_managed) + m_display_list = 0; + else + m_display_list = orig.m_display_list; m_display_list_refcnt = orig.m_display_list_refcnt; if (m_display_list_refcnt != NULL) (*m_display_list_refcnt)++; m_scale = orig.m_scale; m_is_scaled = orig.m_is_scaled; - m_is_managed = orig.m_is_managed; m_geom_type = orig.m_geom_type; m_args = orig.m_args; for (int i = 0; i < 4; i++) m_color[i] = orig.m_color[i]; m_texture = orig.m_texture; + if (m_is_managed) + render(); } Engine::Object::~Object() @@ -953,6 +990,10 @@ Engine::Object::~Object() glDeleteLists(m_display_list, 1); } } + else + { + glDeleteLists(m_display_list, 1); + } } void Engine::Object::createManagedObject() @@ -997,7 +1038,10 @@ void Engine::Object::render() return; GLUquadric * quad = gluNewQuadric(); if (m_display_list <= 0) + { m_display_list = glGenLists(1); + } + checkGLError(); glNewList(m_display_list, GL_COMPILE); glPushAttrib(GL_ENABLE_BIT); if (m_texture != 0) @@ -1182,6 +1226,7 @@ void Engine::Object::render() gluDeleteQuadric(quad); glPopAttrib(); glEndList(); + checkGLError(); } void Engine::Object::loadPhy(const FileLoader::Path & path) @@ -1257,6 +1302,7 @@ void Engine::Object::draw() { if (m_is_visible) { + checkGLError(); const dReal * pos = m_ode_object->getPosition(); const dReal * rot = m_ode_object->getRotation(); bool transform = (pos != NULL && rot != NULL); @@ -1271,13 +1317,16 @@ void Engine::Object::draw() glScalef(m_scale, m_scale, m_scale); } + checkGLError(); glCallList(m_display_list); + checkGLError(); if (m_is_scaled) glPopAttrib(); if (transform) glPopMatrix(); + checkGLError(); } } diff --git a/Engine.h b/Engine.h index 7203461..efc11a1 100644 --- a/Engine.h +++ b/Engine.h @@ -82,7 +82,6 @@ class Engine float m_scale; bool m_is_scaled; bool m_is_managed; - GLuint m_texture; /* for "pre-loaded" objects */ int * m_display_list_refcnt; @@ -91,6 +90,7 @@ class Engine OdeWorld::GeomType m_geom_type; refptr< std::vector > m_args; float m_color[4]; + GLuint m_texture; }; class EngineFileLoader : public FileLoader diff --git a/tests/bowling.lua b/tests/bowling.lua index 8cb5541..36e408b 100644 --- a/tests/bowling.lua +++ b/tests/bowling.lua @@ -57,6 +57,7 @@ end function update_overlay_event(width, height) local tw, th = ag.getTextSize("Hi there", 18) ag.drawText("Hi there", 1, 1, 0, 18, width - tw - 40, height - th - 40) + ag.drawText("Hello", 1, 0, 1, 20, 200, 200) -- ag.drawLine(1, 0, 0.3, 10, 10, 40, 60) -- ag.drawLine(1, 0.7, 1, 50, 10, 100, 60, 5) -- ag.drawRect(0, 1, 0, 40, 40, width / 2, height / 2)