home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / xap / xgames / xtetris-.6 / xtetris- / xtetris-2.6 / main.c < prev    next >
C/C++ Source or Header  |  1995-02-11  |  10KB  |  259 lines

  1. static char * aboutstring = 
  2. "Copyright 1992, \n\
  3. Dan R. Greening, Didier Tallot, Phill Everson, and Martyn Shortley.\n\
  4. \n\
  5. Permission to use, copy, modify, and distribute this program for any\n\
  6. purpose and without fee is hereby granted, provided that the above\n\
  7. copyright notice and this permission notice appear in all copies, and\n\
  8. that the about box (which contains the copyright and credits the\n\
  9. authors) is retained verbatim.  The names of the authors may not be\n\
  10. used in advertising or publicity pertaining to this document without\n\
  11. specific, written prior permission.  The authors make no\n\
  12. representations about the suitability of this document for any\n\
  13. purpose.  It is provided ``as is'' without expressed or implied\n\
  14. warranty.\n\
  15. \n\
  16. We invite communication.  Since this program is provided free of charge,\n\
  17. please be polite in documenting bugs or missing features.\n\
  18. \n\
  19. E-mail:        <xtetris@cs.ucla.edu>";
  20.  
  21.  
  22. #include <X11/IntrinsicP.h>
  23. #include "defs.h"
  24.  
  25. static XtActionsRec actions[] = {
  26.   {"Refresh", restore_widget},
  27.   {"ShowScore", show_score},
  28.   {"Quit", quit_proc},
  29.   {"Done", done_proc},
  30.   {"EndGame", end_game},
  31.   {"NewGame", newgame_proc},
  32.   {"Resume", resume_proc},
  33.   {"Start", start_proc},
  34.   {"Pause", pause_proc},
  35.   {"MoveLeft", left_proc},
  36.   {"MoveRight", right_proc},
  37.   {"RotateCW", clock_proc},
  38.   {"RotateCCW", anti_proc},
  39.   {"Drop", fast_proc},
  40.   {"About", about_proc},
  41.   {"Scores", print_high_scores},
  42. };
  43.  
  44. #define offset(field) XtOffset(struct resource_struct *, field)
  45. #define soffset(field) XtOffset(struct shape_table *, field)
  46.  
  47. static XtResource Resources[] = {
  48.   {"foreground",    "Foreground",    XtRPixel,    sizeof(Pixel),
  49.      offset(foreground),    XtRString,    XtDefaultForeground},
  50.   {"background",    "Background",    XtRPixel,    sizeof(Pixel),
  51.      offset(background),    XtRString,    XtDefaultBackground},
  52.   {"eraseStipple","Bitmap",    XtRBitmap,    sizeof(Pixmap),
  53.      offset(erasestipple),    XtRString,    "gray"},
  54.   {"useScoreFile",    "Boolean",    XtRBoolean,    sizeof(Boolean),
  55.      offset(usescorefile),    XtRImmediate,    (XtPointer)True},
  56.   {"customization",    "Customization",    XtRString,    sizeof(String),
  57.      offset(customization),    XtRString,    ".bw"},
  58.   {"boxSize",    "BoxSize",    XtRDimension,    sizeof(Dimension),
  59.      offset(boxsize),            XtRImmediate,    (XtPointer)16},
  60.   {"speed",       "Speed",        XtRDimension,   sizeof(Dimension),
  61.      offset(speed),             XtRImmediate,      (XtPointer)STANDARD_SPEED},
  62.   {"scorefile",    "ScoreFile",   XtRString,      sizeof(String),
  63.      offset(scorefile),         XtRString,      HIGH_SCORE_TABLE},
  64.   {"quayle",    "Boolean",   XtRBoolean,      sizeof(Boolean),
  65.      offset(quayle),         XtRImmediate,      (XtPointer)False},
  66. };
  67.   
  68. static XrmOptionDescRec Options[] = {
  69.   {"-score",    "useScoreFile",               XrmoptionNoArg,    "TRUE"},
  70.   {"-noscore",    "useScoreFile",               XrmoptionNoArg,    "FALSE"},
  71.   {"-speed",    "speed",                      XrmoptionSepArg,  NULL },
  72.   {"-boxsize",  "boxSize",                    XrmoptionSepArg,  NULL },
  73.   {"-color",    "customization",          XrmoptionNoArg,    ".c" },
  74.   {"-bw",    "customization",          XrmoptionNoArg,    ".bw" },
  75.   {"-quayle",   "quayle",                     XrmoptionNoArg,   "TRUE" }
  76. };
  77.  
  78. static XtResource ShapeResources[] = {
  79.   {"foreground",  "Foreground",   XtRPixel,       sizeof(Pixel),
  80.      soffset(foreground),         XtRString,      XtDefaultForeground},
  81.   {"background",    "Background",    XtRPixel,    sizeof(Pixel),
  82.      soffset(background),    XtRString,    XtDefaultBackground},
  83. };
  84. static void usage() {
  85.     printf( "usage: xtetris [-score|-noscore] [-boxsize size] [-speed value]\n"
  86.         "               [-color|-bw] [-quayle]\n" );
  87. }
  88.  
  89. static char * fallback_resources[] = {
  90.   "customization: .bw",
  91.   "*TitleBar.Font:    -*-clean-*-*-*-*-10-*-*-*-*-*-*-*",
  92.   "*TitleBar.Label:    XTETRIS REQUIRES INSTALLATION OF APP-DEFAULTS OR SET YOUR XUSERFILESEARCHPATH TO .../xtetris-dir/%N%C.ad",
  93.   "*TitleBar.height:    60",
  94.   "*TitleBar.width:    700",
  95.   NULL,
  96. };
  97.  
  98. #include <X11/Xaw/Text.h>
  99. #include <X11/Xaw/AsciiText.h>
  100. #include <X11/Xaw/Command.h>
  101. #include <X11/Xaw/Toggle.h>
  102. #include <X11/Xaw/Simple.h>
  103. #include <X11/Xaw/Form.h>
  104. #include <X11/Xaw/Label.h>
  105. #include <X11/Xaw/Box.h>
  106.  
  107. int main(argc, argv, envp)
  108.   int     argc;
  109.   char  **argv;
  110.   char  **envp;
  111. {
  112.   int     i, j;
  113.   Widget 
  114.     status, buttons, nextobjectlabel,
  115.     tbar, scores_bt, quit_bt, about_bt;
  116.   static XtConvertArgRec screenConvertArg[] = {
  117.     {XtWidgetBaseOffset, (caddr_t) XtOffset(Widget, core.screen),
  118.        sizeof(Screen *)}
  119.   };
  120.   XVisualInfo vi;
  121.   int screenno;
  122.   Display * display;
  123.   XrmDatabase db;
  124.   
  125.   programname = argv[0];
  126.   XtToolkitInitialize();
  127.   context = XtCreateApplicationContext();
  128.   display = XtOpenDisplay( context, NULL, NULL, "Xtetris", Options, XtNumber(Options),
  129.         &argc, argv );
  130.   if (argc != 1) {
  131.     usage();
  132.     exit(1);
  133.   }
  134.  
  135.   /* Get a default customization. */
  136.  
  137.   screenno = DefaultScreen(display);
  138.   if (XMatchVisualInfo( display, screenno, 4, PseudoColor, &vi )
  139.       || XMatchVisualInfo( display, screenno, 4, StaticColor, &vi )
  140.       || XMatchVisualInfo( display, screenno, 6, DirectColor, &vi )
  141.       || XMatchVisualInfo( display, screenno, 6, TrueColor, &vi ))
  142.   {
  143.     resources.customization = ".c";
  144.   }
  145.   else
  146.   {
  147.     resources.customization = ".bw";
  148.   }
  149.  
  150.   db = XtDatabase(display);
  151.   XrmPutStringResource( &db, "customization", resources.customization );
  152.   
  153.   toplevel = XtVaAppCreateShell( NULL, "Xtetris",
  154.                 applicationShellWidgetClass,
  155.                 display,
  156.                 NULL );
  157.     
  158.   XtAppAddConverter( context, "String", "Bitmap", XmuCvtStringToBitmap, 
  159.             screenConvertArg, XtNumber(screenConvertArg) );
  160.   XtGetApplicationResources( toplevel, (caddr_t) &resources,
  161.                 Resources, XtNumber(Resources),
  162.                 NULL, (Cardinal) 0);
  163.   XtAppAddActions( context, actions, XtNumber(actions) );
  164.   
  165.   if (resources.quayle) resources.usescorefile = False;
  166.  
  167.   for (i = 0; i < 8; i++) {
  168.     static char *names[] = { "object0","object1","object2","object3","object4",
  169.                  "object5","object6", "objectQ" };
  170.     XtGetSubresources( toplevel, (caddr_t) &shape[i], names[i], "Object",
  171.               ShapeResources, XtNumber(ShapeResources),
  172.               NULL, (Cardinal) 0 );
  173.   }
  174.   frame = XtVaCreateManagedWidget("Frame", formWidgetClass, toplevel, NULL );
  175.   tbar = XtVaCreateManagedWidget("TitleBar", labelWidgetClass, frame, NULL );
  176.   status = XtVaCreateManagedWidget("Status", boxWidgetClass, frame, NULL );
  177.   score_item = XtVaCreateManagedWidget("Score", labelWidgetClass, status, NULL );
  178.   level_item = XtVaCreateManagedWidget("Level", labelWidgetClass, status, NULL );
  179.   rows_item = XtVaCreateManagedWidget("Rows", labelWidgetClass, status, NULL );
  180.   game_over = XtVaCreateManagedWidget("Game", labelWidgetClass, status, NULL );
  181.   
  182.   buttons = XtVaCreateManagedWidget("Buttons", formWidgetClass, frame, NULL );
  183.   start_bt = XtVaCreateManagedWidget("Start", commandWidgetClass, buttons, NULL );
  184.   pause_bt = XtVaCreateManagedWidget("Pause", commandWidgetClass, buttons, NULL );
  185.   newgame_bt = XtVaCreateManagedWidget("NewGame", commandWidgetClass, buttons, NULL );
  186.   quit_bt = XtVaCreateManagedWidget("Quit", commandWidgetClass, buttons, NULL );
  187.   about_bt = XtVaCreateManagedWidget("About", commandWidgetClass, buttons, NULL );
  188.   if (resources.usescorefile)
  189.     scores_bt = XtVaCreateManagedWidget("Scores", commandWidgetClass, buttons, NULL );
  190.   canvas = XtVaCreateManagedWidget( "Canvas", simpleWidgetClass, frame, 
  191.                    XtNwidth, (XtArgVal)(10*resources.boxsize),
  192.                    XtNheight, (XtArgVal)(30*resources.boxsize),
  193.                    NULL );
  194.   shadow = XtVaCreateManagedWidget("Shadow", simpleWidgetClass, frame, 
  195.                    XtNwidth, (XtArgVal)(10*resources.boxsize),
  196.                    XtNheight, (XtArgVal)resources.boxsize,
  197.                    NULL );
  198.   nextobjectlabel = XtVaCreateManagedWidget("NextObjectLabel", labelWidgetClass, frame, NULL );
  199.   nextobject = XtVaCreateManagedWidget("NextObject", simpleWidgetClass, frame,
  200.                    XtNwidth, (XtArgVal)(4*resources.boxsize),
  201.                    XtNheight, (XtArgVal)(4*resources.boxsize),
  202.                    NULL );
  203.   
  204.   if (resources.usescorefile) 
  205.   {
  206.     score_frame = XtVaCreatePopupShell("ScoreFrame", transientShellWidgetClass, toplevel, 
  207.                        XtNtransientFor, (XtArgVal)toplevel, 
  208.                        NULL ); 
  209.     score_text = XtVaCreateManagedWidget( "ScoreText", asciiTextWidgetClass, score_frame, NULL );
  210.   }
  211.   about_frame = XtVaCreatePopupShell("AboutFrame", transientShellWidgetClass, toplevel,
  212.                      XtNtransientFor, (XtArgVal)toplevel, 
  213.                      NULL ); 
  214.   about_text = XtVaCreateManagedWidget( "AboutText", asciiTextWidgetClass, about_frame, 
  215.                        XtNstring, (XtArgVal)aboutstring,
  216.                        XtNlength, (XtArgVal)(strlen(aboutstring)+1),
  217.                        NULL );
  218.   XtInstallAllAccelerators( canvas, toplevel );
  219.   XtInstallAllAccelerators( shadow, toplevel );
  220.   XtInstallAllAccelerators( nextobject, toplevel );
  221.   XtInstallAllAccelerators( frame, toplevel );
  222.   XtRealizeWidget(toplevel);
  223.     
  224.   XtMapWidget(toplevel);
  225.   
  226.   {
  227.     XGCValues gcv;
  228.     
  229.     gcv.foreground = resources.foreground;
  230.     gcv.background = resources.background;
  231.     gc = XCreateGC( XtDisplay(toplevel),XtWindow(toplevel), 
  232.            (unsigned long) GCForeground|GCBackground, &gcv );
  233.     
  234.     gcv.foreground = resources.background;
  235.     gcv.background = resources.foreground;
  236.     gcv.fill_style = FillStippled;
  237.     gcv.stipple = resources.erasestipple;
  238.     erasegc = XCreateGC( XtDisplay(toplevel),XtWindow(toplevel), 
  239.             (unsigned long) GCForeground|GCBackground|GCStipple|GCFillStyle, &gcv );
  240.   }
  241.   initialise(); 
  242.   
  243.   XtAppMainLoop( context );
  244. }
  245. /*
  246. emacs mode: indented-text
  247.  
  248. emacs Local Variables: 
  249. emacs mode: c 
  250. emacs c-indent-level: 2
  251. emacs c-continued-statement-offset: 2
  252. emacs c-continued-brace-offset: -2
  253. emacs c-tab-always-indent: nil
  254. emacs c-brace-offset: 0 
  255. emacs tab-width: 8
  256. emacs tab-stop-list: (2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84)
  257. emacs End:
  258. */
  259.