summaryrefslogtreecommitdiff
path: root/progs/tests/miniglxtest.c
blob: bcdb468282585cc541956bbb511b376e6dec6cbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* $Id: miniglxtest.c,v 1.1.4.7 2003/02/21 22:18:24 keithw Exp $ */

/*
 * Test the mini GLX interface.
 */


#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#define USE_MINI_GLX 1
#if USE_MINI_GLX
#include <GL/miniglx.h>
#else
#include <GL/glx.h>
#endif

#define FRONTBUFFER 1
#define NR          6
#define DO_SLEEPS   1
#define NR_DISPLAYS 2

GLXContext ctx;


static void _subset_Rectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 )
{
   glBegin( GL_QUADS );
   glVertex2f( x1, y1 );
   glVertex2f( x2, y1 );
   glVertex2f( x2, y2 );
   glVertex2f( x1, y2 );
   glEnd();
}



static void redraw( Display *dpy, Window w, int rot )
{
   printf("Redraw event\n");

#if FRONTBUFFER
    glDrawBuffer( GL_FRONT ); 
#else
/*     glDrawBuffer( GL_BACK );    */
#endif

   glClearColor( rand()/(float)RAND_MAX, 
		 rand()/(float)RAND_MAX, 
		 rand()/(float)RAND_MAX,
		 1);

   glClear( GL_COLOR_BUFFER_BIT ); 

#if 1
   glColor3f( rand()/(float)RAND_MAX, 
	      rand()/(float)RAND_MAX, 
	      rand()/(float)RAND_MAX );
   glPushMatrix();
   glRotatef(rot, 0, 0, 1);
   glScalef(.5, .5, .5);
   _subset_Rectf( -1, -1, 1, 1 );
   glPopMatrix();
#endif

#if FRONTBUFFER
   glFlush();
#else
   glXSwapBuffers( dpy, w ); 
#endif
   glFinish();
}


static Window make_rgb_db_window( Display *dpy,
				  unsigned int width, unsigned int height )
{
   int attrib[] = { GLX_RGBA,
		    GLX_RED_SIZE, 1,
		    GLX_GREEN_SIZE, 1,
		    GLX_BLUE_SIZE, 1,
#if !FRONT_BUFFER
 		    GLX_DOUBLEBUFFER, 
#endif
		    None };
   int scrnum;
   XSetWindowAttributes attr;
   unsigned long mask;
   Window root;
   Window win;
   XVisualInfo *visinfo;

   scrnum = 0;
   root = RootWindow( dpy, scrnum );

   visinfo = glXChooseVisual( dpy, scrnum, attrib );
   if (!visinfo) {
      printf("Error: couldn't get an RGB, Double-buffered visual\n");
      exit(1);
   }

   ctx = glXCreateContext( dpy, visinfo, NULL, True );
   if (!ctx) {
      printf("Error: glXCreateContext failed\n");
      exit(1);
   }

   /* window attributes */
   attr.background_pixel = 0;
   attr.border_pixel = 0;
   attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
   attr.event_mask = StructureNotifyMask | ExposureMask;
   mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;

   win = XCreateWindow( dpy, root, 0, 0, width, height,
		        0, visinfo->depth, InputOutput,
		        visinfo->visual, mask, &attr );
   if (!win) {
      printf("Error: XCreateWindow failed\n");
      exit(1);
   }

   glXMakeCurrent( dpy, win, ctx );

   return win;
}


static void event_loop( Display *dpy, Window win )
{
   int i;

   printf("Hang on... drawing %d frames\n", NR);
   for (i = 0; i < NR; i++) {
      redraw( dpy, win, i*10 );
      if (DO_SLEEPS) {
	 printf("sleep(1)\n");   
	 sleep(1);  
      }
   }
}


int foo( )
{
   Display *dpy;
   Window win;

   dpy = XOpenDisplay(NULL);
   if (!dpy) {
      printf("Error: XOpenDisplay failed\n");
      return 1;
   }

   win = make_rgb_db_window( dpy, 800, 600);

   srand(getpid());

   glShadeModel( GL_FLAT );
   glClearColor( 0.5, 0.5, 0.5, 1.0 );

   XMapWindow( dpy, win );

#if !USE_MINI_GLX
   {
      XEvent e;
      while (1) {
	 XNextEvent( dpy, &e );
	 if (e.type == MapNotify && e.xmap.window == win) {
	    break;
	 }
      }
   }
#endif


   event_loop( dpy, win );

   glXDestroyContext( dpy, ctx );
   XDestroyWindow( dpy, win );

   XCloseDisplay( dpy );

   return 0;
}


int main()
{
   int i;
   for (i = 0 ; i < NR_DISPLAYS ; i++) {
      if (foo() != 0)
	 break;
   }

   return 0;
}