[Xcb] Setting colors and drawing

Spam Hater ireallyhatespam at yandex.com
Thu Mar 12 18:18:18 UTC 2020


Hello again, what am I overlooking when attempting to draw the line? How 
do I set color for the set of points I am drawing? How to set color 
other than black? Sorry for the messy formatting, hopefully it will be 
good enough to be readable.

#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;
         xcb_drawable_t drawable           = xcb_generate_id(connection);
         xcb_generic_event_t *event            = NULL;
         xcb_point_t *points               = malloc(sizeof(xcb_point_t) *
                                                         POINTS);
         uint32_t points_len                   = 0;
         xcb_gcontext_t gc                 = xcb_generate_id(connection);
         uint16_t res_width                    = WIDTH;
         uint16_t res_height                   = HEIGHT;
         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;
         uint32_t value_mask                = XCB_GC_FOREGROUND |
                                              XCB_GC_GRAPHICS_EXPOSURES;

         uint32_t value_list[2]             = {screen -> black_pixel, 0};

         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 line color
         value_mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
 
//<https://stackoverflow.com/questions/41279160/xcb-library-how-to-draw-in-colours>
         //value_list[0] = 0xFF000000; // Supposed to be the red color,
         //does this work?
         value_list[0] = screen -> black_pixel;
         value_list[1] = screen -> white_pixel;
         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);
}


More information about the Xcb mailing list