Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

miniglxtest.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* $Id: miniglxtest.c,v 1.2 2003/08/23 01:28:59 jonsmirl Exp $ */
  2. /*
  3. * Test the mini GLX interface.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <GL/gl.h>
  9. #define USE_MINI_GLX 1
  10. #if USE_MINI_GLX
  11. #include <GL/miniglx.h>
  12. #else
  13. #include <GL/glx.h>
  14. #endif
  15. #define FRONTBUFFER 1
  16. #define NR 6
  17. #define DO_SLEEPS 1
  18. #define NR_DISPLAYS 2
  19. GLXContext ctx;
  20. static void _subset_Rectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 )
  21. {
  22. glBegin( GL_QUADS );
  23. glVertex2f( x1, y1 );
  24. glVertex2f( x2, y1 );
  25. glVertex2f( x2, y2 );
  26. glVertex2f( x1, y2 );
  27. glEnd();
  28. }
  29. static void redraw( Display *dpy, Window w, int rot )
  30. {
  31. printf("Redraw event\n");
  32. #if FRONTBUFFER
  33. glDrawBuffer( GL_FRONT );
  34. #else
  35. /* glDrawBuffer( GL_BACK ); */
  36. #endif
  37. glClearColor( rand()/(float)RAND_MAX,
  38. rand()/(float)RAND_MAX,
  39. rand()/(float)RAND_MAX,
  40. 1);
  41. glClear( GL_COLOR_BUFFER_BIT );
  42. #if 1
  43. glColor3f( rand()/(float)RAND_MAX,
  44. rand()/(float)RAND_MAX,
  45. rand()/(float)RAND_MAX );
  46. glPushMatrix();
  47. glRotatef(rot, 0, 0, 1);
  48. glScalef(.5, .5, .5);
  49. _subset_Rectf( -1, -1, 1, 1 );
  50. glPopMatrix();
  51. #endif
  52. #if FRONTBUFFER
  53. glFlush();
  54. #else
  55. glXSwapBuffers( dpy, w );
  56. #endif
  57. glFinish();
  58. }
  59. static Window make_rgb_db_window( Display *dpy,
  60. unsigned int width, unsigned int height )
  61. {
  62. int attrib[] = { GLX_RGBA,
  63. GLX_RED_SIZE, 1,
  64. GLX_GREEN_SIZE, 1,
  65. GLX_BLUE_SIZE, 1,
  66. #if !FRONT_BUFFER
  67. GLX_DOUBLEBUFFER,
  68. #endif
  69. None };
  70. int scrnum;
  71. XSetWindowAttributes attr;
  72. unsigned long mask;
  73. Window root;
  74. Window win;
  75. XVisualInfo *visinfo;
  76. scrnum = 0;
  77. root = RootWindow( dpy, scrnum );
  78. if (!(visinfo = glXChooseVisual( dpy, scrnum, attrib ))) {
  79. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  80. exit(1);
  81. }
  82. if(!(ctx = glXCreateContext( dpy, visinfo, NULL, True ))) {
  83. printf("Error: glXCreateContext failed\n");
  84. exit(1);
  85. }
  86. /* window attributes */
  87. attr.background_pixel = 0;
  88. attr.border_pixel = 0;
  89. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  90. attr.event_mask = StructureNotifyMask | ExposureMask;
  91. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  92. win = XCreateWindow( dpy, root, 0, 0, width, height,
  93. 0, visinfo->depth, InputOutput,
  94. visinfo->visual, mask, &attr );
  95. if (!win) {
  96. printf("Error: XCreateWindow failed\n");
  97. exit(1);
  98. }
  99. glXMakeCurrent( dpy, win, ctx );
  100. glViewport(0, 0, width, height);
  101. return win;
  102. }
  103. static void event_loop( Display *dpy, Window win )
  104. {
  105. int i;
  106. printf("Hang on... drawing %d frames\n", NR);
  107. for (i = 0; i < NR; i++) {
  108. redraw( dpy, win, i*10 );
  109. if (DO_SLEEPS) {
  110. printf("sleep(1)\n");
  111. sleep(1);
  112. }
  113. }
  114. }
  115. int foo( )
  116. {
  117. Display *dpy;
  118. Window win;
  119. dpy = XOpenDisplay(NULL);
  120. if (!dpy) {
  121. printf("Error: XOpenDisplay failed\n");
  122. return 1;
  123. }
  124. win = make_rgb_db_window( dpy, 800, 600);
  125. srand(getpid());
  126. glShadeModel( GL_FLAT );
  127. glClearColor( 0.5, 0.5, 0.5, 1.0 );
  128. XMapWindow( dpy, win );
  129. {
  130. XEvent e;
  131. while (1) {
  132. XNextEvent( dpy, &e );
  133. if (e.type == MapNotify && e.xmap.window == win) {
  134. break;
  135. }
  136. }
  137. }
  138. event_loop( dpy, win );
  139. glXDestroyContext( dpy, ctx );
  140. XDestroyWindow( dpy, win );
  141. XCloseDisplay( dpy );
  142. return 0;
  143. }
  144. int main()
  145. {
  146. int i;
  147. for (i = 0 ; i < NR_DISPLAYS ; i++) {
  148. if (foo() != 0)
  149. break;
  150. }
  151. return 0;
  152. }