简体   繁体   中英

Cannot draw polygons using XCB in C

Just started with XCB programming with C. I have written this code which will create a window and draw some points and a rectangle:

#include <stdlib.h>
#include <stdio.h>
#include <xcb/xcb.h>

int main() {
    xcb_connection_t *connection = xcb_connect(NULL, NULL);
    if (xcb_connection_has_error(connection)) {
        printf("Error setting up connection to X\n");
        exit(1);
    }
    const xcb_setup_t *setup = xcb_get_setup(connection);
    xcb_screen_t *screen = xcb_setup_roots_iterator(setup).data;

    // Create wndow
    xcb_window_t window_id = xcb_generate_id(connection);
    uint32_t prop_name = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
    uint32_t prop_value[2];
    prop_value[0] = screen->white_pixel;
    prop_value[1] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_EXPOSURE;
    xcb_create_window(connection, screen->root_depth, window_id, screen->root, 100, 100, 500, 400, 1,
                            XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, prop_name, &prop_value);
    xcb_map_window(connection, window_id);
    xcb_flush(connection);

    uint32_t gc_value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
    uint32_t gc_value_list[2];
    gc_value_list[0] = screen->black_pixel;
    gc_value_list[1] = 0;
    xcb_drawable_t drawable_window = screen->root;
    xcb_gcontext_t context_id = xcb_generate_id(connection);
    xcb_void_cookie_t cookie = xcb_create_gc(connection, context_id, drawable_window, gc_value_mask, gc_value_list);

    // Create polygons
    xcb_point_t points[5] = {{10, 10}, {10, 50}, {40, 70}, {70, 40}, {50, 10}};
    xcb_rectangle_t rect = {50, 50, 100, 50};

    // Main loop
    xcb_generic_event_t *event;
    while ((event = xcb_wait_for_event(connection))) {
        switch (event->response_type) {
            case XCB_KEY_PRESS:
                break;
            case XCB_BUTTON_PRESS:
                break;
            case XCB_EXPOSE:
                printf("Expose event called\n");
                // Draw polygons
                xcb_poly_point(connection, XCB_COORD_MODE_ORIGIN, drawable_window, context_id, 4, points);
                xcb_poly_fill_rectangle(connection, drawable_window, context_id, 1, &rect);
                xcb_flush(connection);
            default:
                break;
        }
        free(event);
    }

    return 0;
}

It has compiled properly and there was no warning or error at runtime. But, I just get a window with no graphics. What am I doing wrong?

I think your xcb_poly_fill_rectangle() , etc., are trying to draw on the root window. At least, you're passing screen->root as the drawable argument. It works for me if I pass window_id here.

I'm really not sure why XCB needs all these various context arguments, and the exact functions of each do not seem to be well-documented.

Kevin Boone is correct: You are drawing to the root window.

I did the following change to your program (it is the same one that Kevin suggested):

--- t.c.orig    2020-09-18 15:06:38.344441255 +0200
+++ t.c 2020-09-18 15:06:48.104167556 +0200
@@ -26,7 +26,7 @@ int main() {
     uint32_t gc_value_list[2];
     gc_value_list[0] = screen->black_pixel;
     gc_value_list[1] = 0;
-    xcb_drawable_t drawable_window = screen->root;
+    xcb_drawable_t drawable_window = window_id;
     xcb_gcontext_t context_id = xcb_generate_id(connection);
     xcb_void_cookie_t cookie = xcb_create_gc(connection, context_id, drawable_window, gc_value_mask, gc_value_list);

This results in the following window contents being shown. You can see both the rectangle and the individual pixels being shown:

在此处输入图片说明

Please do not accept this answer, but instead Kevin Boone's. I only wanted to add the screenshot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM