• Drawing on drawable and setting color

    From Francis Assissi@21:1/5 to All on Wed Mar 11 20:24:10 2020
    Hello, I am trying to learn libxcb, how do I draw on a drawable
    with a specific color using the C language? Source code below.

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

    #define WIDTH 640
    #define HEIGHT 480
    #define POINTS 360

    // cc program.c -o program -lxcb -lm
    int main(void)
    {
    xcb_connection_t *connection = xcb_connect(NULL, NULL);;
    uint8_t coordinate_mode = XCB_COORD_MODE_ORIGIN;
    const xcb_setup_t *setup = xcb_get_setup(connection);
    xcb_screen_iterator_t screen_iterator = xcb_setup_roots_iterator(setup);
    xcb_screen_t *screen = screen_iterator.data;
    xcb_drawable_t drawable =
    xcb_generate_id(connection);
    uint32_t value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
    uint32_t value_list[2] = {screen -> black_pixel,
    0};
    xcb_gcontext_t gc =
    xcb_generate_id(connection);
    uint32_t points_len = 0;
    uint16_t res_width = WIDTH;
    uint16_t res_height = HEIGHT;
    xcb_point_t *points =
    malloc(sizeof(xcb_point_t) * POINTS);
    xcb_generic_event_t *event = NULL;

    xcb_create_gc(connection, gc, drawable, value_mask, value_list);
    value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
    value_list[0] = screen -> white_pixel;
    value_list[1] = XCB_EVENT_MASK_EXPOSURE;
    xcb_create_window(connection,
    XCB_COPY_FROM_PARENT,
    drawable,
    screen -> root,
    0, 0,
    res_width, res_height,
    10,
    XCB_WINDOW_CLASS_INPUT_OUTPUT,
    screen -> root_visual,
    value_mask, value_list);
    xcb_map_window(connection, drawable);
    xcb_flush(connection);
    // Map line in memory
    while(points_len < POINTS) {
    points[points_len].x = points_len;
    points[points_len].y = 150;
    ++points_len;
    }
    // Set circle color
    value_mask = XCB_GC_FOREGROUND;
    value_list[0] = 0xFF000000; // Red
    value_list[1] = 0;
    xcb_change_gc(connection, gc, value_mask, value_list);
    // Draw it on the window
    while((event = xcb_wait_for_event(connection))) {
    switch(event -> response_type & ~0x80) {
    case XCB_EXPOSE: {
    xcb_poly_point(connection,
    coordinate_mode, drawable, gc, POINTS, points);
    xcb_flush(connection);
    break;
    }
    default: {
    break;
    }
    }
    free(event);
    }
    xcb_free_gc(connection, gc);
    xcb_disconnect(connection);
    exit(EXIT_SUCCESS);
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From patch@21:1/5 to Francis Assissi on Thu Mar 26 03:00:44 2020
    On Wed, 11 Mar 2020 20:24:10 +0000, Francis Assissi wrote:

    Hello, I am trying to learn libxcb, how do I draw on a drawable with a specific color using the C language? Source code below.

    When you create the GC you get the depth from 'drawable' (your window)
    which hasn't been created yet.

    xcb_create_gc(connection, gc, drawable, value_mask, value_list);

    You should supply the depth from screen->root at that point.

    value_list[0] = 0xFF000000; // Red value_list[1] = 0;

    That is most likely not the color red, but black. 0xFF0000, should be red
    on TrueColor and DirectColor.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)