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

  1. /* Image_Demo © Paweî Marciniak */
  2. /* Obrazek pochodzi z pîyty MACD 2 */
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <exec/execbase.h>
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <intuition/intuition.h>
  9. #include <libraries/gadtools.h>
  10. #include <graphics/text.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/scale.h>
  13. #include <proto/dos.h>
  14. #include <proto/exec.h>
  15. #include <proto/intuition.h>
  16. #include <proto/graphics.h>
  17. #include <proto/gadtools.h>
  18. #include <proto/diskfont.h>
  19. #include <proto/dos.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22.  
  23. #include <proto/image.h>
  24. #include <libraries/image.h>
  25.  
  26. #include "APP_globals.h"
  27.  
  28. extern UBYTE home_pal[];
  29. extern UBYTE __far home_data[];
  30.  
  31. extern struct ExecBase *SysBase;
  32. struct Screen *Screen=NULL;
  33. struct Window *APP_Window=NULL;
  34.  
  35. STRPTR PubScreenName="Workbench";
  36. STRPTR APP_TitleWindow="Image.library HAM Demo Window";
  37. STRPTR APP_TitleScreenWindow="Image.library Demo Screen";
  38.  
  39. APTR  VisualInfo = NULL;
  40.  
  41. /* Font */
  42.  
  43. struct TextAttr *Font, ScreenFont;
  44. struct TextFont *APP_Font;
  45.  
  46. WORD FontX=NULL;
  47. WORD FontY=NULL;
  48. UWORD OffX, OffY;
  49.  
  50. ULONG     IClass;
  51. UWORD     Code, Qualifier;
  52. struct Gadget *IObject;
  53.  
  54. UBYTE hammode;
  55.  
  56. /* Rozmiar Okna */
  57.  
  58.   WORD Width=320;
  59.   WORD Height=150;
  60.   WORD MinWidth=50;
  61.   WORD MinHeight=30;
  62.   WORD MaxWidth=654;
  63.   WORD MaxHeight=511;
  64.  
  65.   struct ChunkyImg homeimg;
  66.  
  67. /* Tytaj zaczynajâ sië wszystkie poûyteczne funkcje */
  68.  
  69.  
  70. /* Funkcja otwiera okreôlony font */
  71.  
  72. int OpenFonts( void )
  73. {
  74.   if (!(APP_Font=OpenDiskFont( &ScreenFont )))
  75.     return( FALSE );
  76.   return( TRUE );
  77. }
  78.  
  79.  
  80. /* Funkcja zamyka otwarty font */
  81.  
  82. void CloseFonts( void )
  83. {
  84.   if ( APP_Font ) 
  85.   {
  86.     CloseFont( APP_Font );
  87.     APP_Font=NULL;
  88.   }
  89. }
  90.  
  91.  
  92. UWORD ComputeX( UWORD value )
  93. {
  94.   return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
  95. }
  96.  
  97.  
  98. UWORD ComputeY( UWORD value )
  99. {
  100.   return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
  101. }
  102.  
  103.  
  104. void ComputeFont( void )
  105. {
  106.   Font = &ScreenFont;
  107.   Font->ta_Name = (STRPTR)Screen->RastPort.Font->tf_Message.mn_Node.ln_Name;
  108.   Font->ta_YSize = FontY = Screen->RastPort.Font->tf_YSize;
  109.   FontX = Screen->RastPort.Font->tf_XSize;
  110.  
  111.   OffX = Screen->WBorLeft;
  112.   OffY = Screen->RastPort.TxHeight + Screen->WBorTop + 1;
  113.   return;
  114. }
  115.  
  116.  
  117. /* Funkcja otwiera okno */
  118.  
  119. int OpenAPP_Display( WORD Left, WORD Top )
  120. {
  121.  
  122.   if(!(APP_Window=OpenWindowTags( 0,
  123.     WA_Left,        Left,
  124.     WA_Top,         Top,
  125.     WA_Width,       (ComputeX(Width) + OffX + Screen->WBorRight),
  126.     WA_Height,      (ComputeY(Height) + OffY + Screen->WBorBottom),
  127.     WA_MinWidth,    (ComputeX(MinWidth) + OffX + Screen->WBorRight),
  128.     WA_MinHeight,   (ComputeY(MinHeight) + OffY + Screen->WBorBottom),
  129.     WA_MaxWidth,    (MaxWidth + OffX + Screen->WBorRight),
  130.     WA_MaxHeight,   (MaxHeight + OffY + Screen->WBorBottom),
  131.     WA_Title,       APP_TitleWindow,
  132.     WA_ScreenTitle, APP_TitleScreenWindow,
  133.     WA_Flags,       WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_SIZEGADGET | WFLG_CLOSEGADGET | WFLG_SIMPLE_REFRESH,
  134.     WA_IDCMP,       IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  135.     WA_Gadgets,     FALSE,
  136.     WA_AutoAdjust,  FALSE,
  137.     WA_PubScreen,   (struct Screen *)Screen,
  138.     TAG_DONE)))
  139.       return( FALSE );
  140.   GT_RefreshWindow( APP_Window, NULL );
  141.   return( TRUE );
  142. }
  143.  
  144.  
  145. /* Funkcja zamyka otwarte okno */
  146.  
  147. void CloseAPP_Display(void)
  148. {
  149.   if(APP_Window) 
  150.   {
  151.     CloseWindow((struct Window *)APP_Window);
  152.     APP_Window=NULL;
  153.   }
  154. }
  155.  
  156.  
  157. /* Funkcja odczytuje komunikat Intuition */
  158.  
  159. LONG ReadIMsg( struct Window *iwnd )
  160. {
  161.   struct IntuiMessage *imsg;
  162.  
  163.   if ( imsg = GT_GetIMsg( iwnd->UserPort ))
  164.         {
  165.     IClass    = imsg->Class;
  166.     Qualifier = imsg->Qualifier;
  167.     Code    = imsg->Code;
  168.     IObject  =  imsg->IAddress;
  169.  
  170.     GT_ReplyIMsg( imsg );
  171.  
  172.     return( TRUE );
  173.   }
  174.   return( FALSE );
  175. }
  176.  
  177.  
  178. /* Funkcja alokuje ekran */
  179.  
  180. int SetupScreen( void )
  181. {
  182.   if (!(Screen = LockPubScreen( PubScreenName )))
  183.     return( FALSE );
  184.  
  185.   ComputeFont();
  186.  
  187.   if(!(OpenFonts()))
  188.     return( FALSE );
  189.  
  190.  
  191.   if (!( VisualInfo = GetVisualInfo( Screen, TAG_DONE )))
  192.     return( FALSE );
  193.  
  194.   return( TRUE );
  195. }
  196.  
  197.  
  198. /* Funkcja dealokuje ekran */
  199.  
  200. void CloseDownScreen( void )
  201. {
  202.   if ( VisualInfo )
  203.   {
  204.     FreeVisualInfo( VisualInfo );
  205.     VisualInfo = NULL;
  206.   }
  207.  
  208.   CloseFonts();
  209.  
  210.   if ( Screen )
  211.   {
  212.     UnlockPubScreen( NULL, Screen );
  213.     Screen = NULL;
  214.   }
  215. }
  216.  
  217.  
  218. /* Funkcja otwiera wszystkie biblioteki */
  219.  
  220. LONG OpenLibraries( void )
  221. {
  222.   if ( !(DosBase = (struct DosLibrary *) OpenLibrary((UBYTE *) "dos.library", 36 )))
  223.     return( FALSE );
  224.   if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE *) "intuition.library", 36 )))
  225.     return( FALSE );
  226.   if ( !(GadToolsBase = (struct Library *) OpenLibrary((UBYTE *) "gadtools.library", 36 )))
  227.     return( FALSE );
  228.   if ( !(GfxBase = (struct GfxBase *) OpenLibrary((UBYTE *) "graphics.library" , 36 )))
  229.     return( FALSE );
  230.   if ( !(DiskFontBase = (struct Library *) OpenLibrary((UBYTE *) "diskfont.library" , 36 )))
  231.     return( FALSE );
  232.   if ( !(ImageBase = (struct Library *) OpenLibrary((UBYTE *) "image.library" , 36 )))
  233.     return( FALSE );
  234.   return( TRUE );
  235. }
  236.  
  237.  
  238. /* Funkcja zamyka wszystkie biblioteki */
  239.  
  240. void CloseLibraries( void )
  241. {
  242.   if (ImageBase)      CloseLibrary( (struct Library *) ImageBase );
  243.   if (DiskFontBase)   CloseLibrary( (struct Library *) DiskFontBase );
  244.   if (GfxBase)        CloseLibrary( (struct Library *) GfxBase );
  245.   if (GadToolsBase)   CloseLibrary( (struct Library *) GadToolsBase );
  246.   if (IntuitionBase)  CloseLibrary( (struct Library *) IntuitionBase );
  247.   if (DosBase)        CloseLibrary( (struct Library *) DOSBase );
  248. }
  249.  
  250.  
  251.  
  252. /* Gîówna funkcja */
  253.  
  254. int main( int argc, char *argv[] )
  255. {
  256. BOOL running=TRUE;
  257. UBYTE text[50];
  258. struct BitMap *HomeBM;
  259.  
  260.   if(!(OpenLibraries()))
  261.   {
  262.     printf("OpenLibraries failed\n");
  263.     CloseLibraries();
  264.     return( FALSE );
  265.   }
  266.   
  267.   if(!(SetupScreen()))
  268.   {
  269.     printf("Can't lock screen\n");
  270.     CloseDownScreen();
  271.     CloseLibraries();
  272.     return( FALSE );
  273.   }
  274.  
  275. /* Sprawdzamy czy ekran jest w trybie HAM */
  276.   if( Screen->ViewPort.Modes & HAM )
  277.   {
  278.     if( Screen->BitMap.Depth == 6 )
  279.     {
  280.       printf("HAM6 mode\n");
  281.       hammode=MODE_HAM6;
  282.     }
  283.     if( Screen->BitMap.Depth == 8 )
  284.     {
  285.       printf("HAM8 mode\n");
  286.       hammode=MODE_HAM8;
  287.     }
  288.   }
  289.   else
  290.   {
  291.     printf("I need HAM screen\n");
  292.     CloseDownScreen();
  293.     CloseLibraries();
  294.     return( FALSE );
  295.   }
  296.  
  297.  
  298.   if(!(OpenAPP_Display( 20, 20 )))
  299.   {
  300.     printf("Can't open window\n");
  301.     CloseDownScreen();
  302.     CloseLibraries();
  303.     return( FALSE );
  304.   }
  305.  
  306.   SetWindowTitles( APP_Window, "Converce c2h wait...", (UBYTE *)~0 );
  307.   homeimg.ci_Width=640;
  308.   homeimg.ci_Height=512;
  309.   homeimg.ci_NumColors=128;
  310.   homeimg.ci_Palette=home_pal;
  311.   homeimg.ci_ChunkyData=home_data;
  312.  
  313.   if(!(HomeBM = ChunkyToHAM( &homeimg, CTBH_HamMode, hammode, TAG_DONE)))
  314.   {
  315.     printf("ChunkyToHAM failed - no mem\n");
  316.     CloseAPP_Display();
  317.     CloseDownScreen();
  318.     CloseLibraries();
  319.     return( TRUE );
  320.   }
  321.   SetWindowTitles( APP_Window, "Drawing...", (UBYTE *)~0 );
  322.   DrawBitMap( HomeBM, OffX, OffY, (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)), APP_Window->RPort );
  323.   sprintf(text,"Image size: %dx%d", (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)));
  324.   SetWindowTitles( APP_Window, text, (UBYTE *)~0 );
  325.  
  326.   do
  327.   {
  328.     WaitPort( APP_Window->UserPort );
  329.  
  330.     while ( ReadIMsg( APP_Window ))
  331.     {
  332.       switch ( IClass )
  333.       {
  334.         case  IDCMP_REFRESHWINDOW:
  335.           SetWindowTitles( APP_Window, "Refreshing...", (UBYTE *)~0 );
  336.           GT_BeginRefresh( APP_Window );
  337.           DrawBitMap( HomeBM, OffX, OffY, (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)), APP_Window->RPort );
  338.           GT_EndRefresh( APP_Window, TRUE );
  339.           sprintf(text,"Image size: %dx%d", (APP_Window->Width - (OffX + APP_Window->BorderRight)), (APP_Window->Height - (OffY + APP_Window->BorderBottom)));
  340.           SetWindowTitles( APP_Window, text, (UBYTE *)~0 );
  341.         break;
  342.  
  343.         case  IDCMP_CLOSEWINDOW:
  344.           running=FALSE;
  345.           FreeChunky( Screen, HomeBM );
  346.           break;
  347.             
  348.         default:
  349.         break;
  350.       }
  351.     }
  352.   } while ( running );
  353.  
  354.   CloseAPP_Display();
  355.   CloseDownScreen();
  356.   CloseLibraries();
  357.   return( TRUE );
  358. }
  359.