#include <GL/gl.h> #include <GL/glx.h> #include <stdlib.h> void Render(Display &display, Window &window) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(400,400); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(400,200); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(200,200); glEnd(); glFlush(); glXSwapBuffers(&display, window); } int main(int argc, char* argv[]) { Display* display = XOpenDisplay(getenv("DISPLAY")); int width = 640; int height = 480; int screen = DefaultScreen(display); int screen_width = DisplayWidth(display, screen); int screen_height = DisplayHeight(display, screen); Window window = XCreateWindow(display, XRootWindow(display, screen), (screen_width - width) / 2, (screen_height - height) / 2, width, height, 0, CopyFromParent, InputOutput, DefaultVisual(display, screen), 0, nullptr); XMapWindow(display, window); int configNum; GLXFBConfig* config = glXGetFBConfigs(display, screen, &configNum); XVisualInfo* visualInfo = glXGetVisualFromFBConfig(display, *config); GLXContext context = glXCreateContext(display, visualInfo, nullptr, false); glXMakeCurrent(display, window, context); int event_mask = (1 << 25) - 1; XSelectInput(display, window, event_mask); //listen to all types of events Atom wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True); XSetWMProtocols(display, window, &wmDelete, 1); bool needQuit = false; while(needQuit == false) { XEvent e; XNextEvent(display, &e); switch (e.type) { case Expose: Render(*display, window); break; case ClientMessage: needQuit = true; break; default: break; } } glViewport(0, 0, width, height); glClearColor(0, 0, 0, 1); glMatrixMode(GL_PROJECTION); glOrtho(0, width, 0, height, 0, 1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glXDestroyWindow(display, window); XDestroyWindow(display, window); XCloseDisplay(display); }