diff --git a/src/Imlib2.h b/src/Imlib2.h index 8e0bf94..aa3e45b 100644 --- a/src/Imlib2.h +++ b/src/Imlib2.h @@ -248,6 +248,7 @@ void imlib_image_copy_rect(int x, int y, int width, int height, int new_x,int ne ImlibPolygon imlib_polygon_new(int type); void imlib_polygon_free(ImlibPolygon poly); void imlib_polygon_add_point(ImlibPolygon poly, int x, int y); +void imlib_image_draw_polygon(ImlibPolygon poly); Imlib_Color_Range imlib_create_color_range(void); void imlib_free_color_range(void); diff --git a/src/api.c b/src/api.c index c3c85c5..01b8423 100644 --- a/src/api.c +++ b/src/api.c @@ -2720,3 +2720,31 @@ void imlib_polygon_free(ImlibPolygon poly) __imlib_polygon_free(poly); } +void imlib_image_draw_polygon(ImlibPolygon poly) +{ + ImlibImage *im; + + CHECK_PARAM_POINTER("imlib_image_draw_polygon", "image", ctxt_image); + CAST_IMAGE(im, ctxt_image); + if ((!(im->data)) && (im->loader) && (im->loader->load)) + im->loader->load(im, NULL, 0, 1); + if (!(im->data)) + return; + __imlib_DirtyImage(im); + __imlib_DirtyPixmapsForImage(im); + if(ctxt_cliprect.w) + { + __imlib_draw_polygon_clipped(im, poly, + ctxt_cliprect.x, + ctxt_cliprect.x + ctxt_cliprect.w, + ctxt_cliprect.y, + ctxt_cliprect.y + ctxt_cliprect.h, + ctxt_color.red, ctxt_color.green, + ctxt_color.blue, ctxt_color.alpha, ctxt_operation); + } + else + { + __imlib_draw_polygon(im, poly, ctxt_color.red, ctxt_color.green, + ctxt_color.blue, ctxt_color.alpha, ctxt_operation); + } +} diff --git a/src/rgbadraw.c b/src/rgbadraw.c index 2604f5f..072cff3 100644 --- a/src/rgbadraw.c +++ b/src/rgbadraw.c @@ -1666,3 +1666,42 @@ void __imlib_polygon_free(ImlibPoly poly) free(poly->points); free(poly); } + + +void +__imlib_draw_polygon(ImlibImage * im, ImlibPoly poly, DATA8 r, DATA8 g, + DATA8 b, DATA8 a, ImlibOp op) +{ + int i; + if(!poly || !poly->points || (poly->pointcount < 2)) + return; + + for(i = 0; i < poly->pointcount; i++) + { + if(i < poly->pointcount - 1) + __imlib_draw_line(im, poly->points[i].x, poly->points[i].y, poly->points[i+1].x, poly->points[i+1].y, r, g, b, a, op, 0); + else if(poly->closed) + __imlib_draw_line(im, poly->points[i].x, poly->points[i].y, poly->points[0].x, poly->points[0].y, r, g, b, a, op, 0); + else break; + } +} + +void +__imlib_draw_polygon_clipped(ImlibImage * im, ImlibPoly poly, int clip_xmin, + int clip_xmax, int clip_ymin, int clip_ymax, DATA8 r, DATA8 g, DATA8 b, + DATA8 a, ImlibOp op) +{ + int i; + if(!poly || !poly->points || (poly->pointcount < 2)) + return; + + for(i = 0; i < poly->pointcount; i++) + { + if(i < poly->pointcount - 1) + __imlib_draw_line_clipped(im, poly->points[i].x, poly->points[i].y, poly->points[i+1].x, poly->points[i+1].y, clip_xmin, clip_xmax, clip_ymin, clip_ymax, r, g, b, a, op, 0); + else if(poly->closed) + __imlib_draw_line_clipped(im, poly->points[i].x, poly->points[i].y, poly->points[0].x, poly->points[0].y, clip_xmin, clip_xmax, clip_ymin, clip_ymax, r, g, b, a, op, 0); + else break; + } +} + diff --git a/src/rgbadraw.h b/src/rgbadraw.h index 53bc65c..c5cac68 100644 --- a/src/rgbadraw.h +++ b/src/rgbadraw.h @@ -31,7 +31,6 @@ struct _imlib_polygon unsigned char closed, filled; ImlibPoint *points; int pointcount; - DATA8 r,g,b,a; }; void __imlib_polygon_free(ImlibPoly poly); @@ -60,6 +59,9 @@ void __imlib_copy_image_data(ImlibImage *im, int x, int y, int w, int h, int nx, void __imlib_copy_alpha_data(ImlibImage *src, ImlibImage *dst, int x, int y, int w, int h, int nx, int ny); ImlibOutCode __imlib_comp_outcode(double x, double y, double xmin, double xmax, double ymin, double ymax); +void +__imlib_draw_polygon(ImlibImage * im, ImlibPoly poly, DATA8 r, DATA8 g, + DATA8 b, DATA8 a, ImlibOp op); ImlibUpdate * __imlib_draw_line_clipped(ImlibImage * im, int x1, int y1, int x2, int y2, int clip_xmin, int clip_xmax, int clip_ymin, @@ -70,4 +72,8 @@ __imlib_draw_box_clipped(ImlibImage * im, int x, int y, int w, int h, int clip_xmin, int clip_xmax, int clip_ymin, int clip_ymax, DATA8 r, DATA8 g, DATA8 b, DATA8 a, ImlibOp op); +void +__imlib_draw_polygon_clipped(ImlibImage * im, ImlibPoly poly, int clip_xmin, + int clip_xmax, int clip_ymin, int clip_ymax, DATA8 r, DATA8 g, DATA8 b, + DATA8 a, ImlibOp op); #endif diff --git a/test/main.c b/test/main.c index 4912bc7..2018be3 100644 --- a/test/main.c +++ b/test/main.c @@ -79,6 +79,7 @@ int main (int argc, char **argv) int origone = 0; int bump_map_to_point = 0; Imlib_Color_Modifier colormod = 0; + ImlibPolygon poly, poly2, poly3; /** * Parse all the command line arguments @@ -293,6 +294,21 @@ int main (int argc, char **argv) gettimeofday(&timev,NULL); sec1=(int)timev.tv_sec; /* and stores it so we can time outselves */ usec1=(int)timev.tv_usec; /* we will use this to vary speed of rot */ + + poly = imlib_polygon_new(POLY_CLOSED); + imlib_polygon_add_point(poly, 400,50); + imlib_polygon_add_point(poly, 450,100); + imlib_polygon_add_point(poly, 350,100); + + poly2 = imlib_polygon_new(POLY_OPEN); + imlib_polygon_add_point(poly2, 400,150); + imlib_polygon_add_point(poly2, 450,200); + imlib_polygon_add_point(poly2, 350,200); + + poly3 = imlib_polygon_new(POLY_CLOSED); + imlib_polygon_add_point(poly3, 400,250); + imlib_polygon_add_point(poly3, 450,300); + imlib_polygon_add_point(poly3, 350,300); if (loop) { @@ -816,6 +832,21 @@ int main (int argc, char **argv) imlib_image_draw_rectangle(30,120,50,50); up = imlib_update_append_rect(up, 30,120,50,50); + imlib_context_set_cliprect(0,0,0,0); + + /* test polygons */ + imlib_context_set_color(255, 255, 255, 255); + imlib_image_draw_polygon(poly); + imlib_image_draw_polygon(poly2); + imlib_image_draw_polygon(poly3); + imlib_image_draw_rectangle(380,260,50,50); + + imlib_context_set_color(255, 55, 55, 255); + imlib_context_set_cliprect(380,260,50,50); + imlib_image_draw_polygon(poly3); + imlib_context_set_cliprect(0,0,0,0); + + } { static Imlib_Color_Range rg = NULL;