home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xpool-10.zip / Xpool / event.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  13KB  |  499 lines

  1.  
  2.  
  3.  
  4. /* I. ARIT 1992 Hidirbeyli,AYDIN,TR.  09400
  5.                 Golden,    CO,   USA. 80401
  6.  
  7.  
  8.   Copyright (C) 1992 Ismail ARIT
  9.  
  10.   This file  is distributed in the hope that it will be useful,
  11.   but without any warranty.  No author or distributor accepts
  12.   responsibility to anyone for the consequences of using it or for
  13.   whether it serves any particular purpose or works at all.
  14.  
  15.  
  16.   Everyone is granted permission to copy, modify and redistribute
  17.   this file under the following conditions:
  18.  
  19.      Permission is granted to anyone to make or distribute copies
  20.      of the source code, either as received or modified, in any
  21.      medium, provided that all copyright notices, permission and
  22.      nonwarranty notices are preserved, and that the distributor
  23.      grants the recipient permission for further redistribution as
  24.      permitted by this document.
  25.      No part of this program can be used in any commercial product.
  26.  
  27.  
  28. */
  29.  
  30.  
  31.  
  32.  
  33. #include   <math.h>
  34. #include    <X11/Xlib.h>
  35. #include   <stdio.h>
  36. #include    "definition.h"
  37.  
  38. int     WhoseTurn = MYTURN;
  39.  
  40.  
  41. extern  Display * disp;
  42. extern  GC gc;
  43. extern  GC XorGc;
  44.  
  45.  
  46. extern  Window main_window,
  47.         Pane,
  48.         exitbutton,
  49.         newgame,
  50.         Yourscoreboard,
  51.         Myscoreboard,
  52.         WhoseTurnWindow;
  53. extern  Window MyPlus,
  54.         MyMinus,
  55.         YourPlus,
  56.         YourMinus;
  57. extern  Window TraJectory;
  58.  
  59.  
  60. extern int  WhiteBallOut,
  61.             BlackBallOut;
  62. extern int  YourScore,
  63.             MyScore;
  64. extern int  StartX,
  65.             StartY,
  66.             NewX,
  67.             NewY;
  68. extern  void PutIn (Window win, char *string, GC gc, int fg, int bg);
  69. extern void RubberLineOnPane (int type,int x, int y, int xx, int yy, int color);
  70. extern  void line (int x, int y, int xx, int yy);
  71. extern  get_color (char *name);
  72. extern  setbkcolor (int color);
  73. extern Distance * DistanceChecker_Between (Ball * ThisBall, Ball * ThatBall);
  74.  
  75.  
  76.  
  77.  
  78. extern  Stick * PoolStick;
  79. extern  Stick * Trajectory;
  80.  
  81. int WantsTrajectory =NO ;
  82.  
  83.  
  84. int     OldSx = 0,
  85.         OldSy = 0,
  86.         OldEx = 0,
  87.         OldEy = 0;
  88.  
  89. extern unsigned long    fg,
  90.                         bg,
  91.                         bd;
  92.  
  93. extern  XFontStruct * fontstruct;/* Font descriptor */
  94.  
  95.  
  96. extern  Coordinator * Dept_Head;
  97.  
  98.  
  99.  
  100.  
  101. extern  void LayTheFloor (void);
  102.  
  103. int     ButtonPressedBefore = NO;
  104. int     main_x,
  105.         main_y,
  106.         pane_x,
  107.         pane_y;
  108. unsigned int    buttons;
  109. char    Message[20] = {
  110.     "Ready"
  111. };                /* just for a short message */
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. int     MyBallsOut () {
  120.     int     i;
  121.     int     InList = 0;
  122.     for (i = 2; i < 9; i++)
  123.     if (Dept_Head -> Objects[i] -> In)
  124.         InList++;
  125.     return ((InList) ? NO : YES);
  126.  
  127. }
  128.  
  129. int     YourBallsOut () {
  130.     int     i;
  131.     int     InList = 0;
  132.     for (i = 9; i < 16; i++)
  133.     if (Dept_Head -> Objects[i] -> In)
  134.         InList++;
  135.     return ((InList) ? NO : YES);
  136. }
  137.  
  138.  
  139. void ShowWhatHappenned (char *message) {
  140.  PutIn (WhoseTurnWindow, message, gc, get_color (PANE_BG), get_color (PANE_FG));
  141. };
  142.  
  143. void handle_scratch(XEvent event);/* defined down here somewhere */
  144.                              /* need to use in handle_stick() */
  145.  
  146. handle_stick(XEvent event)
  147. {
  148.  
  149.     double  hip;
  150.     double  lenght;
  151.     double  Speed;
  152.     int i;
  153.     int Trax,Tray,TraEndx,TraEndy;
  154.  
  155.                Trax= StartX = (int) Dept_Head -> Objects[0] -> CurrentL.x;
  156.                Tray= StartY = (int) Dept_Head -> Objects[0] -> CurrentL.y;
  157.  
  158.                 NewX = pane_x;
  159.                 NewY = pane_y;
  160.                TraEndx = Trax + (Trax - NewX);
  161.                TraEndy = Tray + (Tray - NewY);
  162.  
  163. /* we don't want to draw on the ball */
  164.                 hip = sqrt ((float) ((NewX - StartX) * (NewX - StartX) + (NewY -
  165.  StartY) *
  166.                             (NewY - StartY)));
  167.  
  168.                 StartX += (int) ((float) (NewX - StartX) / hip
  169.                         * (Dept_Head -> Objects[0] -> radius + 10));
  170.  
  171.                 StartY += (int) ((float) (NewY - StartY) / hip
  172.                         * (Dept_Head -> Objects[0] -> radius + 10));
  173.  
  174.  
  175.  
  176.                 /* 256 button1 */
  177.                 if (buttons == 256) {
  178.  
  179.                     PoolStick -> MoveTo (PoolStick, StartX, StartY, NewX, NewY);
  180.                     PoolStick -> DoneWithTheStick (PoolStick, NO);
  181.                  if(WantsTrajectory==YES){
  182.                     Trajectory-> MoveTo (Trajectory,Trax,Tray,TraEndx,TraEndy);
  183.                     Trajectory-> DoneWithTheStick (Trajectory, NO);}
  184.  
  185.                     ButtonPressedBefore = YES;
  186.  
  187.                 }
  188.                 else
  189.                     if (buttons == 0 && ButtonPressedBefore) {
  190.  
  191.                         /* here we are done with the stick, hit the ball
  192.                            now */
  193.                         PoolStick -> DoneWithTheStick (PoolStick, YES);
  194.                         PoolStick -> Show (PoolStick);
  195.                      if(WantsTrajectory==YES){
  196.                         Trajectory-> DoneWithTheStick (Trajectory,YES);
  197.                         Trajectory->Show(Trajectory);} 
  198.                         ButtonPressedBefore = NO;
  199.  
  200.                         lenght = PoolStick -> Lenght (PoolStick);
  201.  
  202.               Speed = lenght * (float) SPEEDLIMIT / (float) MAXSTICKLENGHT;
  203.                         Dept_Head -> Objects[0] -> CurrentV.x = -(float) ((NewX
  204. - StartX) / hip)
  205.                             * Speed;
  206.                         Dept_Head -> Objects[0] -> CurrentV.y = -(float) ((NewY
  207. - StartY) / hip)
  208.                             * Speed;
  209.                         Dept_Head -> Objects[0] -> Stopped = NO;
  210.   sprintf (Message, "%s","Running");
  211.   ShowWhatHappenned (Message);
  212.   setbkcolor (get_color (PANE_BG));/* one more quick fix */
  213.                         Dept_Head -> StartAction (Dept_Head);
  214.   sprintf (Message, "%s","Ready");
  215.   ShowWhatHappenned (Message);
  216.  
  217.  
  218. /* let's see what the hell happenned  */
  219.                         if (WhiteBallOut) {
  220.                             sprintf (Message, "%s", "Scrach");
  221.                             ShowWhatHappenned (Message);
  222.                             handle_scratch(event);
  223.                             sprintf (Message, "%s","Ready");
  224.                             ShowWhatHappenned (Message);
  225.                         }
  226.  
  227.                         if (BlackBallOut)
  228.                            { Dept_Head -> InitConfig (Dept_Head);
  229.                         setbkcolor (get_color (PANE_BG));}
  230.                     }
  231.  
  232.  
  233.  
  234. };
  235.  
  236.  
  237.  handle_white_ball(XEvent event)
  238. {
  239. if((pane_x > WHITE_BALL_LIMIT) && (buttons==256)) 
  240. { Dept_Head -> Objects[0]->Hide(Dept_Head -> Objects[0]);
  241.  
  242.  Dept_Head -> Objects[0] -> CurrentL.x = pane_x;
  243.  Dept_Head -> Objects[0] -> CurrentL.y = pane_y;
  244.  
  245.  Dept_Head -> Objects[0]->Show(Dept_Head -> Objects[0]);
  246. }
  247. /* we need this sucker to avoid fast movement of the mouse /* 
  248.     PoolStick -> Old.x = PoolStick -> New.x = 1000;/* some far place*/
  249.     PoolStick -> Old.xx = PoolStick -> New.xx = 1000;
  250.     PoolStick -> Old.y = PoolStick -> New.y = 1000;
  251.     PoolStick -> Old.yy = PoolStick -> New.yy = 1000;
  252.  
  253. };
  254.  
  255.  
  256. void handle_scratch(XEvent event)
  257. {
  258.  
  259.      int     j,Done=NO,
  260.             i,TryAgain=YES;
  261.     Distance * theDistance;
  262. while(TryAgain){
  263.          TryAgain=NO;
  264. /* if random()  doesn't work you, fix something  for your machine */
  265. /* you only need to pick two random numbers for white ball position*/
  266.  
  267. srandom(random());/* it looks like working on my machine ( DEC 3100 ) */
  268.  
  269. do{ /* this thing sets the x position somewhere between 500 & 650 */
  270. Dept_Head->Objects[0]->CurrentL.x = (float)(random()& 0x02FF) ;
  271.  if(Dept_Head->Objects[0]->CurrentL.x > 550 &&
  272.     Dept_Head->Objects[0]->CurrentL.x < 650)
  273.    Done=YES;
  274. }while(!Done);
  275.  
  276. Done=NO; /* for the next pick */
  277.  
  278. do{ /* this thing sets the y position somewhere between 20 & 400 */
  279. Dept_Head->Objects[0]->CurrentL.y = (float)(random()& 0x01FF) ;
  280.    if(Dept_Head->Objects[0]->CurrentL.y > 20 && 
  281.       Dept_Head->Objects[0]->CurrentL.y < 400)
  282.      Done=YES;
  283. }while(!Done);
  284.  
  285. dprintf(" %f  %f   \n",Dept_Head->Objects[0]->CurrentL.x,
  286.                       Dept_Head->Objects[0]->CurrentL.y);
  287.  
  288. /* let's see if this sucker overlaps */
  289.     for (i = 1; i < Dept_Head -> NumberOfObjects ; i++){
  290. theDistance=DistanceChecker_Between(Dept_Head->Objects[0],Dept_Head->Objects[i]);
  291.             if (theDistance -> amount > 0) 
  292.               TryAgain = YES; /* this sucker overlaps, try again */
  293.         }
  294. }
  295.  
  296. /* time to refresh ourself and start movin' */
  297. Dept_Head->Objects[0]->In = YES;
  298. WhiteBallOut = NO;
  299. Dept_Head->Objects[0]->Stopped = NO;
  300. Dept_Head->Objects[0]->ShowOut = NO;
  301. Dept_Head->Objects[0]->Visible = NO;
  302. Dept_Head->Objects[0]->NeedOneMoreDrawing = YES;
  303. setbkcolor(get_color(PANE_BG));/* another quick fix */
  304. Dept_Head->Objects[0]->Show(Dept_Head->Objects[0]);
  305.  
  306. };
  307.  
  308.  
  309. take_event () {
  310.     XEvent event;
  311.     Window win;
  312.     Window parent, current;
  313.     XButtonPressedEvent * button_event;
  314.     XExposeEvent * expose_event;
  315.     double  hip;
  316.     double  lenght;
  317.     double  Speed;
  318.     char    buf[15];
  319.     int     i;
  320.     float howFar;
  321.     XNextEvent (disp, &event);
  322.     switch (event.type) {
  323.  
  324.     case ButtonPress: 
  325.         {
  326.         button_event = (XButtonPressedEvent *) & event;
  327.         /* handle event here */
  328.         /* check windows and determine which option wanted */
  329.         press_handle (button_event);
  330.  
  331.         }
  332.         break;
  333.  
  334.  
  335.     case MotionNotify: 
  336.         {
  337.                  /* let's find where the mouse is..*/
  338.         if (!XQueryPointer (disp, event.xmotion.window,
  339.                 &parent, ¤t, &main_x, &main_y, &pane_x,
  340.                 &pane_y, &buttons))
  341.             break;
  342. /* let's see what the hell player wants to do..*/
  343.  if(sqrt((double)((pane_x - (int)Dept_Head -> Objects[0] ->CurrentL.x) *
  344.                   (pane_x - (int)Dept_Head -> Objects[0] ->CurrentL.x) 
  345.            +
  346.                   (pane_y - (int)Dept_Head -> Objects[0] ->CurrentL.y) *
  347.                   (pane_y - (int)Dept_Head -> Objects[0] ->CurrentL.y))
  348.         ) < Dept_Head -> Objects[0] ->radius ) 
  349.     handle_white_ball(event);/* wants to move the white ball*/
  350.    else   
  351.     handle_stick( event);/* wants to move stick */ 
  352. /* note that if you move your mouse fast, blt will not be able to catch up
  353.    with you, so you are gonna end up moving the stick not the white ball. */
  354.         }
  355.         break;
  356.  
  357.  
  358.     case ButtonRelease:
  359.  /* now you are done with the stick one more MotionNotify needed to inform 
  360.     handle_stick()  */ 
  361.         if (ButtonPressedBefore) {
  362.         event.type = MotionNotify;
  363.         XSendEvent (disp, Pane, True, 0, &event);
  364.         }
  365.  
  366.         break;
  367.  
  368.     case Expose: 
  369.         {
  370.         expose_event = (XExposeEvent *) & event;
  371.         expose_window (expose_event);
  372.         }
  373.         break;
  374.  
  375.  
  376.     default: 
  377.         dprintf ("unknown option..!\n");
  378.         break;
  379.     }                /* end of swithc */
  380. }
  381.  
  382.  
  383.  
  384.  
  385. press_handle (press_event)
  386. XButtonPressedEvent * press_event;
  387. {
  388.     Window win;
  389.     int     i;
  390.     char    buf[15];
  391.  
  392.     win = press_event -> window;
  393.     if (win == exitbutton)
  394.     exit (0);
  395.     if (win == newgame) {
  396.     YourScore = 0;
  397.     MyScore = 0;
  398.     Dept_Head -> InitConfig (Dept_Head);
  399.     for (i = 0; i < Dept_Head -> NumberOfObjects; i++)
  400.         Dept_Head -> Objects[i] -> Visible = NO;
  401.  
  402.     }
  403.  
  404. /* for now you are supposed to enter the score by 'hand'. */
  405.  
  406.     if (win == MyPlus) {
  407.     MyScore += 1;
  408.     sprintf (buf, "%d", MyScore);
  409.     PutIn (Myscoreboard, buf, gc, fg, get_color (MY_BALLS_COLOR));
  410.     }
  411.     if (win == MyMinus) {
  412.     MyScore -= 1;
  413.     sprintf (buf, "%d", MyScore);
  414.     PutIn (Myscoreboard, buf, gc, fg, get_color (MY_BALLS_COLOR));
  415.     }
  416.  
  417.  
  418.     if (win == YourPlus) {
  419.     YourScore += 1;
  420.     sprintf (buf, "%d", YourScore);
  421.     PutIn (Yourscoreboard, buf, gc, fg, get_color (YOUR_BALLS_COLOR));
  422.     }
  423.     if (win == YourMinus) {
  424.     YourScore -= 1;
  425.     sprintf (buf, "%d", YourScore);
  426.     PutIn (Yourscoreboard, buf, gc, fg, get_color (YOUR_BALLS_COLOR));
  427.     }
  428.  
  429.    if(win==TraJectory) {
  430.          if(WantsTrajectory==YES){ WantsTrajectory = NO;
  431.            PutIn (TraJectory,"No Cheat", gc, fg,get_color (PANE_BG));}
  432.          else { WantsTrajectory = YES;
  433.            PutIn (TraJectory,"Cheat", gc, fg,get_color (PANE_BG));}
  434.     }           
  435.  
  436.  
  437.     setbkcolor (get_color (PANE_BG));/* one more quick fix */
  438.  
  439.  
  440.  
  441. };
  442.  
  443.  
  444.  
  445.  
  446.  
  447. expose_window (expose_event)
  448. XExposeEvent * expose_event;
  449. {
  450.     char    buf[15];
  451.     Window win;
  452.     int     i;
  453.     win = expose_event -> window;
  454.  
  455.     if (win == exitbutton)
  456.     PutIn (exitbutton, "  Exit   ", gc, fg, bg);
  457.     if (win == newgame)
  458.     PutIn (newgame, " New Game  ", gc, fg, bg);
  459.     if (win == Yourscoreboard) {
  460.     sprintf (buf, " %d ", YourScore);
  461.     PutIn (Yourscoreboard, buf, gc, fg, get_color (YOUR_BALLS_COLOR));
  462.     }
  463.     if (win == Myscoreboard) {
  464.     sprintf (buf, " %d ", MyScore);
  465.     PutIn (Myscoreboard, buf, gc, fg, get_color (MY_BALLS_COLOR));
  466.     }
  467.     if (win == main_window) {
  468.     Dept_Head -> DrawPockets (Dept_Head);
  469.     }
  470.     setbkcolor (get_color (PANE_BG));/* quick fix */
  471.     if (win == Pane) {
  472.     for (i = 0; i < Dept_Head -> NumberOfObjects; i++)
  473.         Dept_Head -> Objects[i] -> ShowOut = NO;
  474.     Dept_Head -> ShowStaticPosition (Dept_Head);
  475.     }
  476.  
  477.     if (win == WhoseTurnWindow)
  478.     ShowWhatHappenned (Message);
  479.  
  480.     if (win == MyPlus)
  481.     PutIn (MyPlus, "+", gc, fg, get_color (MY_BALLS_COLOR));
  482.     if (win == MyMinus)
  483.     PutIn (MyMinus, "-", gc, fg, get_color (MY_BALLS_COLOR));
  484.  
  485.     if (win == YourPlus)
  486.     PutIn (YourPlus, "+", gc, fg, get_color (YOUR_BALLS_COLOR));
  487.     if (win == YourMinus)
  488.     PutIn (YourMinus, "-", gc, fg, get_color (YOUR_BALLS_COLOR));
  489.  
  490.     if(win==TraJectory) {
  491.          if(WantsTrajectory==YES)
  492.            PutIn (TraJectory,"Cheat", gc, fg,get_color (PANE_BG));
  493.          else 
  494.            PutIn (TraJectory,"No Cheat", gc, fg,get_color (PANE_BG));
  495.       }
  496.  
  497. }
  498.  
  499.