Browse Source

love.graphics.circle/ellipse/arc/rectangle now take transformation scale into account when determining the number of segments to use. Resolves issue #1184.

--HG--
branch : minor
Alex Szpakowski 9 years ago
parent
commit
22056271ec

+ 35 - 1
src/modules/graphics/opengl/Graphics.cpp

@@ -1347,7 +1347,7 @@ void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, floa
 	if (h >= 0.02f)
 	if (h >= 0.02f)
 		ry = std::min(ry, h / 2.0f - 0.01f);
 		ry = std::min(ry, h / 2.0f - 0.01f);
 
 
-	points = std::max(points, 1);
+	points = std::max(points / 4, 1);
 
 
 	const float half_pi = static_cast<float>(LOVE_M_PI / 2);
 	const float half_pi = static_cast<float>(LOVE_M_PI / 2);
 	float angle_shift = half_pi / ((float) points + 1.0f);
 	float angle_shift = half_pi / ((float) points + 1.0f);
@@ -1394,11 +1394,28 @@ void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, floa
 	delete[] coords;
 	delete[] coords;
 }
 }
 
 
+int Graphics::calculateEllipsePoints(float rx, float ry) const
+{
+	float pixelScale = 1.0f / std::max((float) pixelSizeStack.back(), 0.00001f);
+	int points = (int) sqrtf(((rx + ry) / 2.0f) * 20.0f * pixelScale);
+	return std::max(points, 8);
+}
+
+void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry)
+{
+	rectangle(mode, x, y, w, h, rx, ry, calculateEllipsePoints(rx, ry));
+}
+
 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)
 {
 {
 	ellipse(mode, x, y, radius, radius, points);
 	ellipse(mode, x, y, radius, radius, points);
 }
 }
 
 
+void Graphics::circle(DrawMode mode, float x, float y, float radius)
+{
+	ellipse(mode, x, y, radius, radius);
+}
+
 void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
 void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
 {
 {
 	float two_pi = static_cast<float>(LOVE_M_PI * 2);
 	float two_pi = static_cast<float>(LOVE_M_PI * 2);
@@ -1421,6 +1438,11 @@ void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int po
 	delete[] coords;
 	delete[] coords;
 }
 }
 
 
+void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
+{
+	ellipse(mode, x, y, a, b, calculateEllipsePoints(a, b));
+}
+
 void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
 void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
 {
 {
 	// Nothing to display with no points or equal angles. (Or is there with line mode?)
 	// Nothing to display with no points or equal angles. (Or is there with line mode?)
@@ -1499,6 +1521,18 @@ void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float r
 	delete[] coords;
 	delete[] coords;
 }
 }
 
 
+void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2)
+{
+	float points = (float) calculateEllipsePoints(radius, radius);
+
+	// The amount of points is based on the fraction of the circle created by the arc.
+	float angle = fabsf(angle1 - angle2);
+	if (angle < 2.0f * (float) LOVE_M_PI)
+		points *= angle / (2.0f * (float) LOVE_M_PI);
+
+	arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
+}
+
 /// @param mode    the draw mode
 /// @param mode    the draw mode
 /// @param coords  the coordinate array
 /// @param coords  the coordinate array
 /// @param count   the number of coordinates/size of the array
 /// @param count   the number of coordinates/size of the array

+ 10 - 4
src/modules/graphics/opengl/Graphics.h

@@ -397,7 +397,8 @@ public:
 	 * @param ry The radius of the corners on the y axis
 	 * @param ry The radius of the corners on the y axis
 	 * @param points The number of points to use per corner
 	 * @param points The number of points to use per corner
 	 **/
 	 **/
-	void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points = 10);
+	void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points);
+	void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry);
 
 
 	/**
 	/**
 	 * Draws a circle using the specified arguments.
 	 * Draws a circle using the specified arguments.
@@ -407,7 +408,8 @@ public:
 	 * @param radius Radius of the circle.
 	 * @param radius Radius of the circle.
 	 * @param points Number of points to use to draw the circle.
 	 * @param points Number of points to use to draw the circle.
 	 **/
 	 **/
-	void circle(DrawMode mode, float x, float y, float radius, int points = 10);
+	void circle(DrawMode mode, float x, float y, float radius, int points);
+	void circle(DrawMode mode, float x, float y, float radius);
 
 
 	/**
 	/**
 	 * Draws an ellipse using the specified arguments.
 	 * Draws an ellipse using the specified arguments.
@@ -418,7 +420,8 @@ public:
 	 * @param b Radius in y-direction
 	 * @param b Radius in y-direction
 	 * @param points Number of points to use to draw the circle.
 	 * @param points Number of points to use to draw the circle.
 	 **/
 	 **/
-	void ellipse(DrawMode mode, float x, float y, float a, float b, int points = 10);
+	void ellipse(DrawMode mode, float x, float y, float a, float b, int points);
+	void ellipse(DrawMode mode, float x, float y, float a, float b);
 
 
 	/**
 	/**
 	 * Draws an arc using the specified arguments.
 	 * Draws an arc using the specified arguments.
@@ -431,7 +434,8 @@ public:
 	 * @param angle2 The angle at which the arc terminates.
 	 * @param angle2 The angle at which the arc terminates.
 	 * @param points Number of points to use to draw the arc.
 	 * @param points Number of points to use to draw the arc.
 	 **/
 	 **/
