added polyline support to love.graphics.line (the syntax is the same as love.graphics.polygon - either a list or a table of vertices)

This commit is contained in:
Bill Meltsner
2010-01-19 08:27:12 -05:00
parent 46b3d2e254
commit 8de70ca2aa
3 changed files with 57 additions and 5 deletions
+41
View File
@@ -844,6 +844,47 @@ namespace opengl
glPopMatrix(); glPopMatrix();
glEnable(GL_TEXTURE_2D); 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 ) void Graphics::triangle(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3 )
{ {
+6
View File
@@ -461,6 +461,12 @@ namespace opengl
* @param y2 Second y-coordinate. * @param y2 Second y-coordinate.
**/ **/
void line(float x1, float y1, float x2, float y2); 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. * Draws a triangle using the three coordinates passed.
+10 -5
View File
@@ -660,11 +660,16 @@ namespace opengl
int w_line(lua_State * L) int w_line(lua_State * L)
{ {
float x1 = (float)luaL_checknumber(L, 1); int args = lua_gettop(L);
float y1 = (float)luaL_checknumber(L, 2); if( args == 1 || args > 4) {
float x2 = (float)luaL_checknumber(L, 3); instance->polyline(L);
float y2 = (float)luaL_checknumber(L, 4); } else {
instance->line(x1, y1, x2, y2); 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; return 0;
} }