home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / precog2_1.lha / Precognition2_1 / src / src.lha / Precognition / imagebob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-13  |  4.6 KB  |  194 lines

  1. #include "ImageBob.h"
  2. #include <intuition/intuition.h>
  3. #include <exec/memory.h>
  4. #include <stdlib.h>
  5. #include <string.h> /* added for memset() prototype -- EDB */
  6. #ifndef __GNUC__
  7. #include <clib/graphics_protos.h>
  8. #include <clib/intuition_protos.h>
  9. #include <clib/layers_protos.h>
  10. #endif
  11. #ifdef __GNUC__
  12. #include <proto/graphics.h>
  13. #include <proto/intuition.h>
  14. #include <proto/layers.h>
  15. #endif
  16. #ifdef __SASC
  17. #include <proto/graphics.h>
  18. #include <proto/intuition.h>
  19. #include <proto/layers.h>
  20. #endif
  21.  
  22. #define ALLOCRASTER( w, h ) (WORD *)AllocRaster( w, h )
  23. #define WORDALIGN( n ) ( ( n + 15 ) & ~0x0F )
  24. /* rounds 'n' up to the nearest 16. */
  25.  
  26.  
  27. BOOL
  28. ImageBob_InitImage( struct Image *image,
  29.                     UWORD         width,
  30.                     UWORD         height,
  31.                     UWORD         depth )
  32. {
  33.    UWORD    *image_mem;
  34.    UWORD     wa_width;
  35.    UWORD     i, planepick;
  36.  
  37.    /* Allocate memory for image. */
  38.    wa_width = WORDALIGN( width );
  39.  
  40.    if( image_mem = (UWORD *)AllocRaster( width, height * depth ) )
  41.    {
  42.       /* clear the memory. */
  43.       memset( image_mem, (char)0, ( wa_width/8 )* height * depth );
  44.  
  45.       /* initialize image. */
  46.       image->LeftEdge   = 0;
  47.       image->TopEdge    = 0;
  48.       image->Width      = width;
  49.       image->Height     = height;
  50.       image->Depth      = depth;
  51.       image->ImageData  = image_mem;
  52.  
  53.       for( i = 0, planepick = 0; i < depth; i++ )
  54.       {
  55.          planepick |= ( 1 << i );
  56.       }
  57.  
  58.       image->PlanePick  = planepick;
  59.       image->PlaneOnOff = 0x00;
  60.       image->NextImage  = NULL;
  61.  
  62.       return TRUE;
  63.    }
  64.    return FALSE;
  65. }
  66.  
  67.  
  68. BOOL
  69. ImageBob_InitShadowMask( ImageBob *ibob )
  70. {
  71.    struct Image image;
  72.    UWORD wa_width;
  73.  
  74.    wa_width = WORDALIGN( ibob->Image.Width );
  75.  
  76.    /* Allocate memory for ShadowMask. */
  77.    if( ibob->Bob.ImageShadow = ALLOCRASTER( wa_width,
  78.                                            ibob->Image.Height ) )
  79.    {
  80.       /* printf( "Allocated Raster memory for ImageShadow.\n" ); */
  81.  
  82.       memset( ibob->Bob.ImageShadow, (char)0, ( wa_width/8 ) * ibob->Image.Height );
  83.  
  84.       /* cobble up a fake 'image' for the shadow port. */
  85.  
  86.       image           = ibob->Image;
  87.       image.Width     = wa_width;
  88.       image.Depth     = 1;
  89.       image.PlanePick = 1;
  90.       image.ImageData = (UWORD *)ibob->Bob.ImageShadow;
  91.  
  92.       if( ClippedImageRastPort_Init( &ibob->ShadowPort, &image ) )
  93.       {
  94.          /* printf( "Initialized clipped shadowport.\n" ); */
  95.          return TRUE;
  96.       }
  97.    }
  98.  
  99.    return FALSE;
  100. }
  101.  
  102.  
  103.  
  104. BOOL
  105. ImageBob_InitVSpriteBob( ImageBob *ibob )
  106. {
  107.    UWORD wa_width;
  108.  
  109.    /* Initialize Sprite and Bob */
  110.    wa_width = WORDALIGN( ibob->Image.Width );
  111.  
  112.    ibob->VSprite.Height      = ibob->Image.Height;
  113.    ibob->VSprite.Width       = wa_width / 16;
  114.    ibob->VSprite.Depth       = ibob->Image.Depth;
  115.    ibob->VSprite.ImageData   = ibob->Image.ImageData;
  116.    ibob->VSprite.VSBob       = &ibob->Bob;
  117.    ibob->VSprite.PlanePick   = ibob->Image.PlanePick;
  118.    ibob->VSprite.PlaneOnOff  = ibob->Image.PlaneOnOff;
  119.    ibob->VSprite.Flags       = SAVEBACK | OVERLAY;
  120.    ibob->Bob.BobVSprite      = &ibob->VSprite;
  121.    ibob->Bob.ImageShadow     = NULL;
  122.  
  123.    /* Allocate Chip Memory for Save Buffer. */
  124.    if( ibob->Bob.SaveBuffer = ALLOCRASTER( ibob->Image.Width,
  125.       ibob->Image.Height * ibob->Image.Depth ) )
  126.    {
  127.       /* Create RastPorts for the Image and the Shadow. */
  128.       if( ClippedImageRastPort_Init( &ibob->ImagePort, &ibob->Image ) )
  129.       {
  130.          if( ImageBob_InitShadowMask( ibob ) )
  131.             return TRUE;
  132.       }
  133.    }
  134.  
  135.    return FALSE;
  136. }
  137.  
  138.  
  139. BOOL
  140. ImageBob_Init( ImageBob *ibob,
  141.                UWORD      width,
  142.                UWORD      height,
  143.                UWORD      depth )
  144. {
  145.    memset( ibob, 0, sizeof( ImageBob ) );
  146.  
  147.    if( ImageBob_InitImage( &ibob->Image, width, height, depth ) )
  148.    {
  149.       if( ImageBob_InitVSpriteBob( ibob ) )
  150.       {
  151.          return TRUE;
  152.       }
  153.    }
  154.  
  155.    ImageBob_CleanUp( ibob );
  156.    return FALSE;
  157. }
  158.  
  159. void
  160. ImageBob_CleanUpImage( struct Image *image )
  161. {
  162.    if( image->ImageData )
  163.       FreeRaster( (PLANEPTR) image->ImageData,
  164.                   image->Width, image->Height * image->Depth );
  165.    image->ImageData = NULL;
  166. }
  167.  
  168.  
  169. void
  170. ImageBob_CleanUp( ImageBob *ibob )
  171. {
  172.    USHORT width, height, depth;
  173.  
  174.    ClippedImageRastPort_CleanUp( &ibob->ImagePort );
  175.    ClippedImageRastPort_CleanUp( &ibob->ShadowPort );
  176.  
  177.    ImageBob_CleanUpImage( &ibob->Image );
  178.  
  179.    width  = ibob->Image.Width;
  180.    height = ibob->Image.Height;
  181.    depth  = ibob->Image.Depth;
  182.  
  183.    if( ibob->Bob.SaveBuffer )
  184.       FreeRaster( (PLANEPTR)ibob->Bob.SaveBuffer, width, height * depth );
  185.  
  186.    if( ibob->Bob.ImageShadow )
  187.       FreeRaster( (PLANEPTR)ibob->Bob.ImageShadow, WORDALIGN( width ), height );
  188.  
  189.  
  190.    memset( ibob, 0, sizeof( ImageBob ) );
  191.  
  192. }
  193.  
  194.