Start hashing out a gauge cluster in Cairo

This commit is contained in:
XANTRONIX Development 2019-05-25 18:19:56 -05:00
parent 043c226341
commit b05df93160
2 changed files with 119 additions and 1 deletions

View file

@ -8,7 +8,7 @@ CFLAGS += -I$(INCLUDE_PATH)
LDFLAGS = -L../src -lhexagram
STATIC = ../src/libhexagram.a
EXAMPLES = view
EXAMPLES = view cluster
RM = rm
@ -17,6 +17,9 @@ all: $(EXAMPLES)
$(EXAMPLES): %: %.c $(STATIC)
$(CC) $(CFLAGS) $< -o $@ $(STATIC)
cluster: cluster.c $(STATIC)
$(CC) $(CFLAGS) $< -o $@ $(STATIC) $(shell pkg-config --cflags --libs cairo x11)
view: view.c $(STATIC)
$(CC) $(CFLAGS) $< -o $@ $(STATIC) -lncurses

115
examples/cluster.c Normal file
View file

@ -0,0 +1,115 @@
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <stdio.h>
#include <stdlib.h>
static void redraw(cairo_surface_t *sfc, cairo_t *ctx) {
/*
* Paint canvas black
*/
cairo_set_source_rgb(ctx, 0, 0, 0);
cairo_paint(ctx);
/*
* Draw two identical circles
*/
cairo_set_source_rgb(ctx, 1, 1, 1);
cairo_arc(ctx, 208, 240, 192, 0, 360);
cairo_stroke(ctx);
cairo_arc(ctx, 816, 240, 192, 0, 360);
cairo_stroke(ctx);
/*
* Draw two smaller circles
*/
cairo_arc(ctx, 442, 368, 64, 0, 360);
cairo_stroke(ctx);
cairo_arc(ctx, 578, 368, 64, 0, 360);
cairo_stroke(ctx);
/*
* Draw a rectangle
*/
cairo_move_to(ctx, 432, 48);
cairo_line_to(ctx, 592, 48);
cairo_line_to(ctx, 592, 288);
cairo_line_to(ctx, 432, 288);
cairo_line_to(ctx, 432, 48);
cairo_stroke(ctx);
}
int main(int argc, char **argv) {
Display *display;
Window win;
Pixmap buf;
GC gc;
int screen, x = 1024, y = 480;
XGCValues values = {
.foreground = 0x000000,
.background = 0xffffff,
.graphics_exposures = 0
};
cairo_surface_t *sfc;
cairo_t *ctx;
XEvent e;
if ((display = XOpenDisplay(NULL)) == NULL)
exit(1);
screen = DefaultScreen(display);
win = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, x, y, 0, 0, 0);
gc = XCreateGC(display, win, GCForeground | GCBackground | GCGraphicsExposures, &values);
buf = XCreatePixmap(display, win, x, y, 24);
XSelectInput(display, win, ExposureMask | ButtonPressMask | KeyPressMask);
XMapWindow(display, win);
sfc = cairo_xlib_surface_create(display, buf, DefaultVisual(display, screen), x, y);
cairo_xlib_surface_set_size(sfc, x, y);
ctx = cairo_create(sfc);
redraw(sfc, ctx);
while (1) {
XExposeEvent *expose;
XNextEvent(cairo_xlib_surface_get_display(sfc), &e);
switch (e.type) {
case Expose:
expose = (XExposeEvent *)&e;
XCopyArea(display, buf, win, gc, expose->x, expose->y,
expose->width, expose->height,
expose->x, expose->y);
break;
case ButtonPress:
case KeyPress:
goto done;
default:
fprintf(stderr, "Dropping unhandled XEvent.type = %d.\n", e.type);
}
}
done:
cairo_destroy(ctx);
cairo_surface_destroy(sfc);
XCloseDisplay(display);
return 0;
}