home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / winpool / ball.c next >
C/C++ Source or Header  |  1992-12-22  |  6KB  |  276 lines

  1.  
  2.  
  3.  
  4. /*
  5.  * I. ARIT 1992 Hidirbeyli,AYDIN,TR.  09400 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,but without any
  11.  * warranty.  No author or distributor accepts responsibility to anyone for
  12.  * the consequences of using it or for whether it serves any particular
  13.  * purpose or works at all. 
  14.  *
  15.  *
  16.  * Everyone is granted permission to copy, modify and redistribute this file
  17.  * under the following conditions: 
  18.  *
  19.  * Permission is granted to anyone to make or distribute copies of the source
  20.  * code, either as received or modified, in any medium, provided that all
  21.  * copyright notices, permission and nonwarranty notices are preserved, and
  22.  * that the distributor grants the recipient permission for further
  23.  * redistribution as permitted by this document. 
  24.  *
  25.  * No part of this program can be used in any commercial product. 
  26.  */
  27.  
  28.  
  29.  
  30. #include <windows.h>
  31. #include <stdio.h>
  32. #include <malloc.h>
  33. #include <math.h>
  34. #include <stdlib.h>
  35. #include "definiti.h"
  36.  
  37. extern int      WhiteBallOut;
  38. extern int      BlackBallOut;
  39. extern BITMAP   BitmapDim;
  40. extern HDC      hDCBall[16];
  41. extern HDC      hDCBallDel;
  42.  
  43.  
  44.  
  45. void
  46. showBallOut(int x, int y, int number, HDC hdc)
  47. {
  48.     BitBlt(hdc, x, y,
  49.            BitmapDim.bmWidth, BitmapDim.bmHeight, hDCBall[number],
  50.            0, 0, SRCCOPY);
  51.  
  52. };
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. static void
  63. Ball__MyIDis(Ball * this, int id)
  64. {
  65.     this->MyID = id;
  66. };
  67.  
  68. static void
  69. Ball__UpdateVelocity(Ball * this)
  70. {
  71.  float temp = this->mytime.dt / this->mass ;
  72.  
  73.     this->CurrentV.x += this->CurrentF.x * temp ;
  74.     this->CurrentV.y += this->CurrentF.y * temp ;
  75.     /* friction */
  76.     this->CurrentV.x -= DROPDEADFACTOR * this->CurrentV.x;
  77.     this->CurrentV.y -= DROPDEADFACTOR * this->CurrentV.y;
  78.  
  79.     /*
  80.      * dprintf ("problem with %d  %f %f \n", this -> MyID, this ->
  81.      * CurrentV.x, this -> CurrentV.y);
  82.      */
  83.     if ((fabs(this->CurrentV.x) < MINSPEED) &&
  84.         (fabs(this->CurrentV.y) < MINSPEED)) {
  85.         this->CurrentV.x = 0.0;
  86.         this->CurrentV.y = 0.0;
  87.         this->Stopped = YES;
  88.     } else
  89.         this->Stopped = NO;
  90.  
  91.  
  92. };
  93.  
  94.  
  95. static void
  96. Ball__Hide(Ball * this, HDC hdc)
  97. {
  98.     BitBlt(hdc, this->LastDrawingL.x - this->radius, this->LastDrawingL.y - this->radius,
  99.            BitmapDim.bmWidth, BitmapDim.bmHeight, hDCBallDel,
  100.            0, 0, SRCCOPY);
  101.  
  102.     this->Visible = NO;
  103. };
  104.  
  105.  
  106. static void
  107. Ball__Show(Ball * this, HDC hdc)
  108. {
  109.     BitBlt(hdc, this->CurrentL.x - this->radius, this->CurrentL.y - this->radius,
  110.            BitmapDim.bmWidth, BitmapDim.bmHeight, hDCBall[this->MyID],
  111.            0, 0, SRCCOPY);
  112.  
  113.     this->Visible = YES;
  114.     this->LastDrawingL.x = this->CurrentL.x;
  115.     this->LastDrawingL.y = this->CurrentL.y;
  116.  
  117. };
  118.  
  119.  
  120.  
  121.  
  122. static void
  123. Ball__MoveToNextLocation(Ball * this, HDC hdc)
  124. {
  125.     int             x, y;
  126.     if (this->In) {
  127.         this->UpdateVelocity(this);
  128.         this->UpdateLocation(this);
  129.  
  130.         if (((this->CurrentL.x != this->LastDrawingL.x) &&
  131.              (this->CurrentL.y != this->LastDrawingL.y))) {
  132.  
  133.             if (!(this->framecounter % FRAMEDENSITY)) {
  134.  
  135.                 this->Hide(this, hdc);    /* delete the old one */
  136.                 this->Show(this, hdc);    /* put the new one */
  137.             }
  138.         } else if (this->NeedOneMoreDrawing) {
  139.             this->Show(this, hdc);    /* put the new one */
  140.             this->NeedOneMoreDrawing = NO;
  141.         }
  142.         this->UpdateMe(this);
  143.     } else if (this->Visible == YES) {
  144.         this->Stopped = YES;
  145.         this->Hide(this, hdc);
  146.         /*
  147.          * check this code before you go because it defines the
  148.          * output locations for each ball 
  149.          */
  150.  
  151.         if (!this->ShowOut) {
  152.             if (this->MyID > 1 && this->MyID < 9) {
  153.                 x = 25;
  154.                 y = 30 + (this->MyID - 2) * 20;
  155.             }
  156.             if (this->MyID > 8 && this->MyID < 16) {
  157.                 x = 5;
  158.                 y = 30 + (this->MyID - 9) * 20;
  159.             }
  160.             if (this->MyID == 0)
  161.                 WhiteBallOut = YES;
  162.             if (this->MyID == 1)
  163.                 BlackBallOut = YES;
  164.  
  165.             this->ShowOut = YES;
  166.  
  167.             if (this->MyID > 1)    /* one of my/your balls is
  168.                          * out */
  169.                 showBallOut(x, y, this->MyID, hdc);
  170.  
  171.         }
  172.     }
  173. };
  174.  
  175.  
  176.  
  177. static void
  178. Ball__UpdateLocation(Ball * this)
  179. {
  180.     this->CurrentL.x += this->CurrentV.x * this->mytime.dt;
  181.     this->CurrentL.y += this->CurrentV.y * this->mytime.dt;
  182.  
  183. };
  184.  
  185.  
  186.  
  187. static void
  188. Ball__UpdateMyself(Ball * this)
  189. {
  190.  
  191.     this->OneStepBackL.x = this->CurrentL.x;
  192.     this->OneStepBackL.y = this->CurrentL.y;
  193.  
  194.     /*
  195.      * this -> TwoStepBackL.x = this -> OneStepBackL.x; this ->
  196.      * TwoStepBackL.y = this -> OneStepBackL.y; 
  197.      */
  198. /*    this->OneStepBackV.x = this->CurrentV.x;
  199.     this->OneStepBackV.y = this->CurrentV.y;
  200.   */
  201.     this->framecounter++;
  202.  
  203. };
  204.  
  205.  
  206.  
  207.  
  208.  
  209. static void
  210. Ball__UpdateForce(Ball * this, Force * NewForce)
  211. {
  212.     this->CurrentF.x += NewForce->x;
  213.     this->CurrentF.y += NewForce->y;
  214. };
  215.  
  216. static void
  217. Ball__NewSpeed(Ball * this, float Vx, float Vy)
  218. {
  219.     this->CurrentV.x = Vx;
  220.     this->CurrentV.y = Vy;
  221.     this->Stopped = NO;
  222. };
  223.  
  224.  
  225.  
  226. Ball           *
  227. new__Ball(double Mass, double radious, float initX, float initY, double initVx, double initVy, double initVr)
  228. {
  229.     Ball           *A_Ball;
  230.     A_Ball = (Ball *) malloc(sizeof(Ball));
  231.  
  232.     if (!A_Ball) {
  233.         printf("problem..\n");
  234.         exit(0);
  235.     };
  236.  
  237.     A_Ball->mass = Mass;
  238.     A_Ball->radius = radious;
  239.     A_Ball->checkRad= 2.*radious;
  240.     A_Ball->In = YES;    /* everybody is in,  at the begining */
  241.     A_Ball->kConstant = 10.00;
  242.     A_Ball->Stopped = NO;
  243.     A_Ball->ShowOut = NO;
  244.     A_Ball->CurrentL.x = initX;
  245.     A_Ball->CurrentL.y = initY;
  246.     A_Ball->OneStepBackL.x = 0.00000000;
  247.     A_Ball->OneStepBackL.y = 0.00000000;
  248.  
  249.     A_Ball->LastDrawingL.x = 2000.00000000;    /* so what? */
  250.     A_Ball->LastDrawingL.y = 2000.00000000;
  251.     A_Ball->CurrentV.x = initVx;
  252.     A_Ball->CurrentV.y = initVy;
  253.     A_Ball->Ip = A_Ball->mass * A_Ball->radius * A_Ball->radius / 2.;
  254.     A_Ball->Visible = NO;
  255.     A_Ball->NeedOneMoreDrawing = YES;
  256.     A_Ball->framecounter = 0;
  257.  
  258.  
  259.     A_Ball->UpdateVelocity = Ball__UpdateVelocity;
  260.     A_Ball->MyIDis = Ball__MyIDis;
  261.     A_Ball->UpdateLocation = Ball__UpdateLocation;
  262.     A_Ball->UpdateForce = Ball__UpdateForce;
  263.     A_Ball->UpdateMe = Ball__UpdateMyself;
  264.     A_Ball->MoveToNextLocation = Ball__MoveToNextLocation;
  265.     A_Ball->Hide = Ball__Hide;
  266.     A_Ball->Show = Ball__Show;
  267.     A_Ball->NewSpeed = Ball__NewSpeed;
  268.  
  269.     dprintf("  %f %f \n", A_Ball->CurrentL.y, A_Ball->CurrentL.x);
  270.  
  271.     return (A_Ball);
  272.  
  273. };
  274.  
  275.  
  276.