home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CENVIW9.ZIP / PONGTIME.CMM < prev    next >
Text File  |  1994-03-08  |  4KB  |  107 lines

  1. /*****************************************************************
  2.  *** PongTime - Make a bouncy clock. Randomize speed after     ***
  3.  *** ver.1      each bounce, within limites. This demonstrates ***
  4.  ***            control over window size and positoining, as   ***
  5.  ***            well as use of sound and menus.                ***
  6.  *****************************************************************/
  7.  
  8. #define  COL_AVERAGE_MOVE  3           // average column movement
  9. #define  ROW_AVERAGE_MOVE  3           // average row movement
  10. #define  COL_RANDOM_DELTA  1           // allowed change from average move to
  11. #define  ROW_RANDOM_DELTA  1           // randomize the bounces
  12.  
  13.  
  14. #include <WinTools.lib> // lot of useful routines
  15. #include <WinUtil.lib>  // system metrics
  16. #include <Message.lib>  // Send messages to windows
  17. #include <MenuCtrl.lib> // send menu command switching between analog and digital
  18.  
  19. window = spawn(P_NOWAIT,"CLOCK.EXE");
  20.  
  21. if ( window != NULL ) {
  22.  
  23.    // Get size of the screen
  24.    ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  25.    ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  26.  
  27.    // will make window fill 1/4 of the screen height
  28.    WinWidth = WinHeight = ScreenHeight / 4 - 1;
  29.  
  30.    // Get Initial position of the window
  31.    GetWindowRect(window,WinPos);
  32.  
  33.    // find menu items for analog and digital
  34.    AnalogMenuID = FindMenuString(GetMenu(window),"Analog");
  35.    DigitalMenuID = FindMenuString(GetMenu(window),"Digital");
  36.    ViewingAnalog = MF_CHECKED & GetMenuState(GetMenu(window),AnalogMenuID,MF_BYCOMMAND);
  37.  
  38.    // start random direction and delta
  39.    srand();
  40.    ColSpeed = GetRandomSpeed(COL_AVERAGE_MOVE,COL_RANDOM_DELTA,rand() & 1);
  41.    RowSpeed = GetRandomSpeed(ROW_AVERAGE_MOVE,ROW_RANDOM_DELTA,rand() & 1);
  42.  
  43.    // Finally, keep bouncing the window around at every interval
  44.    while ( IsWindow(window) ) {
  45.  
  46.       // move window to new Row and Col position
  47.       WinPos.right = (WinPos.left += ColSpeed) + WinWidth;
  48.       WinPos.bottom = (WinPos.top += RowSpeed) + WinHeight;
  49.       SetWindowRect(window,WinPos);
  50.  
  51.       // check if needs to bounce back to to hitting desktop edge
  52.       if ( ColSpeed < 0 ) {
  53.          if ( WinPos.left < 0 )
  54.             Bounce(),
  55.             ColSpeed = GetRandomSpeed(COL_AVERAGE_MOVE,COL_RANDOM_DELTA,TRUE);
  56.       } else {
  57.          if ( ScreenWidth <= WinPos.right )
  58.             Bounce(),
  59.             ColSpeed = GetRandomSpeed(COL_AVERAGE_MOVE,COL_RANDOM_DELTA,FALSE);
  60.       }
  61.       if ( RowSpeed < 0 ) {
  62.          if ( WinPos.top < 0 )
  63.             Bounce(),
  64.             RowSpeed = GetRandomSpeed(ROW_AVERAGE_MOVE,ROW_RANDOM_DELTA,TRUE);
  65.       } else {
  66.          if ( ScreenHeight <= WinPos.bottom )
  67.             Bounce(),
  68.             RowSpeed = GetRandomSpeed(ROW_AVERAGE_MOVE,ROW_RANDOM_DELTA,FALSE);
  69.       }
  70.  
  71.    }
  72.  
  73. }
  74.  
  75. Bounce() // make a beep sound, and change state of the clock
  76. {
  77.    WinBeep();
  78.    MenuCommand(window,ViewingAnalog ? DigitalMenuID : AnalogMenuID,True);
  79.    ViewingAnalog = !ViewingAnalog;
  80. }
  81.  
  82.  
  83. //****** ROUTINES CALLED BY THE ABOVE CODE *******
  84.  
  85. GetRandomSpeed(average,deviation,positive) // if not positive then negative
  86. {
  87.    // get a random deviation from average
  88.    deviate = (rand() % (2*deviation + 1)) - deviation;
  89.  
  90.    if ( positive )
  91.       return( average + deviate );
  92.    else
  93.       return( -average - deviate );
  94. }
  95.  
  96. IsWindow(WindowHandle)
  97. {
  98.    return DynamicLink("USER","ISWINDOW",SWORD16,PASCAL,WindowHandle);
  99. }
  100.  
  101. WinBeep()
  102. {
  103.    #define  MESSAGE_BEEP_ORDINAL 104
  104.    DynamicLink("USER.EXE",MESSAGE_BEEP_ORDINAL,SWORD16,PASCAL,0);
  105. }
  106.  
  107.