gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / xdemos / overlay.c
blob758f85c4492d90284313b602086999aeed51f9f1
1 /*
2 * GLX overlay test/demo.
4 * Brian Paul
5 * 18 July 2005
6 */
8 #include <GL/gl.h>
9 #include <GL/glx.h>
10 #include <X11/keysym.h>
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.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;
24 static void
25 RedrawNormal(Display *dpy)
27 glXMakeCurrent(dpy, NormalWindow, NormalContext);
28 glViewport(0, 0, WinWidth, WinHeight);
29 glMatrixMode(GL_PROJECTION);
30 glLoadIdentity();
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);
36 glPushMatrix();
37 glRotatef(Angle, 0, 0, 1);
38 glRectf(-0.8, -0.8, 0.8, 0.8);
39 glPopMatrix();
40 glXSwapBuffers(dpy, NormalWindow);
44 static void
45 RedrawOverlay(Display *dpy)
47 glXMakeCurrent(dpy, OverlayWindow, OverlayContext);
48 glViewport(0, 0, WinWidth, WinHeight);
49 glMatrixMode(GL_PROJECTION);
50 glLoadIdentity();
51 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
52 glMatrixMode(GL_MODELVIEW);
53 glClear(GL_COLOR_BUFFER_BIT);
54 if (RGBOverlay) {
55 glColor3f(0.0, 1.0, 1.0);
57 else {
58 glIndexi(2);
60 glBegin(GL_LINES);
61 glVertex2f(-1, -1);
62 glVertex2f(1, 1);
63 glVertex2f(1, -1);
64 glVertex2f(-1, 1);
65 glEnd();
66 glXSwapBuffers(dpy, OverlayWindow);
70 static Window
71 MakeWindow(Display *dpy, XVisualInfo *visinfo, Window parent,
72 unsigned int width, unsigned int height)
74 int scrnum;
75 XSetWindowAttributes attr;
76 unsigned long mask;
77 Window root;
78 Window win;
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);
93 return win;
97 static void
98 MakeNormalWindow(Display *dpy)
100 int attrib[] = { GLX_RGBA,
101 GLX_RED_SIZE, 1,
102 GLX_GREEN_SIZE, 1,
103 GLX_BLUE_SIZE, 1,
104 GLX_DOUBLEBUFFER,
105 None };
106 int scrnum;
107 Window root;
108 XVisualInfo *visinfo;
110 scrnum = DefaultScreen(dpy);
111 root = RootWindow(dpy, scrnum);
113 visinfo = glXChooseVisual(dpy, scrnum, attrib);
114 if (!visinfo) {
115 printf("Error: couldn't get an RGB, Double-buffered visual\n");
116 exit(1);
119 NormalWindow = MakeWindow(dpy, visinfo, root, WinWidth, WinHeight);
120 assert(NormalWindow);
122 NormalContext = glXCreateContext(dpy, visinfo, NULL, True);
123 assert(NormalContext);
127 static void
128 MakeOverlayWindow(Display *dpy)
130 int rgbAttribs[] = {
131 GLX_RGBA,
132 GLX_RED_SIZE, 1,
133 GLX_GREEN_SIZE, 1,
134 GLX_BLUE_SIZE, 1,
135 GLX_DOUBLEBUFFER,
136 GLX_LEVEL, 1,
137 None
139 int indexAttribs[] = {
140 /*GLX_RGBA, leave this out */
141 GLX_RED_SIZE, 1,
142 GLX_GREEN_SIZE, 1,
143 GLX_BLUE_SIZE, 1,
144 GLX_DOUBLEBUFFER,
145 GLX_LEVEL, 1,
146 None
148 int scrnum;
149 Window root;
150 XVisualInfo *visinfo;
152 scrnum = DefaultScreen(dpy);
153 root = RootWindow(dpy, scrnum);
155 visinfo = glXChooseVisual(dpy, scrnum, rgbAttribs);
156 if (visinfo) {
157 printf("Found RGB overlay visual 0x%x\n", (int) visinfo->visualid);
158 RGBOverlay = GL_TRUE;
160 else {
161 visinfo = glXChooseVisual(dpy, scrnum, indexAttribs);
162 if (visinfo) {
163 printf("Found Color Index overlay visual 0x%x\n",
164 (int) visinfo->visualid);
165 /* XXX setup the colormap entries! */
167 else {
168 printf("Couldn't get an overlay visual.\n");
169 printf("Your hardware probably doesn't support framebuffer overlay planes.\n");
170 exit(1);
174 OverlayWindow = MakeWindow(dpy, visinfo, NormalWindow, WinWidth, WinHeight);
175 assert(OverlayWindow);
177 OverlayContext = glXCreateContext(dpy, visinfo, NULL, True);
178 assert(OverlayContext);
182 static void
183 EventLoop(Display *dpy)
185 XEvent event;
187 while (1) {
188 XNextEvent(dpy, &event);
190 switch (event.type) {
191 case Expose:
192 RedrawNormal(dpy);
193 RedrawOverlay(dpy);
194 break;
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);
200 break;
201 case KeyPress:
203 char buffer[10];
204 int r, code;
205 code = XLookupKeysym(&event.xkey, 0);
206 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
207 NULL, NULL);
208 if (buffer[0] == 27) {
209 /* escape */
210 return;
212 else if (buffer[0] == ' ') {
213 Angle += 5.0;
214 RedrawNormal(dpy);
217 break;
218 default:
219 ; /* nothing */
226 main(int argc, char *argv[])
228 Display *dpy = XOpenDisplay(NULL);
230 assert(dpy);
232 MakeNormalWindow(dpy);
233 MakeOverlayWindow(dpy);
235 XMapWindow(dpy, NormalWindow);
236 XMapWindow(dpy, OverlayWindow);
238 EventLoop(dpy);
240 glXDestroyContext(dpy, OverlayContext);
241 glXDestroyContext(dpy, NormalContext);
242 XDestroyWindow(dpy, OverlayWindow);
243 XDestroyWindow(dpy, NormalWindow);
245 return 0;