From bc2b18ca7c8d12cfe79784f8715cbacf69502930 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Mon, 1 Jul 2019 15:33:48 -0400 Subject: [PATCH] Rename files, update .vimrc and build.sh --- .vimrc | 2 +- blit3.c => blit_opengl.c | 142 ++++++++++++++++++++++++++++----------- blit.c => blit_xcb.c | 0 build.sh | 2 +- 4 files changed, 103 insertions(+), 43 deletions(-) rename blit3.c => blit_opengl.c (73%) rename blit.c => blit_xcb.c (100%) diff --git a/.vimrc b/.vimrc index b182026..303061f 100644 --- a/.vimrc +++ b/.vimrc @@ -1,2 +1,2 @@ -set makeprg=./build.sh +set makeprg=./build.sh\ % set autowrite diff --git a/blit3.c b/blit_opengl.c similarity index 73% rename from blit3.c rename to blit_opengl.c index cc1fb45..278eefb 100644 --- a/blit3.c +++ b/blit_opengl.c @@ -1,16 +1,21 @@ +#include +#include +#include /* for XGetXCBConnection, link with libX11-xcb */ +#include +#include #include #include -#include -#include /* for XGetXCBConnection, link with libX11-xcb */ +#include +#include #include -#include -#include xcb_window_t getWindow(xcb_connection_t*, xcb_colormap_t, int, - xcb_screen_t*); + xcb_screen_t*, + uint16_t, + uint16_t); xcb_colormap_t getColorMap(xcb_connection_t*, @@ -47,58 +52,95 @@ static int visual_attribs[] = { */ #define RECEIVE_EVENT(ev) (ev->response_type & ~0x80) -void draw() { - glClearColor(0.2, 0.4, 0.9, 1.0); - glClear(GL_COLOR_BUFFER_BIT); +void draw(GLint offset) { + /* This is needed because now the contents of `drawable` are undefined */ + + printf("Drawing!\n"); + + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0.0, 500, 500, 0.0, 0.0, 1.0); + + glBegin(GL_POINTS); + for (int i = 0; i < 100; i++) { + glVertex2i(offset, i); + } + glEnd(); + +} + +static struct timespec +genSleep(time_t sec, + long nanosec) { + struct timespec t; + t.tv_sec = sec; + t.tv_nsec = nanosec; + return t; } int -main_loop(Display *display, - xcb_connection_t *xcb_display, - xcb_window_t window, - GLXDrawable drawable) { +message_loop(Display *display, + xcb_connection_t *xcb_display, + xcb_window_t window, + GLXDrawable drawable) { int running = 1; + + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + GLint offset = 0; + + /* Used to handle the event loop */ + struct timespec req = genSleep(0, 20000000); + struct timespec rem = genSleep(0, 0); + while (running) { - /* Wait for event */ - xcb_generic_event_t *event = xcb_wait_for_event(xcb_display); - if (!event) { - fprintf(stderr, "i/o error in xcb_wait_for_event"); - return -1; - } + /* Poll for events */ + + xcb_generic_event_t *event = xcb_poll_for_event(xcb_display); - switch (RECEIVE_EVENT(event)) { + if (event != NULL) { + switch (RECEIVE_EVENT(event)) { case XCB_KEY_PRESS: /* Quit on key press */ - running = 0; + // running = 0; break; case XCB_EXPOSE: - /* Handle expose event, draw and swap buffers */ - draw(); - - /* This is where the magic happens */ - /* This call will NOT block.*/ - /* It will be sync'd with vertical refresh */ - glXSwapBuffers(display, drawable); + printf("Got expose event\n"); break; default: break; - } + } - free(event); + free(event); + } + draw(offset); + offset++; + + /* This is where the magic happens */ + /* This call will NOT block.*/ + /* It will be sync'd with vertical refresh */ + glXSwapBuffers(display, drawable); + nanosleep(&req, &rem); } return 0; } int -setup_and_run(Display* display, +setup_message_loop(Display* display, xcb_connection_t *xcb_display, int default_screen, xcb_screen_t *screen) { int visualID = 0; + uint16_t width = 500; + uint16_t height = 500; /* Query framebuffer configurations that match visual_attribs */ GLXFBConfig *fb_configs = 0; @@ -115,7 +157,7 @@ setup_and_run(Display* display, return -1; } - printf("Found %d matching FB configs", num_fb_configs); + printf("Found %d matching FB configs\n", num_fb_configs); /* Select first framebuffer config and query visualID */ GLXFBConfig fb_config = fb_configs[0]; @@ -127,7 +169,13 @@ setup_and_run(Display* display, /* This is how we can use the Xlib GLX functions */ /* Since these XIDs are transferrable between xcb and xlib */ xcb_colormap_t colormap = getColorMap(xcb_display, screen, visualID); - xcb_window_t window = getWindow(xcb_display, colormap, visualID, screen); + + xcb_window_t window = getWindow(xcb_display, + colormap, + visualID, + screen, + width, + height); /* NOTE: window must be mapped before glXMakeContextCurrent */ xcb_map_window(xcb_display, window); @@ -159,6 +207,7 @@ setup_and_run(Display* display, /* make OpenGL context current */ /* This will allow us to write to it with the GLX functions */ + /* https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glXMakeContextCurrent.xml */ if (!glXMakeContextCurrent(display, drawable, drawable, @@ -171,8 +220,11 @@ setup_and_run(Display* display, return -1; } - /* run main loop */ - int retval = main_loop(display, xcb_display, window, drawable); + /* run message loop */ + int retval = message_loop(display, + xcb_display, + window, + drawable); /* Cleanup */ glXDestroyWindow(display, glxwindow); @@ -191,6 +243,12 @@ getGLXContext(Display *display, GLXContext context; /* Create OpenGL context */ + /* Display* dpy + * GLXFBConfig config + * int render_type + * GLXContext share_list + * Bool direct (indicates we want direct rendering) + */ context = glXCreateNewContext(display, fb_config, GLX_RGBA_TYPE, @@ -228,7 +286,9 @@ xcb_window_t getWindow(xcb_connection_t *xcb_display, xcb_colormap_t colormap, int visualID, - xcb_screen_t *screen) { + xcb_screen_t *screen, + uint16_t width, + uint16_t height) { xcb_window_t window = xcb_generate_id(xcb_display); /* Create window */ @@ -241,9 +301,9 @@ getWindow(xcb_connection_t *xcb_display, XCB_COPY_FROM_PARENT, window, screen->root, - 0, 0, - 150, 150, - 0, + 0, 0, /* x y */ + width, height, /* width height */ + 0, /* border width */ XCB_WINDOW_CLASS_INPUT_OUTPUT, visualID, valuemask, @@ -307,8 +367,8 @@ getScreen(xcb_connection_t *xcb_display, return screen_iter.data; } -int main(int argc, char* argv[]) -{ +int +main(void) { Display *display = getDisplay(); /* Get the XCB connection from the display */ @@ -323,7 +383,7 @@ int main(int argc, char* argv[]) xcb_screen_t *screen = getScreen(xcb_display, default_screen); /* Initialize window and OpenGL context, run main loop and deinitialize */ - int retval = setup_and_run(display, xcb_display, default_screen, screen); + int retval = setup_message_loop(display, xcb_display, default_screen, screen); /* Cleanup */ XCloseDisplay(display); diff --git a/blit.c b/blit_xcb.c similarity index 100% rename from blit.c rename to blit_xcb.c diff --git a/build.sh b/build.sh index 1da2a15..15d3fd5 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #! /usr/bin/env bash -gcc $(pkg-config --cflags --libs x11 x11-xcb xcb gl xcb-glx) blit3.c +gcc $(pkg-config --cflags --libs x11 x11-xcb xcb gl xcb-glx) $1