home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / ImageLib / Image_Demo_2 / Image_Demo_2.c < prev    next >
C/C++ Source or Header  |  1999-06-01  |  12KB  |  484 lines

  1. /* Image_Demo_2 © Paweî Marciniak <pmarciniak@lodz.home.pl> */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <exec/execbase.h>
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <intuition/intuition.h>
  8. #include <libraries/gadtools.h>
  9. #include <graphics/text.h>
  10. #include <graphics/gfxbase.h>
  11. #include <graphics/scale.h>
  12. #include <proto/dos.h>
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15. #include <proto/graphics.h>
  16. #include <proto/gadtools.h>
  17. #include <proto/diskfont.h>
  18. #include <proto/dos.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21.  
  22. #include <proto/image.h>
  23. #include <libraries/image.h>
  24.  
  25. #include "APP_globals.h"
  26.  
  27. extern struct ExecBase *SysBase;
  28. struct Screen *Screen=NULL;
  29. struct Window *APP_Window=NULL;
  30.  
  31. STRPTR PubScreenName="Workbench";
  32. STRPTR APP_TitleWindow="Demo okno";
  33. STRPTR APP_TitleScreenWindow="Demo ekran okno";
  34.  
  35. APTR  VisualInfo = NULL;
  36.  
  37. /* Opcjonalny Font */
  38.  
  39. struct TextAttr HelveticaFont13 = {
  40.     ( STRPTR )"helvetica.font", 13, 0x00, 0x00 };
  41.  
  42. struct TextAttr    HelveticaFont8 = {
  43.     ( STRPTR )"helvetica.font", 8, 0x00, 0x00 };
  44.  
  45. struct TextAttr    TopazFont8 = {
  46.     ( STRPTR )"topaz.font", 8, 0x00, 0x00 };
  47.  
  48. struct TextFont    *HelveticaFont;
  49.  
  50. /* Koniec opcjonalnego Fontu */
  51.  
  52.  
  53. struct TextAttr       *Font, Attr;
  54. WORD FontX=NULL;
  55. WORD FontY=NULL;
  56. UWORD OffX, OffY;
  57.  
  58. ULONG            IClass;
  59. UWORD            Code, Qualifier;
  60. struct Gadget    *IObject;
  61.  
  62. /* Rozmiar Okna */
  63.  
  64.     WORD Width=520;
  65.     WORD Height=200;
  66.     WORD MinWidth=520;
  67.     WORD MinHeight=150;
  68.     WORD MaxWidth=520;
  69.     WORD MaxHeight=375;
  70.  
  71.  
  72. struct NewBitMap
  73. {
  74.     UWORD BytesPerRow;
  75.     UWORD Rows;
  76.     UBYTE Flags;
  77.     UBYTE Depth;
  78.     UWORD pad;
  79.     PLANEPTR Planes[8];
  80.     LONG ColorTab[256];
  81.     UWORD Width;
  82.     UWORD Height;
  83.     UWORD NumColors;
  84. };
  85.  
  86.  
  87. UBYTE chunky[]=
  88. {
  89. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  90. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  91. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  92. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,
  93. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,2,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,
  94. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,2,2,2,2,4,4,0,0,0,0,0,0,0,0,0,0,0,0,
  95. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,2,2,2,2,2,4,4,4,0,0,0,0,0,0,0,0,0,0,0,
  96. 0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,2,2,2,2,2,2,2,2,4,4,0,0,0,0,0,0,0,0,0,0,0,
  97. 0,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0,0,0,0,0,0,0,0,0,0,
  122. 0,0,0,0,0,0,0,0,0,0,0,6,6,4,4,1,4,4,4,4,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,
  123. 0,0,0,0,0,0,0,0,0,0,0,6,6,4,4,4,4,4,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  124. 0,0,0,0,0,0,0,0,0,0,0,0,6,6,4,4,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  125. 0,0,0,0,0,0,0,0,0,0,0,0,0,6,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  126. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  127. };
  128.  
  129. UBYTE Palette[]={ 0, 0, 0,
  130.                                  0, 255, 0,
  131.                                  255, 0, 0,
  132.                                  255, 255, 0,
  133.                                  255, 255, 255,
  134.                                  0, 0, 255,
  135.                                  127, 127, 127 };
  136.  
  137.  
  138. struct ChunkyImg chunkyimg;
  139.  
  140. /* Tytaj zaczynajâ sië wszystkie poûyteczne funkcje */
  141.  
  142.  
  143. void ShowText( STRPTR text )
  144. {
  145.     SetAPen( APP_Window->RPort, 1 );
  146.     SetDrMd( APP_Window->RPort, JAM2 );
  147.     Move( APP_Window->RPort, 20, 384);
  148.     Text( APP_Window->RPort, text, (ULONG)strlen( text ) );
  149. }
  150.  
  151. /* Funkcja otwiera okreôlony (Helvetica 8 lub 13) */
  152. /* font w zaleûnoôci od zmiennej DisplayLace      */
  153.  
  154. int OpenFonts( int DisplayLace )
  155. {
  156.     if( DisplayLace )
  157.     {
  158.         if (!(HelveticaFont=OpenDiskFont( &HelveticaFont13 )))
  159.         {
  160.             HelveticaFont=OpenDiskFont( &TopazFont8 );
  161.             return( FALSE );
  162.         }
  163.     }
  164.     else
  165.     {
  166.         if (!(HelveticaFont=OpenDiskFont( &HelveticaFont8 )))
  167.         {
  168.             HelveticaFont=OpenDiskFont( &TopazFont8 );
  169.             return( FALSE );
  170.         }
  171.     }
  172.     return( TRUE );
  173. }
  174.  
  175.  
  176. /* Funkcja zamyka otwarty font */
  177.  
  178. void CloseFonts( void )
  179. {
  180.     if ( HelveticaFont )    
  181.     {
  182.         CloseFont( HelveticaFont );
  183.         HelveticaFont=NULL;
  184.     }
  185. }
  186.  
  187.  
  188. UWORD ComputeX( UWORD value )
  189. {
  190.     return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
  191. }
  192.  
  193.  
  194. UWORD ComputeY( UWORD value )
  195. {
  196.     return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
  197. }
  198.  
  199.  
  200. void ComputeFont( void )
  201. {
  202.     Font = &Attr;
  203.     Font->ta_Name = (STRPTR)Screen->RastPort.Font->tf_Message.mn_Node.ln_Name;
  204.     Font->ta_YSize = FontY = Screen->RastPort.Font->tf_YSize;
  205.     FontX = Screen->RastPort.Font->tf_XSize;
  206.  
  207.     OffX = Screen->WBorLeft;
  208.     OffY = Screen->RastPort.TxHeight + Screen->WBorTop + 1;
  209.  
  210.     if (( ComputeX( Width ) + OffX + Screen->WBorRight ) > Screen->Width )
  211.     {
  212.         Font->ta_Name = (STRPTR)"topaz.font";
  213.         FontX = FontY = Font->ta_YSize = 8;
  214.     }
  215.     if (( ComputeY( Height ) + OffY + Screen->WBorBottom ) > Screen->Height )
  216.     {
  217.         Font->ta_Name = (STRPTR)"topaz.font";
  218.         FontX = FontY = Font->ta_YSize = 8;
  219.     }
  220.     return;
  221. }
  222.  
  223.  
  224. /* Funkcja otwiera okno */
  225.  
  226. int OpenAPP_Display( WORD Left, WORD Top )
  227. {
  228.     if(!(APP_Window=OpenWindowTags( 0,
  229.         WA_Left,                Left,
  230.         WA_Top,                    Top,
  231.         WA_Width,                (ComputeX(Width) + OffX + Screen->WBorRight),
  232.         WA_Height,            (ComputeY(Height) + OffY + Screen->WBorBottom),
  233.         WA_MinHeight,        OffY,
  234.         WA_MinWidth,        (ComputeX(Width) + OffX + Screen->WBorRight),
  235.         WA_MaxHeight,        (ComputeY(Height) + OffY + Screen->WBorBottom),
  236.         WA_MaxWidth,        (ComputeX(Width) + OffX + Screen->WBorRight),
  237.         WA_Title,                APP_TitleWindow,
  238.         WA_ScreenTitle,    APP_TitleScreenWindow,
  239.         WA_Flags,                WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SIZEGADGET | WFLG_SMART_REFRESH | WFLG_HASZOOM,
  240.         WA_IDCMP,                IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  241.         WA_Gadgets,            FALSE,
  242.         WA_AutoAdjust,    FALSE,
  243.         WA_PubScreen,        (struct Screen *)Screen,
  244.         TAG_DONE)))
  245.             return( FALSE );
  246.     GT_RefreshWindow( APP_Window, NULL );
  247.     return( TRUE );
  248. }
  249.  
  250.  
  251. /* Funkcja zamyka otwarte okno */
  252.  
  253. void CloseAPP_Display(void)
  254. {
  255.     if(APP_Window) 
  256.     {
  257.         CloseWindow((struct Window *)APP_Window);
  258.         APP_Window=NULL;
  259.     }
  260. }
  261.  
  262.  
  263. /* Funkcja odczytuje komunikat Intuition */
  264.  
  265. LONG ReadIMsg( struct Window *iwnd )
  266. {
  267.     struct IntuiMessage *imsg;
  268.  
  269.     if ( imsg = GT_GetIMsg( iwnd->UserPort ))
  270.         {
  271.         IClass      =    imsg->Class;
  272.         Qualifier    =    imsg->Qualifier;
  273.         Code        =    imsg->Code;
  274.         IObject     =    imsg->IAddress;
  275.  
  276.         GT_ReplyIMsg( imsg );
  277.  
  278.         return( TRUE );
  279.     }
  280.     return( FALSE );
  281. }
  282.  
  283.  
  284. /* Funkcja alokuje ekran */
  285.  
  286. int SetupScreen( void )
  287. {
  288.     if (!(Screen = LockPubScreen( PubScreenName )))
  289.         return( FALSE );
  290.  
  291.     if(!(OpenFonts( TRUE )))
  292.         return( FALSE );
  293.  
  294.     ComputeFont();
  295.  
  296.     if (!( VisualInfo = GetVisualInfo( Screen, TAG_DONE )))
  297.         return( FALSE );
  298.  
  299.     return( TRUE );
  300. }
  301.  
  302.  
  303. /* Funkcja dealokuje ekran */
  304.  
  305. void CloseDownScreen( void )
  306. {
  307.     if ( VisualInfo )
  308.     {
  309.         FreeVisualInfo( VisualInfo );
  310.         VisualInfo = NULL;
  311.     }
  312.  
  313.     CloseFonts();
  314.  
  315.     if ( Screen )
  316.     {
  317.         UnlockPubScreen( NULL, Screen );
  318.         Screen = NULL;
  319.     }
  320. }
  321.  
  322.  
  323. /* Funkcja otwiera wszystkie biblioteki */
  324.  
  325. LONG OpenLibraries( void )
  326. {
  327.  
  328.     if ( !(DosBase = (struct DosLibrary *) OpenLibrary((UBYTE *) "dos.library", 36 )))
  329.         return( FALSE );
  330.     if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE *) "intuition.library", 36 )))
  331.         return( FALSE );
  332.     if ( !(GadToolsBase = (struct Library *) OpenLibrary((UBYTE *) "gadtools.library", 36 )))
  333.         return( FALSE );
  334.     if ( !(GfxBase = (struct GfxBase *) OpenLibrary((UBYTE *) "graphics.library" , 36 )))
  335.         return( FALSE );
  336.     if ( !(ImageBase = (struct Library *) OpenLibrary((UBYTE *) "image.library" , 36 )))
  337.     {
  338.         printf("error\n");
  339.         return( FALSE );
  340.     }
  341.     if ( !(DiskFontBase = (struct Library *) OpenLibrary((UBYTE *) "diskfont.library" , 36 )))
  342.         return( FALSE );
  343.     return( TRUE );
  344. }
  345.  
  346.  
  347. /* Funkcja zamyka wszystkie biblioteki */
  348.  
  349. void CloseLibraries( void )
  350. {
  351.     if (ImageBase)            CloseLibrary( (struct Library *) ImageBase );
  352.     if (DiskFontBase)        CloseLibrary( (struct Library *) DiskFontBase );
  353.     if (GfxBase)                CloseLibrary( (struct Library *) GfxBase );
  354.     if (GadToolsBase)        CloseLibrary( (struct Library *) GadToolsBase );
  355.     if (IntuitionBase)    CloseLibrary( (struct Library *) IntuitionBase );
  356.     if (DosBase)                CloseLibrary( (struct Library *) DOSBase );
  357. }
  358.  
  359.  
  360.  
  361. /* Gîówna funkcja */
  362.  
  363. int main( int argc, char *argv[] )
  364. {
  365. BOOL running=TRUE;
  366. struct BitMap *BM;
  367. struct BitMap *DestBM;
  368. struct Image MyImage;
  369.  
  370.     if(!(OpenLibraries()))
  371.     {
  372.         CloseLibraries();
  373.         return( FALSE );
  374.     }
  375.     
  376.     if(!(SetupScreen()))
  377.     {
  378.         CloseDownScreen();
  379.         CloseLibraries();
  380.         return( FALSE );
  381.     }
  382.  
  383.     if(!(OpenAPP_Display( 20, 20 )))
  384.     {
  385.         CloseDownScreen();
  386.         CloseLibraries();
  387.         return( FALSE );
  388.     }
  389.  
  390.     SetAPen( APP_Window->RPort, 1 );
  391.     ShowText("Chunky to BitMap please wait...           ");
  392.  
  393.     chunkyimg.ci_Width=39;
  394.     chunkyimg.ci_Height=38;
  395.     chunkyimg.ci_NumColors=7;
  396.     chunkyimg.ci_Palette=Palette;
  397.     chunkyimg.ci_ChunkyData=chunky;
  398.     BM = ChunkyToBitMap( Screen, &chunkyimg, TAG_DONE);
  399.  
  400.     ShowText("Displaing BitMap please wait...        ");
  401.  
  402.     DrawBitMap( BM, 20, 20, 39, 38, APP_Window->RPort );
  403.  
  404.     ShowText("Drawing Text please wait...             ");
  405.  
  406.     DrawText( DT_Texts,"1bimage.library\n© 1993 Paweî Marciniak\ne-mail <pmarciniak@lodz.home.pl>\n\
  407. Idea i programowanie\n1Paweî Marciniak",
  408.  DT_FontA, HelveticaFont,
  409.  DT_FontB, HelveticaFont,
  410.  DT_RastPort , APP_Window->RPort,
  411.  DT_Top, 40,
  412.  DT_Left, 200,
  413.  TAG_DONE);
  414.  
  415.     ShowText("Drawing 3DBox please wait...                 ");
  416.  
  417.     Draw3DBox( APP_Window->RPort, HelveticaFont,
  418.                         DBA_PosX,        80,
  419.                         DBA_PosY,        30,
  420.                         DBA_Width,    240,
  421.                         DBA_Height,    100,
  422.                         DBA_Text,        "Draw3DBox",
  423.             DBA_Align,  ALIGN_CENTER,
  424.                         TAG_DONE );
  425.  
  426.     ShowText("Scaling BitMap please wait...                 ");
  427.  
  428.     if(DestBM = ScaleBitMap( BM,
  429.                                         SBA_SrcWidth, 39,
  430.                                         SBA_SrcHeight, 38,
  431.                                         SBA_DestWidth, 39,
  432.                                         SBA_DestHeight, 70,
  433.                                         TAG_DONE))
  434.     {
  435.         DrawBitMap( DestBM, 340, 20, 39, 70, APP_Window->RPort );
  436.         FreeBitMap( DestBM );
  437.     }
  438.  
  439.     MyImage.LeftEdge = 0;
  440.     MyImage.TopEdge = 0;
  441.     MyImage.Width = 39;
  442.     MyImage.Height = 38;
  443.     MyImage.Depth = Screen->RastPort.BitMap->Depth;
  444.     MyImage.ImageData = BM->Planes[0];
  445.     MyImage.PlanePick = 255;
  446.     MyImage.PlaneOnOff = 0;
  447.     MyImage.NextImage = 0;
  448.  
  449.     DrawImage( APP_Window->RPort, &MyImage, 340, 100 );
  450.  
  451.     ShowText("That's all folks                                    ");
  452.  
  453.     do
  454.     {
  455.         WaitPort( APP_Window->UserPort );
  456.  
  457.         while ( ReadIMsg( APP_Window ))
  458.         {
  459.             switch ( IClass )
  460.             {
  461.                 case    IDCMP_REFRESHWINDOW:
  462.                     GT_BeginRefresh( APP_Window );
  463.                     DrawBitMap( BM, 20, 20, 39, 38, APP_Window->RPort );
  464.                     GT_EndRefresh( APP_Window, TRUE );
  465.                     break;
  466.  
  467.                 case    IDCMP_CLOSEWINDOW:
  468.                     FreeChunky( Screen, BM );
  469.                     running=FALSE;
  470.  
  471.                     break;
  472.                         
  473.                 default:
  474.                 break;
  475.             }
  476.         }
  477.     } while ( running );
  478.  
  479.     CloseAPP_Display();
  480.     CloseDownScreen();
  481.     CloseLibraries();
  482.     return( TRUE );
  483. }
  484.