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 / ball.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  7KB  |  271 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. #include <stdio.h>
  33. #include <malloc.h>
  34. #include <math.h>
  35. #include <stdlib.h>
  36. #include    <X11/Xlib.h>
  37. #include    <X11/Xutil.h>
  38.  
  39. #include "definition.h"
  40.  
  41.  
  42. extern  void line (int x, int y, int xx, int yy);
  43. extern  void circle (int x, int y, int rad);
  44. extern  void circleOut (int x, int y, int rad, int color);
  45. extern int  get_color (char *name);
  46. extern  void showBall (int x, int y);
  47. extern  void showBallOut (int x, int y, int color);
  48.  
  49. extern int  WhiteBallOut;
  50. extern int  BlackBallOut;
  51.  
  52.  
  53. static  void
  54.         Ball__MyColor_is (Ball * this, char *color) {
  55.             this -> mycolor = get_color (color);
  56. };
  57.  
  58.  
  59. static  void
  60.         Ball__MyIDis (Ball * this, int id) {
  61.             this -> MyID = id;
  62. };
  63.  
  64. static  void
  65.         Ball__UpdateVelocity (Ball * this) {
  66.  
  67.             this -> CurrentV.x += (this -> CurrentF.x / this -> mass) * this -> mytime.dt;
  68.     this -> CurrentV.y += (this -> CurrentF.y / this -> mass) * this -> mytime.dt;
  69. /* friction */
  70.     this -> CurrentV.x -= DROPDEADFACTOR * this -> CurrentV.x;
  71.     this -> CurrentV.y -= DROPDEADFACTOR * this -> CurrentV.y;
  72.  
  73. /*    dprintf ("problem with %d  %f %f \n", this -> MyID, this -> CurrentV.x,
  74.         this -> CurrentV.y);
  75. */
  76.     if ((fabs (this -> CurrentV.x) < MINSPEED) &&
  77.         (fabs (this -> CurrentV.y) < MINSPEED)) {
  78.     this -> CurrentV.x = 0.0;
  79.     this -> CurrentV.y = 0.0;
  80.     this -> Stopped = YES;
  81.     }
  82.     else
  83.     this -> Stopped = NO;
  84.  
  85.  
  86. };
  87.  
  88.  
  89. static  void
  90.         Ball__Hide (Ball * this) {
  91.             setcolor (this -> erasecolor);
  92.     if (bitmapMode)
  93.     showBall (this -> LastDrawingL.x - this -> radius, this -> LastDrawingL.y - this -> radius);
  94.     else
  95.     circle (this -> LastDrawingL.x, this -> LastDrawingL.y, this -> radius);
  96.     this -> Visible = NO;
  97. };
  98.  
  99.  
  100. static  void
  101.         Ball__Show (Ball * this) {
  102.             setcolor (this -> mycolor);
  103.     if (bitmapMode)
  104.     showBall (this -> CurrentL.x - this -> radius, this -> CurrentL.y - this -> radius);
  105.     else
  106.     circle (this -> CurrentL.x, this -> CurrentL.y, this -> radius);
  107.     this -> Visible = YES;
  108.     this -> LastDrawingL.x = this -> CurrentL.x;
  109.     this -> LastDrawingL.y = this -> CurrentL.y;
  110.  
  111. };
  112.  
  113.  
  114.  
  115.  
  116. static  void
  117.         Ball__MoveToNextLocation (Ball * this) {
  118.     int     x,
  119.             y;
  120.     if (this -> In) {
  121.     this -> UpdateVelocity (this);
  122.     this -> UpdateLocation (this);
  123.  
  124.     if (((this -> CurrentL.x != this -> LastDrawingL.x) && 
  125.             (this -> CurrentL.y != this -> LastDrawingL.y))) 
  126.     {
  127.  
  128.         if (!(this -> framecounter % FRAMEDENSITY)) {
  129.  
  130.         this -> Hide (this);/* delete the old one */
  131.         this -> Show (this);/* put the new one */
  132.         }
  133.     }
  134.     else
  135.              if (this -> NeedOneMoreDrawing ) {
  136.         this -> Show (this);/* put the new one */
  137.         this -> NeedOneMoreDrawing = NO;
  138.         }
  139.  
  140.  
  141.     this -> UpdateMe (this);
  142.     }
  143.     else
  144.     if (this -> Visible == YES) {
  145.         this -> Stopped = YES;
  146.         this -> Hide (this);
  147.         if (!this -> ShowOut) {
  148.         if (this -> MyID > 1 && this -> MyID < 9) {
  149.             x = 60;
  150.             y = 160 + (this -> MyID - 2) * 30;
  151.         }
  152.         if (this -> MyID > 8 && this -> MyID < 16) {
  153.             x = 10;
  154.             y = 160 + (this -> MyID - 9) * 30;
  155.         }
  156.         if (this -> MyID == 0)
  157.             WhiteBallOut = YES;
  158.         if (this -> MyID == 1)
  159.             BlackBallOut = YES;
  160.  
  161.         this -> ShowOut = YES;
  162.  
  163.         if (this -> MyID > 1) {/* means blue or red ball out */
  164.             if (bitmapMode)
  165.             showBallOut (x, y, this -> mycolor);
  166.             else
  167.             circleOut (x, y, this -> radius, this -> mycolor);
  168.         }
  169.  
  170.         }
  171.  
  172.  
  173.     }
  174.  
  175. };
  176.  
  177.  
  178.  
  179. static  void
  180.         Ball__UpdateLocation (Ball * this) {
  181.             this -> CurrentL.x += this -> CurrentV.x * this -> mytime.dt;
  182.     this -> CurrentL.y += this -> CurrentV.y * this -> mytime.dt;
  183.  
  184. };
  185.  
  186.  
  187.  
  188. static  void
  189.         Ball__UpdateMyself (Ball * this) {
  190.  
  191.             this -> OneStepBackL.x = this -> CurrentL.x;
  192.     this -> OneStepBackL.y = this -> CurrentL.y;
  193.  
  194. /*    this -> TwoStepBackL.x = this -> OneStepBackL.x;
  195.     this -> TwoStepBackL.y = this -> OneStepBackL.y;
  196. */
  197.     this -> OneStepBackV.x = this -> CurrentV.x;
  198.     this -> OneStepBackV.y = this -> CurrentV.y;
  199.  
  200.     this -> framecounter++;
  201.  
  202. };
  203.  
  204.  
  205.  
  206.  
  207.  
  208. static  void Ball__UpdateForce (Ball * this, Force * NewForce) {
  209.             this -> CurrentF.x += NewForce -> x;
  210.     this -> CurrentF.y += NewForce -> y;
  211. };
  212.  
  213. static  void Ball__NewSpeed (Ball * this, float Vx, float Vy) {
  214.             this -> CurrentV.x = Vx;
  215.     this -> CurrentV.y = Vy;
  216.     this -> Stopped = NO;
  217. };
  218.  
  219.  
  220.  
  221. Ball * new__Ball (double Mass, double radious, float initX, float initY, double initVx, double initVy, double initVr) {
  222.     Ball * A_Ball;
  223.     A_Ball = (Ball *) malloc (sizeof (Ball));
  224.  
  225.     if (!A_Ball) {
  226.     printf ("problem..\n");
  227.     exit (0);
  228.     };
  229.  
  230.     A_Ball -> mass = Mass;
  231.     A_Ball -> radius = radious;
  232.     A_Ball -> In = YES;        /* everybody is in,  at the begining */
  233.     A_Ball -> kConstant = 10.00;
  234.     A_Ball -> Stopped = NO;
  235.     A_Ball -> ShowOut = NO;
  236.     A_Ball -> CurrentL.x = initX;
  237.     A_Ball -> CurrentL.y = initY;
  238.     A_Ball -> OneStepBackL.x = 0.00000000;
  239.     A_Ball -> OneStepBackL.y = 0.00000000;
  240.  
  241.     A_Ball -> TwoStepBackL.x = initX;
  242.     A_Ball -> TwoStepBackL.y = initY;
  243.     A_Ball -> LastDrawingL.x = 2000.00000000;/* so what? */
  244.     A_Ball -> LastDrawingL.y = 2000.00000000;
  245.     A_Ball -> CurrentV.x = initVx;
  246.     A_Ball -> CurrentV.y = initVy;
  247.     A_Ball -> Ip = A_Ball -> mass * A_Ball -> radius * A_Ball -> radius / 2.;
  248.     A_Ball -> Visible = NO;
  249.     A_Ball -> NeedOneMoreDrawing = YES;
  250.     A_Ball -> framecounter = 0;
  251.  
  252.     A_Ball -> mycolor = get_color (RED);
  253.     A_Ball -> erasecolor = get_color (PANE_BG);
  254.  
  255.     A_Ball -> UpdateVelocity = Ball__UpdateVelocity;
  256.     A_Ball -> MyColor_is = Ball__MyColor_is;
  257.     A_Ball -> MyIDis = Ball__MyIDis;
  258.     A_Ball -> UpdateLocation = Ball__UpdateLocation;
  259.     A_Ball -> UpdateForce = Ball__UpdateForce;
  260.     A_Ball -> UpdateMe = Ball__UpdateMyself;
  261.     A_Ball -> MoveToNextLocation = Ball__MoveToNextLocation;
  262.     A_Ball -> Hide = Ball__Hide;
  263.     A_Ball -> Show = Ball__Show;
  264.     A_Ball -> NewSpeed = Ball__NewSpeed;
  265.  
  266.     dprintf ("  %f %f \n", A_Ball -> CurrentL.y, A_Ball -> CurrentL.x);
  267.  
  268.     return (A_Ball);
  269.  
  270. };
  271.