Merge branch 'master' into gallium-0.2
[mesa.git] / progs / miniglx / miniglxsample.c
1
2 #define USE_MINIGLX 1 /* 1 = use Mini GLX, 0 = use Xlib/GLX */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <GL/gl.h>
8
9 #if USE_MINIGLX
10 #include <GL/miniglx.h>
11 #else
12 #include <GL/glx.h>
13 #include <X11/Xlib.h>
14 #endif
15
16 static void _subset_Rectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 )
17 {
18 glBegin( GL_QUADS );
19 glVertex2f( x1, y1 );
20 glVertex2f( x2, y1 );
21 glVertex2f( x2, y2 );
22 glVertex2f( x1, y2 );
23 glEnd();
24 }
25
26
27 /*
28 * Create a simple double-buffered RGBA window.
29 */
30 static Window
31 MakeWindow(Display * dpy, unsigned int width, unsigned int height)
32 {
33 int visAttributes[] = {
34 GLX_RGBA,
35 GLX_RED_SIZE, 1,
36 GLX_GREEN_SIZE, 1,
37 GLX_BLUE_SIZE, 1,
38 GLX_DOUBLEBUFFER,
39 None
40 };
41 XSetWindowAttributes attr;
42 unsigned long attrMask;
43 Window root;
44 Window win;
45 GLXContext ctx;
46 XVisualInfo *visinfo;
47
48 root = RootWindow(dpy, 0);
49
50 /* Choose GLX visual / pixel format */
51 visinfo = glXChooseVisual(dpy, 0, visAttributes);
52 if (!visinfo) {
53 printf("Error: couldn't get an RGB, Double-buffered visual\n");
54 exit(1);
55 }
56
57 /* Create the window */
58 attr.background_pixel = 0;
59 attr.border_pixel = 0;
60 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
61 attrMask = CWBackPixel | CWBorderPixel | CWColormap;
62 win = XCreateWindow(dpy, root, 0, 0, width, height,
63 0, visinfo->depth, InputOutput,
64 visinfo->visual, attrMask, &attr);
65 if (!win) {
66 printf("Error: XCreateWindow failed\n");
67 exit(1);
68 }
69
70 /* Display the window */
71 XMapWindow(dpy, win);
72
73 /* Create GLX rendering context */
74 ctx = glXCreateContext(dpy, visinfo, NULL, True);
75 if (!ctx) {
76 printf("Error: glXCreateContext failed\n");
77 exit(1);
78 }
79
80 /* Bind the rendering context and window */
81 glXMakeCurrent(dpy, win, ctx);
82
83 glViewport(0, 0, width, height);
84
85 return win;
86 }
87
88
89 /*
90 * Draw a few frames of a rotating square.
91 */
92 static void
93 DrawFrames(Display * dpy, Window win)
94 {
95 int angle;
96 glShadeModel(GL_FLAT);
97 glClearColor(0.5, 0.5, 0.5, 1.0);
98 for (angle = 0; angle < 360; angle += 10) {
99 glClear(GL_COLOR_BUFFER_BIT);
100 glColor3f(1.0, 1.0, 0.0);
101 glPushMatrix();
102 glRotatef(angle, 0, 0, 1);
103 _subset_Rectf(-0.8, -0.8, 0.8, 0.8);
104 glPopMatrix();
105 glXSwapBuffers(dpy, win);
106 sleep(1);
107 }
108 }
109
110
111 int
112 main(int argc, char *argv[])
113 {
114 Display *dpy;
115 Window win;
116
117 dpy = XOpenDisplay(NULL);
118 if (!dpy) {
119 printf("Error: XOpenDisplay failed\n");
120 return 1;
121 }
122
123 win = MakeWindow(dpy, 300, 300);
124
125 DrawFrames(dpy, win);
126
127 return 0;
128 }