home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / GDIDEMO.ZIP / BITBLT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.5 KB  |  193 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. // TBitBltWindow demo window object for GDIDEMO program
  4.  
  5. #include <owl.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include "demobase.h"
  9. #include "bitblt.h"
  10.  
  11. const int test = OS2_MODE + 10;
  12.  
  13. /* TBitBltWindow ---------------------------------------------------- */
  14.  
  15. /* Initialize the bitblt demo window and allocate bitmaps */
  16. TBitBltWindow::TBitBltWindow( PTWindowsObject AParent, LPSTR ATitle ) :
  17.                   TBaseDemoWindow( AParent, ATitle )
  18. {
  19.   Background = LoadBitmap(GetApplication()->hInstance, MAKEINTRESOURCE(BackgroundID));
  20.   Ship = LoadBitmap(GetApplication()->hInstance, MAKEINTRESOURCE(ShipID));
  21.   MonoShip = LoadBitmap(GetApplication()->hInstance, MAKEINTRESOURCE(MonoShipID));
  22.   ScratchBitmap = 0;
  23.   StretchedBkgnd = 0;
  24.   OldX = 0;
  25.   OldY = 0;
  26.   X = 0;
  27.   Y = 0;
  28.   Delta = 5;
  29.   CurClick = 1;
  30. };
  31.  
  32. /* Dispose of all used resources */
  33. TBitBltWindow::~TBitBltWindow()
  34. {
  35.   DeleteObject(Background);
  36.   DeleteObject(Ship);
  37.   DeleteObject(MonoShip);
  38.   if (ScratchBitmap)
  39.     DeleteObject(ScratchBitmap);
  40.   if (StretchedBkgnd)
  41.     DeleteObject(StretchedBkgnd);
  42. };
  43.  
  44. void TBitBltWindow::GetWindowClass( WNDCLASS& WndClass )
  45. {
  46.   TBaseDemoWindow::GetWindowClass( WndClass );
  47.   WndClass.hIcon = 0;  // we'll paint on our icon when minimized
  48. };
  49.  
  50. /* Allocate scratch bitmaps */
  51. void TBitBltWindow::SetupWindow()
  52. {
  53.   HDC HandleDC;
  54.  
  55.   TBaseDemoWindow::SetupWindow();
  56.   HandleDC = GetDC(HWindow);
  57.   ScratchBitmap = CreateCompatibleBitmap(HandleDC, 80, 80);
  58.   StretchedBkgnd = CreateCompatibleBitmap(HandleDC, 1000, 1000);
  59.   ReleaseDC(HWindow, HandleDC);
  60. };
  61.  
  62. /* Record the new size and stretch the background to it */
  63. void TBitBltWindow::WMSize( TMessage& Message )
  64. {
  65.   HDC HandleDC, MemDC, StretchedDC;
  66.   HANDLE StretchObject, MemObject;
  67.   HCURSOR OldCur;
  68.  
  69.   TBaseDemoWindow::WMSize(Message);
  70.   WindowSize.x = Message.LP.Lo;
  71.   WindowSize.y = Message.LP.Hi;
  72.  
  73.   HandleDC = GetDC(HWindow);
  74.  
  75.   /* Create a stretched to fit background */
  76.   StretchedDC = CreateCompatibleDC(HandleDC);
  77.   MemDC = CreateCompatibleDC(HandleDC);
  78.   StretchObject = SelectObject(StretchedDC, StretchedBkgnd);
  79.   MemObject = SelectObject(MemDC, Background);
  80.   OldCur = SetCursor(LoadCursor(0, IDC_WAIT));   // set the cursor to an hourglass - this might take awhile
  81.   StretchBlt(StretchedDC, 0, 0, WindowSize.x, WindowSize.y, MemDC, 0, 0, 100, 100, SRCCOPY);
  82.   SetCursor(OldCur);
  83.   SelectObject(StretchedDC, StretchObject);
  84.   SelectObject(MemDC, MemObject);
  85.   DeleteDC(MemDC);
  86.   DeleteDC(StretchedDC);
  87.   ReleaseDC(HWindow, HandleDC);
  88. };
  89.  
  90. /* Need to ensure that the Old copy of the ship gets redrawn with
  91.   any paint messages. */
  92. void TBitBltWindow::WMPaint( TMessage& Message )
  93. {
  94.   RECT Rect;
  95.  
  96.   Rect.top = OldY;
  97.   Rect.left = OldX;
  98.   Rect.bottom = OldY + BitmapSize;
  99.   Rect.right = OldX + BitmapSize;
  100.   InvalidateRect(HWindow, &Rect, FALSE);
  101.   TBaseDemoWindow::WMPaint(Message);
  102. };
  103.  
  104. void TBitBltWindow::Paint( HDC PaintDC, PAINTSTRUCT& )
  105. {
  106.   HDC MemDC;
  107.   HANDLE MemObject;
  108.  
  109.   MemDC = CreateCompatibleDC(PaintDC);
  110.   MemObject = SelectObject(MemDC, StretchedBkgnd);
  111.   BitBlt(PaintDC, 0, 0, WindowSize.x, WindowSize.y, MemDC, 0, 0, SRCCOPY);
  112.   SelectObject(MemDC, MemObject);
  113.   DeleteDC(MemDC);
  114. };
  115.  
  116. /* TimerTick deletes the old position of the saucer and blt's a new one */
  117. void TBitBltWindow::TimerTick()
  118. {
  119.   const int ClicksToSkip = 4;
  120.  
  121.   HDC Bits, BackingStore, WindowDC;
  122.   HANDLE SavedBitsObject, SavedStoreObject;
  123.   int BX, BY, OX, OY, BH, BW;
  124.  
  125.   /* Make the saucer go slower then everyone else - only move on every 4th tick */
  126.   if (CurClick < ClicksToSkip) {
  127.     CurClick++;
  128.     return;
  129.   } else {
  130.     CurClick = 1;
  131.   };
  132.  
  133.   /* Setup the DC's */
  134.   WindowDC = GetDC(HWindow);
  135.   Bits = CreateCompatibleDC(WindowDC);
  136.   BackingStore = CreateCompatibleDC(WindowDC);
  137.  
  138.   CalculateNewXY();
  139.  
  140.   /* Calculate the offsets into and dimentions of the backing store */
  141.   BX = min(X, OldX);
  142.   BY = min(Y, OldY);
  143.   OX = abs(X - BX);
  144.   OY = abs(Y - BY);
  145.   BW = BitmapSize + abs(OldX - X);
  146.   BH = BitmapSize + abs(OldY - Y);
  147.  
  148.   /* Create an image into the backing store the will that, when blt into
  149.     the window will both erase the old image and draw the new one.
  150.     ( to minimize screen flicker ) */
  151.  
  152.   SavedStoreObject = SelectObject(BackingStore, ScratchBitmap);
  153.   SavedBitsObject = SelectObject(Bits, StretchedBkgnd);
  154.   BitBlt(BackingStore, 0, 0, BW, BH, Bits, BX, BY, SRCCOPY);
  155.   SelectObject(Bits, MonoShip);
  156.   BitBlt(BackingStore, OX, OY, BitmapSize, BitmapSize, Bits, 0, 0, SRCAND);
  157.   SelectObject(Bits, Ship);
  158.   BitBlt(BackingStore, OX, OY, BitmapSize, BitmapSize, Bits, 0, 0, SRCPAINT);
  159.  
  160.   /* Blt the backing store to the window */
  161.   BitBlt(WindowDC, BX, BY, BW, BH, BackingStore, 0, 0, SRCCOPY);
  162.  
  163.   /* Clean up the DC's */
  164.   SelectObject(Bits, SavedBitsObject);
  165.   SelectObject(BackingStore, SavedStoreObject);
  166.   DeleteDC(Bits);
  167.   DeleteDC(BackingStore);
  168.   ReleaseDC(HWindow, WindowDC);
  169.  
  170.   OldX = X;
  171.   OldY = Y;
  172. };
  173.  
  174. void TBitBltWindow::CalculateNewXY()
  175. {
  176.   if (WindowSize.x < BitmapSize)
  177.     return;  /* Don't move if too small */
  178.   if ((X > WindowSize.x - BitmapSize) || (X < 0)) {
  179.     Delta = -Delta;
  180.     if (X > (WindowSize.x - BitmapSize))
  181.       X = WindowSize.x - BitmapSize - 5;
  182.   };
  183.   X += Delta;
  184.   Y += random(10) - 5;  // range from -5 to +5
  185.   if (Y > (WindowSize.y - BitmapSize)) {
  186.     Y = WindowSize.y - BitmapSize;
  187.   } else {
  188.     if (Y < 0)
  189.       Y = 0;
  190.   }
  191. };
  192.  
  193.