WIP - port to Mali EGL
[mesa-demos/mali.git] / src / xdemos / xdemo.c
blob52039cb6424b10c2a094c610f2846cef903f8400
2 /*
3 * Very simple demo of how to use the Mesa/X11 interface instead of the
4 * glx, tk or aux toolkits. I highly recommend using the GLX interface
5 * instead of the X/Mesa interface, however.
7 * This program is in the public domain.
9 * Brian Paul
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <X11/Xlib.h>
16 #include <X11/Xutil.h>
17 #include "GL/xmesa.h"
18 #include "GL/gl.h"
22 static GLint Black, Red, Green, Blue;
26 static void make_window( char *title, int color_flag )
28 int x = 10, y = 10, width = 400, height = 300;
29 Display *dpy;
30 int scr;
31 Window root, win;
32 Colormap cmap;
33 XColor xcolor;
34 int attr_flags;
35 XVisualInfo *visinfo;
36 XSetWindowAttributes attr;
37 XTextProperty tp;
38 XSizeHints sh;
39 XEvent e;
40 XMesaContext context;
41 XMesaVisual visual;
42 XMesaBuffer buffer;
46 * Do the usual X things to make a window.
49 dpy = XOpenDisplay(NULL);
50 if (!dpy) {
51 printf("Couldn't open default display!\n");
52 exit(1);
55 scr = DefaultScreen(dpy);
56 root = RootWindow(dpy, scr);
58 /* alloc visinfo struct */
59 visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
61 /* Get a visual and colormap */
62 if (color_flag) {
63 /* Open TrueColor window */
66 if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
67 printf("Couldn't get 24-bit TrueColor visual!\n");
68 exit(1);
71 if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
72 printf("Couldn't get 8-bit PseudoColor visual!\n");
73 exit(1);
76 cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
77 Black = Red = Green = Blue = 0;
79 else {
80 /* Open color index window */
82 if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
83 printf("Couldn't get 8-bit PseudoColor visual\n");
84 exit(1);
87 cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
89 /* Allocate colors */
90 xcolor.red = 0x0;
91 xcolor.green = 0x0;
92 xcolor.blue = 0x0;
93 xcolor.flags = DoRed | DoGreen | DoBlue;
94 if (!XAllocColor( dpy, cmap, &xcolor )) {
95 printf("Couldn't allocate black!\n");
96 exit(1);
98 Black = xcolor.pixel;
100 xcolor.red = 0xffff;
101 xcolor.green = 0x0;
102 xcolor.blue = 0x0;
103 xcolor.flags = DoRed | DoGreen | DoBlue;
104 if (!XAllocColor( dpy, cmap, &xcolor )) {
105 printf("Couldn't allocate red!\n");
106 exit(1);
108 Red = xcolor.pixel;
110 xcolor.red = 0x0;
111 xcolor.green = 0xffff;
112 xcolor.blue = 0x0;
113 xcolor.flags = DoRed | DoGreen | DoBlue;
114 if (!XAllocColor( dpy, cmap, &xcolor )) {
115 printf("Couldn't allocate green!\n");
116 exit(1);
118 Green = xcolor.pixel;
120 xcolor.red = 0x0;
121 xcolor.green = 0x0;
122 xcolor.blue = 0xffff;
123 xcolor.flags = DoRed | DoGreen | DoBlue;
124 if (!XAllocColor( dpy, cmap, &xcolor )) {
125 printf("Couldn't allocate blue!\n");
126 exit(1);
128 Blue = xcolor.pixel;
131 /* set window attributes */
132 attr.colormap = cmap;
133 attr.event_mask = ExposureMask | StructureNotifyMask;
134 attr.border_pixel = BlackPixel( dpy, scr );
135 attr.background_pixel = BlackPixel( dpy, scr );
136 attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
138 /* Create the window */
139 win = XCreateWindow( dpy, root, x,y, width, height, 0,
140 visinfo->depth, InputOutput,
141 visinfo->visual,
142 attr_flags, &attr);
143 if (!win) {
144 printf("Couldn't open window!\n");
145 exit(1);
148 XStringListToTextProperty(&title, 1, &tp);
149 sh.flags = USPosition | USSize;
150 XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
151 XMapWindow(dpy, win);
152 while (1) {
153 XNextEvent( dpy, &e );
154 if (e.type == MapNotify && e.xmap.window == win) {
155 break;
161 * Now do the special Mesa/Xlib stuff!
164 visual = XMesaCreateVisual( dpy, visinfo,
165 (GLboolean) color_flag,
166 GL_FALSE, /* alpha_flag */
167 GL_FALSE, /* db_flag */
168 GL_FALSE, /* stereo flag */
169 GL_FALSE, /* ximage_flag */
170 0, /* depth size */
171 0, /* stencil size */
172 0,0,0,0, /* accum_size */
173 0, /* num samples */
174 0, /* level */
175 0 /* caveat */
177 if (!visual) {
178 printf("Couldn't create Mesa/X visual!\n");
179 exit(1);
182 /* Create a Mesa rendering context */
183 context = XMesaCreateContext( visual,
184 NULL /* share_list */
186 if (!context) {
187 printf("Couldn't create Mesa/X context!\n");
188 exit(1);
191 buffer = XMesaCreateWindowBuffer( visual, win );
192 if (!buffer) {
193 printf("Couldn't create Mesa/X buffer!\n");
194 exit(1);
198 XMesaMakeCurrent( context, buffer );
200 /* Ready to render! */
205 static void draw_cube( void )
207 /* X faces */
208 glIndexi( Red );
209 glColor3f( 1.0, 0.0, 0.0 );
210 glBegin( GL_POLYGON );
211 glVertex3f( 1.0, 1.0, 1.0 );
212 glVertex3f( 1.0, -1.0, 1.0 );
213 glVertex3f( 1.0, -1.0, -1.0 );
214 glVertex3f( 1.0, 1.0, -1.0 );
215 glEnd();
217 glBegin( GL_POLYGON );
218 glVertex3f( -1.0, 1.0, 1.0 );
219 glVertex3f( -1.0, 1.0, -1.0 );
220 glVertex3f( -1.0, -1.0, -1.0 );
221 glVertex3f( -1.0, -1.0, 1.0 );
222 glEnd();
224 /* Y faces */
225 glIndexi( Green );
226 glColor3f( 0.0, 1.0, 0.0 );
227 glBegin( GL_POLYGON );
228 glVertex3f( 1.0, 1.0, 1.0 );
229 glVertex3f( 1.0, 1.0, -1.0 );
230 glVertex3f( -1.0, 1.0, -1.0 );
231 glVertex3f( -1.0, 1.0, 1.0 );
232 glEnd();
234 glBegin( GL_POLYGON );
235 glVertex3f( 1.0, -1.0, 1.0 );
236 glVertex3f( -1.0, -1.0, 1.0 );
237 glVertex3f( -1.0, -1.0, -1.0 );
238 glVertex3f( 1.0, -1.0, -1.0 );
239 glEnd();
241 /* Z faces */
242 glIndexi( Blue );
243 glColor3f( 0.0, 0.0, 1.0 );
244 glBegin( GL_POLYGON );
245 glVertex3f( 1.0, 1.0, 1.0 );
246 glVertex3f( -1.0, 1.0, 1.0 );
247 glVertex3f( -1.0, -1.0, 1.0 );
248 glVertex3f( 1.0, -1.0, 1.0 );
249 glEnd();
251 glBegin( GL_POLYGON );
252 glVertex3f( 1.0, 1.0, -1.0 );
253 glVertex3f( 1.0,-1.0, -1.0 );
254 glVertex3f( -1.0,-1.0, -1.0 );
255 glVertex3f( -1.0, 1.0, -1.0 );
256 glEnd();
262 static void display_loop( void )
264 GLfloat xrot, yrot, zrot;
266 xrot = yrot = zrot = 0.0;
268 glClearColor( 0.0, 0.0, 0.0, 0.0 );
269 glClearIndex( Black );
271 glMatrixMode( GL_PROJECTION );
272 glLoadIdentity();
273 glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
274 glTranslatef( 0.0, 0.0, -5.0 );
276 glMatrixMode( GL_MODELVIEW );
277 glLoadIdentity();
279 glCullFace( GL_BACK );
280 glEnable( GL_CULL_FACE );
282 glShadeModel( GL_FLAT );
284 while (1) {
285 glClear( GL_COLOR_BUFFER_BIT );
286 glPushMatrix();
287 glRotatef( xrot, 1.0, 0.0, 0.0 );
288 glRotatef( yrot, 0.0, 1.0, 0.0 );
289 glRotatef( zrot, 0.0, 0.0, 1.0 );
291 draw_cube();
293 glPopMatrix();
294 glFinish();
296 xrot += 10.0;
297 yrot += 7.0;
298 zrot -= 3.0;
306 int main( int argc, char *argv[] )
308 int mode = 0;
310 if (argc >= 2)
312 if (strcmp(argv[1],"-ci")==0)
313 mode = 0;
314 else if (strcmp(argv[1],"-rgb")==0)
315 mode = 1;
316 else
318 printf("Bad flag: %s\n", argv[1]);
319 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
320 exit(1);
323 else
325 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
326 printf("Defaulting to 8-bit color index\n");
329 make_window( argv[0], mode );
331 display_loop();
332 return 0;