From 1261427304336254edf1e7e3f9a52aeb24ce3288 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 19 Aug 2015 17:48:27 -0300 Subject: [PATCH] Replaced love.graphics.point with love.graphics.points, which draws a number of points at a time. It currently has 3 variants, with the last one using optional per-point colors: - love.graphics.points(x1, y1, x2, y2, ...) - love.graphics.points({x1, y1, x2, y2, ...}) - love.graphics.points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...}). --- src/modules/graphics/opengl/Graphics.cpp | 21 +++-- src/modules/graphics/opengl/Graphics.h | 2 +- src/modules/graphics/opengl/wrap_Graphics.cpp | 85 +++++++++++++++++-- src/modules/graphics/opengl/wrap_Graphics.h | 2 +- 4 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index dd13fa219..0b830c78e 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -1109,17 +1109,24 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Font * Primitives **/ -void Graphics::point(float x, float y) +void Graphics::points(const float *coords, const uint8 *colors, size_t numpoints) { - OpenGL::TempDebugGroup debuggroup("Graphics point draw"); - - GLfloat coord[] = {x, y}; + OpenGL::TempDebugGroup debuggroup("Graphics points draw"); gl.prepareDraw(); gl.bindTexture(gl.getDefaultTexture()); - gl.useVertexAttribArrays(ATTRIBFLAG_POS); - glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, coord); - gl.drawArrays(GL_POINTS, 0, 1); + + uint32 attribflags = ATTRIBFLAG_POS; + glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, coords); + + if (colors) + { + attribflags |= ATTRIBFLAG_COLOR; + glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors); + } + + gl.useVertexAttribArrays(attribflags); + gl.drawArrays(GL_POINTS, 0, numpoints); } void Graphics::polyline(const float *coords, size_t count) diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 1dfe655e4..cda952424 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -351,7 +351,7 @@ public: * @param x Point along x-axis. * @param y Point along y-axis. **/ - void point(float x, float y); + void points(const float *coords, const uint8 *colors, size_t numpoints); /** * Draws a series of lines connecting the given vertices. diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 990af032b..4a445b7e5 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -1507,11 +1507,86 @@ int w_printf(lua_State *L) return 0; } -int w_point(lua_State *L) +int w_points(lua_State *L) { - float x = (float)luaL_checknumber(L, 1); - float y = (float)luaL_checknumber(L, 2); - instance()->point(x, y); + // love.graphics.points has 3 variants: + // - points(x1, y1, x2, y2, ...) + // - points({x1, y1, x2, y2, ...}) + // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...}) + + int args = lua_gettop(L); + bool is_table = false; + bool is_table_of_tables = false; + if (args == 1 && lua_istable(L, 1)) + { + is_table = true; + args = (int) luax_objlen(L, 1); + + lua_rawgeti(L, 1, 1); + is_table_of_tables = lua_istable(L, -1); + lua_pop(L, 1); + } + + if (args % 2 != 0 && !is_table_of_tables) + return luaL_error(L, "Number of vertex components must be a multiple of two"); + + int numpoints = args / 2; + if (is_table_of_tables) + numpoints = args; + + float *coords = nullptr; + uint8 *colors = nullptr; + + coords = new float[numpoints * 2]; + + if (is_table_of_tables) + colors = new uint8[numpoints * 4]; + + if (is_table) + { + if (is_table_of_tables) + { + // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...}) + for (int i = 0; i < args; i++) + { + lua_rawgeti(L, 1, i + 1); + for (int j = 1; j <= 6; j++) + lua_rawgeti(L, -j, j); + + coords[i * 2 + 0] = luax_tofloat(L, -6); + coords[i * 2 + 1] = luax_tofloat(L, -5); + + colors[i * 4 + 0] = (uint8) luaL_optnumber(L, -4, 255); + colors[i * 4 + 1] = (uint8) luaL_optnumber(L, -3, 255); + colors[i * 4 + 2] = (uint8) luaL_optnumber(L, -2, 255); + colors[i * 4 + 3] = (uint8) luaL_optnumber(L, -1, 255); + + lua_pop(L, 7); + } + } + else + { + // points({x1, y1, x2, y2, ...}) + for (int i = 0; i < args; i++) + { + lua_rawgeti(L, 1, i + 1); + coords[i] = luax_tofloat(L, -1); + lua_pop(L, 1); + } + } + } + else + { + for (int i = 0; i < args; i++) + coords[i] = luax_tofloat(L, i + 1); + } + + instance()->points(coords, colors, numpoints); + + delete[] coords; + if (colors) + delete[] colors; + return 0; } @@ -1830,7 +1905,7 @@ static const luaL_Reg functions[] = { "setStencilTest", w_setStencilTest }, { "getStencilTest", w_getStencilTest }, - { "point", w_point }, + { "points", w_points }, { "line", w_line }, { "rectangle", w_rectangle }, { "circle", w_circle }, diff --git a/src/modules/graphics/opengl/wrap_Graphics.h b/src/modules/graphics/opengl/wrap_Graphics.h index 35e630854..478375999 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.h +++ b/src/modules/graphics/opengl/wrap_Graphics.h @@ -106,7 +106,7 @@ int w_getStats(lua_State *L); int w_draw(lua_State *L); int w_print(lua_State *L); int w_printf(lua_State *L); -int w_point(lua_State *L); +int w_points(lua_State *L); int w_line(lua_State *L); int w_rectangle(lua_State *L); int w_circle(lua_State *L);