📄 draw.c
字号:
/**********************************************************************
* $Workfile: draw.c $
* $Revision: 1.0 $
* $Author: WellsK $
* $Date: Sep 23 2002 13:54:18 $
*
* Project: LH7A400
*
* Description:
* Basic draw functions (line, box, circle). All pixel coordinates
* are positive. (X=0, Y=0 is the upper left hand corner of the
* display).
*
* Note
* Pen width is current not implemented.
*
* References:
* None
*
* Revision History:
* $Log: //smaicnt2/pvcs/VM/CDROM/archives/KEV7A400/Software/Board applications/Simple startup demo/draw.c-arc $
*
* Rev 1.0 Sep 23 2002 13:54:18 WellsK
* Initial revision.
*
*
* COPYRIGHT (C) 2001 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
*********************************************************************/
#include "draw.h"
// Maximum X and Y display coordinates
int xlim, ylim;
// Saved address of frame buffer (must be physical address)
UNS_16 *frame_addr;
// Pen widths and colors
UNS_16 draw_pen_width;
UNS_16 draw_pen_color;
UNS_16 draw_fill_color;
/**********************************************************************
*
* Function: draw_init
*
* Purpose:
* Initialize the draw package, set the frame buffer base address
*
* Processing:
* TBD
*
* Parameters:
* fb_addr: Physical address of the frame buffer
* xmax : Maximum X value of the display
* ymax : Maximum Y value of the display
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_init (UNS_16 *fb_addr, int xmax, int ymax)
{
// Save display and frame buffer settings
frame_addr = fb_addr;
xlim = xmax;
ylim = ymax;
// Set default colors and widths
draw_pen_width = 1;
draw_pen_color = 0x0;
draw_fill_color = 0x0;
}
/**********************************************************************
*
* Function: draw_set_pen_width
*
* Purpose:
* Set the pen width (in pixels)
*
* Processing:
* TBD
*
* Parameters:
* pen_width: Pen width in pixels
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_set_pen_width (int pen_width)
{
draw_pen_width = pen_width;
}
/**********************************************************************
*
* Function: draw_set_pen_width
*
* Purpose:
* Set the pen color
*
* Processing:
* TBD
*
* Parameters:
* pen_width: Pen width in pixels
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_set_pen_color (UNS_16 pen_color)
{
draw_pen_color = pen_color;
}
/**********************************************************************
*
* Function: draw_set_pen_width
*
* Purpose:
* Set fill color (used for boxes and circles)
*
* Processing:
* TBD
*
* Parameters:
* pen_width: Pen width in pixels
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_set_fill_color (UNS_16 fill_color)
{
draw_fill_color = fill_color;
}
/**********************************************************************
*
* Function: draw_line
*
* Purpose:
* Draw a line from (X1, Y1) to (X2, Y2)
*
* Processing:
* TBD
*
* Parameters:
* x1: Upper left X coordinate
* y1: Upper left Y coordinate
* x2: Lower left X coordinate
* y2: Lower left Y coordinate
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_line (int x1, int y1, int x2, int y2)
{
int xinc, yinc;
int pixel_start, pixel_end;
int line;
int xv, yv;
int offs;
// Drawing is performed at a much 'higher' resolution that the
// actual display. Before writing to the frame buffer, the 'lower'
// resolution is restored. xscale and yscale are virtual limits
// for the display size
// Swap x1 and x2 if x1 is larger than x2
if (x1 > x2)
{
xinc = x1;
x1 = x2;
x2 = xinc;
}
// Swap y1 and y2 if y1 is larger than y2
if (y1 > y2)
{
yinc = y1;
y1 = y2;
y2 = yinc;
}
// Clip edges of display
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
if (x2 >= xlim) x2 = xlim - 1;
if (y2 >= ylim) y2 = ylim - 1;
// Determine difference X and Y values
xinc = x2 - x1;
yinc = y2 - y1;
// Use the X or Y axis for drawing (whichever is longer)
if ((xinc == 0) && (yinc == 0))
{
pixel_start = x1; // Starting virtual pixel
pixel_end = x2; // Ending virtual pixel
xinc = xlim;
yinc = ylim;
}
if (xinc > yinc)
{
// Use X axis for drawing
pixel_start = x1; // Starting virtual pixel
pixel_end = x2; // Ending virtual pixel
// Determine incremental values for X and Y
yinc = ylim * yinc / xinc;
xinc = xlim;
}
else
{
// Use Y axis for drawing
pixel_start = y1; // Starting virtual pixel
pixel_end = y2; // Ending virtual pixel
// Determine scaling factors
xinc = xlim * xinc / yinc;
yinc = ylim;
}
// Starting virtual X and Y values
xv = x1 * xlim;
yv = y1 * ylim;
// Draw line
for (line = pixel_start; line <= pixel_end;)
{
// Convert virtual pixel address into real offset
offs = (xv / xlim) + (yv / ylim) * xlim;
* (frame_addr + offs) = draw_pen_color;
// Increment X and Y virtual pixel locations
xv = xv + xinc;
yv = yv + yinc;
// Increment to next virtual pixel
line++;
}
}
/**********************************************************************
*
* Function: draw_box
*
* Purpose:
* Place a box with corners (X1, Y1) and (X2, Y2)
*
* Processing:
* TBD
*
* Parameters:
* x1: Upper left X coordinate
* y1: Upper left Y coordinate
* x2: Lower left X coordinate
* y2: Lower left Y coordinate
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_box (int x1, int y1, int x2, int y2)
{
int xinc, yinc;
int xlines, ylines;
int x, y;
if (x1 > x2)
{
xinc = x1;
x1 = x2;
x2 = xinc;
}
// Swap y1 and y2 if y1 is larger than y2
if (y1 > y2)
{
yinc = y1;
y1 = y2;
y2 = yinc;
}
// Clip edges of display
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
if (x2 >= xlim) x2 = xlim - 1;
if (y2 >= ylim) y2 = ylim - 1;
// Get X and Y differences
xinc = x2 - x1;
yinc = y2 - y1;
// Number of X and Y lines to put with fill color
xlines = xinc - 2;
ylines = yinc - 2;
// First, draw the 'guts' with the fill color
if ((xlines > 0) && (ylines > 0))
{
for (x = (x1 + 1); x < x2; x++)
{
for (y = (y1 + 1); y < y2; y++)
{
* (frame_addr + x + (y * xlim)) = draw_fill_color;
}
}
}
// Make outer edge of box in pen color
draw_line (x1, y1, x2, y1);
draw_line (x2, y1, x2, y2);
draw_line (x2, y2, x1, y2);
draw_line (x1, y2, x1, y1);
}
/**********************************************************************
*
* Function: draw_circle
*
* Purpose:
* Place a circle with center (X1, Y1) and radius RAD (in pixels)
*
* Processing:
* TBD
*
* Parameters:
* None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_circle (int x1, int y1, int RAD)
{
;
}
/**********************************************************************
*
* Function: draw_clear_screen
*
* Purpose:
* Fills the display with the active fill color
*
* Processing:
* TBD
*
* Parameters:
* None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_clear_screen (void)
{
int J;
for (J = 0; J < (xlim * ylim); J++)
{
* (frame_addr + J) = draw_fill_color;
}
}
/**********************************************************************
*
* Function: draw_pixel
*
* Purpose:
* Puts a pixel at (X, Y) in the pen color
*
* Processing:
* TBD
*
* Parameters:
* x1: X coordinate
* y1: Y coordinate
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void draw_pixel (int x1, int y1)
{
* (frame_addr + x1 + (y1 * xlim)) = draw_pen_color;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -