From b0bab3a9d9ac1da45d8cee4c586ef50788d93df1 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Thu, 27 Jun 2019 01:08:16 -0400 Subject: [PATCH] Various updates --- blit.c | 116 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 27 deletions(-) diff --git a/blit.c b/blit.c index 08130ec..3986889 100644 --- a/blit.c +++ b/blit.c @@ -1,29 +1,60 @@ #include +#include +#include #include #include #include +#include + +Display* +getDisplay() { + /* Get a display to use */ + /* Currently just uses the default display */ + Display *display = XOpenDisplay(NULL); + + if (display == NULL) { + fprintf(stderr, "Could not open the display! :(\n"); + exit(1); + } + + return display; +} + +XColor +getColor(unsigned short red, + unsigned short green, + unsigned short blue) { + /* Return a new XColor structure */ + /* Initialize it with RGB */ + XColor xcolor; + + xcolor.red = red; + xcolor.green = green; + xcolor.blue = blue; + + xcolor.flags = DoRed | DoGreen | DoBlue; + + return xcolor; +} int main(void) { - Display *display; Window window; XEvent event; const char *msg = "Hello, world!\n"; - int screen; + XColor xcolor = getColor(0xffff, 0xffff, 0xffff); - // Opens the current display - display = XOpenDisplay(NULL); + Display *display = getDisplay(); - if (display == NULL) { - fprintf(stderr, "Could not open the display! :(\n"); - exit(1); - } + int screen = DefaultScreen(display); - printf("%p\n", display); + GC gc = XDefaultGC(display, screen); - screen = DefaultScreen(display); + XAllocColor(display, + XDefaultColormap(display, screen), + &xcolor); window = XCreateSimpleWindow(display, RootWindow(display, screen), @@ -32,8 +63,8 @@ main(void) { 100, 100, 1, - BlackPixel(display, screen), - WhitePixel(display, screen)); + WhitePixel(display, screen), + xcolor.pixel); printf("%p\n", window); @@ -43,6 +74,23 @@ main(void) { XMapWindow(display, window); + int depth = DefaultDepth(display, DefaultScreen(display)); + + Pixmap pixmap; + pixmap = XCreatePixmap(display, + window, + 100, + 100, + depth); + + int factor = 0; + + struct timespec req; + struct timespec rem; + + req.tv_sec = 0; + req.tv_nsec = 20000000; + while (1) { /* Event loop that handles events from the X server's event queue */ /* Will actually block if there are no events */ @@ -50,27 +98,41 @@ main(void) { XNextEvent(display, &event); if (event.type == Expose) { - XFillRectangle(display, - window, - DefaultGC(display, screen), - 20, - 20, - 10, - 10); - - XDrawString(display, - window, - DefaultGC(display, screen), - 10, - 50, - msg, - strlen(msg)); + XColor boxcolor = getColor(32000, 0, 32000); + + XAllocColor(display, + XDefaultColormap(display, screen), + &boxcolor); + + XSetForeground(display, gc, boxcolor.pixel); + } if (event.type == KeyPress) { break; } + for(int x = 0; x < 100; x++) { + for(int y = 0; y < 100; y++) { + XDrawPoint(display, pixmap, gc, x, y); + } + } + + factor++; + + XCopyArea(display, + pixmap, + window, + gc, + 0, + 0, + 100, + 100, + factor, + factor); + + nanosleep(&req, &rem); + } XCloseDisplay(display);