diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 47a9fb1bc..389891eb8 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -844,6 +844,47 @@ namespace opengl glPopMatrix(); glEnable(GL_TEXTURE_2D); } + + int Graphics::polyline( lua_State * L) + { + // Get number of params. + int args = lua_gettop(L); + bool table = false; + + if (args == 1) { // we've got a table, hopefully + int type = lua_type(L, 1); + if (type != LUA_TTABLE) + return luaL_error(L, "Function requires a table or series of numbers"); + table = true; + args = lua_objlen(L, 1); + } + + if (args % 2) // an odd number of arguments, no good for a polyline + return luaL_error(L, "Number of vertices must be a multiple of two"); + + // right, let's draw this polyline, then + glDisable(GL_TEXTURE_2D); + glBegin(GL_LINE_STRIP); + if (table) { + lua_pushnil(L); + while (true) { + if(lua_next(L, 1) == 0) break; + GLfloat x = (GLfloat)lua_tonumber(L, -1); + lua_pop(L, 1); // pop value + if(lua_next(L, 1) == 0) break; + GLfloat y = (GLfloat)lua_tonumber(L, -1); + lua_pop(L, 1); // pop value + glVertex2f(x, y); + } + } else { + for (int i = 1; i < args; i+=2) { + glVertex2f((GLfloat)lua_tonumber(L, i), (GLfloat)lua_tonumber(L, i+1)); + } + } + glEnd(); + glEnable(GL_TEXTURE_2D); + return 0; + } void Graphics::triangle(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3 ) { diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 42bfa7afe..14e24fb01 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -461,6 +461,12 @@ namespace opengl * @param y2 Second y-coordinate. **/ void line(float x1, float y1, float x2, float y2); + + /** + * Draws a series of lines connecting the given vertices. + * @param ... Vertex components (x1, y1, x2, y2, etc.) + **/ + int polyline(lua_State * L); /** * Draws a triangle using the three coordinates passed. diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index f60b1e89d..223b23f9e 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -660,11 +660,16 @@ namespace opengl int w_line(lua_State * L) { - float x1 = (float)luaL_checknumber(L, 1); - float y1 = (float)luaL_checknumber(L, 2); - float x2 = (float)luaL_checknumber(L, 3); - float y2 = (float)luaL_checknumber(L, 4); - instance->line(x1, y1, x2, y2); + int args = lua_gettop(L); + if( args == 1 || args > 4) { + instance->polyline(L); + } else { + float x1 = (float)luaL_checknumber(L, 1); + float y1 = (float)luaL_checknumber(L, 2); + float x2 = (float)luaL_checknumber(L, 3); + float y2 = (float)luaL_checknumber(L, 4); + instance->line(x1, y1, x2, y2); + } return 0; }