Commits

Anonymous committed 8742f84

Pixel lines. Not particularly useful, but I learned something.

  • Participants
  • Parent commits 73d89b7
  • Branches alfonse-line-test

Comments (0)

Files changed (3)

File src/modules/graphics/opengl/Graphics.cpp

 	polygon(mode, coords, 5 * 2);
 }
 
+void Graphics::pixelRectangle(DrawMode mode, float x, float y, float w, float h)
+{
+	if(mode == DRAW_FILL)
+	{
+		float coords[] = {x,y, x,y+h, x+w,y+h, x+w,y, x,y};
+		polygon(mode, coords, 5 * 2);
+	}
+	else
+	{
+		glLineWidth(getLineWidth());
+		float coords[] =	 {
+			x + 0.5f,      y + 0.5f,
+			x + 0.5f,      y + h - 0.5f,
+			x + w - 0.5f,  y + h - 0.5f,
+			x + w - 0.5f,  y + 0.5f,
+		};
+
+		glEnableClientState(GL_VERTEX_ARRAY);
+		glVertexPointer(2, GL_FLOAT, 0, coords);
+
+		glDrawArrays(GL_LINE_LOOP, 0, 4);
+
+		glDisableClientState(GL_VERTEX_ARRAY);
+	}
+}
+
 void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
 {
 	float two_pi = static_cast<float>(LOVE_M_PI * 2);

File src/modules/graphics/opengl/Graphics.h

 	/**
 	 * Draws a series of lines connecting the given vertices.
 	 * @param coords Vertex components (x1, y1, ..., xn, yn). If x1,y1 == xn,yn the line will be drawn closed.
-	 * @param count Number of items in the array, i.e. count = 2 * n
+	 * @param count Number of floats in the array, i.e. count = 2 * n
 	 **/
 	void polyline(const float *coords, size_t count);
 
 	void rectangle(DrawMode mode, float x, float y, float w, float h);
 
 	/**
+	 * Draws a pixel-perfect rectangle.
+	 *
+	 * "Pixel-perfect" means that the rectangle will cover every pixel from the given \a x and \a y coordinates
+	 * up to (but not including) the \a w and \a h sizes. So every pixel on the half-open ranges
+	 * from [x, x + w) and [y, y + h) will be filled in.
+	 *
+	 * The above range \em includes line drawing. So if you draw a rectangle outline, the line will be \em fully
+	 * within the range of the rectangle. No pixels outside of the half-open ranges above will be touched.
+	 *
+	 * Pixel-perfect rectangle rendering is only guaranteed if:
+	 *
+	 * \li You use integers for the position and the sizes.
+	 * \li The line width must be 1.0 when using "lines" \a mode.
+	 * \li Your transform contains only translations (no scaling, rotation, or sheering).
+	 * \li The translations in your transform are all integers.
+	 *
+	 * The results you get will not be pixel-perfect if you violate these requirements. Pixels outside
+	 * of the range may be set.
+	 *
+	 * Scaling will not affect the size of the lines. The line width will however.
+	 *
+	 * @param mode The rendering mode. "line" for outlines, or "fill" for a filled rectangle.
+	 * @param x Position along x-axis for top-left corner.
+	 * @param y Position along y-axis for top-left corner.
+	 * @param w The width of the rectangle.
+	 * @param h The height of the rectangle.
+	 **/
+	void pixelRectangle(DrawMode mode, float x, float y, float w, float h);
+
+	/**
 	 * Draws a circle using the specified arguments.
 	 * @param mode The mode of drawing (line/filled).
 	 * @param x X-coordinate.

File src/modules/graphics/opengl/wrap_Graphics.cpp

 	return 0;
 }
 
+int w_testCmd(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 w = (float)luaL_checknumber(L, 4);
+	float h = (float)luaL_checknumber(L, 5);
+	instance->pixelRectangle(mode, x, y, w, h);
+	return 0;
+}
+
 int w_circle(lua_State *L)
 {
 	Graphics::DrawMode mode;
 	{ "rectangle", w_rectangle },
 	{ "circle", w_circle },
 	{ "arc", w_arc },
+	
+	{ "testCmd", w_testCmd },
 
 	{ "polygon", w_polygon },