]> git.cworth.org Git - apitrace-tests/blob - cli/src/glxsimple.c
Add the source of a very simple program using OpenGL through GLX.
[apitrace-tests] / cli / src / glxsimple.c
1 /**************************************************************************
2  * Copyright 2012 Intel corporation
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  *
23  **************************************************************************/
24
25 #include <stdio.h>
26
27 #include <X11/Xlib.h>
28 #include <GL/glew.h>
29 #include <GL/gl.h>
30 #include <GL/glx.h>
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 int width = 64;
38 int height = 64;
39
40 static void
41 draw (Display *dpy, Window window, int width, int height)
42 {
43         GLenum glew_err;
44
45         int visual_attr[] = {
46                 GLX_RGBA,
47                 GLX_RED_SIZE,           8,
48                 GLX_GREEN_SIZE,         8,
49                 GLX_BLUE_SIZE,          8,
50                 GLX_ALPHA_SIZE,         8,
51                 GLX_DOUBLEBUFFER,
52                 GLX_DEPTH_SIZE,         24,
53                 GLX_STENCIL_SIZE,       8,
54                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
55                 None
56         };
57         XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
58         GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
59         glXMakeCurrent(dpy, window, ctx);
60
61         glew_err = glewInit();
62         if (glew_err != GLEW_OK)
63         {
64                 fprintf (stderr, "glewInit failed: %s\n",
65                          glewGetErrorString(glew_err));
66                 exit (1);
67         }
68
69         glViewport(0, 0, width, height);
70         glClearColor(1, 0, 1, 1);
71         glClear(GL_COLOR_BUFFER_BIT);
72
73         glXSwapBuffers (dpy, window);
74
75         glXDestroyContext (dpy, ctx);
76 }
77
78 static void
79 handle_events(Display *dpy, Window window, int width, int height)
80 {
81         XEvent xev;
82         KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
83
84         XNextEvent (dpy, &xev);
85
86         while (1) {
87                 XNextEvent (dpy, &xev);
88                 switch (xev.type) {
89                 case KeyPress:
90                         if (xev.xkey.keycode == quit_code) {
91                                 return;
92                         }
93                         break;
94                 case ConfigureNotify:
95                         width = xev.xconfigure.width;
96                         height = xev.xconfigure.height;
97                         break;
98                 case Expose:
99                         if (xev.xexpose.count == 0) {
100                                 draw (dpy, window, width, height);
101                                 return;
102                         }
103                         break;
104                 }
105         }
106 }
107
108 int
109 main (void)
110 {
111         Display *dpy;
112         Window window;
113
114         dpy = XOpenDisplay (NULL);
115
116         if (dpy == NULL) {
117                 fprintf(stderr, "Failed to open display %s\n",
118                         XDisplayName(NULL));
119                 return 1;
120         }
121
122         window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
123                                      0, 0, width, height, 0,
124                                      BlackPixel (dpy, DefaultScreen (dpy)),
125                                      BlackPixel (dpy, DefaultScreen (dpy)));
126
127         XSelectInput(dpy, window,
128                      KeyPressMask | StructureNotifyMask | ExposureMask);
129
130         XMapWindow (dpy, window);
131
132         handle_events (dpy, window, width, height);
133
134         XDestroyWindow (dpy, window);
135         XCloseDisplay (dpy);
136
137         return 0;
138 }