home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / COSEmulator / COSEmulator- SRC / src / Screen.cpp < prev    next >
Encoding:
Text File  |  1998-06-21  |  8.7 KB  |  350 lines  |  [TEXT/CWIE]

  1. //===================================================================
  2. //=======================  Headers        =============================
  3.  
  4. #include "Screen.h"
  5. #include "GameUtilities.h"
  6. #include "ApplicationHandler.h"
  7. #include "MenuBar.h"
  8. #include "Quickdraw.h"
  9.  
  10. //===================================================================
  11. //=======================  Globals        =============================
  12.  
  13. COSScreen    screen;
  14.  
  15.  
  16. //===================================================================
  17. //=======================  #define        =============================
  18.  
  19.  
  20. //===================================================================
  21. //=======================  Function Prototypes    =====================
  22.  
  23. /*----------------------------------------------------------------------------\
  24.  
  25.     COSScreen :: Constructor
  26.  
  27. \----------------------------------------------------------------------------*/
  28.     COSScreen :: COSScreen( void )
  29. {
  30.     numUpdate = 0;
  31.     drawAll = false;
  32.     MySetRect( &screenSize , 0 , 0 , 1024 , 768 );
  33.     MySetRect( &viewArea , 0 , 20 , 1024, 768);
  34. }
  35.  
  36. /*----------------------------------------------------------------------------\
  37.  
  38.     COSScreen :: Deconstructor
  39.  
  40. \----------------------------------------------------------------------------*/
  41.     COSScreen :: ~COSScreen( void )
  42. {
  43.     LMSetMBarHeight( OldMBarHeight );
  44. }
  45.  
  46. /*----------------------------------------------------------------------------\
  47.  
  48.     COSScreen :: InitScreen
  49.  
  50. \----------------------------------------------------------------------------*/
  51. Boolean COSScreen :: InitScreen( void )
  52. {
  53.     OldMBarHeight = LMGetMBarHeight();
  54.     LMSetMBarHeight( 0 );
  55.     
  56. // first center the window
  57.     Rect    myScreenBoundsRect;
  58.     short    mX,mY;
  59.     rect    bounds;
  60.     
  61.     bounds = screenSize;
  62.     
  63. //    bounds.top += 20;
  64. //    bounds.bottom += 20;
  65.     
  66.     mX = rToR( bounds).right - rToR( bounds).left; // find the width
  67.     mY = rToR( bounds).bottom - rToR( bounds).top; // find the hieght, 
  68.  
  69.     myScreenBoundsRect = qd.screenBits.bounds;         // get the screens rect
  70.     
  71.     rToR( bounds).left = ((myScreenBoundsRect.right - myScreenBoundsRect.left) >> 1 ) - (mX>>1);
  72.     rToR( bounds).top = ((myScreenBoundsRect.bottom - ( myScreenBoundsRect.top  )) >> 1 ) - (mY>>1);
  73.     rToR( bounds).right = bounds.left + mX;
  74.     rToR( bounds).bottom = bounds.top + mY;
  75.  
  76. // now make the window
  77.     screen = NewCWindow(      nil,
  78.                                 & rToR(bounds),
  79.                                 "\p",
  80.                                 true,
  81.                                 plainDBox,
  82.                                 WindowPtr( -1 ),
  83.                                 false,
  84.                                 nil );
  85.     if( screen == NULL )
  86.         return false;
  87.                                         
  88.     bounds.right -= bounds.left;
  89.     bounds.bottom -= bounds.top;
  90.     bounds.left = 0;
  91.     bounds.top = 0;
  92.  
  93.     ClipRect( & rToR( bounds ) );
  94.     
  95.         
  96.     Rect    mBarRect;
  97.     RgnHandle    mBarRgn = NewRgn();
  98.     RgnHandle   grayRegion  =   nil;
  99.     
  100.     grayRegion = GetGrayRgn();
  101.     
  102.     SetRect( &mBarRect, qd.screenBits.bounds.left,
  103.                     qd.screenBits.bounds.top,
  104.                     qd.screenBits.bounds.right,
  105.                     qd.screenBits.bounds.top+OldMBarHeight );    
  106.     
  107.     
  108.     RectRgn( mBarRgn , &mBarRect );
  109.     //UnionRgn( grayRegion, mBarRgn, grayRegion);
  110.     RectRgn( grayRegion, &rToR(bounds) );
  111.     UnionRgn( screen->visRgn , mBarRgn , screen->visRgn );
  112.     
  113.     CopyRgn( screen->visRgn , screen->clipRgn);
  114.     
  115.     DisposeRgn( mBarRgn );
  116.     
  117.     
  118.  
  119.  
  120.     
  121.     SetPort( (GrafPtr) screen );
  122. // create the screen buffer now
  123.  
  124.     MySetRectWH( &bounds , 0 , 0 , 1024 , 768 );
  125.     
  126.     if( !viewBuffer.NewBuff( bounds ) )
  127.         return false;
  128.     
  129.     PaintRect( &viewBuffer , &bounds , 0xffff );
  130.     AddRectToUpdate( bounds );
  131.     
  132.     return true;
  133. }
  134.  
  135. /*----------------------------------------------------------------------------\
  136.  
  137.     COSScreen :: UpdateMainWindow
  138.  
  139. \----------------------------------------------------------------------------*/
  140. void COSScreen :: UpdateMainWindow( void )
  141. {
  142.  
  143.     if( !drawAll )
  144.     {
  145.         short    i;
  146.         
  147.         for( i = 0; i < numUpdate; i++ )
  148.         {
  149.                 CleanUp( &update[ i ] );
  150.         
  151.                 CopyBits( (BitMap *) (*(viewBuffer.GetOffScreen()->PixMap)), &(screen->portBits),
  152.                     & rToR( update[ i ] ), & rToR( update[ i ] ), srcCopy , (RgnHandle)nil );    
  153.     
  154.         }
  155.         
  156.         numUpdate = 0;
  157.     }
  158.     else
  159.     {
  160.         CleanUp( &viewArea );
  161.  
  162.         CopyBits( (BitMap *) (*(viewBuffer.GetOffScreen()->PixMap)), &(screen->portBits),
  163.                 & rToR( screenSize ), & rToR( screenSize ), srcCopy , (RgnHandle)nil );    
  164.  
  165.         drawAll = false;
  166.     }
  167. }
  168.  
  169. /*----------------------------------------------------------------------------\
  170.  
  171.     COSScreen :: RectAllInScreen
  172.  
  173. \----------------------------------------------------------------------------*/
  174. Boolean COSScreen :: RectAllInScreen( const rect *where )
  175. {
  176.     if( where->left < 0 )
  177.         return false;
  178.     
  179.     if( where->top < 0 )
  180.         return false;
  181.         
  182.     if( where->right >= screenSize.right )
  183.         return false;
  184.     
  185.     if( where->bottom >= screenSize.bottom )
  186.         return false;
  187.         
  188.     return true;
  189. }
  190.  
  191. /*----------------------------------------------------------------------------\
  192.  
  193.     COSScreen :: DrawGeneric
  194.  
  195. \----------------------------------------------------------------------------*/
  196. void COSScreen :: DrawGeneric(     OffScreenBuff *srcBuff , rect *srcRect , 
  197.                                 rect *destRect , rect *crop , ushort options , 
  198.                                 uchar more , ushort color , Boolean menu )
  199. {
  200.     rect    blarg;
  201.     
  202.     if( menu )
  203.         blarg = screenSize;
  204.     else
  205.         blarg = viewArea;
  206.         
  207.     if( crop == NULL )
  208.     {    
  209.         ::DrawGeneric( srcBuff , srcRect , &viewBuffer , destRect , 
  210.                      &blarg , kDrawCrop1 | options, more , color );
  211.  
  212.     }
  213.     else
  214.     {
  215.         rect    where;
  216.         
  217.         MyCropRect( crop , &blarg , &where );
  218.         
  219.         ::DrawGeneric( srcBuff , srcRect , &viewBuffer , destRect ,
  220.                      &where , kDrawCrop1 | options , more , color );
  221.     }
  222. }
  223.  
  224. /*----------------------------------------------------------------------------\
  225.  
  226.     COSScreen :: DrawAll
  227.  
  228. \----------------------------------------------------------------------------*/
  229. void    COSScreen :: DrawAll( void )
  230. {
  231.     drawAll = true;
  232. }
  233.  
  234. /*----------------------------------------------------------------------------\
  235.  
  236.     COSScreen :: AddRectToUpdate
  237.  
  238. - this asumes everything is with in the view area and will not curropt memory
  239.  
  240. \----------------------------------------------------------------------------*/
  241. void    COSScreen :: AddRectToUpdate( rect theRect )
  242. {
  243. ulong    area = 0xffffffff;
  244. ulong    tempArea;
  245. int        counter;
  246. int        best = -1;
  247. rect    tempRect;
  248. rect    bestRect;
  249.  
  250.     // crop it to make sure it is with in the buffer
  251.     MyCropRect( &theRect , &screenSize , &theRect );
  252.     
  253.     // adjust the rect so that it can be blited faster
  254.     AdjustRect( &theRect );
  255.     
  256.     // see if it over laps any other rectangle
  257.  
  258.     for( counter = 0; counter < numUpdate; counter++ )
  259.     {
  260.         if( MySectRect( &theRect , &update[ counter ] ) )
  261.         {
  262.         // combined the two rects
  263.             theRect.left = ( theRect.left < update[ counter ].left ) 
  264.                                 ? theRect.left :  update[ counter ].left;
  265.                                 
  266.             theRect.right = ( theRect.right > update[ counter ].right ) 
  267.                                 ? theRect.right :  update[ counter ].right;
  268.                                 
  269.             theRect.top = ( theRect.top < update[ counter ].top ) 
  270.                                 ? theRect.top :  update[ counter ].top;
  271.                                 
  272.             theRect.bottom = ( theRect.bottom > update[ counter ].bottom ) 
  273.                                 ? theRect.bottom : update[ counter ].bottom;        
  274.         
  275.         // remove the rectfrom the list
  276.             numUpdate--;
  277.             for( ; counter < numUpdate; counter++ )
  278.             {
  279.                 update[ counter ] = update[ counter + 1];
  280.             }
  281.         
  282.         // put the new combined rect through this proccess again
  283.             AddRectToUpdate( theRect );
  284.             
  285.             return;
  286.         }
  287.     }
  288.     
  289.     // if there is room just add it to the list
  290.     if( numUpdate < kMaxUpdate )
  291.     {
  292.         update[ numUpdate ] = theRect;
  293.         numUpdate++;
  294.     }
  295.     else
  296.     {
  297.     // else find what rect combined with this will be the smallest area
  298.         for( counter = 0; counter < kMaxUpdate; counter++ )
  299.         {
  300.             tempRect.left = ( tempRect.left < update[ counter ].left ) ?
  301.                             tempRect.left : update[ counter ].left;
  302.                             
  303.             tempRect.right = ( tempRect.right > update[ counter ].right ) ?
  304.                             tempRect.right : update[ counter ].right;
  305.                                 
  306.             tempRect.top = ( tempRect.top < update[ counter ].top ) ?
  307.                             tempRect.top : update[ counter ].top;
  308.                                 
  309.             tempRect.bottom = ( tempRect.bottom > update[ counter ].bottom ) ?
  310.                             tempRect.bottom : update[ counter ].bottom;    
  311.         
  312.             tempArea = (tempRect.right - tempRect.left) * (tempRect.bottom - tempRect.top );
  313.             
  314.             if( tempArea < area )
  315.             {
  316.                 area = tempArea;
  317.                 best = counter;
  318.                 bestRect = tempRect;
  319.             }            
  320.         }
  321.         
  322.         AdjustRect( &bestRect );
  323.         update[ best ] = bestRect;
  324.     }
  325. }
  326.  
  327. /*----------------------------------------------------------------------------\
  328.  
  329.     COSScreen :: AdjustRect
  330.  
  331. - makes it so it is faster to blit
  332. \----------------------------------------------------------------------------*/
  333. void    COSScreen :: AdjustRect( rect *theRect )
  334. {
  335. // make it aligned
  336.     theRect->left = theRect->left & 0xfffc;
  337.     theRect->right = ( theRect->right + 3 ) & 0xfffc;
  338. }
  339.  
  340. /*----------------------------------------------------------------------------\
  341.  
  342.     COSScreen :: CleanUp
  343.  
  344. \----------------------------------------------------------------------------*/
  345. void COSScreen :: CleanUp( rect *where )
  346. {
  347.     AH.CleanUp( where );
  348.     menuBar.UpdateMenuBar( where );
  349. }
  350.