home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / GrafSys 2.0 / GrafSys 2.0 source / OffScreenGraphics.p < prev    next >
Encoding:
Text File  |  1993-07-27  |  5.0 KB  |  168 lines  |  [TEXT/PJMM]

  1. unit OffScreenGraphics;
  2.  
  3.  
  4. interface
  5.  
  6.     uses
  7.         Matrix, OffScreenCore, GrafSysCore, GrafSysScreen;
  8.  
  9.     const
  10.         cBitDepth = 8; (* Standard bit-depth for all offscreen devices GrafSys uses *)
  11.         cMaxBitDepth = 8;
  12.         cStdColorCLUT = 72;
  13.  
  14.     var
  15.         currOSPixMap: PixMapHandle;
  16.  
  17.     function AttachOffScreen (theWindow: WindowPtr; theColors: CTabHandle): integer; (* attach OS port to window *)
  18. (* pass CTabHandle(-1) to use window's CLUT or CTabHandle(-2) as parameter if you want standard color CLUT *)
  19.  
  20.     function ChangeOffscreen (theWindow: WindowPtr; theColors: CTabHandle): integer; (* make OS port the same as the window *)
  21. (* pass CTabHandle(-1) as parameter if no change to CLUT *)
  22.  
  23.     function CloseOffscreen (theWindow: WindowPtr): integer; (* dispose OS port *)
  24.  
  25.     function BeginOSDraw (theWindow: WindowPtr): integer; (* begin drawing to Off-Screen *)
  26.     function EndOSDraw (theWindow: WindowPtr): integer; (* end drawing to Off-Screen *)
  27.     function CopyOS2Screen (theWindow: WindowPtr; theRect: Rect; copyMode: Integer): integer; (* copy offscreen to screen *)
  28.  
  29. implementation
  30.  
  31.  
  32.     function AttachOffScreen (theWindow: WindowPtr; theColors: CTabHandle): integer; (* attach OS port to window *)
  33.         var
  34.             the3DWindow: TPort3DPtr;
  35.             theErr: Integer;
  36.  
  37.     begin
  38.         if not is3DPort(theWindow) then
  39.             begin
  40.                 AttachOffScreen := cNo3DWindow;
  41.                 Exit(AttachOffScreen);
  42.             end;
  43.         the3DWindow := TPort3DPtr(theWindow); (* convert it to this pointer type *)
  44.  
  45.      (* get color information *)
  46.         if theColors = CTabHandle(-2) then (* get standard 256 color CLUT *)
  47.             theColors := GetCTable(cStdColorCLUT)
  48.         else if theColors = CTabHandle(-1) then (* use window's curently active color table *)
  49.             begin
  50.                 if CGrafPtr(theWindow)^.portPixMap^^.pixelSize > cMaxBitDepth then (* Can only use 1-8 bits/pixel CLUT. Check it *)
  51.                     begin
  52.                         AttachOffScreen := cCantUseWindowCLUT;
  53.                         Exit(AttachOffScreen);
  54.                     end;
  55.                 theColors := CGrafPtr(theWindow)^.portPixMap^^.pmTable;
  56.             end;
  57.  
  58.         the3DWindow^.theOffscreen.theColors := theColors;
  59.         theErr := CreateOffScreen(theWindow^.portRect, cBitDepth, theColors, the3DWindow^.theOffscreen.thePort, the3DWindow^.theOffscreen.theDevice);
  60.         if theErr <> noErr then
  61.             AttachOffscreen := cCantCreateOffscreen
  62.         else
  63.             AttachOffscreen := noErr;
  64.     end;
  65.  
  66.     function ChangeOffscreen (theWindow: WindowPtr; theColors: CTabHandle): integer; (* make OS port the same as the window *)
  67.         var
  68.             the3DWindow: TPort3DPtr;
  69.             theErr: Integer;
  70.  
  71.     begin
  72.         if not is3DPort(theWindow) then
  73.             begin
  74.                 ChangeOffscreen := cNo3DWindow;
  75.                 Exit(ChangeOffscreen);
  76.             end;
  77.  
  78.         the3DWindow := TPort3DPtr(theWindow);
  79.         if theColors = CTabHandle(-1) then
  80.             theColors := the3DWindow^.theOffscreen.theColors;
  81.         the3DWindow^.theOffscreen.theColors := theColors; (* save for later references *)
  82.         theErr := UpdateOffScreen(theWindow^.portRect, cBitDepth, theColors, the3DWindow^.theOffscreen.thePort, the3DWindow^.theOffscreen.theDevice);
  83.         if theErr <> noErr then
  84.             ChangeOffscreen := cCantChangeOffscreen
  85.         else
  86.             ChangeOffscreen := noErr;
  87.     end;
  88.  
  89.     function CloseOffscreen (theWindow: WindowPtr): integer; (* dispose OS port *)
  90.         var
  91.             the3DWindow: TPort3DPtr;
  92.  
  93.     begin
  94.         if not is3DPort(theWindow) then
  95.             begin
  96.                 CloseOffscreen := cNo3DWindow;
  97.                 Exit(CloseOffscreen);
  98.             end;
  99.         the3DWindow := TPort3DPtr(theWindow);
  100.         if the3DWindow^.theOffscreen.thePort <> nil then
  101.             DisposeOffScreen(the3DWindow^.theOffscreen.thePort, the3DWindow^.theOffscreen.theDevice);
  102.         CloseOffscreen := noErr;
  103.     end;
  104.  
  105.  
  106.     function BeginOSDraw (theWindow: WindowPtr): integer; (* begin drawing to Off-Screen *)
  107.         var
  108.             the3DWindow: TPort3DPtr;
  109.  
  110.     begin
  111.         if not is3DPort(theWindow) then
  112.             begin
  113.                 BeginOSDraw := cNo3DWindow;
  114.                 Exit(BeginOSDraw);
  115.             end;
  116.         the3DWindow := TPort3DPtr(theWindow);
  117.         if the3DWindow^.theOffscreen.thePort = nil then
  118.             begin
  119.                 BeginOSDraw := cNoOSAttached;
  120.                 Exit(BeginOSDraw);
  121.             end;
  122.  
  123.         the3DWindow^.theOffscreen.mainDevice := GetGDevice;
  124.         SetPort(GrafPtr(the3DWindow^.theOffscreen.thePort));
  125.         SetGDevice(the3DWindow^.theOffscreen.theDevice);
  126.         currOSPixMap := the3DWindow^.theOffScreen.thePort^.portPixMap;
  127.         BeginOSDraw := noErr;
  128.     end;
  129.  
  130.     function EndOSDraw (theWindow: WindowPtr): integer; (* end drawing to Off-Screen *)
  131.         var
  132.             the3DWindow: TPort3DPtr;
  133.  
  134.     begin
  135.         if not is3DPort(theWindow) then
  136.             begin
  137.                 EndOSDraw := cNo3DWindow;
  138.                 Exit(EndOSDraw);
  139.             end;
  140.         the3DWindow := TPort3DPtr(theWindow);
  141.         SetPort(theWindow);
  142.         SetGDevice(the3DWindow^.theOffscreen.mainDevice);
  143.         currOSPixMap := nil;
  144.         EndOSDraw := noErr;
  145.     end;
  146.  
  147.     function CopyOS2Screen (theWindow: WindowPtr; theRect: Rect; copyMode: Integer): integer; (* copy offscreen to screen *)
  148.         var
  149.             the3DWindow: TPort3DPtr;
  150.  
  151.     begin
  152.         if not is3DPort(theWindow) then
  153.             begin
  154.                 CopyOS2Screen := cNo3DWindow;
  155.                 Exit(CopyOS2Screen);
  156.             end;
  157.         the3DWindow := TPort3DPtr(theWindow);
  158.         if the3DWindow^.theOffscreen.thePort = nil then
  159.             begin
  160.                 CopyOS2Screen := cNoOSAttached;
  161.                 Exit(CopyOS2Screen);
  162.             end;
  163.  
  164.         CopyBits(GrafPtr(the3DWindow^.theOffscreen.thePort)^.portBits, theWindow^.portBits, theRect, theRect, copyMode, nil);
  165.         CopyOS2Screen := noErr;
  166.     end;
  167.  
  168. end.