home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Windows 95 Special 2 / CD_1.iso / scrnsavr / bouncer / bouncer.c < prev    next >
C/C++ Source or Header  |  1992-07-17  |  15KB  |  511 lines

  1. /*  BOUNCER.C - ScreenSaverProc(), RegisterDialogClasses(),
  2.  *   ScreenSaverConfigureDialog() and other support code for
  3.  *   BOUNCER.
  4.  *
  5.  *   BOUNCER is a sample screen saver application. It bounces a
  6.  *   bitmap across the display and produces a sound when the
  7.  *   bitmap image is at the bottom of the screen.
  8.  *
  9.  *    (C) Copyright Microsoft Corp. 1991.  All rights reserved.
  10.  *
  11.  *    You have a royalty-free right to use, modify, reproduce and
  12.  *    distribute the Sample Files (and/or any modified version) in
  13.  *    any way you find useful, provided that you agree that
  14.  *    Microsoft has no warranty obligations or liability for any
  15.  *    Sample Application Files which are modified.
  16.  */
  17. // COPYRIGHT:
  18. //
  19. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  20. //
  21. //   You have a royalty-free right to use, modify, reproduce and
  22. //   distribute the Sample Files (and/or any modified version) in
  23. //   any way you find useful, provided that you agree that
  24. //   Microsoft has no warranty obligations or liability for any
  25. //   Sample Application Files which are modified.
  26. #include <windows.h>
  27. #include <mmsystem.h>
  28. #include "bouncer.h"
  29.  
  30.  
  31. /* Global used by SCRNSAVE.LIB. Required for all screen savers.
  32.  */
  33.  
  34. char szAppName[40];
  35.  
  36.  
  37. /* Globals specific to BOUNCER.
  38.  */
  39.  
  40. char szDIBName[] = "BounceDIB";
  41.  
  42. char szSpeedName[] = "Speed";
  43.  
  44. char szXPosName[] = "xPosition";
  45.  
  46. char szYPosName[] = "yPosition";
  47.  
  48. char szXVelocityName[] = "xVelocity";
  49.  
  50. char szGravityName[] = "Gravity";
  51.  
  52. char szSoundName[] = "Sound";
  53.  
  54. char szDIBNumName[] = "DIBNum";
  55.  
  56. char szPauseName[] = "Pause at bottom";
  57.  
  58. char szName[] = "Bounce a bitmap";
  59.  
  60. /* Externals defined in SCRNSAVE.LIB. Required for all screen savers.
  61.  */
  62.  
  63. HINSTANCE _cdecl hMainInstance;
  64.  
  65. HWND _cdecl hMainWindow;
  66.  
  67. char _cdecl szName[TITLEBARNAMELEN];
  68.  
  69. char _cdecl szIsPassword[22];
  70.  
  71. char _cdecl szIniFile[MAXFILELEN];
  72.  
  73. char _cdecl szScreenSaver[22];
  74.  
  75. char _cdecl szPassword[16];
  76.  
  77. char _cdecl szDifferentPW[BUFFLEN];
  78.  
  79. char _cdecl szChangePW[30];
  80.  
  81. char _cdecl szBadOldPW[BUFFLEN];
  82.  
  83. char _cdecl szHelpFile[MAXFILELEN];
  84.  
  85. char _cdecl szNoHelpMemory[BUFFLEN];
  86.  
  87. UINT _cdecl MyHelpMessage;
  88.  
  89. HOOKPROC _cdecl fpMessageFilter;
  90.  
  91. HBITMAP hbmImage;                   // image handle
  92.  
  93. WORD wElapse;                       // speed parameter
  94.  
  95. WORD wTimer;                        // timer id
  96.  
  97. BOOL bBottom;                       // TRUE if frog is at bottom of screen
  98.  
  99. int xPos;                           // current x position
  100.  
  101. int yPos;                           // current y position
  102.  
  103. int xPosInit;                       // initial x position
  104.  
  105. int yPosInit;                       // initial y position
  106.  
  107. int xVelocInit;                     // x initial velocity
  108.  
  109. int nGravity;                       // acceleration factor
  110.  
  111. BOOL bSound;                        // sound on/off flag
  112.  
  113. BOOL bPause;                        // stick at bottom of screen?
  114.  
  115. BOOL bPassword;                     // password protected?
  116.  
  117. HANDLE hresWave;                    // handle to sound resource
  118.  
  119. LPSTR lpWave;                       // pointer to wave resource
  120.  
  121.  
  122. /* ScreenSaverProc - Main entry point for screen saver messages.
  123.  *  This function is required for all screen savers.
  124.  *
  125.  * Params:  Standard window message handler parameters.
  126.  *
  127.  * Return:  The return value depends on the message.
  128.  *
  129.  *  Note that all messages go to the DefScreenSaverProc(), except
  130.  *  for ones we process.
  131.  */
  132.  
  133.  
  134. LONG FAR PASCAL ScreenSaverProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM
  135.                                  lParam)
  136. {
  137.    RECT rc;
  138.    static WORD wBottomCount;
  139.  
  140.    switch (msg)
  141.       {
  142.    case WM_CREATE:                             // BOUNCER-specific
  143.    {
  144.       HANDLE hResInfo;
  145.  
  146.       /* Load the strings from the STRINGTABLE
  147.        */
  148.  
  149.       GetIniEntries();
  150.  
  151.       /* Load the initial bounce settings.
  152.        */
  153.       GetIniSettings();
  154.  
  155.       /* Load the DIB image we want to use.
  156.        */
  157.       hbmImage = LoadBitmap(hMainInstance, szDIBName);
  158.  
  159.       /* Load and lock the sound resource
  160.        */
  161.       if (hResInfo = FindResource(hMainInstance, "Sound", "WAVE"))
  162.       {
  163.          if (hresWave = LoadResource(hMainInstance, hResInfo))
  164.          {
  165.             lpWave = LockResource(hresWave);
  166.          }
  167.       }
  168.  
  169.       /* Create a timer to move the image
  170.        */
  171.       wTimer = SetTimer(hWnd, ID_TIMER, wElapse, NULL);
  172.       xPos = xPosInit;
  173.       yPos = yPosInit;
  174.       break;
  175.    }
  176.  
  177.    case WM_TIMER:                              // BOUNCER-specific
  178.       if (bPause && bBottom)
  179.       {
  180.          if (++wBottomCount == 10)
  181.          {
  182.             wBottomCount = 0;
  183.             bBottom = FALSE;
  184.          }
  185.          break;
  186.       }
  187.  
  188.       /* Move the image around a bit
  189.        */
  190.       MoveImage(hWnd);
  191.       break;
  192.  
  193.    case WM_DESTROY:                            // BOUNCER-specific
  194.  
  195.       /* Destroy any objects we created
  196.        */
  197.       if (hbmImage)
  198.          DeleteObject(hbmImage);
  199.       if (wTimer)
  200.          KillTimer(hWnd, ID_TIMER);
  201.       sndPlaySound(NULL, 0);
  202. /*s-*/ if( lpWave )   UnlockResource(hresWave); /*s+*/
  203.       if (hresWave)
  204.          FreeResource(hresWave);
  205.       break;
  206.  
  207.    case WM_ERASEBKGND:
  208.       GetClientRect(hWnd, &rc);
  209.       FillRect((HDC)wParam, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
  210.       return 0L;
  211.  
  212.    default:
  213.       break;
  214.       }
  215.    return DefScreenSaverProc(hWnd, msg, wParam, lParam);
  216. }
  217.  
  218. /* RegisterDialogClasses -- Entry point for registering window
  219.  * classes required by configuration dialog box.
  220.  *
  221.  * Params:  hWnd -- Handle to window
  222.  *
  223.  * Return:  None
  224.  */
  225.  
  226.  
  227. BOOL RegisterDialogClasses (HINSTANCE hInst)
  228. {
  229.    return TRUE;
  230. }
  231.  
  232.  
  233. /* ScreenSaverConfigureDialog -- Dialog box function for configuration
  234.  * dialog.
  235.  *
  236.  * Params:  hWnd -- Handle to window
  237.  *
  238.  * Return:  None
  239.  */
  240.  
  241.  
  242. BOOL FAR PASCAL ScreenSaverConfigureDialog (HWND hDlg, UINT msg, WPARAM wParam,
  243.                                             LPARAM lParam)
  244. {
  245.    static HWND hIDOK;
  246.    static HWND hSetPassword;
  247.  
  248.    switch (msg)
  249.       {
  250.    case WM_INITDIALOG:                         // BOUNCER-specific
  251.       GetIniEntries();
  252.       GetIniSettings();
  253.       SetDlgItemInt(hDlg, ID_SPEED, wElapse, FALSE);
  254.       SetDlgItemInt(hDlg, ID_XPOS, xPosInit, TRUE);
  255.       SetDlgItemInt(hDlg, ID_YPOS, yPosInit, TRUE);
  256.       SetDlgItemInt(hDlg, ID_VELOCITY, xVelocInit, TRUE);
  257.       SetDlgItemInt(hDlg, ID_GRAVITY, nGravity, TRUE);
  258.       SendDlgItemMessage(hDlg, ID_SOUND, BM_SETCHECK, bSound, NULL);
  259.       SendDlgItemMessage(hDlg, ID_PAUSE, BM_SETCHECK, bPause, NULL);
  260.       SendDlgItemMessage(hDlg, ID_PASSWORDPROTECTED, BM_SETCHECK, bPassword,
  261.                          NULL);
  262.       hSetPassword = GetDlgItem(hDlg, ID_SETPASSWORD);
  263.       EnableWindow(hSetPassword, bPassword);
  264.       hIDOK = GetDlgItem(hDlg, IDOK);
  265.       return TRUE;
  266.  
  267.    case WM_COMMAND:                            // BOUNCER-specific
  268.       switch (wParam)
  269.          {
  270.       case IDOK:
  271.          wElapse = GetDlgItemInt(hDlg, ID_SPEED, NULL, FALSE);
  272.          xPosInit = GetDlgItemInt(hDlg, ID_XPOS, NULL, TRUE);
  273.          yPosInit = GetDlgItemInt(hDlg, ID_YPOS, NULL, TRUE);
  274.          xVelocInit = GetDlgItemInt(hDlg, ID_VELOCITY, NULL, TRUE);
  275.          nGravity = GetDlgItemInt(hDlg, ID_GRAVITY, NULL, TRUE);
  276.          bSound = IsDlgButtonChecked(hDlg, ID_SOUND);
  277.          bPause = IsDlgButtonChecked(hDlg, ID_PAUSE);
  278.          bPassword = IsDlgButtonChecked(hDlg, ID_PASSWORDPROTECTED);
  279.          WriteProfileInt(szAppName, szSpeedName, wElapse);
  280.          WriteProfileInt(szAppName, szXPosName, xPosInit);
  281.          WriteProfileInt(szAppName, szYPosName, yPosInit);
  282.          WriteProfileInt(szAppName, szXVelocityName, xVelocInit);
  283.          WriteProfileInt(szAppName, szGravityName, nGravity);
  284.          WriteProfileInt(szAppName, szSoundName, bSound);
  285.          WriteProfileInt(szAppName, szPauseName, bPause);
  286.          WriteProfileInt(szAppName, szIsPassword, bPassword);
  287.          EndDialog(hDlg, TRUE);
  288.          return TRUE;
  289.  
  290.       case IDCANCEL: