home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmarch.zip / CATCH_IC.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  8KB  |  277 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/catch_icon.c,v $
  3.  * Date:     $Date: 1992/09/09 00:09:49 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. /*     
  10. ** Chase a box around the screen, 
  11. ** attempting to click the    
  12. ** mouse in the box.    
  13. ** Scores are kept of your accuracy    
  14. */    
  15.     
  16. #include <stdio.h>    
  17. #include <X11/Xlib.h>    
  18. #include <X11/Xutil.h>    
  19.  
  20. #include "catch_icon.h"
  21.     
  22. #define DEBUG    
  23.     
  24. #define BOX_SIZE 4  /* in pixels */   
  25. #define TEXT_HT 20   /* in pixels - and a kludge */ 
  26. char WINDOW_NAME[] = "catch";    
  27. char ICON_NAME[] = "catch";    
  28.     
  29. Display *display;      /* the display device */    
  30. int     screen;        /* the screen on the display */
  31. int     theDepth;      /* number of color planes */  
  32. GC      gc;            /* graphics context */    
  33.     
  34. Window frame_window;   /* holds all the other windows */
  35. Window hit_window;     /* the count of hits */    
  36. Window miss_window;    /* the count of misses */    
  37. Window pane_window;    /* the background window for
  38.                          the box to move in */    
  39. Window box_window;     /* the box to hit with the
  40.                           cursor */    
  41. unsigned long  foreground, background;    
  42. int hits = 0;          /* start off with zero score */
  43. int misses = 0;        /* start off with zero score */  
  44.     
  45. int frame_x, frame_y, 
  46.     frame_width, frame_ht;  /* size of frame window */    
  47.  
  48. Pixmap icon_map;
  49.  
  50. /*    
  51. ** function openWindow    
  52. */    
  53.     
  54. Window    
  55. openWindow (x, y, width, height, border_width, parent,
  56.             istoplevel, argc, argv)    
  57.     int x, y;  /* coords of the upper left 
  58.                   corner in pixels */    
  59.     int width,    
  60.             height;  /* size of the window in pixels */    
  61.     int border_width;  /* the border width is
  62.                           not included in the    
  63.                           other dimensions */    
  64.     Window parent;   
  65.     int istoplevel;  /* a Boolean value */    
  66.     int argc;    
  67.     char **argv;    
  68. {    
  69.     Window  new_window;    
  70.     XSizeHints  size_hints;    
  71.         
  72.     /* now create the window */    
  73.     new_window = XCreateSimpleWindow (display,     
  74.                 parent,    
  75.                 x, y, width, height,    
  76.                 border_width,    
  77.                 foreground, background);    
  78.     
  79.     /* If the window is a toplevel window,    
  80.        set up the size hints for the window manager */    
  81.     if (istoplevel)    
  82.     {    
  83.           size_hints.x = x;    
  84.           size_hints.y = y;    
  85.           size_hints.width = width;    
  86.           size_hints.height = height;    
  87.           /* and state what hints are included */    
  88.           size_hints.flags = PPosition | PSize;    
  89.  
  90.       /* create the icon */
  91.       icon_map = XCreateBitmapFromData (display,
  92.                     parent,
  93.                     catch_icon_bits,
  94.                     catch_icon_width,
  95.                     catch_icon_height);
  96.         
  97.           /* let the window manager know about
  98.              the window */    
  99.           XSetStandardProperties (display, new_window,
  100.                 WINDOW_NAME, ICON_NAME,    
  101.                 icon_map,  /* the icon map */    
  102.                 argv, argc, &size_hints);    
  103.     }    
  104.     /* Display the window on the screen */    
  105.     XMapWindow (display, new_window);    
  106.     
  107.     /* Return the window ID */    
  108.     return (new_window);    
  109. }    
  110.     
  111. /*    
  112. ** function getGC    
  113. **    
  114. ** create a graphics context using default values, and
  115. ** return it in the pointer gc    
  116. */    
  117. GC
  118. getGC ()    
  119. {   GC gc;
  120.     
  121.     gc = XCreateGC (display, frame_window, 0, NULL);  
  122.  
  123.     XSetBackground (display, gc, background);    
  124.     XSetForeground (display, gc, foreground);    
  125.     return (gc);
  126. }    
  127.     
  128. /*    
  129. ** function quitX    
  130. **    
  131. ** terminate the program gracefully    
  132. */    
  133. quitX ()    
  134. {    
  135.     XCloseDisplay (display);    
  136.     exit (0);    
  137. }    
  138.     
  139. void    
  140. doExposeEvent (pEvent)    
  141.     XExposeEvent *pEvent;    
  142. {    
  143.     if  (pEvent->window == hit_window)    
  144.     {      char hit_str[20];    
  145.     
  146.           sprintf (hit_str, "Hits: %d", hits);    
  147.           XDrawImageString (display, hit_window, gc,    
  148.                 5, 10, hit_str, strlen (hit_str));    
  149.     }    
  150.     if (pEvent-> window == miss_window)    
  151.     {      char miss_str[20];    
  152.     
  153.           sprintf (miss_str, "Misses: %d", misses);    
  154.           XDrawImageString (display, miss_window, gc,
  155.                 5, 10, miss_str, strlen (miss_str));    
  156.     }    
  157. }    
  158.     
  159. void
  160. doButtonPressEvent (pEvent)    
  161.     XButtonEvent *pEvent;    
  162. {   int box_x, box_y;    
  163.     
  164.     if (pEvent->window == pane_window)    
  165.     {    
  166.           misses++;    
  167.           XClearArea (display, miss_window, 0, 0,     
  168.                 0, 0, True);    
  169.     }    
  170.     else /* pEvent->window == box_window */    
  171.     {    
  172.           hits++;    
  173.           XClearArea (display, hit_window, 0, 0,    
  174.                 0, 0, True);    
  175.     }    
  176.     box_x = rand () % (frame_width - BOX_SIZE);    
  177.     box_y = rand () % (frame_ht - TEXT_HT - BOX_SIZE); 
  178.     XMoveWindow (display, box_window, box_x, box_y);    
  179. }    
  180.     
  181. void    
  182. doKeyPressEvent (pEvent)    
  183.     XKeyEvent *pEvent;    
  184. {   int key_buffer_size = 10;    
  185.     char key_buffer[64];    
  186.     XComposeStatus compose_status;    
  187.     KeySym key_sym;    
  188.     
  189.     XLookupString (pEvent, key_buffer, key_buffer_size, 
  190.           &key_sym, &compose_status);    
  191.     if (key_buffer[0] == 'q')    
  192.     {      printf ("Hits: %d; Misses: %d\n", 
  193.                    hits, misses);    
  194.           quitX ();    
  195.     }    
  196. }    
  197.     
  198. initX () 
  199. {    
  200.     display = XOpenDisplay (NULL);    
  201.     screen = DefaultScreen (display);    
  202.     /* use the default foreground and 
  203.        background colors */
  204.     foreground = BlackPixel (display, screen);    
  205.     background = WhitePixel (display, screen);    
  206. }    
  207. main (argc, argv)    
  208.     int argc;    
  209.     char **argv;    
  210. {   XEvent event;    
  211.     int box_x, box_y;    
  212.     
  213.     initX ();
  214.     
  215.     /* create the frame window to occupy a 
  216.        large area of the screen,    
  217.        in the middle */    
  218.     frame_width = DisplayWidth (display, screen) / 2; 
  219.     frame_ht = (DisplayHeight (display, screen)*2) / 3;
  220.     frame_x = frame_width / 2;    
  221.     frame_y = frame_ht / 6;    
  222.     frame_window = openWindow (frame_x, frame_y,
  223.                       frame_width, frame_ht, 5,
  224.                       DefaultRootWindow (display),    
  225.                       True, argc, argv);    
  226.     hit_window = openWindow (0, 0, frame_width / 2,
  227.                       TEXT_HT,
  228.                       1, frame_window, False, 0, NULL);
  229.     miss_window = openWindow (frame_width / 2, 0,     
  230.                       frame_width / 2, TEXT_HT,    
  231.                       1, frame_window, False, 0, NULL);
  232.     pane_window = openWindow (0, TEXT_HT,    
  233.                       frame_width, frame_ht - TEXT_HT, 
  234.                       1, frame_window, False, 0, NULL);
  235.     srand (1);    
  236.     box_x = rand () % (frame_width - BOX_SIZE);    
  237.     box_y = rand () % (frame_ht - TEXT_HT - BOX_SIZE);
  238.     box_window = openWindow (box_x, box_y, BOX_SIZE,
  239.                       BOX_SIZE, 1,
  240.                       pane_window, False, 0, 
  241.                       NULL);
  242.     gc = getGC ();     
  243.     XSelectInput (display, frame_window,    
  244.           KeyPressMask);    
  245.     XSelectInput (display, pane_window,    
  246.           ButtonPressMask);    
  247.     XSelectInput (display, hit_window,    
  248.           ExposureMask);    
  249.     XSelectInput (display, miss_window,    
  250.                        ExposureMask);    
  251.     XSelectInput (display, box_window,    
  252.                        ButtonPressMask);    
  253.         
  254.     while (True)    
  255.     {    
  256.           XNextEvent (display, &event);    
  257. #ifdef DEBUG    
  258.           fprintf (stderr, "Event number is %d\n",
  259.                    event.type);    
  260. #endif    
  261.           switch (event.type)    
  262.           {    
  263.           case Expose:doExposeEvent (&event);    
  264.                       break;   
  265.           case ButtonPress:    
  266.                       doButtonPressEvent (&event);    
  267.                       break;   
  268.           case KeyPress:
  269.                       doKeyPressEvent (&event);    
  270.                       break;    
  271.           case MappingNotify:    
  272.                        XRefreshKeyboardMapping (&event);
  273.                       break;    
  274.           }    
  275.     }    
  276.