Skip to content

Instantly share code, notes, and snippets.

@halferty
Created October 7, 2016 02:11
Show Gist options
  • Save halferty/0c0204c948bb3ee882f797431c6e2708 to your computer and use it in GitHub Desktop.
Save halferty/0c0204c948bb3ee882f797431c6e2708 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
struct Hints {
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long inputMode;
unsigned long status;
};
Display *display;
Window rootWindow, window;
Colormap colormap;
XVisualInfo *visualInfo;
XSetWindowAttributes setWindowAttributes;
int screenWidth, screenHeight, scissorOffsetX, scissorOffsetY;
GLint attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
Atom property;
struct Hints hints;
GLXContext context;
XWindowAttributes windowAttributes;
int x11FileDescriptor;
fd_set inputFileDescriptorSet;
struct timeval timer;
int test = 0;
void SetupWindow()
{
display = XOpenDisplay(NULL);
rootWindow = DefaultRootWindow(display);
visualInfo = glXChooseVisual(display, 0, attributes);
colormap = XCreateColormap(display, rootWindow, visualInfo->visual, AllocNone);
setWindowAttributes.colormap = colormap;
setWindowAttributes.event_mask = ExposureMask | KeyPressMask;
screenWidth = XDisplayWidth(display, 0);
screenHeight = XDisplayHeight(display, 0);
window = XCreateWindow(display, rootWindow, 0, 0, screenWidth, screenHeight, 0, visualInfo->depth, InputOutput, visualInfo->visual, CWColormap | CWEventMask, &setWindowAttributes);
hints.flags = 2;
hints.decorations = 0;
property = XInternAtom(display, "_MOTIF_WM_HINTS", 1);
XChangeProperty(display, window, property, property, 32, PropModeReplace, (unsigned char *)&hints, 5);
XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
ButtonPressMask | ButtonReleaseMask | StructureNotifyMask | ClientMessage );
XMapWindow(display, window);
XStoreName(display, window, "Hello, world!");
XFlush(display);
x11FileDescriptor = ConnectionNumber(display);
context = glXCreateContext(display, visualInfo, NULL, GL_TRUE);
glXMakeCurrent(display, window, context);
glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
XGetWindowAttributes(display, window, &windowAttributes);
//scissorOffsetX = (windowAttributes.width - 1280) / 2;
//scissorOffsetY = (windowAttributes.height - 720) / 2;
//glScissor(scissorOffsetX, scissorOffsetY, 1280, 720);
}
void DrawScene()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_SCISSOR_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);
glClearColor(0.5, 0.0, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0); glVertex3f(-0.75, -0.75, 0.0);
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.75, -0.75, 0.0);
glColor3f(0.0, 0.0, 1.0); glVertex3f(0.75, 0.75, 0.0);
glColor3f(1.0, 1.0, test ? 1.0 : 0.0); glVertex3f(-.75, 0.75, 0.0);
glEnd();
glXSwapBuffers(display, window);
}
int main(int argc, char **argv)
{
SetupWindow();
XEvent event;
int quit = 0;
do
{
FD_ZERO(&inputFileDescriptorSet);
FD_SET(x11FileDescriptor, &inputFileDescriptorSet);
timer.tv_usec = 5000;
timer.tv_sec = 0;
int numReadyFDs = select(x11FileDescriptor + 1, &inputFileDescriptorSet, 0, 0, &timer);
if (numReadyFDs > 0)
{
printf("Event received!\n");
}
else if (numReadyFDs == 0)
{
DrawScene();
}
else
{
printf("Error!\n");
}
while (XPending(display))
{
XNextEvent(display, &event);
printf("event.type=%d\n",event.type);
if (event.type == KeyPress)
{
KeySym key = XLookupKeysym(&event.xkey, 0);
if (key == (KeySym)0x71) {
quit = 1;
}
else if (key == 0x61) {
test = 0;
}
else if (key == 0x62) {
test = 1;
}
}
}
} while (!quit);
printf("Quitting!\n");
XResizeWindow(display, window, 10, 10);
XFlush(display);
printf("Quitting2!\n");
sleep(1);
printf("Quitting3!\n");
glXDestroyContext(display, context);
XFlush(display);
printf("Quitting4!\n");
sleep(1);
XCloseDisplay(display);
printf("Quitting5!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment