home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Samples / Balloons / Source / client.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  5KB  |  213 lines

  1. // Client.cpp (Client.exe)
  2. // Client Application Game for Balloons
  3.  
  4. #define __OCL_RESOLVE_TEMPLATES__
  5. #include "..\Source\Client.hpp"
  6.  
  7. #if defined(__EMX__)
  8.   template class  OThread<BalloonClient>;
  9. #endif
  10.  
  11.  
  12. BalloonClient client;
  13.  
  14. // code
  15.  
  16. void main(void)
  17. {
  18.  try
  19.   {
  20.    client.init();
  21.   }
  22.  
  23.  
  24.  catch(OPMException err)
  25.   {
  26.    err.exMsg.error(err.errRes);
  27.    _exit(1);
  28.   }
  29.  
  30.  OApp::current().run();
  31. }
  32.  
  33.  
  34. BalloonClient::BalloonClient()
  35.   : OFrame(ID_MAIN, FCF_STANDARD, CS_SAVEBITS | CS_SIZEREDRAW),
  36.     goDown(FALSE),
  37.     drawThread(this, &BalloonClient::draw)
  38. {
  39.  BallCount = 1;
  40.  Speed = 6;
  41. }
  42.  
  43.  
  44. void BalloonClient::init()
  45. {
  46.  
  47.  try
  48.   {
  49.    ONSem svrSem(BALL_SEM);              // Try to create Server semaphore.
  50.    throw OPMException(NO_SERVER, 0);    // If it was succesful the real server isnt running.
  51.   }
  52.  
  53. // ONSem::ONSem(...) throws an OVioException
  54. // when the server semaphore  cannot be created.
  55. // This is expected. So we catch it and
  56. // continue processing.
  57.  
  58.  catch(OVioException err)
  59.    {}              
  60.  
  61.  Buffer = &pipeMessage;
  62.  if (!beginPiping(BALL_PIPE))
  63.    throw OPMException(PIPE_FAILED, 0);
  64.  
  65.  createFrame("Balloon Client");
  66.  
  67.  sizepos.cx = 310;
  68.  sizepos.cy = 310;
  69.  setSizePosFrame(HWND_TOP, sizepos, SWP_SHOW | SWP_SIZE | SWP_ACTIVATE);
  70. }
  71.  
  72.  
  73.  
  74.  
  75. BOOL BalloonClient::OCommand(ULONG msg, MPARAM mp1, MPARAM mp2)
  76. {
  77.  switch(msg)
  78.   {
  79.    case WM_CREATE:
  80.      setWindowText("Balloons Client: Connected.");
  81.      break;
  82.  
  83.    case WM_SIZE:
  84.    case WM_ERASEBACKGROUND:
  85.      hps = WinGetPS(hwnd);
  86.      WinQueryWindowRect(hwnd, &rcl);
  87.      WinFillRect(hps, &rcl, CLR_BLACK);
  88.      cx = rcl.xRight - rcl.xLeft - 40;
  89.      cy = rcl.yTop - rcl.yBottom - 40;
  90.      WinReleasePS(hps);
  91.      break;
  92.  
  93.    case WM_COMMAND:
  94.      switch(SHORT1FROMMP(mp1)) {
  95.         case ID_EXIT:
  96.         case PIPE_STOP_CLIENT:
  97.           goDown = TRUE;
  98.           WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  99.           break;
  100.  
  101.         case PIPE_STOP_BALLOONS:
  102.          goDown = TRUE;
  103.          break;
  104.  
  105.         case PIPE_START_BALLOONS:
  106.           if (!drawThread.isRunning()) {
  107.             goDown = FALSE;
  108.             WinSendMsg(hwnd, WM_ERASEBACKGROUND, NULL, NULL);
  109.             drawThread.run(); }
  110.           break;
  111.  
  112.         case PIPE_SERVER_EXIT:
  113.           goDown = TRUE;
  114.           WinSendMsg(hwnd, WM_ERASEBACKGROUND, NULL, NULL);
  115.           setWindowText("Balloons Client: Disconnected.");
  116.           break;
  117.  
  118.         case PIPE_SET_NUM:
  119.           BallCount = SHORT1FROMMP(mp2);
  120.           break;
  121.  
  122.         case PIPE_SET_SPEED:
  123.           Speed = 6 - SHORT1FROMMP(mp2);
  124.           break;
  125.        } break;
  126.  
  127.    case WM_CLOSE:
  128.     stopPiping();
  129.     goDown = TRUE;
  130.     if (drawThread.isRunning())
  131.       drawThread.waitFor();
  132.     forceQuit();
  133.     break;
  134.  
  135.    default:
  136.     return(OFrame::OCommand(msg, mp1, mp2));
  137.   }
  138.  return(TRUE);
  139. }
  140.  
  141.  
  142.  
  143. void BalloonClient::draw()
  144. {
  145.  ULONG     i;
  146.  ULONG     ulTemp = BallCount;
  147.  DATETIME  Time;
  148.  
  149.  hps = WinGetPS(hwnd);
  150.  hbm = GpiLoadBitmap(hps, NULLHANDLE, ID_BITMAP, 0L, 0L);
  151.  memset(&Balls, 0, sizeof(BALLPTL)*5);
  152.  WinReleasePS(hps);
  153.  
  154.  DosGetDateTime(&Time);
  155.  srand(Time.hundredths);
  156.  
  157.  for (i = 0; i < 5  ; i++)
  158.   {
  159.    Balls[i].Ptl.x = LRAND(cx);
  160.    Balls[i].Ptl.y = LRAND(cy);
  161.    Balls[i].deltax = -1;
  162.    Balls[i].deltay = 1;
  163.   }
  164.  
  165.  
  166.  drawThread.setPrty(PRTYC_REGULAR, 10);
  167.  
  168.  while(!goDown) {
  169.    hps = WinGetPS(hwnd);
  170.    if (ulTemp != BallCount) {
  171.       WinQueryWindowRect(hwnd, &rcl);
  172.       WinFillRect(hps, &rcl, CLR_BLACK);
  173.       ulTemp = BallCount; }
  174.  
  175.    for (i = 0; ((i < BallCount) && (!goDown)); i++) {
  176.       calculate(&Balls[i]);
  177.       WinDrawBitmap(hps, hbm, NULL, &Balls[i].Ptl, 0L, 0L, DBM_IMAGEATTRS); }
  178.    WinReleasePS(hps);
  179.    DosSleep(20*Speed); }
  180.  
  181.  GpiDeleteBitmap(hbm);
  182. }
  183.  
  184.  
  185. void BalloonClient::calculate(PBALLPTL pBptl)
  186. {
  187.  if ((pBptl->Ptl.x + pBptl->deltax) > cx)
  188.    pBptl->deltax = -pBptl->deltax;
  189.  if ((pBptl->Ptl.x + pBptl->deltax) < 1)
  190.    pBptl->deltax = -pBptl->deltax;
  191.  if ((pBptl->Ptl.y + pBptl->deltay) > cy)
  192.    pBptl->deltay = -pBptl->deltay;
  193.  if ((pBptl->Ptl.y + pBptl->deltay) < 1)
  194.    pBptl->deltay = -pBptl->deltay;
  195.  pBptl->Ptl.x += pBptl->deltax;
  196.  pBptl->Ptl.y += pBptl->deltay;
  197. }
  198.  
  199.  
  200. void BPipeClient::OPipeCommand(PVOID pvData)
  201. {
  202.  pOPMinit init  = new OPMinit;     // this is a thread and must be initialized for PM
  203.  PPIPEMSG msg = (PPIPEMSG) pvData; // because the OPipe threads are not pm-initialized by default
  204.  
  205.  WinSendMsg(client.hwnd, WM_COMMAND,
  206.             MPFROMSHORT(msg->Type),
  207.             MPFROMSHORT(msg->Data));
  208.  delete init;
  209. }
  210.  
  211.  
  212. // end of source
  213.