home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / GDIDEMO.PAK / BITBLT.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  4KB  |  168 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //   TBitBltWindow demo window object for GDIDEMO program
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include <owl\applicat.h>
  7. #include <owl\dc.h>
  8. #include "bitblt.h"
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. // TBitBltWindow ----------------------------------------------------
  13.  
  14. DEFINE_RESPONSE_TABLE1(TBitBltWindow, TBaseDemoWindow)
  15.   EV_WM_SIZE,
  16. END_RESPONSE_TABLE;
  17.  
  18. IMPLEMENT_CASTABLE1(TBitBltWindow, TBaseDemoWindow);
  19.  
  20. // Initialize the bitblt demo window and allocate bitmaps
  21. //
  22. TBitBltWindow::TBitBltWindow() : TBaseDemoWindow()
  23. {
  24.   Background = new TBitmap(*GetApplication(), BackgroundId);
  25.   Ship = new TBitmap(*GetApplication(), ShipId);
  26.   MonoShip = new TBitmap(*GetApplication(), MonoShipId);
  27.   ScratchBitmap = 0;
  28.   StretchedBkgnd = 0;
  29.   OldX = 0;
  30.   OldY = 0;
  31.   X = 0;
  32.   Y = 0;
  33.   Delta = 5;
  34.   CurClick = 1;
  35. }
  36.  
  37. // Dispose of all used resources
  38. //
  39. TBitBltWindow::~TBitBltWindow()
  40. {
  41.   delete Background;
  42.   delete Ship;
  43.   delete MonoShip;
  44.   delete ScratchBitmap;
  45.   delete StretchedBkgnd;
  46. }
  47.  
  48. // Allocate scratch bitmaps
  49. //
  50. void
  51. TBitBltWindow::SetupWindow()
  52. {
  53.   TBaseDemoWindow::SetupWindow();
  54.   TClientDC winDC(*this);
  55.   ScratchBitmap = new TBitmap(winDC, 80, 80);
  56.   StretchedBkgnd = new TBitmap(winDC, 1000, 1000);
  57. }
  58.  
  59. // Record the new size and stretch the background to it
  60. //
  61. void
  62. TBitBltWindow::EvSize(UINT sizeType, TSize& size)
  63. {
  64.   TBaseDemoWindow::EvSize(sizeType, size);
  65.   WindowSize.x = size.cx;
  66.   WindowSize.y = size.cy;
  67.  
  68.   TClientDC winDC(*this);
  69.  
  70.   // Create a stretched to fit background
  71.   //
  72.   TMemoryDC stretchedDC(winDC);
  73.   TMemoryDC memDC(winDC);
  74.   stretchedDC.SelectObject(*StretchedBkgnd);
  75.   memDC.SelectObject(*Background);
  76.  
  77.   // set the cursor to an hourglass - this might take awhile
  78.   //
  79.   HCURSOR oldCur = ::SetCursor(::LoadCursor(0, IDC_WAIT));
  80.   stretchedDC.StretchBlt(0, 0, WindowSize.x, WindowSize.y, 
  81.                          memDC, 0, 0, 100, 100, SRCCOPY);
  82.   ::SetCursor(oldCur);
  83.   Invalidate();
  84. }
  85.  
  86. // Need to ensure that the Old copy of the ship gets redrawn with
  87. //  any paint messages.
  88. //
  89. void
  90. TBitBltWindow::Paint(TDC& dc, BOOL, TRect&)
  91. {
  92.   TMemoryDC memDC(dc);
  93.   memDC.SelectObject(*StretchedBkgnd);
  94.   dc.BitBlt(0, 0, WindowSize.x, WindowSize.y, memDC, 0, 0, SRCCOPY);
  95. }
  96.  
  97. // TimerTick deletes the old position of the saucer and blt's a new one
  98. //
  99. void
  100. TBitBltWindow::TimerTick()
  101. {
  102.   const int ClicksToSkip = 4;
  103.  
  104.   // Make the saucer go slower then everyone else- only move on every 4th tick
  105.   //
  106.   if (CurClick < ClicksToSkip) {
  107.     CurClick++;
  108.     return;
  109.   } else {
  110.     CurClick = 1;
  111.   }
  112.  
  113.   // Setup the DC's
  114.   //
  115.   TClientDC windowDC(*this);
  116.   TMemoryDC bits(windowDC);
  117.   TMemoryDC backingStore(windowDC);
  118.  
  119.   // Calculate the offsets into and dimensions of the backing store
  120.   //
  121.   CalculateNewXY();
  122.   int bx = min(X, OldX);
  123.   int by = min(Y, OldY);
  124.   int ox = abs(X - bx);
  125.   int oy = abs(Y - by);
  126.   int bw = BitmapSize + abs(OldX - X);
  127.   int bh = BitmapSize + abs(OldY - Y);
  128.  
  129.   // Create an image into the backing store the will that, when blt into
  130.   //  the window will both erase the old image and draw the new one.
  131.   //  (to minimize screen flicker)
  132.   //
  133.   backingStore.SelectObject(*ScratchBitmap);
  134.   bits.SelectObject(*StretchedBkgnd);
  135.   backingStore.BitBlt(0, 0, bw, bh, bits, bx, by, SRCCOPY);
  136.   bits.SelectObject(*MonoShip);
  137.   backingStore.BitBlt(ox, oy, BitmapSize, BitmapSize, bits, 0, 0, SRCAND);
  138.   bits.SelectObject(*Ship);
  139.   backingStore.BitBlt(ox, oy, BitmapSize, BitmapSize, bits, 0, 0, SRCPAINT);
  140.  
  141.   // Blt the backing store to the window
  142.   //
  143.   windowDC.BitBlt(bx, by, bw, bh, backingStore, 0, 0, SRCCOPY);
  144.  
  145.   OldX = X;
  146.   OldY = Y;
  147. }
  148.  
  149. void
  150. TBitBltWindow::CalculateNewXY()
  151. {
  152.   if (WindowSize.x < BitmapSize)        // Don't move if too small
  153.     return;
  154.   if (X > WindowSize.x - BitmapSize || X < 0) {
  155.     Delta = -Delta;
  156.     if (X > WindowSize.x-BitmapSize)
  157.       X = WindowSize.x - BitmapSize - 5;
  158.   }
  159.   X += Delta;
  160.   Y += random(10) - 5;  // range from -5 to +5
  161.   if (Y > (WindowSize.y - BitmapSize)) {
  162.     Y = WindowSize.y - BitmapSize;
  163.   } else {
  164.     if (Y < 0)
  165.       Y = 0;
  166.   }
  167. }
  168.