Advertisement
Guest User

Amit Pundir

a guest
Feb 17th, 2010
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "X11/Xlib.h"
  4. #include "X11/Xutil.h"
  5. #include <X11/extensions/Xrender.h>
  6.  
  7. #include <EGL/egl.h>
  8. #include <GLES2/gl2.h>
  9.  
  10. // Width and height of the window
  11. #define WINDOW_WIDTH    640
  12. #define WINDOW_HEIGHT   480
  13.  
  14. bool TestEGLError(const char* pszLocation)
  15. {
  16.         EGLint iErr = eglGetError();
  17.         if (iErr != EGL_SUCCESS)
  18.         {
  19.                 printf("%s failed (%d).\n", pszLocation, iErr);
  20.                 return false;
  21.         }
  22.  
  23.         return true;
  24. }
  25.  
  26. void CleanupAndExit ( EGLDisplay eglDisplay
  27.                                         , Display * x11Display
  28.                                         , Window x11Window
  29.                                         , Colormap x11Colormap
  30.                                         )
  31. {
  32.         eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) ;
  33.         eglTerminate(eglDisplay);
  34.  
  35.         if (x11Window) XDestroyWindow(x11Display, x11Window);
  36.         if (x11Colormap) XFreeColormap( x11Display, x11Colormap );
  37.         if (x11Display) XCloseDisplay(x11Display);
  38.         exit(-1);
  39. }
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.         // Variable set in the message handler to finish the demo
  44.         bool                            bDemoDone       = false;
  45.  
  46.         // X11 variables
  47.         Window                          x11Window       = 0;
  48.         Display*                        x11Display      = 0;
  49.         long                            x11Screen       = 0;
  50.         XVisualInfo*            x11Visual       = 0;
  51.         Colormap                        x11Colormap     = 0;
  52.  
  53.         // EGL variables
  54.         EGLDisplay                      eglDisplay      = 0;
  55.         EGLConfig                       eglConfig       = 0;
  56.         EGLSurface                      eglSurface      = 0;
  57.         EGLContext                      eglContext      = 0;
  58.  
  59.         /*
  60.            Step 0 - Create a EGLNativeWindowType that we can use it for OpenGL ES output
  61.            */
  62.         Window                                  sRootWindow;
  63.         XSetWindowAttributes    sWA;
  64.         unsigned int                    ui32Mask;
  65.         int                                             i32Depth;
  66.  
  67.         // Initializes the display and screen
  68.         x11Display = XOpenDisplay( 0 );
  69.         if (!x11Display)
  70.         {
  71.                 printf("Error: Unable to open X display\n");
  72.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  73.         }
  74.         x11Screen = XDefaultScreen( x11Display );
  75.  
  76.         // Gets the window parameters
  77.         sRootWindow = RootWindow(x11Display, x11Screen);
  78.         i32Depth = DefaultDepth(x11Display, x11Screen);
  79.  
  80.         {
  81.                 Visual *visual = 0;
  82.                 bool  argbVisual=false;
  83.                 int eventBase, errorBase;
  84.  
  85.                 if (XRenderQueryExtension(x11Display, &eventBase, &errorBase)) {
  86.                         int nvi;
  87.                         XVisualInfo templ;
  88.                         templ.screen  = x11Screen;
  89.                         templ.depth   = 32;
  90.                         templ.c_class = TrueColor;
  91.                         x11Visual = XGetVisualInfo(x11Display, VisualScreenMask |
  92.                                         VisualDepthMask |
  93.                                         VisualClassMask | VisualIDMask, &templ, &nvi);
  94.  
  95.                         for (int i = 0; i < nvi; ++i) {
  96.                                 XRenderPictFormat *format = XRenderFindVisualFormat(x11Display,
  97.                                                 x11Visual[i].visual);
  98.                                 if (format->type == PictTypeDirect && format->direct.alphaMask) {
  99.                                         visual = x11Visual[i].visual;
  100.                                         x11Colormap = XCreateColormap(x11Display, sRootWindow,
  101.                                                         visual, AllocNone);
  102.                                         argbVisual=true;
  103.                                         break;
  104.                                 }
  105.                         }
  106.                 }
  107.  
  108.                 if (argbVisual == true) {
  109.                         printf("Found ARGB visual.\n");
  110.                 }
  111.                 else  {
  112.                         printf("Couldn't find ARGB visual.\n");
  113.                 }
  114.         }
  115.  
  116.         if (!x11Visual)
  117.         {
  118.                 printf("Error: Unable to acquire visual\n");
  119.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  120.         }
  121.  
  122.         sWA.colormap = x11Colormap;
  123.  
  124.         // Add to these for handling other events
  125.         sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
  126.         ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
  127.  
  128.         // Creates the X11 window
  129.         x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
  130.                         0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);
  131.         XMapWindow(x11Display, x11Window);
  132.         XFlush(x11Display);
  133.  
  134.         /*
  135.            Step 1 - Get the default display.
  136.            */
  137.         eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
  138.  
  139.         /*
  140.            Step 2 - Initialize EGL.
  141.            */
  142.         EGLint iMajorVersion, iMinorVersion;
  143.         if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
  144.         {
  145.                 printf("Error: eglInitialize() failed.\n");
  146.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  147.         }
  148.  
  149.         /*
  150.            Step 3 - Specify the required configuration attributes.
  151.            */
  152.         EGLint pi32ConfigAttribs[5];
  153.         pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
  154.         pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
  155.         pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
  156.         pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
  157.         pi32ConfigAttribs[4] = EGL_NONE;
  158.  
  159.         EGLint pi32ContextAttribs[3];
  160.         pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
  161.         pi32ContextAttribs[1] = 2;
  162.         pi32ContextAttribs[2] = EGL_NONE;
  163.  
  164.         /*
  165.            Step 4 - Find a config that matches all requirements.
  166.            */
  167.         int iConfigs;
  168.         if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
  169.         {
  170.                 printf("Error: eglChooseConfig() failed.\n");
  171.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  172.         }
  173.  
  174.         /*
  175.            Step 5 - Create a surface to draw to.
  176.            */
  177.         eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
  178.  
  179.         if (!TestEGLError("eglCreateWindowSurface"))
  180.         {
  181.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  182.         }
  183.  
  184.         /*
  185.            Step 6 - Create a context.
  186.            */
  187.         eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, pi32ContextAttribs);
  188.  
  189.         if (!TestEGLError("eglCreateContext"))
  190.         {
  191.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  192.         }
  193.  
  194.         /*
  195.            Step 7 - Bind the context to the current thread and use our
  196.            window surface for drawing and reading.
  197.            */
  198.         eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
  199.  
  200.         if (!TestEGLError("eglMakeCurrent"))
  201.         {
  202.                 CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  203.         }
  204.  
  205.         /*
  206.            Step 8 - Draw something with OpenGL ES.
  207.            */
  208.  
  209.         // Clears the screen in different colours
  210.         for(int i = 0; i < 1000; ++i)
  211.         {
  212.                 // Check if the message handler finished the demo
  213.                 if (bDemoDone) break;
  214.  
  215.                 // Sets the clear color to a varying color.
  216.                 // The colours are passed per channel (red,green,blue,alpha) as float values from 0.0 to 1.0
  217.                 if (i&128)
  218.                 {
  219.                         glClearColor(0.6f, 0.8f, 1.0f, 0.3f); // clear blue                    
  220.                 }
  221.                 else
  222.                 {
  223.                         glClearColor(1.0f, 1.0f, 0.66f, 0.3f); // clear yellow
  224.                 }
  225.  
  226.                 /*
  227.                    Clears the color buffer.
  228.                    glClear() can also be used to clear the depth or stencil buffer
  229.                    (GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT)
  230.                    */
  231.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  232.                 if (!TestEGLError("glClear"))
  233.                 {
  234.                         CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  235.                 }
  236.  
  237.                 /*
  238.                    Swap Buffers.
  239.                    Brings to the native display the current render surface.
  240.  
  241.                    */
  242.                 eglSwapBuffers(eglDisplay, eglSurface);
  243.  
  244.                 if (!TestEGLError("eglSwapBuffers"))
  245.                 {
  246.                         CleanupAndExit (eglDisplay, x11Display, x11Window, x11Colormap);
  247.                 }
  248.         }
  249.  
  250.         // Managing the X11 messages
  251.         int i32NumMessages = XPending( x11Display );
  252.         for( int i = 0; i < i32NumMessages; i++ )
  253.         {
  254.                 XEvent  event;
  255.                 XNextEvent( x11Display, &event );
  256.  
  257.                 switch( event.type )
  258.                 {
  259.                         // Exit on mouse click
  260.                         case ButtonPress:
  261.                                 bDemoDone = true;
  262.                                 break;
  263.                         default:
  264.                                 break;
  265.                 }
  266.         }
  267.  
  268.         /*
  269.            Step 9 - Terminate OpenGL ES and destroy the window (if present).
  270.            */
  271.         return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement