📄 gfxdraw.c
字号:
long y, xMin, xMax, yMin, yMax, slope, runningSlope; GFX_FILLBLT_PARM_T parm; /* return an error if any part of the triangle goes out of the region */ if ((X1 < 0) || (X2 < 0) || (Y1 < 0) || (Y2 < 0) ) { return(-1); } /* return if the triangle width or height is 0 */ if (X1 == X2){ return(0); } else if (Y1 == Y2){ return(0); } /* determine the min/max of the x and y coordinates */ if (X1 < X2){ xMin = X1; xMax = X2; } else{ xMin = X2; xMax = X1; } if (Y1 < Y2){ yMin = Y1; yMax = Y2; } else{ yMin = Y2; yMax = Y1; } /* figure out the slope of the left and right triangle edges */ slope = ((xMax - xMin) << 15) / (yMax - yMin); runningSlope = 0; parm.hDesSurface = hDes; parm.uDesX = 0; parm.uDesY = 0; parm.uWidth = 0; parm.uHeight = 1; parm.uFillColor = uColor; /* draw each of the scanlines */ for (y = yMin; y < yMax; y++){ X1 = ((xMax + xMin) >> 1) - (runningSlope >> 16); X2 = ((xMax + xMin) >> 1) + (runningSlope >> 16); if(X2 < xMax) { parm.uDesX = X1; parm.uDesY = y; parm.uWidth = X2 - X1; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_triangle: fillBLT failed for handle %d.\n", hDes); return(-1); } } runningSlope += slope; } return ioctl(fdGfxDev, IOC_GFX_WAIT_FOR_COMPLETE, GFX_DRAW_TIMEOUT);}/******************************************************************************* Function: g2d_draw_circle**** Purpose: draw a circle into a region buffer**** Input** Parameters: regionId: zero-based number to identify the region** bufferId: zero-based number to identify the region buffer** fillFlags: indicates if the circle should be filled or** not filled, as follows:** FILL: circle is filled** NOFILL: circle is not filled** pContext: pointer to a struct that identifies drawing** parameters, as follows:** pContext->penColor: color of the circle** X: x-coordinate of center pixel** Y: y-coordinate of center pixel** radius: radius in pixels (not including center pixel)**** Output** Parameters: errorCode: indicates type of error when return value is -1**** Returns: 0: if successful** -1: if an error occurs*****************************************************************************/int gfx_draw_circle( int fdGfxDev, int hDes, long X, long Y, long radius, unsigned int uColor, int fillMode ){ long x, y, D, length0, length1; GFX_FILLBLT_PARM_T parm; /* return an error if any part of the circle falls outside the region */ if (radius < 0 /* || (X - radius) < 0 || (Y - radius) < 0*/ ) { return(-1); } D = 3 - (radius << 1); x = radius; y = 0; length0 = 1; length1 = 1; parm.hDesSurface = hDes; parm.uDesX = 0; parm.uDesY = 0; parm.uWidth = 0; parm.uHeight = 1; parm.uFillColor = uColor; while (y <= x) { if (fillMode) { if(x>0){ length0=(X+x)-(X-x)+1; length1=(X+y)-(X-y)+1; } else{ length0 = 1; length1 = 1; } } parm.uDesX = X-x; parm.uDesY = Y-y; parm.uWidth = length0; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 0 failed\n\r"); return(-1); } parm.uDesY = Y+y; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 1 failed\n\r"); return(-1); } if (!fillMode) { parm.uDesX = X+x; parm.uDesY = Y-y; parm.uWidth = 1; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 2 failed\n\r"); return(-1); } parm.uDesY = Y+y; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 3 failed\n\r"); return(-1); } } if(y!=x) { parm.uDesX = X-y; parm.uDesY = Y-x; parm.uWidth = length1; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 4 failed\n\r"); return(-1); } parm.uDesY = Y+x; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 5 failed\n\r"); return(-1); } if(!fillMode) { parm.uDesX = X+y; parm.uDesY = Y-x; parm.uWidth = 1; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 6 failed\n\r"); return(-1); } parm.uDesY = Y+x; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm) != 0) { fprintf(stderr, "gfx_draw_circle: fillBLT 7 failed\n\r"); return(-1); } } } if (D < 0){ /* update the decision variable */ D = D + (y << 2) + 6; } else{ /* update the decision variable */ D = D + ((y - x) << 2) + 10; x--; } y++; } return ioctl(fdGfxDev, IOC_GFX_WAIT_FOR_COMPLETE, GFX_DRAW_TIMEOUT);}/******************************************************************************* Function: g2d_draw_line**** Purpose: draw a line into a region buffer**** Input** Parameters: regionId: zero-based number to identify the region** bufferId: zero-based number to identify the region buffer** pContext: pointer to a struct that identifies drawing** parameters, as follows:** pContext->penColor: color of the line** X1: x-coordinate of the first pixel of the line** Y1: y-coordinate of the first pixel of the line** X2: x-coordinate of the extent of the line (pixel not drawn)** Y2: y-coordinate of the extent of the line (pixel not drawn)**** Output** Parameters: errorCode: indicates type of error when return value is -1**** Returns: 0: if successful** -1: if an error occurs*****************************************************************************/int gfx_draw_line( int fdGfxDev, int hDes, long X1, long Y1, long X2, long Y2, unsigned int uColor ){ long xMin, yMin, xMax, yMax, xDelta, yDelta, x, y, D, xInc, yInc; GFX_SURFACE_INFO_T sInfo; GFX_FILLBLT_PARM_T parm; int BltWidth=1; int BltHeight=1; int done=0, rtn; sInfo.hSurface = hDes; // try to get some information regarding the surface rtn = ioctl(fdGfxDev, IOC_GFX_GET_SURFACE_INFO, &sInfo); if( rtn < 0) { return rtn; } parm.hDesSurface = hDes; parm.uFillColor = uColor; /* determine the min and max x and y values */ if (X1 < X2){ xMin = X1; xMax = X2; } else{ xMin = X2; xMax = X1; } if (Y1 < Y2){ yMin = Y1; yMax = Y2; } else{ yMin = Y2; yMax = Y1; } x = X1; if (X1 < X2){ xInc = 1; } else if (X1 > X2){ xInc = -1; } else{ xInc = 0; } y = Y1; if (Y1 < Y2){ yInc = 1; } else if (Y1 > Y2){ yInc = -1; } else{ yInc = 0; } xDelta = xMax - xMin; yDelta = yMax - yMin; if (xDelta > yDelta){ D = (yDelta << 1); for(x = X1; (x != X2) && (done == 0); x+=xInc){ /* ensure "real pixel" y-coordinate stays within the region */ if((y >= 0) && (y < sInfo.plane[0].uHeight)) { if(yInc == 0) { BltWidth=xMax-xMin; BltHeight=1; done=1; x=xMin; if(X2==xMin) { x++; } } parm.uDesX = x; parm.uDesY = y; parm.uWidth = BltWidth; parm.uHeight = BltHeight; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm)) { return -1; } } if (D < 0){ D += (yDelta << 1); } else{ D += ((yDelta - xDelta) << 1); y += yInc; } } } else { D = (xDelta << 1); for(y = Y1; (y != Y2) && (done == 0); y+=yInc) { /* ensure "real pixel" x-coordinate stays within the region */ if((x >= 0) && (x < sInfo.plane[0].uWidth)) { /* if a vertical line, draw it using a single blit */ if(xInc == 0) { BltWidth=1; BltHeight=yMax-yMin; done=1; y=yMin; if(Y2==yMin) { y++; } } parm.uDesX = x; parm.uDesY = y; parm.uWidth = BltWidth; parm.uHeight = BltHeight; if(ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm)) { return -1; } } if (D < 0){ D += (xDelta << 1); } else{ D += ((xDelta - yDelta) << 1); x += xInc; } } } return ioctl(fdGfxDev, IOC_GFX_WAIT_FOR_COMPLETE, GFX_DRAW_TIMEOUT);}/******************************************************************************* Function: g2d_draw_pixel**** Purpose: draw a pixel into a region buffer**** Input** Parameters: regionId: zero-based number to identify the region** bufferId: zero-based number to identify the region buffer** pContext: pointer to a struct that identifies drawing** parameters, as follows:** pContext->penColor: color of the pixel** X: x-coordinate of the pixel** Y: y-coordinate of the pixel**** Output** Parameters: errorCode: indicates type of error when return value is -1**** Returns: 0: if successful** -1: if an error occurs*****************************************************************************/int gfx_draw_pixel( int fdGfxDev, int hDes, long X, long Y, unsigned int uColor ){ GFX_FILLBLT_PARM_T parm; /* return an error if the pixel is out of the region */ if ((X < 0) || (Y < 0)) { return(-1); } parm.hDesSurface = hDes; parm.uDesX = X; parm.uDesY = Y; parm.uWidth = 1; parm.uHeight = 1; parm.uFillColor = uColor; return ioctl(fdGfxDev, IOC_GFX_FILLBLT, &parm);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -