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.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-08
|
8KB
|
265 lines
/* Author: $Author: jan $
* File: $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/catch.c,v $
* Date: $Date: 1992/09/09 00:09:48 $
* Revision: $Revision: 1.1 $
*/
#include "copyright.h"
/*
** Chase a box around the screen,
** attempting to click the
** mouse in the box.
** Scores are kept of your accuracy
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define DEBUG
#define BOX_SIZE 4 /* in pixels */
#define TEXT_HT 20 /* in pixels - and a kludge */
char WINDOW_NAME[] = "catch";
char ICON_NAME[] = "catch";
Display *display; /* the display device */
int screen; /* the screen on the display */
int theDepth; /* number of color planes */
GC gc; /* graphics context */
Window frame_window; /* holds all the other windows */
Window hit_window; /* the count of hits */
Window miss_window; /* the count of misses */
Window pane_window; /* the background window for
the box to move in */
Window box_window; /* the box to hit with the
cursor */
unsigned long foreground, background;
int hits = 0; /* start off with zero score */
int misses = 0; /* start off with zero score */
int frame_x, frame_y,
frame_width, frame_ht; /* size of frame window */
/*
** function openWindow
*/
Window
openWindow (x, y, width, height, border_width, parent,
istoplevel, argc, argv)
int x, y; /* coords of the upper left
corner in pixels */
int width,
height; /* size of the window in pixels */
int border_width; /* the border width is
not included in the
other dimensions */
Window parent;
int istoplevel; /* a Boolean value */
int argc;
char **argv;
{
Window new_window;
XSizeHints size_hints;
/* now create the window */
new_window = XCreateSimpleWindow (display,
parent,
x, y, width, height,
border_width,
foreground, background);
/* If the window is a toplevel window,
set up the size hints for the window manager */
if (istoplevel)
{
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
/* and state what hints are included */
size_hints.flags = PPosition | PSize;
/* let the window manager know about
the window */
XSetStandardProperties (display, new_window,
WINDOW_NAME, ICON_NAME,
None, /* no icon map */
argv, argc, &size_hints);
}
/* Display the window on the screen */
XMapWindow (display, new_window);
/* Return the window ID */
return (new_window);
}
/*
** function getGC
**
** create a graphics context using default values, and
** return it in the pointer gc
*/
GC
getGC ()
{ GC gc;
gc = XCreateGC (display, frame_window, 0, NULL);
XSetBackground (display, gc, background);
XSetForeground (display, gc, foreground);
return (gc);
}
/*
** function quitX
**
** terminate the program gracefully
*/
quitX ()
{
XCloseDisplay (display);
exit (0);
}
void
doExposeEvent (pEvent)
XExposeEvent *pEvent;
{
if (pEvent->window == hit_window)
{ char hit_str[20];
sprintf (hit_str, "Hits: %d", hits);
XDrawImageString (display, hit_window, gc,
5, 10, hit_str, strlen (hit_str));
}
if (pEvent-> window == miss_window)
{ char miss_str[20];
sprintf (miss_str, "Misses: %d", misses);
XDrawImageString (display, miss_window, gc,
5, 10, miss_str, strlen (miss_str));
}
}
void
doButtonPressEvent (pEvent)
XButtonEvent *pEvent;
{ int box_x, box_y;
if (pEvent->window == pane_window)
{
misses++;
XClearArea (display, miss_window, 0, 0,
0, 0, True);
}
else /* pEvent->window == box_window */
{
hits++;
XClearArea (display, hit_window, 0, 0,
0, 0, True);
}
box_x = rand () % (frame_width - BOX_SIZE);
box_y = rand () % (frame_ht - TEXT_HT - BOX_SIZE);
XMoveWindow (display, box_window, box_x, box_y);
}
void
doKeyPressEvent (pEvent)
XKeyEvent *pEvent;
{ int key_buffer_size = 10;
char key_buffer[64];
XComposeStatus compose_status;
KeySym key_sym;
XLookupString (pEvent, key_buffer, key_buffer_size,
&key_sym, &compose_status);
if (key_buffer[0] == 'q')
{ printf ("Hits: %d; Misses: %d\n",
hits, misses);
quitX ();
}
}
initX ()
{
display = XOpenDisplay (NULL);
screen = DefaultScreen (display);
/* use the default foreground and
background colors */
foreground = BlackPixel (display, screen);
background = WhitePixel (display, screen);
}
main (argc, argv)
int argc;
char **argv;
{ XEvent event;
int box_x, box_y;
initX ();
/* create the frame window to occupy a
large area of the screen,
in the middle */
frame_width = DisplayWidth (display, screen) / 2;
frame_ht = (DisplayHeight (display, screen)*2) / 3;
frame_x = frame_width / 2;
frame_y = frame_ht / 6;
frame_window = openWindow (frame_x, frame_y,
frame_width, frame_ht, 5,
DefaultRootWindow (display),
True, argc, argv);
hit_window = openWindow (0, 0, frame_width / 2,
TEXT_HT,
1, frame_window, False, 0, NULL);
miss_window = openWindow (frame_width / 2, 0,
frame_width / 2, TEXT_HT,
1, frame_window, False, 0, NULL);
pane_window = openWindow (0, TEXT_HT,
frame_width, frame_ht - TEXT_HT,
1, frame_window, False, 0, NULL);
srand (1);
box_x = rand () % (frame_width - BOX_SIZE);
box_y = rand () % (frame_ht - TEXT_HT - BOX_SIZE);
box_window = openWindow (box_x, box_y, BOX_SIZE,
BOX_SIZE, 1,
pane_window, False, 0,
NULL);
gc = getGC ();
XSelectInput (display, frame_window,
KeyPressMask);
XSelectInput (display, pane_window,
ButtonPressMask);
XSelectInput (display, hit_window,
ExposureMask);
XSelectInput (display, miss_window,
ExposureMask);
XSelectInput (display, box_window,
ButtonPressMask);
while (True)
{
XNextEvent (display, &event);
#ifdef DEBUG
fprintf (stderr, "Event number is %d\n",
event.type);
#endif
switch (event.type)
{
case Expose:doExposeEvent (&event);
break;
case ButtonPress:
doButtonPressEvent (&event);
break;
case KeyPress:
doKeyPressEvent (&event);
break;
case MappingNotify:
XRefreshKeyboardMapping (&event);
break;
}
}
}