home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / MAXON_C3.DMS / in.adf / Demos / ANSI-C / Intuition / 8hamdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  8.2 KB  |  376 lines

  1. /*
  2.  * 8hamdemo.c - shows off 256000 colors simultaneously
  3.  *
  4.  * (c) Copyright 1992 Commodore-Amiga, Inc.  All Rights Reserved.
  5.  * 
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <exec/types.h>
  10. #include <graphics/displayinfo.h>
  11. #include <intuition/intuition.h>
  12.  
  13. #include <pragma/exec_lib.h>
  14. #include <pragma/graphics_lib.h>
  15. #include <pragma/diskfont_lib.h>
  16. #include <pragma/intuition_lib.h>
  17.  
  18. /*----------------------------------------------------------------------*/
  19.  
  20. void error_exit(STRPTR errorstring);
  21.  
  22. /*----------------------------------------------------------------------*/
  23.  
  24. struct Library *DiskfontBase = NULL;
  25. struct GfxBase *GfxBase = NULL;
  26. struct IntuitionBase *IntuitionBase = NULL;
  27. struct Screen *myscreen = NULL;
  28. struct Window *mywindow = NULL;
  29. struct BitMap *bm[2] = {NULL,NULL};
  30. struct RastPort temprp;
  31. struct TextFont *font = NULL;
  32.  
  33. /*----------------------------------------------------------------------*/
  34.  
  35. UWORD pens[] =
  36. {
  37.     0, /* DETAILPEN */
  38.     1, /* BLOCKPEN    */
  39.     1, /* TEXTPEN    */
  40.     2, /* SHINEPEN    */
  41.     1, /* SHADOWPEN    */
  42.     3, /* FILLPEN    */
  43.     1, /* FILLTEXTPEN    */
  44.     0, /* BACKGROUNDPEN    */
  45.     2, /* HIGHLIGHTTEXTPEN    */
  46.  
  47.     1, /* BARDETAILPEN    */
  48.     2, /* BARBLOCKPEN    */
  49.     1, /* BARTRIMPEN    */
  50.  
  51.     ~0,
  52. };
  53.  
  54.  
  55. /* Color table to pass to SA_Colors32 or LoadRGB32() */
  56. ULONG colortable[] =
  57. {
  58.     0x00040000,    /* 5 colors to load, from zero */
  59.  
  60.     0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA,    /* Gray */
  61.     0x00000000, 0x00000000, 0x00000000,    /* Black */
  62.     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,    /* White */
  63.     0x66666666, 0x88888888, 0xBBBBBBBB,    /* Blue-gray */
  64.  
  65.     0x00000000,    /* terminator */
  66. };
  67.  
  68. /*----------------------------------------------------------------------*/
  69.  
  70. #define PLOTWIDTH 64
  71. #define PLOTHEIGHT 64
  72.  
  73.  
  74. /*----------------------------------------------------------------------*/
  75.  
  76. /* Let's arrange the blue values in a spiral, just to be cute.
  77.  * We'll do it with a table instead of an algorithm out of
  78.  * laziness.
  79.  */
  80. int bluespiral1[] =
  81. {
  82.     56, 55, 54, 53, 52, 51, 50, 49,
  83.     57, 30, 29, 28, 27, 26, 25, 48,
  84.     58, 31, 12, 11, 10,  9, 24, 47,
  85.     59, 32, 13,  2,  1,  8, 23, 46,
  86.     60, 33, 14,  3,  0,  7, 22, 45,
  87.     61, 34, 15,  4,  5,  6, 21, 44,
  88.     62, 35, 16, 17, 18, 19, 20, 43,
  89.     63, 36, 37, 38, 39, 40, 41, 42,
  90. };
  91.  
  92. int bluespiral2[] =
  93. {
  94.     60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45,
  95.     61, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 44,
  96.     62, 27,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 43,
  97.     63, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
  98. };
  99.  
  100. /*----------------------------------------------------------------------*/
  101.  
  102. struct TextAttr topaz80 =
  103. {
  104.     "topaz.font",
  105.     8,
  106.     0,
  107.     0,
  108. };
  109.  
  110. struct TextAttr preffont =
  111. {
  112.     "times.font",
  113.     15,
  114.     0,
  115.     0,
  116. };
  117.  
  118. #define MOD_BLUE    64
  119. #define MOD_RED        128
  120. #define MOD_GREEN    192
  121.  
  122. #define XOFF 6
  123. #define YOFF 5
  124.  
  125. main(argc, argv)
  126. int argc;
  127. char *argv[];
  128. {
  129.     int blue, x, ybase, column;
  130.     int redreverse, red, dred;
  131.     int left, top, width, height, b;
  132.     int perrow = 8;
  133.     int *bluespiral = bluespiral1;
  134.     struct TextAttr *scfont = &topaz80;
  135.  
  136.     /* Super72 800x600 HAM */
  137.     ULONG displayID = 0x89824;
  138.     int overscan = OSCAN_TEXT;
  139.  
  140.     if ( ( argc > 1 ) && ( *argv[1] == '?' ) )
  141.     {
  142.     printf("8HAMDemo [P|N|D]\n");
  143.     printf("- defaults to Super72\n");
  144.     printf("- P = use PAL hires-lace\n");
  145.     printf("- D = use double-PAL hires-lace\n");
  146.     printf("- N = use NTSC superhires-lace\n");
  147.     }
  148.     else
  149.     {
  150.     if ( ( argc > 1 ) && ( ( *argv[1] == 'D' ) || ( *argv[1] == 'd' ) ) )
  151.     {
  152.         displayID = DBLPALHIRESHAMFF_KEY;
  153.         overscan = OSCAN_MAX;
  154.     }
  155.  
  156.     if ( ( argc > 1 ) && ( ( *argv[1] == 'P' ) || ( *argv[1] == 'p' ) ) )
  157.     {
  158.         displayID = PAL_MONITOR_ID | HIRESHAMLACE_KEY;
  159.         overscan = OSCAN_MAX;
  160.     }
  161.  
  162.     if ( ( argc > 1 ) && ( ( *argv[1] == 'N' ) || ( *argv[1] == 'n' ) ) )
  163.     {
  164.         displayID = NTSC_MONITOR_ID | SUPERHAMLACE_KEY;
  165.         overscan = OSCAN_TEXT;
  166.         perrow = 16;
  167.             bluespiral = bluespiral2;
  168.     }
  169.  
  170.     if (!( GfxBase = (struct GfxBase *)
  171.         OpenLibrary("graphics.library", 39L) ))
  172.         {
  173.         error_exit("Couldn't open Gfx V39\n");
  174.         }
  175.  
  176.     if (!( IntuitionBase = (struct IntuitionBase *)
  177.         OpenLibrary("intuition.library", 39L) ))
  178.         {
  179.         error_exit("Couldn't open Intuition V39\n");
  180.         }
  181.  
  182.     if (!( DiskfontBase = (struct Library *)
  183.         OpenLibrary("diskfont.library", 37L) ))
  184.         {
  185.         error_exit("Couldn't open Diskfont V37\n");
  186.         }
  187.  
  188.     if ( font = OpenDiskFont( &preffont ) )
  189.     {
  190.         scfont = &preffont;
  191.     }
  192.  
  193.     InitRastPort( &temprp );
  194.     bm[0] = AllocBitMap( PLOTWIDTH, PLOTHEIGHT, 8, BMF_INTERLEAVED|BMF_CLEAR, NULL );
  195.     temprp.BitMap = bm[0];
  196.     for ( x=0; x<64; x++ )
  197.     {
  198.         SetAPen( &temprp, MOD_GREEN+x );
  199.         Move( &temprp, x, 0 );
  200.         Draw( &temprp, x, 63 );
  201.     }
  202.  
  203.     bm[1] = AllocBitMap( PLOTWIDTH, PLOTHEIGHT, 8, BMF_INTERLEAVED|BMF_CLEAR, NULL );
  204.     temprp.BitMap = bm[1];
  205.  
  206.     for ( x=0; x<64; x++ )
  207.     {
  208.         SetAPen( &temprp, MOD_GREEN+63-x );
  209.         Move( &temprp, x, 0 );
  210.         Draw( &temprp, x, 63 );
  211.     }
  212.  
  213.     if (!(myscreen = OpenScreenTags(NULL,
  214.         SA_DisplayID, displayID,
  215.         SA_Overscan, overscan,
  216.         /*  Other tags can go here: */
  217.         SA_Font, scfont,
  218.         SA_Depth, 8,
  219.         SA_AutoScroll, 1,
  220.         SA_Pens, pens,
  221.         SA_Interleaved, TRUE,
  222.         SA_Title, "8-plane HAM Demo",
  223.         SA_Colors32, colortable,
  224.         SA_Behind, TRUE,
  225.         TAG_DONE )))
  226.         {
  227.         error_exit("Couldn't open screen\n");
  228.         }
  229.  
  230.     width = 28 + (perrow*65);
  231.     height = 10 + scfont->ta_YSize + (64/perrow)*64;
  232.     left = ( myscreen->Width - width - scfont->ta_YSize - 2 ) / 2;
  233.     top = ( myscreen->Height - height ) / 2;
  234.  
  235.     if (!( mywindow = OpenWindowTags(NULL,
  236.         WA_Left, left,
  237.         WA_Top, top,
  238.         WA_Width, width,
  239.         WA_Height, height,
  240.         WA_MinWidth, 50,
  241.         WA_MinHeight, 40,
  242.         WA_MaxWidth, -1,
  243.         WA_MaxHeight, -1,
  244.         WA_IDCMP, CLOSEWINDOW,
  245.         WA_SizeGadget, TRUE,
  246.         WA_DepthGadget, TRUE,
  247.         WA_DragBar, TRUE,
  248.         WA_Title, "262144 Colors!",
  249.         WA_CloseGadget, TRUE,
  250.         WA_NoCareRefresh, TRUE,
  251.         WA_SmartRefresh, TRUE,
  252.         WA_CustomScreen, myscreen,
  253.         WA_Activate, TRUE,
  254.         TAG_DONE ) ))
  255.         {
  256.         error_exit("Couldn't open window\n");
  257.         }
  258.  
  259.     b = 0;
  260.     ybase = 0;
  261.     column = 0;
  262.     redreverse=0;
  263.  
  264.     for ( blue = 0; blue < 64; blue++ )
  265.     {
  266.         if ( column == 0 )
  267.         {
  268.         int dy;
  269.  
  270.         SetAPen( mywindow->RPort, 1 );
  271.         Move( mywindow->RPort, XOFF-1, (YOFF+scfont->ta_YSize)+ybase );
  272.         Draw( mywindow->RPort, XOFF-1, (YOFF+scfont->ta_YSize)+ybase+63 );
  273.  
  274.         for ( dy = 0; dy < 64; dy++ )
  275.         {
  276.             if ( redreverse )
  277.             {
  278.             SetAPen( mywindow->RPort, MOD_RED + 63-dy );
  279.             }
  280.             else
  281.             {
  282.             SetAPen( mywindow->RPort, MOD_RED + dy );
  283.             }
  284.             WritePixel( mywindow->RPort, XOFF, (YOFF+scfont->ta_YSize)+ybase+dy );
  285.         }
  286.         }
  287.  
  288.         SetAPen( mywindow->RPort, MOD_BLUE+63-bluespiral[blue]);
  289.         Move( mywindow->RPort, XOFF+column*65+1, (YOFF+scfont->ta_YSize)+ybase );
  290.         Draw( mywindow->RPort, XOFF+column*65+1, (YOFF+scfont->ta_YSize)+ybase+63 );
  291.     
  292.         BltBitMapRastPort( bm[b], 0, 0,
  293.         mywindow->RPort, XOFF+column*65 + 2, (YOFF+scfont->ta_YSize)+ybase,
  294.         PLOTWIDTH, PLOTHEIGHT, 0xC0 );
  295.         b = 1-b;
  296.  
  297.         column++;
  298.         if ( column == perrow )
  299.         {
  300.         column = 0;
  301.         ybase += 64;
  302.         redreverse = 1-redreverse;
  303.         }
  304.     }
  305.     ScreenToFront( myscreen );
  306.     dred = 1; red = 0;
  307.     while ( !GetMsg( mywindow->UserPort ) )
  308.     {
  309.         ScrollRaster( mywindow->RPort, 0, -1, XOFF, (YOFF+scfont->ta_YSize), XOFF, (YOFF+scfont->ta_YSize)+(4096/perrow)-1 );
  310.         SetAPen( mywindow->RPort, MOD_RED + red );
  311.         red += dred;
  312.         if ( red == -1 )
  313.         {
  314.         dred = 1;
  315.         red = 0;
  316.         }
  317.         else if (red == 64 )
  318.         {
  319.         red = 63;
  320.         dred = -1;
  321.         }
  322.         WritePixel( mywindow->RPort, XOFF, (YOFF+scfont->ta_YSize) );
  323.     }
  324.     }
  325.     error_exit(NULL);
  326. }
  327.  
  328.  
  329. /*----------------------------------------------------------------------*/
  330.  
  331. void error_exit(STRPTR errorstring)
  332.  
  333.     {
  334.     if (mywindow)
  335.     {
  336.     ScreenToBack(myscreen);
  337.     CloseWindow(mywindow);
  338.     }
  339.  
  340.     if (myscreen)
  341.     {
  342.     CloseScreen(myscreen);
  343.     }
  344.  
  345.     if ( bm[1] ) FreeBitMap( bm[1] );
  346.     if ( bm[0] ) FreeBitMap( bm[0] );
  347.  
  348.     if (font) CloseFont(font);
  349.  
  350.     if (DiskfontBase)
  351.     {
  352.     CloseLibrary(DiskfontBase);
  353.     }
  354.  
  355.     if (IntuitionBase)
  356.     {
  357.     CloseLibrary(( struct Library *)IntuitionBase);
  358.     }
  359.  
  360.     if (GfxBase)
  361.     {
  362.     CloseLibrary(( struct Library *)GfxBase);
  363.     }
  364.  
  365.     if (errorstring)
  366.     {
  367.     printf(errorstring);
  368.     exit(20);
  369.     }
  370.  
  371.     exit(0);
  372.     }
  373.  
  374.  
  375. /*----------------------------------------------------------------------*/
  376.