Add a test of the circle rendering logic

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3911 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-08-24 17:00:51 +00:00
parent bf81d60a62
commit 840c4fb49f
4 changed files with 141 additions and 51 deletions

View File

@ -98,3 +98,5 @@
6.9 2011-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> 6.9 2011-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
* apps/examples/nxlines: Extend the line drawing text to include drawing
of circles.

View File

@ -424,8 +424,21 @@ examplex/nxlines
depends on CONFIG_EXAMPLES_NXLINES_BPP. depends on CONFIG_EXAMPLES_NXLINES_BPP.
CONFIG_EXAMPLES_NXLINES_LINEWIDTH - Selects the width of the lines in CONFIG_EXAMPLES_NXLINES_LINEWIDTH - Selects the width of the lines in
pixels (default: 16) pixels (default: 16)
CONFIG_EXAMPLES_NXLINES_LINECOLOR -- The color of the lines drawn in the CONFIG_EXAMPLES_NXLINES_LINECOLOR -- The color of the central lines drawn
background window. Default depends on CONFIG_EXAMPLES_NXLINES_BPP. in the background window. Default depends on CONFIG_EXAMPLES_NXLINES_BPP
(there really is no meaningful default).
CONFIG_EXAMPLES_NXLINES_BORDERWIDTH -- The width of the circular border
drawn in the background window. (default: 4).
CONFIG_EXAMPLES_NXLINES_BORDERCOLOR -- The color of the circular border
drawn in the background window. Default depends on CONFIG_EXAMPLES_NXLINES_BPP
(there really is no meaningful default).
CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR -- The color of the circular region
filled in the background window. Default depends on CONFIG_EXAMPLES_NXLINES_BPP
(there really is no meaningful default).
CONFIG_EXAMPLES_NXLINES_BORDERCOLOR -- The color of the lines drawn in the
background window. Default depends on CONFIG_EXAMPLES_NXLINES_BPP (there
really is no meaningful default).
CONFIG_EXAMPLES_NXLINES_BPP -- Pixels per pixel to use. Valid options CONFIG_EXAMPLES_NXLINES_BPP -- Pixels per pixel to use. Valid options
include 2, 4, 8, 16, 24, and 32. Default is 16. include 2, 4, 8, 16, 24, and 32. Default is 16.
CONFIG_EXAMPLES_NXLINES_EXTERNINIT - The driver for the graphics device on CONFIG_EXAMPLES_NXLINES_EXTERNINIT - The driver for the graphics device on

View File

@ -75,7 +75,7 @@
# define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB16_DARKGREEN # define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB16_DARKGREEN
# else # else
# define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB8_DARKGREEN # define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB8_DARKGREEN
# endif # endif
#endif #endif
#ifndef CONFIG_EXAMPLES_NXLINES_LINEWIDTH #ifndef CONFIG_EXAMPLES_NXLINES_LINEWIDTH
@ -89,7 +89,31 @@
# define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB16_YELLOW # define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB16_YELLOW
# else # else
# define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB8_YELLOW # define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB8_YELLOW
# endif # endif
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_BORDERWIDTH
# define CONFIG_EXAMPLES_NXLINES_BORDERWIDTH 16
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_BORDERCOLOR
# if CONFIG_EXAMPLES_NXLINES_BPP == 24 || CONFIG_EXAMPLES_NXLINES_BPP == 32
# define CONFIG_EXAMPLES_NXLINES_BORDERCOLOR RGB24_YELLOW
# elif CONFIG_EXAMPLES_NXLINES_BPP == 16
# define CONFIG_EXAMPLES_NXLINES_BORDERCOLOR RGB16_YELLOW
# else
# define CONFIG_EXAMPLES_NXLINES_BORDERCOLOR RGB8_YELLOW
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR
# if CONFIG_EXAMPLES_NXLINES_BPP == 24 || CONFIG_EXAMPLES_NXLINES_BPP == 32
# define CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR RGB24_BEIGE
# elif CONFIG_EXAMPLES_NXLINES_BPP == 16
# define CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR RGB16_BEIGE
# else
# define CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR RGB8_YELLOW
# endif
#endif #endif
/* Debug ********************************************************************/ /* Debug ********************************************************************/

View File