-	void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points = 10);
+	void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points);
+	void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2);
 
 
 	/**
 	/**
 	 * Draws a polygon with an arbitrary number of vertices.
 	 * Draws a polygon with an arbitrary number of vertices.
@@ -522,6 +526,8 @@ private:
 
 
 	void checkSetDefaultFont();
 	void checkSetDefaultFont();
 
 
+	int calculateEllipsePoints(float rx, float ry) const;
+
 	StrongRef<love::window::Window> currentWindow;
 	StrongRef<love::window::Window> currentWindow;
 
 
 	StrongRef<Font> defaultFont;
 	StrongRef<Font> defaultFont;

+ 23 - 22
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -1763,13 +1763,14 @@ int w_rectangle(lua_State *L)
 	float rx = (float)luaL_optnumber(L, 6, 0.0);
 	float rx = (float)luaL_optnumber(L, 6, 0.0);
 	float ry = (float)luaL_optnumber(L, 7, rx);
 	float ry = (float)luaL_optnumber(L, 7, rx);
 
 
-	int points;
 	if (lua_isnoneornil(L, 8))
 	if (lua_isnoneornil(L, 8))
-		points = std::max(rx, ry) > 20.0 ? (int)(std::max(rx, ry) / 2) : 10;
+		instance()->rectangle(mode, x, y, w, h, rx, ry);
 	else
 	else
-		points = (int) luaL_checknumber(L, 8);
+	{
+		int points = (int) luaL_checknumber(L, 8);
+		instance()->rectangle(mode, x, y, w, h, rx, ry, points);
+	}
 
 
-	instance()->rectangle(mode, x, y, w, h, rx, ry, points);
 	return 0;
 	return 0;
 }
 }
 
 
@@ -1783,13 +1784,15 @@ int w_circle(lua_State *L)
 	float x = (float)luaL_checknumber(L, 2);
 	float x = (float)luaL_checknumber(L, 2);
 	float y = (float)luaL_checknumber(L, 3);
 	float y = (float)luaL_checknumber(L, 3);
 	float radius = (float)luaL_checknumber(L, 4);
 	float radius = (float)luaL_checknumber(L, 4);
-	int points;
+
 	if (lua_isnoneornil(L, 5))
 	if (lua_isnoneornil(L, 5))
-		points = radius > 10 ? (int)(radius) : 10;
+		instance()->circle(mode, x, y, radius);
 	else
 	else
-		points = (int) luaL_checknumber(L, 5);
+	{
+		int points = (int) luaL_checknumber(L, 5);
+		instance()->circle(mode, x, y, radius, points);
+	}
 
 
-	instance()->circle(mode, x, y, radius, points);
 	return 0;
 	return 0;
 }
 }
 
 
@@ -1805,13 +1808,14 @@ int w_ellipse(lua_State *L)
 	float a = (float)luaL_checknumber(L, 4);
 	float a = (float)luaL_checknumber(L, 4);
 	float b = (float)luaL_optnumber(L, 5, a);
 	float b = (float)luaL_optnumber(L, 5, a);
 
 
-	int points;
 	if (lua_isnoneornil(L, 6))
 	if (lua_isnoneornil(L, 6))
-		points = a + b > 30 ? (int)((a + b) / 2) : 15;
+		instance()->ellipse(mode, x, y, a, b);
 	else
 	else
-		points = (int) luaL_checknumber(L, 6);
+	{
+		int points = (int) luaL_checknumber(L, 6);
+		instance()->ellipse(mode, x, y, a, b, points);
+	}
 
 
-	instance()->ellipse(mode, x, y, a, b, points);
 	return 0;
 	return 0;
 }
 }
 
 
@@ -1841,17 +1845,14 @@ int w_arc(lua_State *L)
 	float angle1 = (float) luaL_checknumber(L, startidx + 3);
 	float angle1 = (float) luaL_checknumber(L, startidx + 3);
 	float angle2 = (float) luaL_checknumber(L, startidx + 4);
 	float angle2 = (float) luaL_checknumber(L, startidx + 4);
 
 
-	int points = (int) radius;
-	float angle = fabs(angle1 - angle2);
-
-	// The amount of points is based on the fraction of the circle created by the arc.
-	if (angle < 2.0f * (float) LOVE_M_PI)
-		points *= angle / (2.0f * (float) LOVE_M_PI);
-
-	points = std::max(points, 10);
-	points = (int) luaL_optnumber(L, startidx + 5, points);
+	if (lua_isnoneornil(L, startidx + 5))
+		instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2);
+	else
+	{
+		int points = (int) luaL_checknumber(L, startidx + 5);
+		instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points);
+	}
 
 
-	instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points);
 	return 0;
 	return 0;
 }
 }