Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "X11/Xlib.h"
- #include "X11/Xutil.h"
- #include <X11/extensions/Xrender.h>
- #include <EGL/egl.h>
- #include <GLES2/gl2.h>
- // Width and height of the window
- #define WINDOW_WIDTH 640
- #define WINDOW_HEIGHT 480
- bool TestEGLError(const char* pszLocation)
- {
- EGLint iErr = eglGetError();
- if (iErr != EGL_SUCCESS)
- {
- printf("%s failed (%d).\n", pszLocation, iErr);
- return false;
- }
- return true;
- }
- void CleanupAndExit ( EGLDisplay eglDisplay
- , Display * x11Display
- , Window x11Window
- , Colormap x11Colormap
- )
- {
- eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) ;
- eglTerminate(eglDisplay);
- if (x11Window) XDestroyWindow(x11Display, x11Window);
- if (x11Colormap) XFreeColormap( x11Display, x11Colormap );
- if (x11Display) XCloseDisplay(x11Display);
- exit(-1);
- }
- int main(int argc, char **argv)
- {
- // Variable set in the message handler to finish the demo
- bool bDemoDone = false;
- // X11 variables
- Window x11Window = 0;
- Display* x11Display = 0;
- long x11Screen = 0;
- XVisualInfo* x11Visual = 0;
- Colormap x11Colormap = 0;
- // EGL variables
- EGLDisplay eglDisplay = 0;
- EGLConfig eglConfig = 0;
- EGLSurface eglSurface = 0;
- EGLContext eglContext = 0;
- /*
- Step 0 - Create a EGLNativeWindowType that we can use it for OpenGL ES output
- */
- Window sRootWindow;
- XSetWindowAttributes sWA;
- unsigned int ui32Mask;
- int i32Depth;
- // Initializes the display and screen
- x11Display = XOpenDisplay( 0 );
- if (!x11Display)
- {
- printf("Error: Unable to open X display\n");
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- x11Screen = XDefaultScreen( x11Display );
- // Gets the window parameters
- sRootWindow = RootWindow(x11Display, x11Screen);
- i32Depth = DefaultDepth(x11Display, x11Screen);
- {
- Visual *visual = 0;
- bool argbVisual=false;
- int eventBase, errorBase;
- if (XRenderQueryExtension(x11Display, &eventBase, &errorBase)) {
- int nvi;
- XVisualInfo templ;
- templ.screen = x11Screen;
- templ.depth = 32;
- templ.c_class = TrueColor;
- x11Visual = XGetVisualInfo(x11Display, VisualScreenMask |
- VisualDepthMask |
- VisualClassMask | VisualIDMask, &templ, &nvi);
- for (int i = 0; i < nvi; ++i) {
- XRenderPictFormat *format = XRenderFindVisualFormat(x11Display,
- x11Visual[i].visual);
- if (format->type == PictTypeDirect && format->direct.alphaMask) {
- visual = x11Visual[i].visual;
- x11Colormap = XCreateColormap(x11Display, sRootWindow,
- visual, AllocNone);
- argbVisual=true;
- break;
- }
- }
- }
- if (argbVisual == true) {
- printf("Found ARGB visual.\n");
- }
- else {
- printf("Couldn't find ARGB visual.\n");
- }
- }
- if (!x11Visual)
- {
- printf("Error: Unable to acquire visual\n");
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- sWA.colormap = x11Colormap;
- // Add to these for handling other events
- sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
- ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
- // Creates the X11 window
- x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
- 0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);
- XMapWindow(x11Display, x11Window);
- XFlush(x11Display);
- /*
- Step 1 - Get the default display.
- */
- eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
- /*
- Step 2 - Initialize EGL.
- */
- EGLint iMajorVersion, iMinorVersion;
- if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
- {
- printf("Error: eglInitialize() failed.\n");
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- /*
- Step 3 - Specify the required configuration attributes.
- */
- EGLint pi32ConfigAttribs[5];
- pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
- pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
- pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
- pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
- pi32ConfigAttribs[4] = EGL_NONE;
- EGLint pi32ContextAttribs[3];
- pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
- pi32ContextAttribs[1] = 2;
- pi32ContextAttribs[2] = EGL_NONE;
- /*
- Step 4 - Find a config that matches all requirements.
- */
- int iConfigs;
- if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
- {
- printf("Error: eglChooseConfig() failed.\n");
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- /*
- Step 5 - Create a surface to draw to.
- */
- eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
- if (!TestEGLError("eglCreateWindowSurface"))
- {
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- /*
- Step 6 - Create a context.
- */
- eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, pi32ContextAttribs);
- if (!TestEGLError("eglCreateContext"))
- {
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- /*
- Step 7 - Bind the context to the current thread and use our
- window surface for drawing and reading.
- */
- eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
- if (!TestEGLError("eglMakeCurrent"))
- {
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- /*
- Step 8 - Draw something with OpenGL ES.
- */
- // Clears the screen in different colours
- for(int i = 0; i < 1000; ++i)
- {
- // Check if the message handler finished the demo
- if (bDemoDone) break;
- // Sets the clear color to a varying color.
- // The colours are passed per channel (red,green,blue,alpha) as float values from 0.0 to 1.0
- if (i&128)
- {
- glClearColor(0.6f, 0.8f, 1.0f, 0.3f); // clear blue
- }
- else
- {
- glClearColor(1.0f, 1.0f, 0.66f, 0.3f); // clear yellow
- }
- /*
- Clears the color buffer.
- glClear() can also be used to clear the depth or stencil buffer
- (GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT)
- */
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if (!TestEGLError("glClear"))
- {
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- /*
- Swap Buffers.
- Brings to the native display the current render surface.
- */
- eglSwapBuffers(eglDisplay, eglSurface);
- if (!TestEGLError("eglSwapBuffers"))
- {
- CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
- }
- }
- // Managing the X11 messages
- int i32NumMessages = XPending( x11Display );
- for( int i = 0; i < i32NumMessages; i++ )
- {
- XEvent event;
- XNextEvent( x11Display, &event );
- switch( event.type )
- {
- // Exit on mouse click
- case ButtonPress:
- bDemoDone = true;
- break;
- default:
- break;
- }
- }
- /*
- Step 9 - Terminate OpenGL ES and destroy the window (if present).
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement