home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / lib / fglut / glut_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.8 KB  |  265 lines

  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2.  
  3. /* This program is freely distributable without licensing fees
  4.    and is provided without guarantee or warrantee expressed or
  5.    implied. This program is -not- in the public domain. */
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <X11/Xlib.h>
  11.  
  12. #include <GL/glut.h>
  13. #include "glutint.h"
  14.  
  15. /* GLUT inter-file variables */
  16. /* *INDENT-OFF* */
  17. char *__glutProgramName = NULL;
  18. int __glutArgc = 0;
  19. char **__glutArgv = NULL;
  20. char *__glutGeometry = NULL;
  21. Display *__glutDisplay = NULL;
  22. int __glutScreen;
  23. Window __glutRoot;
  24. int __glutScreenHeight;
  25. int __glutScreenWidth;
  26. GLboolean __glutIconic = GL_FALSE;
  27. GLboolean __glutDebug = GL_FALSE;
  28. unsigned int __glutDisplayMode =
  29.   GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH;
  30. int __glutConnectionFD;
  31. XSizeHints __glutSizeHints = {0};
  32. int __glutInitWidth = 300, __glutInitHeight = 300;
  33. int __glutInitX = -1, __glutInitY = -1;
  34. GLboolean __glutForceDirect = GL_FALSE,
  35.   __glutTryDirect = GL_TRUE;
  36. Atom __glutWMDeleteWindow;
  37. /* *INDENT-ON* */
  38.  
  39. static Bool synchronize = False;
  40.  
  41. #if defined(__vms)
  42. char *
  43. strdup(const char *string)
  44. {
  45.   char *new;
  46.  
  47.   new = malloc(strlen(string) + 1);
  48.   if (new == NULL)
  49.     return NULL;
  50.   strcpy(new, string);
  51.   return new;
  52. }
  53. #endif
  54.  
  55. void
  56. __glutOpenXConnection(char *display)
  57. {
  58.   int errorBase, eventBase;
  59.  
  60.   __glutDisplay = XOpenDisplay(display);
  61.   if (!__glutDisplay)
  62.     __glutFatalError("could not open display: %s",
  63.       XDisplayName(display));
  64.   if (synchronize)
  65.     XSynchronize(__glutDisplay, True);
  66.   if (!glXQueryExtension(__glutDisplay, &errorBase, &eventBase))
  67.     __glutFatalError(
  68.       "OpenGL GLX extension not supported by display: %s",
  69.       XDisplayName(display));
  70.   __glutScreen = DefaultScreen(__glutDisplay);
  71.   __glutRoot = RootWindow(__glutDisplay, __glutScreen);
  72.   __glutScreenWidth = DisplayWidth(__glutDisplay, __glutScreen);
  73.   __glutScreenHeight = DisplayHeight(__glutDisplay,
  74.     __glutScreen);
  75.   __glutConnectionFD = ConnectionNumber(__glutDisplay);
  76.   __glutWMDeleteWindow = XInternAtom(__glutDisplay,
  77.     "WM_DELETE_WINDOW", False);
  78. }
  79.  
  80. void
  81. __glutInitTime(struct timeval *beginning)
  82. {
  83.   static int beenhere = 0;
  84.   static struct timeval genesis;
  85.  
  86.   if (!beenhere) {
  87.     GETTIMEOFDAY(&genesis);
  88.     beenhere = 1;
  89.   }
  90.   *beginning = genesis;
  91. }
  92.  
  93. static void
  94. removeArgs(int *argcp, char **argv, int numToRemove)
  95. {
  96.   int i, j;
  97.  
  98.   for (i = 0, j = numToRemove; argv[j]; i++, j++) {
  99.     argv[i] = argv[j];
  100.   }
  101.   argv[i] = NULL;
  102.   *argcp -= numToRemove;
  103. }
  104.  
  105. void
  106. glutInit(int *argcp, char **argv)
  107. {
  108.   char *display = NULL;
  109.   char *str;
  110.   struct timeval unused;
  111.   int i;
  112.  
  113.   if (__glutDisplay) {
  114.     __glutWarning("glutInit being called a second time.");
  115.     return;
  116.   }
  117.   /* determine temporary program name */
  118.   str = strrchr(argv[0], '/');
  119.   if (str == NULL) {
  120.     __glutProgramName = argv[0];
  121.   } else {
  122.     __glutProgramName = str + 1;
  123.   }
  124.  
  125.   /* make private copy of command line arguments */
  126.   __glutArgc = *argcp;
  127.   __glutArgv = (char **) malloc(__glutArgc * sizeof(char *));
  128.   if (!__glutArgv)
  129.     __glutFatalError("out of memory.");
  130.   for (i = 0; i < __glutArgc; i++) {
  131.     __glutArgv[i] = strdup(argv[i]);
  132.     if (!__glutArgv[i])
  133.       __glutFatalError("out of memory.");
  134.   }
  135.  
  136.   /* determine permanent program name */
  137.   str = strrchr(__glutArgv[0], '/');
  138.   if (str == NULL) {
  139.     __glutProgramName = __glutArgv[0];
  140.   } else {
  141.     __glutProgramName = str + 1;
  142.   }
  143.  
  144.   /* parse arguments for standard options */
  145.   for (i = 1; i < __glutArgc; i++) {
  146.     if (!strcmp(__glutArgv[i], "-display")) {
  147.       if (++i >= __glutArgc) {
  148.         __glutFatalError(
  149.           "follow -display option with X display name.");
  150.       }
  151.       display = __glutArgv[i];
  152.       removeArgs(argcp, &argv[1], 2);
  153.     } else if (!strcmp(__glutArgv[i], "-geometry")) {
  154.       int flags, x, y, width, height;
  155.  
  156.       if (++i >= __glutArgc) {
  157.         __glutFatalError(
  158.           "follow -geometry option with geometry parameter.");
  159.       }
  160.       /* Fix bogus "{width|height} may be used before set"
  161.          warning */
  162.       width = 0;
  163.       height = 0;
  164.  
  165.       flags = XParseGeometry(__glutArgv[i], &x, &y,
  166.         (unsigned int *) &width, (unsigned int *) &height);
  167.       if (WidthValue & flags) {
  168.         /* Careful because X does not allow zero or negative
  169.            width windows */
  170.         if (width > 0)
  171.           __glutInitWidth = width;
  172.       }
  173.       if (HeightValue & flags) {
  174.         /* Careful because X does not allow zero or negative
  175.            height windows */
  176.         if (height > 0)
  177.           __glutInitHeight = height;
  178.       }
  179.       glutInitWindowSize(__glutInitWidth, __glutInitHeight);
  180.       if (XValue & flags) {
  181.         if (XNegative & flags)
  182.           x = DisplayWidth(__glutDisplay, __glutScreen) +
  183.             x - __glutSizeHints.width;
  184.         /* Play safe: reject negative X locations */
  185.         if (x >= 0)
  186.           __glutInitX = x;
  187.       }
  188.       if (YValue & flags) {
  189.         if (YNegative & flags)
  190.           y = DisplayHeight(__glutDisplay, __glutScreen) +
  191.             y - __glutSizeHints.height;
  192.         /* Play safe: reject negative Y locations */
  193.         if (y >= 0)
  194.           __glutInitY = y;
  195.       }
  196.       glutInitWindowPosition(__glutInitX, __glutInitY);
  197.       removeArgs(argcp, &argv[1], 2);
  198.     } else if (!strcmp(__glutArgv[i], "-direct")) {
  199.       if (!__glutTryDirect)
  200.         __glutFatalError(
  201.           "cannot force both direct and indirect rendering.");
  202.       __glutForceDirect = GL_TRUE;
  203.       removeArgs(argcp, &argv[1], 1);
  204.     } else if (!strcmp(__glutArgv[i], "-indirect")) {
  205.       if (__glutForceDirect)
  206.         __glutFatalError(
  207.           "cannot force both direct and indirect rendering.");
  208.       __glutTryDirect = GL_FALSE;
  209.       removeArgs(argcp, &argv[1], 1);
  210.     } else if (!strcmp(__glutArgv[i], "-iconic")) {
  211.       __glutIconic = GL_TRUE;
  212.       removeArgs(argcp, &argv[1], 1);
  213.     } else if (!strcmp(__glutArgv[i], "-gldebug")) {
  214.       __glutDebug = GL_TRUE;
  215.       removeArgs(argcp, &argv[1], 1);
  216.     } else if (!strcmp(__glutArgv[i], "-sync")) {
  217.       synchronize = GL_TRUE;
  218.       removeArgs(argcp, &argv[1], 1);
  219.     } else {
  220.       /* once unknown option encountered, stop command line
  221.          processing */
  222.       break;
  223.     }
  224.   }
  225.   __glutOpenXConnection(display);
  226.   __glutInitTime(&unused);
  227. }
  228.  
  229. /* CENTRY */
  230. void
  231. glutInitWindowPosition(int x, int y)
  232. {
  233.   __glutInitX = x;
  234.   __glutInitY = y;
  235.   if (x >= 0 && y >= 0) {
  236.     __glutSizeHints.x = x;
  237.     __glutSizeHints.y = y;
  238.     __glutSizeHints.flags |= USPosition;
  239.   } else {
  240.     __glutSizeHints.flags &= ~USPosition;
  241.   }
  242. }
  243.  
  244. void
  245. glutInitWindowSize(int width, int height)
  246. {
  247.   __glutInitWidth = width;
  248.   __glutInitHeight = height;
  249.   if (width > 0 && height > 0) {
  250.     __glutSizeHints.width = width;
  251.     __glutSizeHints.height = height;
  252.     __glutSizeHints.flags |= USSize;
  253.   } else {
  254.     __glutSizeHints.flags &= ~USSize;
  255.   }
  256. }
  257.  
  258. void
  259. glutInitDisplayMode(unsigned int mask)
  260. {
  261.   __glutDisplayMode = mask;
  262. }
  263.  
  264. /* ENDCENTRY */
  265.