home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume18 / xboing / part12 / init.c next >
C/C++ Source or Header  |  1993-09-13  |  25KB  |  899 lines

  1. #include "include/copyright.h"
  2.  
  3. /*
  4.  *  Include file dependencies:
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stddef.h>
  10. #include <sys/time.h>
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13. #include <X11/Xos.h>
  14.  
  15. #include "include/score.h"
  16. #include "include/presents.h"
  17. #include "include/special.h"
  18. #include "include/main.h"
  19. #include "include/version.h"
  20. #include "include/error.h"
  21. #include "include/mess.h"
  22. #include "include/ball.h"
  23. #include "include/gun.h"
  24. #include "include/sfx.h"
  25. #include "include/blocks.h"
  26. #include "include/level.h"
  27. #include "include/bonus.h"
  28. #include "include/stage.h"
  29. #include "include/paddle.h"
  30. #include "include/intro.h"
  31. #include "include/inst.h"
  32. #include "include/highscore.h"
  33. #include "include/keys.h"
  34. #include "include/audio.h"
  35. #include "include/misc.h"
  36.  
  37. #include "include/init.h"
  38.  
  39. /*
  40.  *  Internal macro definitions:
  41.  */
  42.  
  43. #define TITLE_FONT     "-adobe-helvetica-bold-r-*-*-24-*-*-*-*-*-*-*"
  44. #define COPY_FONT   "-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*"
  45. #define TEXT_FONT      "-adobe-helvetica-medium-r-*-*-18-*-*-*-*-*-*-*"
  46.  
  47. /*
  48.  *  Internal type declarations:
  49.  */
  50.  
  51. #if NeedFunctionPrototypes
  52. static void ParseCommandLine(char **argv, int argc);
  53. static void InitialiseSettings(void);
  54. static int compareArgument(char *arg1, char *arg2, int minMatch);
  55. static void HandleDisplayErrors(char *displayName);
  56. static void PrintVersion(void);
  57. static void PrintUsage(void);
  58. static void PrintHelp(void);
  59. static void InitialiseGraphics(Display *display, Window window);
  60. static void InitialiseColourNames(Display *display, Colormap colormap);
  61. static void InitialiseFonts(Display *display);
  62. static void ReleaseGraphics(Display *display);
  63. static void ReleaseFonts(Display *display);
  64. static void ExitProgramNow(int value);
  65. static void TurnOnSynchronise(Display *display);
  66. #else
  67. static void ExitProgramNow();
  68. static void ParseCommandLine();
  69. static void InitialiseSettings();
  70. static int compareArgument();
  71. static void HandleDisplayErrors();
  72. static void PrintVersion();
  73. static void PrintUsage();
  74. static void PrintHelp();
  75. static void InitialiseGraphics();
  76. static void InitialiseColourNames();
  77. static void InitialiseFonts();
  78. static void ReleaseGraphics();
  79. static void ReleaseFonts();
  80. static void TurnOnSynchronise();
  81. #endif
  82.  
  83. /*
  84.  *  Internal variable declarations:
  85.  */
  86.  
  87. int red, tann, yellow, green, white, black, blue, purple, reds[14], greens[14];
  88. GC gc, gcxor, gcand, gcor, gcsfx;
  89. XFontStruct *titleFont, *copyFont, *textFont;
  90. Colormap     colormap;
  91. static XVisualInfo    visual_info;
  92. static unsigned long event_mask;
  93. static char *displayName = (char *) NULL;
  94. static int    syncOn;
  95. static int    grabPointer;
  96. static int    useDefaultColourmap;
  97. int    noSound, debug;
  98.  
  99. #if NeedFunctionPrototypes
  100. static void InitialiseGraphics(Display *display, Window window)
  101. #else
  102. static void InitialiseGraphics(display, window)
  103.     Display *display;
  104.     Window window;
  105. #endif
  106. {
  107.     XGCValues    gcv;
  108.  
  109.     /* Create a graphics context using copy mode */
  110.     gcv.function               = GXcopy;
  111.     gcv.fill_style            = FillTiled;
  112.     gcv.graphics_exposures    = False;
  113.     if (!(gc = XCreateGC(display, window, 
  114.         GCFunction | GCFillStyle | GCGraphicsExposures, &gcv)))
  115.         ShutDown(display, 1, "Cannot create GXcopy graphics context.");
  116.  
  117.     /* Create a graphics context using xor mode */
  118.     gcv.function               = GXxor;
  119.     gcv.graphics_exposures    = False;
  120.     if (!(gcxor = XCreateGC(display, window, 
  121.         GCFunction | GCGraphicsExposures, &gcv)))
  122.         ShutDown(display, 1, "Cannot create GXxor graphics context.");
  123.  
  124.     /* Create a graphics context using and mode */
  125.     gcv.function               = GXand;
  126.     gcv.graphics_exposures    = False;
  127.     if (!(gcand = XCreateGC(display, window, 
  128.         GCFunction | GCGraphicsExposures, &gcv)))
  129.         ShutDown(display, 1, "Cannot create GXand graphics context.");
  130.     XSetForeground(display, gcand, 0);
  131.     XSetBackground(display, gcand, ~0);
  132.  
  133.     /* Create a graphics context using or mode */
  134.     gcv.function               = GXor;
  135.     gcv.graphics_exposures    = False;
  136.     if (!(gcor = XCreateGC(display, window, 
  137.         GCFunction | GCGraphicsExposures, &gcv)))
  138.         ShutDown(display, 1, "Cannot create GXor graphics context.");
  139.  
  140.     /* Create a graphics context for use by sfx */
  141.     gcv.function               = GXcopy;
  142.     gcv.graphics_exposures    = False;
  143.     if (!(gcsfx = XCreateGC(display, window, 
  144.         GCFunction | GCGraphicsExposures, &gcv)))
  145.         ShutDown(display, 1, "Cannot create GXsfx graphics context.");
  146. }
  147.  
  148. #if NeedFunctionPrototypes
  149. static void InitialiseColourNames(Display *display, Colormap colormap)
  150. #else
  151. static void InitialiseColourNames(display, colormap)
  152.     Display *display;
  153.     Colormap colormap;
  154. #endif
  155. {
  156.     /* Obtain the colour index of several colours from colourmap */
  157.     red     = ColourNameToPixel(display, colormap, "red");
  158.     tann    = ColourNameToPixel(display, colormap, "tan");
  159.     yellow  = ColourNameToPixel(display, colormap, "yellow");
  160.     green   = ColourNameToPixel(display, colormap, "green");
  161.     white   = ColourNameToPixel(display, colormap, "white");
  162.     black   = ColourNameToPixel(display, colormap, "black");
  163.     purple  = ColourNameToPixel(display, colormap, "purple");
  164.     blue    = ColourNameToPixel(display, colormap, "blue");
  165. }
  166.  
  167. #if NeedFunctionPrototypes
  168. static void InitialiseCycleColourNames(Display *display, Colormap colormap)
  169. #else
  170. static void InitialiseCycleColourNames(display, colormap)
  171.     Display *display;
  172.     Colormap colormap;
  173. #endif
  174. {
  175.     reds[0] = ColourNameToPixel(display, colormap, "#f00");
  176.     reds[1] = ColourNameToPixel(display, colormap, "#e00");
  177.     reds[2] = ColourNameToPixel(display, colormap, "#d00");
  178.     reds[3] = ColourNameToPixel(display, colormap, "#c00");
  179.     reds[4] = ColourNameToPixel(display, colormap, "#b00");
  180.     reds[5] = ColourNameToPixel(display, colormap, "#a00");
  181.     reds[6] = ColourNameToPixel(display, colormap, "#900");
  182.     reds[7] = ColourNameToPixel(display, colormap, "#800");
  183.     reds[8] = ColourNameToPixel(display, colormap, "#700");
  184.     reds[9] = ColourNameToPixel(display, colormap, "#600");
  185.     reds[10] = ColourNameToPixel(display, colormap, "#500");
  186.     reds[11] = ColourNameToPixel(display, colormap, "#400");
  187.     reds[12] = ColourNameToPixel(display, colormap, "#300");
  188.     reds[13] = ColourNameToPixel(display, colormap, "#200");
  189.  
  190.     greens[0] = ColourNameToPixel(display, colormap, "#0f0");
  191.     greens[1] = ColourNameToPixel(display, colormap, "#0e0");
  192.     greens[2] = ColourNameToPixel(display, colormap, "#0d0");
  193.     greens[3] = ColourNameToPixel(display, colormap, "#0c0");
  194.     greens[4] = ColourNameToPixel(display, colormap, "#0b0");
  195.     greens[5] = ColourNameToPixel(display, colormap, "#0a0");
  196.     greens[6] = ColourNameToPixel(display, colormap, "#090");
  197.     greens[7] = ColourNameToPixel(display, colormap, "#080");
  198.     greens[8] = ColourNameToPixel(display, colormap, "#070");
  199.     greens[9] = ColourNameToPixel(display, colormap, "#060");
  200.     greens[10] = ColourNameToPixel(display, colormap, "#050");
  201.     greens[11] = ColourNameToPixel(display, colormap, "#040");
  202.     greens[12] = ColourNameToPixel(display, colormap, "#030");
  203.     greens[13] = ColourNameToPixel(display, colormap, "#020");
  204. }
  205.  
  206. #if NeedFunctionPrototypes
  207. static void InitialiseFonts(Display *display)
  208. #else
  209. static void InitialiseFonts(display)
  210.     Display *display;
  211. #endif
  212. {
  213.     /* Create all required font structures */
  214.  
  215.     /* Font used for titles */
  216.     if (!(titleFont = XLoadQueryFont(display, TITLE_FONT)))
  217.     {
  218.         WarningMessage("Cannot open the title font. Using fixed.");
  219.         titleFont = XLoadQueryFont(display, "fixed");
  220.     }
  221.  
  222.     /* Very small font used for copyright message etc. */
  223.     if (!(copyFont = XLoadQueryFont(display, COPY_FONT)))
  224.     {
  225.         WarningMessage("Cannot open the copyright font. Using fixed.");
  226.         copyFont = XLoadQueryFont(display, "fixed");
  227.     }
  228.  
  229.     /* Font used for general text everywhere */
  230.     if (!(textFont = XLoadQueryFont(display, TEXT_FONT)))
  231.     {
  232.         WarningMessage("Cannot open the text font. Using fixed.");
  233.         textFont = XLoadQueryFont(display, "fixed");
  234.     }
  235. }
  236.  
  237. #if NeedFunctionPrototypes
  238. static void ReleaseFonts(Display *display)
  239. #else
  240. static void ReleaseFonts(display)
  241.     Display *display;
  242. #endif
  243. {
  244.     /* Free all the fonts used */
  245.     if (titleFont)    XFreeFont(display, titleFont);
  246.     if (copyFont)    XFreeFont(display, copyFont);
  247.     if (textFont)    XFreeFont(display, textFont);
  248. }
  249.  
  250. #if NeedFunctionPrototypes
  251. static void ExitProgramNow(int value)
  252. #else
  253. static void ExitProgramNow(value)
  254.     int value;
  255. #endif
  256. {
  257.     /* Return to the shell with error code */
  258.     exit(value);
  259. }
  260.  
  261. #if NeedFunctionPrototypes
  262. static void ReleaseGraphics(Display *display)
  263. #else
  264. static void ReleaseGraphics(display)
  265.     Display *display;
  266. #endif
  267. {
  268.     /* Free the graphics contexts */
  269.     if (gc)     XFreeGC(display, gc);
  270.     if (gcxor)     XFreeGC(display, gcxor);
  271.     if (gcand)     XFreeGC(display, gcand);
  272.     if (gcor)     XFreeGC(display, gcor);
  273.     if (gcsfx)     XFreeGC(display, gcsfx);
  274. }
  275.  
  276. #if NeedFunctionPrototypes
  277. void ShutDown(Display *display, int exit_code, char *message)
  278. #else
  279. void ShutDown(display, exit_code, message)
  280.     Display *display;
  281.     int exit_code;
  282.     char *message;
  283. #endif
  284. {
  285.     /* This is the last function called when exiting */
  286.  
  287.     /* Remove the colour map */
  288.     if (colormap) XUninstallColormap(display, colormap);
  289.  
  290.     /* Close the audio device if available and wanted */
  291.     if (noSound == False)
  292.         FreeAudioSystem();
  293.  
  294.     FreeMisc(display);                /* Free backing store pixmap*/
  295.     FreeKeyControl(display);        /* Free key control         */
  296.     FreeHighScore(display);            /* Free high score memory     */
  297.     FreeInstructions(display);        /* Free instructions        */
  298.     FreeBonus(display);                /* Free bonus memory         */
  299.     FreeIntroduction(display);        /* Free introduction memory */
  300.     FreeMessageSystem(display);        /* Free message system         */
  301.     FreePaddle(display);            /* Free paddle pixmaps         */
  302.     FreeLevelInfo(display);            /* Free level pixmaps         */
  303.     FreeScoreDigits(display);        /* Free digit pixmaps         */
  304.     FreeBlockPixmaps(display);        /* Free all block pixmaps     */
  305.     FreeBall(display);                /* Free the ball animation     */
  306.     FreeBullet(display);            /* Free the bullet          */
  307.     ReleaseGraphics(display);        /* Free graphics contexts     */
  308.     ReleaseFonts(display);            /* Unload fonts used        */
  309.  
  310.     /* Output an error message if required */
  311.     if (message[0] != '\0')
  312.     {
  313.         /* If we had an error then use error message */
  314.         if (exit_code == 1)
  315.             ErrorMessage(message);
  316.         else
  317.             NormalMessage(message);
  318.     }
  319.  
  320.     /* Exit with the error code */
  321.     ExitProgramNow(exit_code);
  322. }
  323.  
  324. #if NeedFunctionPrototypes
  325. static int ErrorHandler(Display *display, XErrorEvent *err)
  326. #else
  327. static int ErrorHandler(display, err)
  328.     Display *display;
  329.     XErrorEvent *err;
  330. #endif
  331. {
  332.      char msg[80];
  333.      char string[256];
  334.  
  335.     /* Obtain the error message from the server */
  336.     XGetErrorText(display, err->error_code, msg, 80);
  337.     sprintf(string, "Xlib Error: %s", msg);
  338.  
  339.     /* Close down the system */
  340.     ShutDown(display, 1, string);
  341.  
  342.     /* Not reached but makes the compiler happy */
  343.     return True;
  344. }
  345.  
  346. #if NeedFunctionPrototypes
  347. static void HandleDisplayErrors(char *displayName)
  348. #else
  349. static void HandleDisplayErrors(displayName)
  350.     char *displayName;
  351. #endif
  352. {
  353.      char string[256];
  354.  
  355.     /* Check if the DISPLAY variable is set and write correct message */
  356.     if (getenv("DISPLAY") == NULL)
  357.         WarningMessage("Your X-Window system display variable is not set.");
  358.     else
  359.     {
  360.         sprintf(string, "Cannot connect to display called <%s>.", displayName);
  361.         WarningMessage(string);
  362.     }
  363. }
  364.  
  365. #if NeedFunctionPrototypes
  366. static void PrintVersion(void)
  367. #else
  368. static void PrintVersion()
  369. #endif
  370. {
  371.     /* Print version for program to user for command line help */
  372.     fprintf(stdout, 
  373.         "XBoing by Justin Kibell (jck@citri.edu.au)\n"); 
  374.     fprintf(stdout, 
  375.       "Version %d.%d %s\n", VERSION, REVNUM + buildNum, dateString);
  376.     fprintf(stdout, "(c) Copyright 1993, All Rights Reserved\n");
  377.  
  378.     /* Exit now */
  379.     ExitProgramNow(0);
  380. }
  381.  
  382. #if NeedFunctionPrototypes
  383. static void PrintUsage(void)
  384. #else
  385. static void PrintUsage()
  386. #endif
  387. {
  388.     /* Print usage to user for command line help */
  389.  
  390.     fprintf(stdout, "%s%s",
  391.         "Usage: XBoing [-version] [-usage] [-help] [-sync] ",
  392.         "[-display <displayName>]\n"); 
  393.     fprintf(stdout, "%s%s\n%s%s\n",
  394.         "              [-speed <1-9>] [-scores] [-keys] [-sound] [-setup]",
  395.         " [-nosfx]",
  396.         "              [-nograb] [-maxvol <1-100>] [-startlevel <1-MAX>]",
  397.         " [-usedefcmap]");
  398.  
  399.     /* Exit now */
  400.     ExitProgramNow(0);
  401. }
  402.  
  403. #if NeedFunctionPrototypes
  404. static void PrintSetup(void)
  405. #else
  406. static void PrintSetup()
  407. #endif
  408. {
  409.     /* Print setup information about xboing */
  410.     char *str;
  411.  
  412.     fprintf(stdout, "xboing: Setup Information\n");
  413.     fprintf(stdout, "           Audio: %s\n", 
  414.         AUDIO_AVAILABLE ? "Available" : "Not available");
  415.     fprintf(stdout, "         Version: %d.%d\n", VERSION, REVNUM + buildNum);
  416.     fprintf(stdout, "        Compiled: %s\n", dateString);
  417.  
  418.     /* If the environment variable exists - use it */
  419.     if ((str = getenv("XBOING_SCORE_FILE")) != NULL)
  420.         fprintf(stdout, "      Score File: %s\n", str);
  421.     else
  422.         fprintf(stdout, "      Score File: %s\n", HIGH_SCORE_FILE);
  423.  
  424.     /* If the environment variable exists - use it */
  425.     if ((str = getenv("XBOING_LEVELS_DIR")) != NULL)
  426.         fprintf(stdout, " Level directory: %s\n", str);
  427.     else
  428.         fprintf(stdout, " Level directory: %s\n", LEVEL_INSTALL_DIR);
  429.  
  430.     fprintf(stdout, "Number of levels: %d\n", MAX_NUM_LEVELS);
  431.  
  432.     /* If the environment variable exists - use it */
  433.     if ((str = getenv("XBOING_SOUND_DIR")) != NULL)
  434.         fprintf(stdout, "Sounds directory: %s\n", str);
  435.     else
  436.         fprintf(stdout, "Sounds directory: %s\n", SOUNDS_DIR);
  437.  
  438.     /* Exit now */
  439.     ExitProgramNow(0);
  440. }
  441.  
  442.  
  443. #if NeedFunctionPrototypes
  444. static void PrintHelp(void)
  445. #else
  446. static void PrintHelp()
  447. #endif
  448. {
  449.     /* Print help for program to user for command line help */
  450.     fprintf(stdout, 
  451.         "XBoing by Justin Kibell (jck@citri.edu.au)\n"); 
  452.     fprintf(stdout, "Version %d.%d %s\n", VERSION, REVNUM + buildNum,
  453.         "(c) Copyright 1993, All Rights Reserved\n");
  454.  
  455.     fprintf(stdout, "Help: \n");
  456.     fprintf(stdout, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  457.         "    -speed <n>              - The game speed, 1 - 9. 9 = fast\n",
  458.         "    -maxvol <n>             - The maximum volume as percentage.\n",
  459.         "    -startlevel <n>         - The starting level for game.\n",
  460.         "    -help                   - Produce this help message.\n",
  461.         "    -sync                   - Turn on X synchronisation.\n",
  462.         "    -usage                  - Print a brief help message.\n",
  463.         "    -version                - Print out the current version.\n",
  464.         "    -scores                 - Print out the current highscores.\n",
  465.         "    -keys                   - Use keys instead of mouse control.\n",
  466.         "    -sound                  - Turn audio ON for game.\n",
  467.         "    -setup                  - Print setup information.\n",
  468.         "    -nosfx                  - Do not use special effects.\n",
  469.         "    -nograb                 - Turn off pointer grab.\n",
  470.         "    -usedefcmap             - Use the default colourmap.\n",
  471.         "    -display <display>      - Set the display for the game.\n");
  472.  
  473.     /* Exit now */
  474.     ExitProgramNow(0);
  475. }
  476.  
  477. #if NeedFunctionPrototypes
  478. static int compareArgument(char *arg1, char *arg2, int minMatch)
  479. #else
  480. static int compareArgument(arg1, arg2, minMatch)
  481.     char *arg1;
  482.     char *arg2;
  483.     int minMatch;
  484. #endif
  485. {
  486.     if ((strlen(arg1) < minMatch) || (strlen(arg2) < minMatch)) 
  487.         return 1;
  488.     if (strlen(arg1) > strlen(arg2)) 
  489.         return 1;
  490.  
  491.     /* Return true or false basically */
  492.     return (strncmp(arg1, arg2, strlen(arg1)));
  493. }
  494.  
  495. #if NeedFunctionPrototypes
  496. static void TurnOnSynchronise(Display *display)
  497. #else
  498. static void TurnOnSynchronise(display)
  499.     Display *display;
  500. #endif
  501. {
  502.     /* Turn the X synchronisation on to flush all calls each frame */
  503.     XSynchronize(display, True);
  504. }
  505.  
  506. #if NeedFunctionPrototypes
  507. static void InitialiseSettings(void)
  508. #else
  509. static void InitialiseSettings()
  510. #endif
  511. {
  512.     /* Initialise some variables */
  513.     syncOn = False;
  514.     debug = False;
  515.     grabPointer = True;
  516.     useDefaultColourmap = False;
  517.  
  518.     /* The audio is off by default */
  519.     noSound = True;
  520.  
  521.     /* So the audio code will use system default */
  522.     SetMaximumVolume(0);    
  523.  
  524.     /* Always start on level one */
  525.     SetStartingLevel(1);
  526.     SetLevelNumber(1);
  527.  
  528.     /* Not so fast */
  529.     SetUserSpeed(7);
  530.  
  531.     useSpecialEffects(True);
  532.     score = 0L;
  533. }
  534.  
  535. #if NeedFunctionPrototypes
  536. static void ParseCommandLine(char **argv, int argc)
  537. #else
  538. static void ParseCommandLine(argv, argc)
  539.     char **argv;
  540.     int argc;
  541. #endif
  542. {
  543.     /* Parse the command line options */
  544.     int i, l;
  545.     char str[80];
  546.  
  547.     /* Initialise variables */
  548.     InitialiseSettings();
  549.  
  550.     for (i = 1; i < argc; i++)
  551.     {
  552.         if (argv[i][0] != '-')
  553.         {
  554.             PrintUsage();
  555.             break;
  556.         }
  557.  
  558.         if (!compareArgument(argv[i], "-display", 7))
  559.         {
  560.             /* Set the display name for later connection */
  561.             i++;
  562.             if (i < argc)
  563.                 displayName = argv[i];
  564.             else PrintUsage();
  565.  
  566.         } else if (!compareArgument(argv[i], "-help", 4))
  567.         {
  568.             /* Print out the help information and quit */
  569.             PrintHelp();
  570.  
  571.         } else if (!compareArgument(argv[i], "-debug", 5))
  572.         {
  573.             /* Turn on debugging */
  574.             NormalMessage("Debugging mode activated.");
  575.             debug = True;
  576.  
  577.         } else if (!compareArgument(argv[i], "-setup", 5))
  578.         {
  579.             /* Print out information on setup */
  580.             PrintSetup();
  581.  
  582.         } else if (!compareArgument(argv[i], "-sync", 4))
  583.         {
  584.             /* Turn on X protocol synchronisation */
  585.             syncOn = True;
  586.  
  587.         } else if (!compareArgument(argv[i], "-version", 7))
  588.         {
  589.             /* Print out the version information and quit */
  590.             PrintVersion();
  591.  
  592.         } else if (!compareArgument(argv[i], "-sound", 5))
  593.         {
  594.             /* Enable the sound in the game */
  595.             noSound = False;
  596.  
  597.         } else if (!compareArgument(argv[i], "-keys", 4))
  598.         {
  599.             /* Set the paddle control mode to use keys */
  600.             SetPaddleControlMode(CONTROL_KEYS);
  601.  
  602.         } else if (!compareArgument(argv[i], "-scores", 6))
  603.         {
  604.             /* List all the highscores */
  605.             CommandlineHighscorePrint();
  606.             ExitProgramNow(0);
  607.  
  608.         } else if (!compareArgument(argv[i], "-usage", 5))
  609.         {
  610.             /* Print out the usage and quit */
  611.             PrintUsage();
  612.  
  613.         } else if (!compareArgument(argv[i], "-nosfx", 5))
  614.         {
  615.             /* Turn off special effects then */
  616.             useSpecialEffects(False);
  617.             
  618.         } else if (!compareArgument(argv[i], "-nograb", 6))
  619.         {
  620.             /* Turn off pointer grabbing */
  621.             grabPointer = False;
  622.  
  623.         } else if (!compareArgument(argv[i], "-usedefcmap", 10))
  624.         {
  625.             /* Try to use the default colourmap */
  626.             useDefaultColourmap = True;
  627.  
  628.         } else if (!compareArgument(argv[i], "-speed", 5))
  629.         {
  630.             /* Set the speed for the game */
  631.             i++;
  632.             if (i < argc)
  633.             {
  634.                 /* Obtain the speed setting */
  635.                 l = atoi(argv[i]);
  636.                 if ((l > 0) && (l <= 9))
  637.                     SetUserSpeed(10 - l);
  638.                 else 
  639.                 {
  640.                     WarningMessage("The speed setting range is [1-9]");
  641.                     PrintUsage();
  642.                 }
  643.             } else PrintUsage();
  644.  
  645.         } else if (!compareArgument(argv[i], "-startlevel", 10))
  646.         {
  647.             /* Set the starting level */
  648.             i++;
  649.             if (i < argc)
  650.             {
  651.                 /* Obtain the start level setting */
  652.                 l = atoi(argv[i]);
  653.                 if ((l > 0) && (l <= MAX_NUM_LEVELS))
  654.                     SetStartingLevel(l);
  655.                 else 
  656.                 {
  657.                     sprintf(str, "The starting level range is [1-%d]", 
  658.                         MAX_NUM_LEVELS);
  659.                     WarningMessage(str);
  660.                     PrintUsage();
  661.                 }
  662.             } else PrintUsage();
  663.  
  664.         } else if (!compareArgument(argv[i], "-maxvol", 6))
  665.         {
  666.             /* Set the maximum volume to use for the game */
  667.             i++;
  668.             if (i < argc)
  669.             {
  670.                 /* Obtain the maximum volume setting */
  671.                 l = atoi(argv[i]);
  672.                 if ((l >= 1) && (l <= 100))
  673.                     SetMaximumVolume(l);
  674.                 else 
  675.                 {
  676.                     WarningMessage("Maximum volume range is [1-100]");
  677.                     PrintUsage();
  678.                 }
  679.             } else PrintUsage();
  680.  
  681.         } else PrintUsage();
  682.     }
  683. }
  684.  
  685. #if NeedFunctionPrototypes
  686. void UnGrabPointer(Display *display)
  687. #else
  688. void UnGrabPointer(display)
  689.     Display *display;
  690. #endif
  691. {
  692.     /* Ungrab the pointer */
  693.     if (grabPointer == True)
  694.         XUngrabPointer(display, CurrentTime);
  695. }
  696.  
  697. #if NeedFunctionPrototypes
  698. void GrabPointer(Display *display, Window window)
  699. #else
  700. void GrabPointer(display, window)
  701.     Display *display;
  702.     Window window;
  703. #endif
  704. {
  705.     Cursor cursor;
  706.     XColor colour;
  707.     Pixmap cursorPixmap;
  708.  
  709.     /* Create a cursor with nothing inside it */
  710.     colour.pixel = WhitePixel(display, DefaultScreen(display));
  711.     XQueryColor(display, 
  712.         DefaultColormap(display, DefaultScreen(display)), &colour);
  713.     cursorPixmap = XCreatePixmap(display, window, 1, 1, 1);
  714.  
  715.     cursor = XCreatePixmapCursor(display, 
  716.         cursorPixmap, cursorPixmap, &colour, &colour, 0, 0);
  717.     if (cursorPixmap) XFreePixmap(display, cursorPixmap);
  718.  
  719.     if (grabPointer == True)
  720.     {
  721.         /* Grab the pointer so you cannot move the  mouse out of the main
  722.           * window. Also set the cursor to a new cursor with no shape.
  723.           */
  724.         if (XGrabPointer(display, window, True,
  725.             ButtonReleaseMask | ButtonPressMask,
  726.             GrabModeAsync, GrabModeAsync, window,
  727.             cursor, CurrentTime) != GrabSuccess)
  728.         {
  729.             /* Error while grab - not too bad but let user know. */    
  730.             WarningMessage("Pointer grab unsuccessful.");
  731.         }
  732.     }
  733.     else
  734.         XDefineCursor(display, window, cursor);
  735. }
  736.  
  737. #if NeedFunctionPrototypes
  738. Display *InitialiseGame(char **argv, int argc)
  739. #else
  740. Display *InitialiseGame(argv, argc)
  741.     char **argv;
  742.     int argc;
  743. #endif
  744. {
  745.     int screen_num;
  746.     static Display *display;
  747.  
  748.     /* Setup the default speed for game */
  749.     SetUserSpeed(5);
  750.  
  751.     /* Set the paddle control mode to mouse */
  752.     SetPaddleControlMode(CONTROL_MOUSE);
  753.  
  754.     /* Parse all command line arguments - may exit here */
  755.     ParseCommandLine(argv, argc);
  756.  
  757.     DEBUG("Command line parsed.")
  758.  
  759.     /* Open a display connection */
  760.     if (!(display = XOpenDisplay(displayName)))
  761.     {
  762.         /* Handle display connection errors */
  763.         HandleDisplayErrors(displayName);
  764.         ExitProgramNow(1);
  765.     }
  766.  
  767.     /* User wants synchronisation turned on so do it */
  768.     if (syncOn == True) TurnOnSynchronise(display);
  769.  
  770.     /* Set the error handlers to point to mine */
  771.     XSetErrorHandler(ErrorHandler);
  772.  
  773.     /* Seed the random number generator */
  774.     srand(time(NULL));
  775.  
  776.     /* Obtain the screen number for this display */
  777.     screen_num = XDefaultScreen(display);
  778.  
  779.     /* Make sure that we are using a colour visual */
  780.     if (!XMatchVisualInfo(display, screen_num, 
  781.         DefaultDepth(display, screen_num), PseudoColor, &visual_info))
  782.     {
  783.         if (!XMatchVisualInfo(display, screen_num, 
  784.             DefaultDepth(display, screen_num), DirectColor, &visual_info))
  785.         {
  786.             if (!XMatchVisualInfo(display, screen_num, 
  787.                 DefaultDepth(display, screen_num), TrueColor, &visual_info))
  788.             {
  789.                 ErrorMessage("Sorry, you must have a colour display. :-(");
  790.                 ExitProgramNow(1);
  791.             }
  792.         }
  793.     }
  794.  
  795.     DEBUG("Display system checked.")
  796.  
  797.     /* Create our own colour map or use the default one */
  798.     if (useDefaultColourmap == True)
  799.         colormap = XDefaultColormap(display, screen_num);
  800.     else
  801.         colormap = XCreateColormap(display, RootWindow(display, screen_num), 
  802.             visual_info.visual, AllocNone);
  803.  
  804.     DEBUG("Colourmap created.")
  805.  
  806.     /* Be polite */
  807.     NormalMessage("Please wait, initialising xboing ...");
  808.  
  809.     /* Initialise the audio system if possible */
  810.     if (noSound == False)
  811.     {
  812.         /* Try to turn audio on */
  813.         if (SetUpAudioSystem(display) == False)
  814.         {
  815.             /* Audio failed - let user know */
  816.             noSound = True;
  817.             WarningMessage("Audio unavailable or not supported.");
  818.         }
  819.     }
  820.  
  821.     DEBUG("Sound system checked.")
  822.  
  823.     /* Find out some colours */
  824.     InitialiseColourNames(display, colormap);
  825.  
  826.     DEBUG("Initialised colour names.")
  827.  
  828.     /* Create all windows */
  829.     CreateAllWindows(display, colormap, argv, argc);
  830.  
  831.     DEBUG("Created all windows.")
  832.  
  833.     InitialiseGraphics(display, playWindow);
  834.     InitialiseFonts(display);
  835.  
  836.     DEBUG("Created GC and fonts.")
  837.  
  838.     SetBackgrounds(display, colormap);
  839.  
  840.     DEBUG("Created background pixmaps.")
  841.  
  842.     /* Initialise all pixmaps and objects and setup special screens etc. */
  843.     InitialiseMessageSystem(display,     messWindow,     colormap);
  844.     DEBUG("InitialiseMessageSystem done.")
  845.     InitialiseBlocks(display,             playWindow,     colormap);
  846.     DEBUG("InitialiseBlocks done.")
  847.     InitialiseBall(display,             playWindow,     colormap);
  848.     DEBUG("InitialiseBall done.")
  849.     InitialiseBullet(display,             playWindow,     colormap);
  850.     DEBUG("InitialiseBullet done.")
  851.     InitialiseScoreDigits(display,         scoreWindow,     colormap);
  852.     DEBUG("InitialiseScoreDigits done.")
  853.     InitialiseLevelInfo(display,         levelWindow,     colormap);
  854.     DEBUG("InitialiseLevelInfo done.")
  855.     InitialisePaddle(display,             playWindow,     colormap);
  856.     DEBUG("InitialisePaddle done.")
  857.  
  858.     SetUpPresents(display,                 mainWindow,     colormap);
  859.     DEBUG("SetUpPresents done.")
  860.     SetUpKeys(display,                     playWindow,     colormap);
  861.     DEBUG("SetUpKeys done.")
  862.     SetUpInstructions(display,             playWindow,     colormap);
  863.     DEBUG("SetUpInstructions done.")
  864.     SetUpIntroduction(display,             playWindow,     colormap);
  865.     DEBUG("SetUpIntroduction done.")
  866.     SetUpBonus(display,                 mainWindow,     colormap);
  867.     DEBUG("SetUpBonus done.")
  868.     SetUpHighScore(display,             playWindow,     colormap);
  869.     DEBUG("SetUpHighScore done.")
  870.  
  871.     /* Do this after pixmaps all the colourmap */
  872.     InitialiseCycleColourNames(display, colormap);
  873.  
  874.     DEBUG("Colour cycle indexes created.")
  875.  
  876.     DisplayLevelInfo(display, levelWindow, level);
  877.     SetLevelTimeBonus(display, timeWindow, 180);
  878.     DrawSpecials(display);
  879.  
  880.     event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
  881.        ButtonReleaseMask | ExposureMask | StructureNotifyMask;
  882.     XSelectInput(display, mainWindow, event_mask);
  883.  
  884.     event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
  885.        ButtonReleaseMask;
  886.     XSelectInput(display, playWindow, event_mask);
  887.  
  888.     /* Actually map the main window */
  889.     XMapWindow(display, mainWindow);
  890.  
  891.     /* Install our new colormap into the server list */
  892.     XInstallColormap(display, colormap);
  893.  
  894.     DEBUG("Entering main loop.")
  895.  
  896.     /* Return the new display */
  897.     return display;
  898. }
  899.