@ -201,84 +201,135 @@ static void nxlines_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
void nxlines_test(NXWINDOW hwnd) void nxlines_test(NXWINDOW hwnd)
{ {
struct nxgl_point_s center;
struct nxgl_vector_s vector; struct nxgl_vector_s vector;
struct nxgl_vector_s previous; struct nxgl_vector_s previous;
nxgl_mxpixel_t color[CONFIG_NX_NPLANES]; nxgl_mxpixel_t color[CONFIG_NX_NPLANES];
nxgl_coord_t xcenter; nxgl_coord_t maxradius;
nxgl_coord_t ycenter;
nxgl_coord_t radius; nxgl_coord_t radius;
nxgl_coord_t halfx; nxgl_coord_t halfx;
nxgl_coord_t halfy; nxgl_coord_t halfy;
b16_t angle; b16_t angle;
int ret; int ret;
/* Get the radius and center of the circle */ /* Get the maximum radius and center of the circle */
radius = MIN(g_nxlines.yres, g_nxlines.xres) >> 1; maxradius = MIN(g_nxlines.yres, g_nxlines.xres) >> 1;
xcenter = g_nxlines.xres >> 1; center.x = g_nxlines.xres >> 1;
ycenter = g_nxlines.yres >> 1; center.y = g_nxlines.yres >> 1;
/* Draw a circular background */
radius = maxradius - ((CONFIG_EXAMPLES_NXLINES_BORDERWIDTH+1)/2);
color[0] = CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR;
ret = nx_fillcircle((NXWINDOW)hwnd, &center, radius, color);
if (ret < 0)
{
message("nxlines_test: nx_fillcircle failed: %d\n", ret);
}
/* Draw the circular border */
color[0] = CONFIG_EXAMPLES_NXLINES_BORDERCOLOR;
ret = nx_drawcircle((NXWINDOW)hwnd, &center, radius,
CONFIG_EXAMPLES_NXLINES_BORDERWIDTH, color);
if (ret < 0)
{
message("nxlines_test: nx_fillcircle failed: %d\n", ret);
}
/* Back off the radius to account for the thickness of border line
* and with a big fudge factor that will (hopefully) prevent the corners
* of the lines from overwriting the border. This is overly complicated
* here because we don't assume anything about the screen resolution or
* the borderwidth or the line thickness (and there are certainly some
* smarter ways to do this).
*/
if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 80))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 40);
}
else if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 60))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 30);
}
else if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 40))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 20);
}
else if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 20))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 10);
}
else
{
radius = maxradius - CONFIG_EXAMPLES_NXLINES_BORDERWIDTH;
}
/* The loop, showing the line in various orientations */
angle = 0; angle = 0;
previous.pt1.x = xcenter; previous.pt1.x = center.x;
previous.pt1.y = ycenter; previous.pt1.y = center.y;
previous.pt2.x = xcenter; previous.pt2.x = center.x;
previous.pt2.y = ycenter; previous.pt2.y = center.y;
for (;;) for (;;)
{ {
/* Determine the position of the line on this pass */ /* Determine the position of the line on this pass */
halfx = b16toi(b16muli(b16sin(angle), radius)); halfx = b16toi(b16muli(b16sin(angle), radius));
halfy = b16toi(b16muli(b16cos(angle), radius)); halfy = b16toi(b16muli(b16cos(angle), radius));
vector.pt1.x = xcenter + halfx; vector.pt1.x = center.x + halfx;
vector.pt1.y = ycenter + halfy; vector.pt1.y = center.y + halfy;
vector.pt2.x = xcenter - halfx; vector.pt2.x = center.x - halfx;
vector.pt2.y = ycenter - halfy; vector.pt2.y = center.y - halfy;
message("Angle: %08x vector: (%d,%d)->(%d,%d)\n", message("Angle: %08x vector: (%d,%d)->(%d,%d)\n",
angle, vector.pt1.x, vector.pt1.y, vector.pt2.x, vector.pt2.y); angle, vector.pt1.x, vector.pt1.y, vector.pt2.x, vector.pt2.y);
/* Clear the previous line */ /* Clear the previous line by overwriting it with the circle color */
color[0] = CONFIG_EXAMPLES_NXLINES_BGCOLOR; color[0] = CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR;
ret = nx_drawline((NXWINDOW)hwnd, &previous, CONFIG_EXAMPLES_NXLINES_LINEWIDTH, color); ret = nx_drawline((NXWINDOW)hwnd, &previous, CONFIG_EXAMPLES_NXLINES_LINEWIDTH, color);
if (ret < 0) if (ret < 0)
{ {
message("nxlines_test: nx_drawline failed clearing: %d\n", ret); message("nxlines_test: nx_drawline failed clearing: %d\n", ret);
} }
/* Draw the new line */ /* Draw the new line */
color[0] = CONFIG_EXAMPLES_NXLINES_LINECOLOR; color[0] = CONFIG_EXAMPLES_NXLINES_LINECOLOR;
ret = nx_drawline((NXWINDOW)hwnd, &vector, CONFIG_EXAMPLES_NXLINES_LINEWIDTH, color); ret = nx_drawline((NXWINDOW)hwnd, &vector, CONFIG_EXAMPLES_NXLINES_LINEWIDTH, color);
if (ret < 0) if (ret < 0)
{ {
message("nxlines_test: nx_drawline failed clearing: %d\n", ret); message("nxlines_test: nx_drawline failed clearing: %d\n", ret);
} }
/* Set up for the next time through the loop then sleep for a bit. */ /* Set up for the next time through the loop then sleep for a bit. */
angle += b16PI / 16; /* 32 angular positions in full circle */ angle += b16PI / 16; /* 32 angular positions in full circle */
/* Check if we have gone all the way around */ /* Check if we have gone all the way around */
if (angle > (31 * (2 * b16PI) / 32)) if (angle > (31 * (2 * b16PI) / 32))
{ {
#ifdef CONFIG_EXAMPLES_NXLINES_BUILTIN #ifdef CONFIG_EXAMPLES_NXLINES_BUILTIN
/* If this example was built as an NSH add-on, then exit after we /* If this example was built as an NSH add-on, then exit after we
* have gone all the way around once. * have gone all the way around once.
*/ */
return; return;
#else #else
/* Wrap back to zero and continue with the test */ /* Wrap back to zero and continue with the test */
angle = 0; angle = 0;
#endif #endif
} }
memcpy(&previous, &vector, sizeof(struct nxgl_vector_s)); memcpy(&previous, &vector, sizeof(struct nxgl_vector_s));
usleep(500*1000); usleep(500*1000);
} }
} }