From 33a2110b00b920d5db694d77bf6ccb1b22e2c565 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Thu, 17 Mar 2011 20:50:27 -0400 Subject: [PATCH] added love.graphics.arc --HG-- branch : minor --- src/modules/graphics/opengl/Graphics.cpp | 44 ++++++++++++++++++- src/modules/graphics/opengl/Graphics.h | 3 +- src/modules/graphics/opengl/wrap_Graphics.cpp | 18 ++++++++ src/modules/graphics/opengl/wrap_Graphics.h | 1 + 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 4d75047e4..2509529de 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -950,7 +950,7 @@ namespace opengl glEnable(GL_TEXTURE_2D); } - void Graphics::circle(DrawMode mode, float x, float y, float radius, int points ) + void Graphics::circle(DrawMode mode, float x, float y, float radius, int points) { float two_pi = static_cast(LOVE_M_PI * 2); if(points <= 0) points = 1; @@ -986,6 +986,48 @@ namespace opengl glPopMatrix(); glEnable(GL_TEXTURE_2D); } + + void Graphics::arc(DrawMode mode, float x, float y, float radius, float angle1, float angle2, int points) + { + float angle = angle2 - angle1; + while (angle < 0) angle += LOVE_M_PI * 2; + if(points <= 0) points = 1; + float angle_shift = (angle / points); + + glDisable(GL_TEXTURE_2D); + glPushMatrix(); + + glTranslatef(x, y, 0.0f); + + switch(mode) + { + case DRAW_LINE: + glBegin(GL_LINE_LOOP); + + glVertex2f(0, 0); + + for(float i = 0; i <= angle+angle_shift/2; i+= angle_shift) + glVertex2f(radius * cos(i+angle1),radius * sin(i+angle1)); + + glEnd(); + break; + + default: + case DRAW_FILL: + glBegin(GL_TRIANGLE_FAN); + + glVertex2f(0, 0); + + for(float i = 0; i <= angle+angle_shift/2; i+= angle_shift) + glVertex2f(radius * cos(i+angle1),radius * sin(i+angle1)); + + glEnd(); + break; + } + + glPopMatrix(); + glEnable(GL_TEXTURE_2D); + } int Graphics::polygon( lua_State * L ) { diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 551701737..79cb0eb97 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -28,7 +28,6 @@ // SDL #include #include "GLee.h" -#include // LOVE #include @@ -483,6 +482,8 @@ namespace opengl * @param points Amount of points to use to draw the circle. **/ void circle(DrawMode mode, float x, float y, float radius, int points = 10); + + void arc(DrawMode mode, float x, float y, float radius, float angle1, float angle2, int points = 10); /** * Draws a polygon with an arbitrary number of vertices. diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index f2b377f0a..bb991c21b 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -853,6 +853,23 @@ namespace opengl instance->circle(mode, x, y, radius, points); return 0; } + + int w_arc(lua_State * L) + { + Graphics::DrawMode mode; + const char * str = luaL_checkstring(L, 1); + if(!Graphics::getConstant(str, mode)) + return luaL_error(L, "Incorrect draw mode %s", str); + + float x = (float)luaL_checknumber(L, 2); + float y = (float)luaL_checknumber(L, 3); + float radius = (float)luaL_checknumber(L, 4); + float angle1 = (float)luaL_checknumber(L, 5); + float angle2 = (float)luaL_checknumber(L, 6); + int points = luaL_optint(L, 7, 10); + instance->arc(mode, x, y, radius, angle1, angle2, points); + return 0; + } int w_polygon(lua_State * L) { @@ -974,6 +991,7 @@ namespace opengl { "rectangle", w_rectangle }, { "quad", w_quad }, { "circle", w_circle }, + { "arc", w_arc }, { "polygon", w_polygon }, diff --git a/src/modules/graphics/opengl/wrap_Graphics.h b/src/modules/graphics/opengl/wrap_Graphics.h index f2f1c1cb6..bdbd777fc 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.h +++ b/src/modules/graphics/opengl/wrap_Graphics.h @@ -94,6 +94,7 @@ namespace opengl int w_rectangle(lua_State * L); int w_quad(lua_State * L); int w_circle(lua_State * L); + int w_arc(lua_State * L); int w_push(lua_State * L); int w_pop(lua_State * L); int w_rotate(lua_State * L);