2 * GLX overlay test/demo.
10 #include <X11/keysym.h>
15 static int WinWidth
= 300, WinHeight
= 300;
16 static Window NormalWindow
= 0;
17 static Window OverlayWindow
= 0;
18 static GLXContext NormalContext
= 0;
19 static GLXContext OverlayContext
= 0;
20 static GLboolean RGBOverlay
= GL_FALSE
;
21 static GLfloat Angle
= 0.0;
25 RedrawNormal(Display
*dpy
)
27 glXMakeCurrent(dpy
, NormalWindow
, NormalContext
);
28 glViewport(0, 0, WinWidth
, WinHeight
);
29 glMatrixMode(GL_PROJECTION
);
31 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
32 glMatrixMode(GL_MODELVIEW
);
33 glClearColor(0.5, 0.5, 0.5, 1.0);
34 glClear(GL_COLOR_BUFFER_BIT
);
35 glColor3f(1.0, 1.0, 0.0);
37 glRotatef(Angle
, 0, 0, 1);
38 glRectf(-0.8, -0.8, 0.8, 0.8);
40 glXSwapBuffers(dpy
, NormalWindow
);
45 RedrawOverlay(Display
*dpy
)
47 glXMakeCurrent(dpy
, OverlayWindow
, OverlayContext
);
48 glViewport(0, 0, WinWidth
, WinHeight
);
49 glMatrixMode(GL_PROJECTION
);
51 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
52 glMatrixMode(GL_MODELVIEW
);
53 glClear(GL_COLOR_BUFFER_BIT
);
55 glColor3f(0.0, 1.0, 1.0);
66 glXSwapBuffers(dpy
, OverlayWindow
);
71 MakeWindow(Display
*dpy
, XVisualInfo
*visinfo
, Window parent
,
72 unsigned int width
, unsigned int height
)
75 XSetWindowAttributes attr
;
80 scrnum
= DefaultScreen(dpy
);
81 root
= RootWindow(dpy
, scrnum
);
83 /* window attributes */
84 attr
.background_pixel
= 0;
85 attr
.border_pixel
= 0;
86 attr
.colormap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
87 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
88 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
90 win
= XCreateWindow(dpy
, parent
, 0, 0, width
, height
,
91 0, visinfo
->depth
, InputOutput
,
92 visinfo
->visual
, mask
, &attr
);
98 MakeNormalWindow(Display
*dpy
)
100 int attrib
[] = { GLX_RGBA
,
108 XVisualInfo
*visinfo
;
110 scrnum
= DefaultScreen(dpy
);
111 root
= RootWindow(dpy
, scrnum
);
113 visinfo
= glXChooseVisual(dpy
, scrnum
, attrib
);
115 printf("Error: couldn't get an RGB, Double-buffered visual\n");
119 NormalWindow
= MakeWindow(dpy
, visinfo
, root
, WinWidth
, WinHeight
);
120 assert(NormalWindow
);
122 NormalContext
= glXCreateContext(dpy
, visinfo
, NULL
, True
);
123 assert(NormalContext
);
128 MakeOverlayWindow(Display
*dpy
)
139 int indexAttribs
[] = {
140 /*GLX_RGBA, leave this out */
150 XVisualInfo
*visinfo
;
152 scrnum
= DefaultScreen(dpy
);
153 root
= RootWindow(dpy
, scrnum
);
155 visinfo
= glXChooseVisual(dpy
, scrnum
, rgbAttribs
);
157 printf("Found RGB overlay visual 0x%x\n", (int) visinfo
->visualid
);
158 RGBOverlay
= GL_TRUE
;
161 visinfo
= glXChooseVisual(dpy
, scrnum
, indexAttribs
);
163 printf("Found Color Index overlay visual 0x%x\n",
164 (int) visinfo
->visualid
);
165 /* XXX setup the colormap entries! */
168 printf("Couldn't get an overlay visual.\n");
169 printf("Your hardware probably doesn't support framebuffer overlay planes.\n");
174 OverlayWindow
= MakeWindow(dpy
, visinfo
, NormalWindow
, WinWidth
, WinHeight
);
175 assert(OverlayWindow
);
177 OverlayContext
= glXCreateContext(dpy
, visinfo
, NULL
, True
);
178 assert(OverlayContext
);
183 EventLoop(Display
*dpy
)
188 XNextEvent(dpy
, &event
);
190 switch (event
.type
) {
195 case ConfigureNotify
:
196 WinWidth
= event
.xconfigure
.width
;
197 WinHeight
= event
.xconfigure
.height
;
198 if (event
.xconfigure
.window
== NormalWindow
)
199 XResizeWindow(dpy
, OverlayWindow
, WinWidth
, WinHeight
);
205 code
= XLookupKeysym(&event
.xkey
, 0);
206 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
208 if (buffer
[0] == 27) {
212 else if (buffer
[0] == ' ') {
226 main(int argc
, char *argv
[])
228 Display
*dpy
= XOpenDisplay(NULL
);
232 MakeNormalWindow(dpy
);
233 MakeOverlayWindow(dpy
);
235 XMapWindow(dpy
, NormalWindow
);
236 XMapWindow(dpy
, OverlayWindow
);
240 glXDestroyContext(dpy
, OverlayContext
);
241 glXDestroyContext(dpy
, NormalContext
);
242 XDestroyWindow(dpy
, OverlayWindow
);
243 XDestroyWindow(dpy
, NormalWindow
);