diff options
author | brian.thomas.will <brian.thomas.will@gmail.com> | 2007-11-11 20:31:29 -0800 |
---|---|---|
committer | XCB site <xcb@annarchy.freedesktop.org> | 2007-11-11 20:31:29 -0800 |
commit | bad86aded890740044b199c9a4c0db71a68fa8e6 (patch) | |
tree | 990c397e27edee23153d505670bb918356cbf447 /tutorial.mdwn | |
parent | 0a24f46f1726a057be6634dcf9b866bd134e20c5 (diff) |
refactored section 9.3 ("drawing primitives")
Diffstat (limited to 'tutorial.mdwn')
-rw-r--r-- | tutorial.mdwn | 356 |
1 files changed, 175 insertions, 181 deletions
diff --git a/tutorial.mdwn b/tutorial.mdwn index 0c04b87..d83d4c8 100644 --- a/tutorial.mdwn +++ b/tutorial.mdwn @@ -568,97 +568,96 @@ TODO: give an example which sets several attributes. ### 9.3 Drawing primitives: point, line, box, circle,... - After we have created a Graphic Context, we can draw on a window using this Graphic Context, with a set of XCB functions, collectively called "drawing primitives". Let see how they are used. To draw a point, or several points, we use: - xcb_void_cookie_t xcb_poly_point (xcb_connection_t *c, /* The connection to the X server */ - uint8_t coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */ - xcb_drawable_t drawable, /* The drawable on which we want to draw the point(s) */ - xcb_gcontext_t gc, /* The Graphic Context we use to draw the point(s) */ - uint32_t points_len, /* The number of points */ - const xcb_point_t *points); /* An array of points */ + xcb_void_cookie_t xcb_poly_point (xcb_connection_t *c, /* The connection to the X server */ + uint8_t coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */ + xcb_drawable_t drawable, /* The drawable on which we want to draw the point(s) */ + xcb_gcontext_t gc, /* The Graphic Context we use to draw the point(s) */ + uint32_t points_len, /* The number of points */ + const xcb_point_t *points ); /* An array of points */ The coordinate_mode parameter specifies the coordinate mode. Available values are: -* XCB_COORD_MODE_ORIGIN -* XCB_COORD_MODE_PREVIOUS + XCB_COORD_MODE_ORIGIN + XCB_COORD_MODE_PREVIOUS -If XCB_COORD_MODE_PREVIOUS is used, then all points but the first one are relative to the immediately previous point. +If XCB\_COORD\_MODE\_PREVIOUS is used, then all points but the first one are relative to the immediately previous point. -The xcb_point_t type is just a structure with two fields (the coordinates of the point): +The xcb\_point\_t type is just a structure with two fields (the coordinates of the point): - typedef struct { - int16_t x; - int16_t y; - } xcb_point_t; + typedef struct { + int16_t x; + int16_t y; + } xcb_point_t; You could see an example in xpoints.c. TODO Set the link. To draw a line, or a polygonal line, we use: - xcb_void_cookie_t xcb_poly_line (xcb_connection_t *c, /* The connection to the X server */ - uint8_t coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */ - xcb_drawable_t drawable, /* The drawable on which we want to draw the line(s) */ - xcb_gcontext_t gc, /* The Graphic Context we use to draw the line(s) */ - uint32_t points_len, /* The number of points in the polygonal line */ - const xcb_point_t *points); /* An array of points */ + xcb_void_cookie_t xcb_poly_line (xcb_connection_t *c, /* The connection to the X server */ + uint8_t coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */ + xcb_drawable_t drawable, /* The drawable on which we want to draw the line(s) */ + xcb_gcontext_t gc, /* The Graphic Context we use to draw the line(s) */ + uint32_t points_len, /* The number of points in the polygonal line */ + const xcb_point_t *points ); /* An array of points */ This function will draw the line between the first and the second points, then the line between the second and the third points, and so on. -To draw a segment, or several segments, we use +To draw a segment, or several segments, we use: - xcb_void_cookie_t xcb_poly_segment (xcb_connection_t *c, /* The connection to the X server */ - xcb_drawable_t drawable, /* The drawable on which we want to draw the segment(s) */ - xcb_gcontext_t gc, /* The Graphic Context we use to draw the segment(s) */ - uint32_t segments_len, /* The number of segments */ - const xcb_segment_t *segments); /* An array of segments */ - -The xcb_segment_t type is just a structure with four fields (the coordinates of the two points that define the segment): - - typedef struct { - int16_t x1; - int16_t y1; - int16_t x2; - int16_t y2; - } xcb_segment_t; + xcb_void_cookie_t xcb_poly_segment (xcb_connection_t *c, /* The connection to the X server */ + xcb_drawable_t drawable, /* The drawable on which we want to draw the segment(s) */ + xcb_gcontext_t gc, /* The Graphic Context we use to draw the segment(s) */ + uint32_t segments_len, /* The number of segments */ + const xcb_segment_t *segments ); /* An array of segments */ -To draw a rectangle, or several rectangles, we use +The xcb\_segment\_t type is just a structure with four fields (the coordinates of the two points that define the segment): - xcb_void_cookie_t xcb_poly_rectangle (xcb_connection_t *c, /* The connection to the X server */ - xcb_drawable_t drawable, /* The drawable on which we want to draw the rectangle(s) */ - xcb_gcontext_t gc, /* The Graphic Context we use to draw the rectangle(s) */ - uint32_t rectangles_len, /* The number of rectangles */ - const xcb_rectangle_t *rectangles); /* An array of rectangles */ - -The xcb_rectangle_t type is just a structure with four fields (the coordinates of the top-left corner of the rectangle, and its width and height): - - typedef struct { - int16_t x; - int16_t y; - uint16_t width; - uint16_t height; - } xcb_rectangle_t; + typedef struct { + int16_t x1; + int16_t y1; + int16_t x2; + int16_t y2; + } xcb_segment_t; -To draw an elliptical arc, or several elliptical arcs, we use +To draw a rectangle, or several rectangles, we use: - xcb_void_cookie_t xcb_poly_arc (xcb_connection_t *c, /* The connection to the X server */ - xcb_drawable_t drawable, /* The drawable on which we want to draw the arc(s) */ - xcb_gcontext_t gc, /* The Graphic Context we use to draw the arc(s) */ - uint32_t arcs_len, /* The number of arcs */ - const xcb_arc_t *arcs); /* An array of arcs */ + xcb_void_cookie_t xcb_poly_rectangle (xcb_connection_t *c, /* The connection to the X server */ + xcb_drawable_t drawable, /* The drawable on which we want to draw the rectangle(s) */ + xcb_gcontext_t gc, /* The Graphic Context we use to draw the rectangle(s) */ + uint32_t rectangles_len, /* The number of rectangles */ + const xcb_rectangle_t *rectangles ); /* An array of rectangles */ -The xcb_arc_t type is a structure with six fields: +The xcb\_rectangle\_t type is just a structure with four fields (the coordinates of the top-left corner of the rectangle, and its width and height): - typedef struct { - int16_t x; /* Top left x coordinate of the rectangle surrounding the ellipse */ - int16_t y; /* Top left y coordinate of the rectangle surrounding the ellipse */ - uint16_t width; /* Width of the rectangle surrounding the ellipse */ - uint16_t height; /* Height of the rectangle surrounding the ellipse */ - int16_t angle1; /* Angle at which the arc begins */ - int16_t angle2; /* Angle at which the arc ends */ - } xcb_arc_t; + typedef struct { + int16_t x; + int16_t y; + uint16_t width; + uint16_t height; + } xcb_rectangle_t; + +To draw an elliptical arc, or several elliptical arcs, we use: + + xcb_void_cookie_t xcb_poly_arc (xcb_connection_t *c, /* The connection to the X server */ + xcb_drawable_t drawable, /* The drawable on which we want to draw the arc(s) */ + xcb_gcontext_t gc, /* The Graphic Context we use to draw the arc(s) */ + uint32_t arcs_len, /* The number of arcs */ + const xcb_arc_t *arcs ); /* An array of arcs */ + +The xcb\_arc\_t type is a structure with six fields: + + typedef struct { + int16_t x; /* Top left x coordinate of the rectangle surrounding the ellipse */ + int16_t y; /* Top left y coordinate of the rectangle surrounding the ellipse */ + uint16_t width; /* Width of the rectangle surrounding the ellipse */ + uint16_t height; /* Height of the rectangle surrounding the ellipse */ + int16_t angle1; /* Angle at which the arc begins */ + int16_t angle2; /* Angle at which the arc ends */ + } xcb_arc_t; Note: the angles are expressed in units of 1/64 of a degree, so to have an angle of 90 degrees, starting at 0, angle1 = 0 and angle2 = 90 << 6. Positive angles indicate counterclockwise motion, while negative angles indicate clockwise motion. @@ -666,156 +665,151 @@ The corresponding function which fill inside the geometrical object are listed b To Fill a polygon defined by the points given as arguments , we use - xcb_void_cookie_t xcb_fill_poly (xcb_connection_t *c, - xcb_drawable_t drawable, - xcb_gcontext_t gc, - uint8_t shape, - uint8_t coordinate_mode, - uint32_t points_len, - const xcb_point_t *points); - -The shape parameter specifies a shape that helps the server to improve performance. Available values are + xcb_void_cookie_t xcb_fill_poly (xcb_connection_t *c, + xcb_drawable_t drawable, + xcb_gcontext_t gc, + uint8_t shape, + uint8_t coordinate_mode, + uint32_t points_len, + const xcb_point_t *points ); -* XCB_POLY_SHAPE_COMPLEX -* XCB_POLY_SHAPE_NONCONVEX -* XCB_POLY_SHAPE_CONVEX +The shape parameter specifies a shape that helps the server to improve performance. Available values are: -To fill one or several rectangles, we use + XCB_POLY_SHAPE_COMPLEX + XCB_POLY_SHAPE_NONCONVEX + XCB_POLY_SHAPE_CONVEX - xcb_void_cookie_t xcb_poly_fill_rectangle (xcb_connection_t *c, - xcb_drawable_t drawable, - xcb_gcontext_t gc, - uint32_t rectangles_len, - const xcb_rectangle_t *rectangles); +To fill one or several rectangles, we use: -To fill one or several arcs, we use + xcb_void_cookie_t xcb_poly_fill_rectangle (xcb_connection_t *c, + xcb_drawable_t drawable, + xcb_gcontext_t gc, + uint32_t rectangles_len, + const xcb_rectangle_t *rectangles ); - xcb_void_cookie_t xcb_poly_fill_arc (xcb_connection_t *c, - xcb_drawable_t drawable, - xcb_gcontext_t gc, - uint32_t arcs_len, - const xcb_arc_t *arcs); +To fill one or several arcs, we use: + xcb_void_cookie_t xcb_poly_fill_arc (xcb_connection_t *c, + xcb_drawable_t drawable, + xcb_gcontext_t gc, + uint32_t arcs_len, + const xcb_arc_t *arcs ); To illustrate these functions, here is an example that draws four points, a polygonal line, two segments, two rectangles and two arcs. Remark that we use events for the first time, as an introduction to the next section. TODO: Use screen->root_depth for depth parameter. - #include <stdlib.h> - #include <stdio.h> + #include <stdlib.h> + #include <stdio.h> - #include <xcb/xcb.h> + #include <xcb/xcb.h> - int - main () - { - xcb_connection_t *c; - xcb_screen_t *screen; - xcb_drawable_t win; - xcb_gcontext_t foreground; - xcb_generic_event_t *e; - uint32_t mask = 0; - uint32_t values[2]; + int + main () + { + /* geometric objects */ + xcb_point_t points[] = { + {10, 10}, + {10, 20}, + {20, 10}, + {20, 20}}; - /* geometric objects */ - xcb_point_t points[] = { - {10, 10}, - {10, 20}, - {20, 10}, - {20, 20}}; + xcb_point_t polyline[] = { + {50, 10}, + { 5, 20}, /* rest of points are relative */ + {25,-20}, + {10, 10}}; - xcb_point_t polyline[] = { - {50, 10}, - { 5, 20}, /* rest of points are relative */ - {25,-20}, - {10, 10}}; + xcb_segment_t segments[] = { + {100, 10, 140, 30}, + {110, 25, 130, 60}}; - xcb_segment_t segments[] = { - {100, 10, 140, 30}, - {110, 25, 130, 60}}; + xcb_rectangle_t rectangles[] = { + { 10, 50, 40, 20}, + { 80, 50, 10, 40}}; - xcb_rectangle_t rectangles[] = { - { 10, 50, 40, 20}, - { 80, 50, 10, 40}}; + xcb_arc_t arcs[] = { + {10, 100, 60, 40, 0, 90 << 6}, + {90, 100, 55, 40, 0, 270 << 6}}; - xcb_arc_t arcs[] = { - {10, 100, 60, 40, 0, 90 << 6}, - {90, 100, 55, 40, 0, 270 << 6}}; - /* Open the connection to the X server */ - c = xcb_connect (NULL, NULL); + /* Open the connection to the X server */ + xcb_connection_t *connection = xcb_connect (NULL, NULL); - /* Get the first screen */ - screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data; + /* Get the first screen */ + xcb_screen_t *screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data; - /* Create black (foreground) graphic context */ - win = screen->root; + /* Create black (foreground) graphic context */ + xcb_drawable_t window = screen->root; + xcb_gcontext_t foreground = xcb_generate_id (connection); + uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; + uint32_t values[2] = {screen->black_pixel, 0}; - foreground = xcb_generate_id (c); - mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; - values[0] = screen->black_pixel; - values[1] = 0; - xcb_create_gc (c, foreground, win, mask, values); + xcb_create_gc (connection, foreground, window, mask, values); - /* Ask for our window's Id */ - win = xcb_generate_id(c); - /* Create the window */ - mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; - values[0] = screen->white_pixel; - values[1] = XCB_EVENT_MASK_EXPOSURE; - xcb_create_window (c, /* Connection */ - XCB_COPY_FROM_PARENT, /* depth */ - win, /* window Id */ - screen->root, /* parent window */ - 0, 0, /* x, y */ - 150, 150, /* width, height */ - 10, /* border_width */ - XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */ - screen->root_visual, /* visual */ - mask, values); /* masks */ + /* Create a window */ - /* Map the window on the screen */ - xcb_map_window (c, win); + window = xcb_generate_id (connection); + mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + values[0] = screen->white_pixel; + values[1] = XCB_EVENT_MASK_EXPOSURE; - /* We flush the request */ - xcb_flush (c); + xcb_create_window (connection, /* connection */ + XCB_COPY_FROM_PARENT, /* depth */ + window, /* window Id */ + screen->root, /* parent window */ + 0, 0, /* x, y */ + 150, 150, /* width, height */ + 10, /* border_width */ + XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */ + screen->root_visual, /* visual */ + mask, values ); /* masks */ - while ((e = xcb_wait_for_event (c))) { - switch (e->response_type & ~0x80) { - case XCB_EXPOSE: { - /* We draw the points */ - xcb_poly_point (c, XCB_COORD_MODE_ORIGIN, win, foreground, 4, points); - /* We draw the polygonal line */ - xcb_poly_line (c, XCB_COORD_MODE_PREVIOUS, win, foreground, 4, polyline); + /* Map the window on the screen and flush*/ + xcb_map_window (connection, window); + xcb_flush (connection); + + + /* draw primitives */ - /* We draw the segements */ - xcb_poly_segment (c, win, foreground, 2, segments); + xcb_generic_event_t *event; + while (event = xcb_wait_for_event (connection)) { + switch (event->response_type & ~0x80) { + case XCB_EXPOSE: + /* We draw the points */ + xcb_poly_point (connection, XCB_COORD_MODE_ORIGIN, window, foreground, 4, points); - /* We draw the rectangles */ - xcb_poly_rectangle (c, win, foreground, 2, rectangles); + /* We draw the polygonal line */ + xcb_poly_line (connection, XCB_COORD_MODE_PREVIOUS, window, foreground, 4, polyline); - /* We draw the arcs */ - xcb_poly_arc (c, win, foreground, 2, arcs); + /* We draw the segements */ + xcb_poly_segment (connection, window, foreground, 2, segments); - /* We flush the request */ - xcb_flush (c); + /* draw the rectangles */ + xcb_poly_rectangle (connection, window, foreground, 2, rectangles); - break; - } - default: { - /* Unknown event type, ignore it */ - break; - } - } - /* Free the Generic Event */ - free (e); - } + /* draw the arcs */ + xcb_poly_arc (connection, window, foreground, 2, arcs); + + /* flush the request */ + xcb_flush (connection); + + break; + default: + /* Unknown event type, ignore it */ + break; + } + + /* Free the Generic Event */ + free (event); + } + + return 0; + } - return 0; - } # 10. X Events |