home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / WINTUT2.ZIP / Tut2 / demo2.cpp < prev    next >
C/C++ Source or Header  |  1997-04-03  |  5KB  |  183 lines

  1. #include "stdafx.h"    // Okay, so it doesn't include MFC stuff... sue me. It has all the common
  2.                     //    .h files I use.
  3. #include "demo2.h"
  4. #include "resource.h"    // All the information needed to access the bitmaps
  5.  
  6. BOOL    g_bUserTimer = FALSE;
  7.             // Set to use either WM_TIMER or PeekMessage method of animation
  8.             
  9.  
  10. void SetUp(HINSTANCE hInstance);
  11. void ShutDown();
  12.  
  13. void DoAnimation(HWND hWnd);
  14.  
  15. long FAR PASCAL WndProc (HWND hWnd, WORD iMessage, WORD wParam, LONG lParam);
  16.             // Forward declaration
  17.  
  18.  
  19. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
  20. {
  21.     MSG Message;
  22.     WNDCLASS WndClass;
  23.  
  24.     if (!hPrevInstance)
  25.     {
  26.         WndClass.cbClsExtra = 0;
  27.         WndClass.cbWndExtra = 0;
  28.         WndClass.hbrBackground = GetStockObject (BLACK_BRUSH);
  29.         WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
  30.         WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  31.         WndClass.hInstance = hInstance;
  32.         WndClass.lpfnWndProc = (WNDPROC) WndProc;
  33.         WndClass.lpszClassName = "BITMAP_WINDOW";
  34.         WndClass.lpszMenuName = NULL;
  35.         WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  36.  
  37.         RegisterClass (&WndClass);
  38.     }
  39.     HWND hWnd = CreateWindow ("BITMAP_WINDOW", "Asphyxia 16color Bitmaps", WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
  40.                     50, 100, 320, 200, NULL, NULL, hInstance, NULL);
  41.     
  42.               
  43.     SetUp(hInstance);              
  44.  
  45.     ShowWindow (hWnd, SW_SHOWMAXIMIZED);
  46.     UpdateWindow (hWnd);
  47.     
  48.  
  49.     if (g_bUserTimer)
  50.     {
  51.         while (GetMessage (&Message, 0, 0, 0))
  52.         {
  53.             TranslateMessage (&Message);
  54.             DispatchMessage (&Message);
  55.         }
  56.     }
  57.     else
  58.     {
  59.         while (TRUE)
  60.         {
  61.             DoAnimation(hWnd);
  62.             
  63.             if (PeekMessage (&Message, NULL, 0, 0, PM_REMOVE))
  64.             {
  65.                 if (Message.message == WM_QUIT)
  66.                     return Message.wParam;
  67.                 
  68.                 TranslateMessage (&Message);
  69.                 DispatchMessage (&Message);
  70.             }
  71.         }
  72.     }
  73.     
  74.     ShutDown();
  75.     
  76.     return Message.wParam;
  77. }
  78.  
  79.  
  80.  
  81. long FAR PASCAL WndProc (HWND hWnd, WORD iMessage, WORD wParam, LONG lParam)
  82. {
  83.     switch (iMessage)
  84.     {
  85.         case WM_CREATE :
  86.         {
  87.             if (g_bUserTimer)
  88.                 SetTimer (hWnd, 1, 10, NULL);
  89.         }
  90.         break;
  91.         case WM_PAINT :
  92.         {
  93.             PAINTSTRUCT    PtStr;
  94.             HDC pDC = BeginPaint (hWnd, &PtStr);
  95.  
  96.             for (int i = 0; i < g_nNumPictures; i++)
  97.                 g_szcsPicture[g_szcapItem[i].m_nCurFrame].DrawImage (pDC, g_szcapItem[i].GetXPos(), g_szcapItem[i].GetYPos());
  98.  
  99.             EndPaint (hWnd, &PtStr);
  100.             return (0);
  101.         }
  102.         break;
  103.         case WM_TIMER :
  104.         {
  105.             if ((wParam == 1) && g_bUserTimer)
  106.                 DoAnimation(hWnd);
  107.         }
  108.         break;
  109.         case WM_SIZE :
  110.         {
  111.             g_nWinWidth = LOWORD(lParam);
  112.             g_nWinHeight = HIWORD(lParam);
  113.         }
  114.         break;
  115.         case WM_DESTROY :
  116.         {
  117.             if (g_bUserTimer)
  118.                 KillTimer (hWnd, 1);
  119.             PostQuitMessage (0);
  120.             return 0;
  121.         }
  122.         break;
  123.     }
  124.     return (DefWindowProc (hWnd, iMessage, wParam, lParam) );
  125. }
  126.  
  127. void SetUp(HINSTANCE hInstance)
  128. {
  129.     g_szcsPicture[ 0].InitImage (hInstance, IDB_XPIC1, 83, 70);
  130.     g_szcsPicture[ 1].InitImage (hInstance, IDB_XPIC2, 83, 70);
  131.     g_szcsPicture[ 2].InitImage (hInstance, IDB_XPIC3, 83, 70);
  132.     g_szcsPicture[ 3].InitImage (hInstance, IDB_XPIC4, 83, 70);
  133.     g_szcsPicture[ 4].InitImage (hInstance, IDB_XPIC5, 83, 70);
  134.     g_szcsPicture[ 5].InitImage (hInstance, IDB_XPIC6, 83, 70);
  135.     g_szcsPicture[ 6].InitImage (hInstance, IDB_XPIC7, 83, 70);
  136.     g_szcsPicture[ 7].InitImage (hInstance, IDB_XPIC8, 83, 70);
  137.     g_szcsPicture[ 8].InitImage (hInstance, IDB_XPIC9, 83, 70);
  138.     g_szcsPicture[ 9].InitImage (hInstance, IDB_XPIC10, 83, 70);
  139.     g_szcsPicture[10].InitImage (hInstance, IDB_XPIC11, 83, 70);
  140.     g_szcsPicture[11].InitImage (hInstance, IDB_XPIC12, 83, 70);
  141.     g_szcsPicture[12].InitImage (hInstance, IDB_XPIC13, 83, 70);
  142.     g_szcsPicture[13].InitImage (hInstance, IDB_XPIC14, 83, 70);
  143.  
  144.     for (int i = 0; i < g_nNumPictures; i++)
  145.     {
  146.         g_szcapItem[i].PositionImage (RANDOM (0, g_nWinWidth+90), RANDOM (-70, g_nWinHeight));
  147.             //    Randomly place pictures on the screen
  148.     }
  149.     
  150. }
  151.  
  152. void ShutDown()
  153. {
  154.         //    The bitmaps are freed in the class deconstructer
  155.         //    If I had "new"'d or "malloc"'d anything, I would free it here
  156. }
  157.  
  158.  
  159. void DoAnimation(HWND hWnd)
  160. {
  161.     HDC pDC = GetDC(hWnd);
  162.  
  163.     for (int i = 0; i < g_nNumPictures; i++)
  164.     {
  165.         int nTempX = g_szcapItem[i].GetXPos ();
  166.         nTempX--;
  167.         int nTempY = g_szcapItem[i].GetYPos ();
  168.         nTempY++;
  169.             //    Move the picture
  170.         
  171.         if (nTempX < - 83)
  172.         {
  173.             nTempX = RANDOM (0, g_nWinWidth+g_nWinHeight);
  174.             nTempY = -70;
  175.         }        //    Reset position when it leaves the screen
  176.         
  177.         g_szcapItem[i].PositionImage (nTempX, nTempY);
  178.         g_szcsPicture[g_szcapItem[i].m_nCurFrame].DrawImage (pDC, g_szcapItem[i].GetXPos(), g_szcapItem[i].GetYPos());
  179.     }
  180.     
  181.     ReleaseDC(hWnd, pDC);
  182. }
  183.