home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / gblanker3.5.src.lha / GSource / Blankers / Garshnelib / Garshnelib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  12.4 KB  |  530 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <graphics/gfxmacros.h>
  11. #include <graphics/videocontrol.h>
  12. #include <intuition/intuitionbase.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/graphics_protos.h>
  16. #include <clib/intuition_protos.h>
  17. #include <clib/asl_protos.h>
  18. #include <clib/mathffp_protos.h>
  19. #include <clib/alib_protos.h>
  20.  
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/graphics_pragmas.h>
  23. #include <pragmas/intuition_pragmas.h>
  24. #include <pragmas/asl_pragmas.h>
  25. #include <pragmas/mathffp_pragmas.h>
  26.  
  27. #include "Garshnelib_protos.h"
  28.  
  29. #define CastAndShift( x ) (( SPFix( SPMul( x, SPFlt( Colors )))) << Shift )
  30.  
  31. VOID SASM ScreenModeRequest( AREG(0) struct Window *Wnd,
  32.                             AREG(1) LONG *Mode, AREG(2) LONG *Depth )
  33. {
  34.     struct AslBase *AslBase;
  35.     
  36.     if( AslBase = ( struct AslBase * )OpenLibrary( AslName, 38L ))
  37.     {
  38.         struct ScreenModeRequester *smRequest;
  39.         
  40.         if( smRequest = AllocAslRequestTags( ASL_ScreenModeRequest, TAG_END ))
  41.         {
  42.             if( Depth )
  43.             {
  44.                 if( AslRequestTags( smRequest, ASLSM_Window, Wnd,
  45.                                    ASLSM_SleepWindow, TRUE,
  46.                                    ASLSM_TitleText, "Screen Mode",
  47.                                    ASLSM_InitialDisplayID, *Mode,
  48.                                    ASLSM_InitialDisplayDepth, *Depth,
  49.                                    ASLSM_DoDepth, TRUE, TAG_END ))
  50.                 {
  51.                     *Mode = smRequest->sm_DisplayID;
  52.                     *Depth = smRequest->sm_DisplayDepth;
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 if( AslRequestTags( smRequest, ASLSM_Window, Wnd,
  58.                                    ASLSM_SleepWindow, TRUE,
  59.                                    ASLSM_TitleText, "Screen Mode",
  60.                                    ASLSM_InitialDisplayID, *Mode, TAG_END ))
  61.                     *Mode = smRequest->sm_DisplayID;
  62.             }
  63.             FreeAslRequest( smRequest );
  64.         }
  65.         CloseLibrary(( struct Library * )AslBase );
  66.     }
  67. }
  68.  
  69. #define NUMCOLORS 45
  70. LONG spectrum[] = {
  71.     0x0F00, 0x0E10, 0x0D20, 0x0C30, 0x0B40, 0x0A50, 0x0960,    0x0870, 0x0780,
  72.     0x0690, 0x05A0, 0x04B0, 0x03C0, 0x02D0,    0x01E0, 0x00F0, 0x00E1, 0x00D2,
  73.     0x00C3, 0x00B4, 0x00A5,    0x0096, 0x0087, 0x0078, 0x0069, 0x005A, 0x004B,
  74.     0x003C,    0x002D, 0x001E, 0x000F, 0x010E, 0x020D, 0x030C, 0x040B,    0x050A,
  75.     0x0609, 0x0708, 0x0807, 0x0906, 0x0A05, 0x0B04,    0x0C03, 0x0D02, 0x0E01
  76.     };
  77.  
  78. VOID SASM setCopperList( DREG(0) LONG Hei, DREG(1) LONG Col,
  79.                         AREG(0) struct ViewPort *VPort,
  80.                         AREG(1) struct Custom *Custom )
  81. {
  82.     struct Library *GfxBase, *IntuitionBase;
  83.     struct UCopList *uCopList;
  84.  
  85.     IntuitionBase = OpenLibrary( "intuition.library", 39 );
  86.     GfxBase = OpenLibrary( "graphics.library", 39 );
  87.     
  88.     if( IntuitionBase && GfxBase &&
  89.        ( uCopList = AllocVec( sizeof( struct UCopList ), MEMF_CLEAR )))
  90.     {
  91.         struct TagItem uCopTags[] = {{VTAG_USERCLIP_SET,0L},{VTAG_END_CM,0L}};
  92.         LONG i, spc;
  93.  
  94.         spc = Hei/NUMCOLORS;
  95.         CINIT( uCopList, NUMCOLORS );
  96.         for( i = 0; i < NUMCOLORS; ++i )
  97.         {
  98.             CWAIT( uCopList, i * spc, 0 );
  99.             CMOVE( uCopList, Custom->color[Col], spectrum[i%NUMCOLORS] );
  100.         }
  101.         CEND( uCopList );
  102.         Forbid();
  103.         VPort->UCopIns = uCopList;
  104.         Permit();
  105.         VideoControl( VPort->ColorMap, uCopTags );
  106.         RethinkDisplay();
  107.     }
  108.     
  109.     if( GfxBase )
  110.         CloseLibrary( GfxBase );
  111.     
  112.     if( IntuitionBase )
  113.         CloseLibrary( IntuitionBase );
  114. }
  115.  
  116. VOID SASM clearCopperList( AREG(0) struct ViewPort *VPort )
  117. {
  118.     struct Library *GfxBase;
  119.     
  120.     if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  121.     {
  122.         struct TagItem uCopTags[] = {{VTAG_USERCLIP_CLR,0L},{VTAG_END_CM,0L}};
  123.         struct UCopList *uCopList;
  124.  
  125.         VideoControl( VPort->ColorMap, uCopTags );
  126.         Forbid();
  127.         uCopList = VPort->UCopIns;
  128.         VPort->UCopIns = 0L;
  129.         Permit();
  130.         FreeVec( uCopList );
  131.     }
  132. }
  133.  
  134. LONG SASM getTopScreenMode( VOID )
  135. {
  136.     struct IntuitionBase *IntuitionBase;
  137.     struct Library *GfxBase;
  138.     struct Screen *pubScr;
  139.     LONG scrMode;
  140.     BPTR lock;
  141.     
  142.     IntuitionBase =
  143.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 39L );
  144.     GfxBase = OpenLibrary( "graphics.library", 39L );
  145.     
  146.     if( IntuitionBase && GfxBase )
  147.     {
  148.         lock = LockIBase( 0 );
  149.         pubScr = IntuitionBase->FirstScreen;
  150.         scrMode = GetVPModeID(&( pubScr->ViewPort));
  151.         UnlockIBase( lock );
  152.     }
  153.     
  154.     if( GfxBase )
  155.         CloseLibrary( GfxBase );
  156.     
  157.     if( IntuitionBase )
  158.         CloseLibrary(( struct Library * )IntuitionBase );
  159.     
  160.     return scrMode;
  161. }
  162.  
  163. LONG SASM getTopScreenDepth( VOID )
  164. {
  165.     struct IntuitionBase *IntuitionBase;
  166.     LONG Dep = 0;
  167.     
  168.     if( IntuitionBase =
  169.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L ))
  170.     {
  171.         struct Screen *PubScr;
  172.         struct DrawInfo *dri;
  173.         BPTR Lock;
  174.  
  175.         Lock = LockIBase( 0 );
  176.         PubScr = IntuitionBase->FirstScreen;
  177.         if( dri = GetScreenDrawInfo( PubScr ))
  178.         {
  179.             Dep = dri->dri_Depth;
  180.             FreeScreenDrawInfo( PubScr, dri );
  181.         }
  182.         UnlockIBase( Lock );
  183.     
  184.         CloseLibrary(( struct Library * )IntuitionBase );
  185.     }
  186.     
  187.     return Dep;
  188. }
  189.  
  190. struct Screen *SASM cloneTopScreen( DREG(0) LONG MoreColors,
  191.                                    DREG(1) LONG GetPublic )
  192. {
  193.     LONG sMod, sDep, i, Wid, Hei, offx, offy;
  194.     struct IntuitionBase *IntuitionBase;
  195.     struct Screen *Scr, *nScr = 0L;
  196.     struct Rectangle DispRect;
  197.     struct Library *GfxBase;
  198.     struct DrawInfo *dri;
  199.     UWORD *cols;
  200.     BPTR lock;
  201.     
  202.     IntuitionBase =
  203.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 39L );
  204.     GfxBase = OpenLibrary( "graphics.library", 39L );
  205.     
  206.     if( IntuitionBase && GfxBase )
  207.     {
  208.         /* Lock IntuitionBase so nothing goes away */
  209.         lock = LockIBase( 0 );
  210.         
  211.         /* Grab the first screen and get its attributes */
  212.         if( GetPublic )
  213.             Scr = LockPubScreen( 0L );
  214.         else
  215.             Scr = IntuitionBase->FirstScreen;
  216.         sMod = GetVPModeID(&( Scr->ViewPort )); /* Screen Mode ID */
  217.         offx = Scr->LeftEdge; 
  218.         offy = Scr->TopEdge;
  219.         Wid = Scr->Width;
  220.         Hei = Scr->Height;
  221.         if( dri = GetScreenDrawInfo( Scr ))
  222.             sDep = MoreColors ? dri->dri_Depth + 1 : dri->dri_Depth;
  223.         if( cols = AllocVec( sizeof( WORD ) * ( 1L << sDep ), MEMF_CLEAR ))
  224.             for( i = 0; i < ( 1L << dri->dri_Depth ); ++i )
  225.                 cols[i] = GetRGB4( Scr->ViewPort.ColorMap, i );
  226.         
  227.         if( GetPublic )
  228.             UnlockPubScreen( 0L, Scr );
  229.         
  230.         UnlockIBase( lock );
  231.         
  232.         QueryOverscan( sMod, &DispRect, OSCAN_TEXT );
  233.         
  234.         Wid = min( Wid, DispRect.MaxX - DispRect.MinX + 1 );
  235.         Hei = min( Hei, DispRect.MaxY - DispRect.MinY + 1 );
  236.         
  237.         if( sMod != INVALID_ID )
  238.         {
  239.             nScr = OpenScreenTags( NULL, SA_DisplayID, sMod, SA_Depth, sDep,
  240.                                   SA_Width, Wid, SA_Height, Hei,
  241.                                   SA_Behind, TRUE, SA_Quiet, TRUE, TAG_DONE );
  242.             if( nScr )
  243.             {
  244.                 BltBitMap( Scr->RastPort.BitMap, offx < 0 ? -offx : 0,
  245.                           offy < 0 ? -offy : 0, nScr->RastPort.BitMap, 0, 0,
  246.                           Wid, Hei, 0x00C0, 0x00FF, NULL );
  247.                 LoadRGB4( &(nScr->ViewPort), cols, 1L << sDep );
  248.                 if( offx > 0 )
  249.                     MoveScreen( nScr, offx, 0 );
  250.                 WaitBlit();
  251.                 ScreenToFront( nScr );
  252.             }
  253.         }
  254.         
  255.         if( cols )
  256.             FreeVec( cols );
  257.         
  258.         if( dri )
  259.             FreeScreenDrawInfo( Scr, dri );
  260.     }
  261.     
  262.     if( GfxBase )
  263.         CloseLibrary( GfxBase );
  264.     
  265.     if( IntuitionBase )
  266.         CloseLibrary(( struct Library * )IntuitionBase );
  267.     
  268.     return nScr;
  269. }
  270.  
  271. ULONG *SASM GetColorTable( AREG(0) struct Screen *Screen )
  272. {
  273.     struct Library *GfxBase;
  274.     ULONG *ColorTable = 0L;
  275.  
  276.     if( GfxBase = OpenLibrary( "graphics.library", 39L ))
  277.     {
  278.         LONG NumCols = ( 1L << Screen->RastPort.BitMap->Depth );
  279.  
  280.         ColorTable = AllocVec( sizeof( LONG ) * ( 3 * NumCols + 2 ),
  281.                               MEMF_CLEAR );
  282.         if( ColorTable )
  283.         {
  284.             GetRGB32( Screen->ViewPort.ColorMap, 0, NumCols, ColorTable + 1 );
  285.             ColorTable[0] = ( NumCols << 16L );
  286.         }
  287.  
  288.         CloseLibrary( GfxBase );
  289.     }
  290.  
  291.     return ColorTable;
  292. }
  293.  
  294. LONG SASM AvgBitsPerGun( DREG(0) LONG ModeID )
  295. {
  296.     struct Library *GfxBase;
  297.     LONG BPG = 8;
  298.  
  299.     if( GfxBase = OpenLibrary( "graphics.library", 37L ))
  300.     {
  301.         struct DisplayInfo Inf;
  302.     
  303.         if( GetDisplayInfoData( 0L, ( UBYTE * )&Inf, sizeof( Inf ), DTAG_DISP,
  304.                                ModeID ))
  305.             BPG = ( Inf.RedBits + Inf.GreenBits + Inf.BlueBits ) / 3;
  306.  
  307.         CloseLibrary( GfxBase );
  308.     }
  309.  
  310.     return BPG;
  311. }
  312.  
  313. VOID SASM FadeAndLoadTable( AREG(0) struct Screen *Screen, DREG(0) LONG BPG,
  314.                            AREG(1) ULONG *ColorTable )
  315. {
  316.     struct Library *IntuitionBase, *GfxBase;
  317.     
  318.     if( !ColorTable )
  319.         return;
  320.  
  321.     IntuitionBase = OpenLibrary( "intuition.library", 39L );
  322.     GfxBase = OpenLibrary( "graphics.library", 39L );
  323.     
  324.     if( IntuitionBase && GfxBase )
  325.     {
  326.         LONG NumCols = ( 1L << Screen->RastPort.BitMap->Depth ), i;
  327.         
  328.         for( i = 0; i < 3 * NumCols; ++i )
  329.         {
  330.             if( ColorTable[i+1] > 1L << ( 32 - BPG ))
  331.                 ColorTable[i+1] -= ( 1L << ( 32 - BPG ));
  332.             else
  333.                 ColorTable[i+1] = 0;
  334.         }
  335.             
  336.         LoadRGB32(&( Screen->ViewPort ), ColorTable );
  337.     }
  338.     
  339.     if( GfxBase )
  340.         CloseLibrary( GfxBase );
  341.     
  342.     if( IntuitionBase )
  343.         CloseLibrary( IntuitionBase );
  344. }
  345.  
  346. struct Window *SASM BlankMousePointer( AREG(0) struct Screen *Scr )
  347. {
  348.     struct Library *IntuitionBase, *GfxBase;
  349.     struct Window *Wnd;
  350.     ULONG oldmodes;
  351.     
  352.     IntuitionBase = OpenLibrary( "intuition.library", 39L );
  353.     GfxBase = OpenLibrary( "graphics.library", 39L );
  354.     
  355.     if( IntuitionBase && GfxBase )
  356.     {
  357.         oldmodes = SetPubScreenModes( 0 );
  358.         if( Wnd = OpenWindowTags( 0L, WA_Activate, TRUE, WA_Left, 0, WA_Top, 0,
  359.                                  WA_Width, 1, WA_Height, 1, WA_Borderless,
  360.                                  TRUE, WA_CustomScreen, Scr, TAG_END ))
  361.         {
  362.             if( Wnd->UserData = AllocVec( 3 * 2 * sizeof( UWORD ),
  363.                                          MEMF_CHIP|MEMF_CLEAR ))
  364.             {
  365.                 /* Set a pointer sprite of size 16*1 (alloc and clear data for
  366.                    sprite def which is 2 control words, 2 words 4-color data
  367.                    for each line and 2 empty words at the end (as suggested in
  368.                    Hardware Reference Manual)). */
  369.                 SetPointer( Wnd, ( UWORD * )Wnd->UserData, 1, 16, 0, 0 );
  370.             }
  371.         }
  372.         SetPubScreenModes( oldmodes );
  373.     }
  374.     
  375.     if( GfxBase )
  376.         CloseLibrary( GfxBase );
  377.     
  378.     if( IntuitionBase )
  379.         CloseLibrary( IntuitionBase );
  380.  
  381.     return Wnd;
  382. }
  383.  
  384. VOID SASM UnblankMousePointer( AREG(0) struct Window *Wnd )
  385. {
  386.     struct Library *IntuitionBase;
  387.     
  388.     if( IntuitionBase = OpenLibrary( "intuition.library", 39L ))
  389.     {
  390.         if( Wnd )
  391.         {
  392.             if( Wnd->UserData )
  393.                 FreeVec( Wnd->UserData );
  394.             CloseWindow( Wnd );
  395.         }
  396.         CloseLibrary( IntuitionBase );
  397.     }
  398. }
  399.  
  400. Triplet *AllocTable( LONG Colors, LONG Dep, LONG Offset )
  401. {
  402.     struct Library *MathBase;
  403.     FLOAT incr, p2, p3, f, h;
  404.     LONG i, Shift, ih;
  405.     Triplet *Table;
  406.     
  407.     MathBase = OpenLibrary( "mathffp.library", 0L );
  408.     Table = AllocVec( 2*( Colors + Offset ) * sizeof( Triplet ), MEMF_CLEAR );
  409.     
  410.     if( MathBase && Table )
  411.     {
  412.         incr = SPDiv( SPFlt( Colors ), SPFlt( 360 ));
  413.         Shift = 32 - Dep;
  414.         
  415.         for( i = Offset, h = SPFlt( 0 ); i < Colors + Offset;
  416.             i++, h = SPAdd( h, incr ))
  417.         {
  418.             ih = SPFix( SPDiv( SPFlt( 60 ), h ));
  419.             f = SPSub( SPFlt( ih ), SPDiv( SPFlt( 60 ), h ));
  420.             p2 = SPSub( f, SPFlt( 1 ));
  421.             p3 = f;
  422.             switch( ih )
  423.             {
  424.             case 0:
  425.                 Table[i].Red = Colors << Shift;
  426.                 Table[i].Green = CastAndShift( p3 );
  427.                 Table[i].Blue = 0;
  428.                 break;
  429.             case 1:
  430.                 Table[i].Red = CastAndShift( p2 );
  431.                 Table[i].Green = Colors << Shift;
  432.                 Table[i].Blue = 0;
  433.                 break;
  434.             case 2:
  435.                 Table[i].Red = 0;
  436.                 Table[i].Green = Colors << Shift;
  437.                 Table[i].Blue = CastAndShift( p3 );
  438.                 break;
  439.             case 3:
  440.                 Table[i].Red = 0;
  441.                 Table[i].Green = CastAndShift( p2 );
  442.                 Table[i].Blue = Colors << Shift;
  443.                 break;
  444.             case 4:
  445.                 Table[i].Red = CastAndShift( p3 );
  446.                 Table[i].Green = 0;
  447.                 Table[i].Blue = Colors << Shift;
  448.                 break;
  449.             case 5:
  450.                 Table[i].Red = Colors << Shift;
  451.                 Table[i].Green = 0;
  452.                 Table[i].Blue = CastAndShift( p2 );
  453.                 break;
  454.             }
  455.         }
  456.         CopyMem(&( Table[Offset] ), &( Table[Colors+Offset] ),
  457.                 Colors * sizeof( Triplet ));
  458.     }
  459.     
  460.     if( MathBase )
  461.         CloseLibrary( MathBase );
  462.     else
  463.     {
  464.         FreeVec( Table );
  465.         Table = 0L;
  466.     }
  467.     
  468.     return Table;
  469. }
  470.  
  471. Triplet *SASM RainbowPalette( AREG(0) struct Screen *Screen,
  472.                              AREG(1) Triplet *Table, DREG(0) LONG Offset,
  473.                              DREG(1) LONG EP )
  474. {
  475.     struct Library *IntuitionBase, *GfxBase;
  476.     static LONG ColorPos = 0L;
  477.     
  478.     if( Screen )
  479.     {
  480.         IntuitionBase = OpenLibrary( "intuition.library", 39L );
  481.         GfxBase = OpenLibrary( "graphics.library", 39L );
  482.         
  483.         if( IntuitionBase && GfxBase )
  484.         {
  485.             LONG Colors = ( 1L << Screen->BitMap.Depth ) - Offset;
  486.             LONG PDep = ( Screen->BitMap.Depth < ( 9 - EP )) ?
  487.                 Screen->BitMap.Depth + EP : 8;
  488.             LONG PColors = ( 1L << PDep ) - Offset;
  489.             
  490.             if( !Table )
  491.             {
  492.                 Table = AllocTable( PColors, PDep, Offset );
  493.                 if( Offset )
  494.                     SetRGB32(&(Screen->ViewPort), 0, 0, 0, 0 );
  495.             }
  496.             
  497.             ColorPos = ( ++ColorPos % PColors );
  498.             
  499.             if( Table )
  500.             {
  501.                 LONG Temp1, Temp2;
  502.                 
  503.                 Temp1 = Table[ColorPos].Blue;
  504.                 Temp2 = Table[ColorPos+Colors+1].Red;
  505.                 
  506.                 Table[ColorPos].Blue = ( Colors << 16L ) + Offset;
  507.                 Table[ColorPos+Colors+1].Red = 0;
  508.                 
  509.                 LoadRGB32(&(Screen->ViewPort), &( Table[ColorPos].Blue ));
  510.                 
  511.                 Table[ColorPos].Blue = Temp1;
  512.                 Table[ColorPos+Colors+1].Red = Temp2;
  513.             }
  514.         }
  515.         
  516.         if( GfxBase )
  517.             CloseLibrary( GfxBase );
  518.         
  519.         if( IntuitionBase )
  520.             CloseLibrary( IntuitionBase );
  521.     }
  522.     else
  523.     {
  524.         if( Table )
  525.             FreeVec( Table );
  526.     }
  527.     
  528.     return Table;
  529. }
  530.