home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505b.lha / Ham_e_library / FontDemo.c < prev    next >
C/C++ Source or Header  |  1991-05-05  |  10KB  |  402 lines

  1.  
  2. #include <intuition/intuition.h>
  3. #include <exec/types.h>
  4. #include <exec/execbase.h>
  5. #include <graphics/text.h>
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/graphics.h>
  9.  
  10. #include "hame_pragmas.h"
  11. #include "HameStruct.h"
  12. #include "HameProtos.h"
  13. #include "HameRev.h"
  14.  
  15. #include <string.h>
  16. #include <exec/memory.h>
  17. #include <exec/tasks.h>
  18.  
  19. #include <graphics/view.h>
  20. #include <graphics/gfx.h>
  21. #include <graphics/rastport.h>
  22.  
  23. #include <proto/intuition.h>
  24.  
  25.  
  26. struct Library *HameBase;
  27. struct HamePort *HamePort;
  28.  
  29. extern struct Screen *OpenScreen();
  30. extern struct Window *OpenWindow();
  31.  
  32. extern struct ExecBase *SysBase;
  33. struct IntuitionBase *IntuitionBase;
  34. struct GfxBase       *GfxBase;
  35.  
  36. struct Screen        *screen;
  37. struct Window        *window;
  38. struct HameFont *f1 = NULL;
  39. struct HameFont *f2 = NULL;
  40.  
  41. #define WIDTH 640
  42. #define HEIGHT 200
  43.  
  44. struct NewScreen my_screen =
  45.   {
  46.     0, 0, WIDTH, HEIGHT,
  47.     4,
  48.     0, 1,
  49.     HIRES,
  50.     CUSTOMSCREEN,
  51.     (struct TextAttr *) NULL,
  52.     NULL,
  53.     NULL, NULL
  54.   };
  55.  
  56. struct NewWindow my_window =
  57.   {
  58.     0, 0, WIDTH, HEIGHT,
  59.     (UBYTE) 0, (UBYTE) 1,
  60.     MOUSEBUTTONS
  61.        | INTUITICKS,
  62.     SMART_REFRESH
  63.        | BORDERLESS
  64.        | BACKDROP
  65.        | ACTIVATE
  66.        | NOCAREREFRESH
  67.        | REPORTMOUSE,
  68.     NULL, NULL,
  69.     NULL,
  70.     NULL, NULL,
  71.     0, 0, 0, 0,
  72.     CUSTOMSCREEN
  73.   };
  74.  
  75.  
  76. /*
  77.   Clean up and exit
  78. */
  79. void CloseStuff()
  80.   {
  81.     struct Library *result;
  82.       if (f2)                HAME_CloseFont(f2);
  83.       if (f1)                HAME_CloseFont(f1);
  84.       if (window)             CloseWindow(window);
  85.       if (screen)             CloseScreen(screen);
  86.       if (HamePort)           HAME_Dispose(HamePort);
  87.       if (HameBase)           CloseLibrary(HameBase);
  88.       if (GfxBase)            CloseLibrary(GfxBase);
  89.       if (IntuitionBase)      CloseLibrary(IntuitionBase);
  90.  
  91. /* This sequence expunges hame.library if it is no longer in use */
  92. /* In normal use, this is not required */
  93.       Forbid();
  94.       if ( (result = (struct Library *) 
  95.            FindName(&SysBase->LibList,"hame.library")) != NULL ) 
  96.         {
  97.           RemLibrary(result);
  98.         }
  99.       Permit();
  100.  
  101.       exit(0);
  102.   }
  103.  
  104. /*
  105.   Draw a vertical column of pixels at the left margin to prevent hame from
  106.   turning off.
  107. */
  108. void DrawLeftMargin()
  109.   {
  110.   int i;
  111.  
  112.   HAME_SetAPen( HamePort, 2 );
  113.   for ( i = 0; i < 200; i++ )
  114.     {
  115.       HAME_WritePixel( HamePort, 0, i);
  116.     }
  117.   }
  118.  
  119. /*
  120.   Initialize the pallette to random colors
  121. */
  122. void palette1()
  123.   {
  124.   int i, r, g, b;
  125.  
  126.   for ( i = 1; i < 256; i++ )
  127.     {
  128.       r = rand() & 0xff;
  129.       g = rand() & 0xff;
  130.       b = rand() & 0xff;
  131.       HAME_SetRGB8(HamePort, i, r, g, b);
  132.     }
  133.  
  134.   /* faux noir */
  135.   HAME_SetRGB8(HamePort, 2, 0, 0, 0);
  136.   }
  137.  
  138. /*
  139.   Main routine
  140. */
  141. int main(argc, argv)
  142.   int argc;
  143.   char *argv[];
  144.   {
  145.   ULONG  msg_class;
  146.   USHORT msg_code;
  147.   int    fh, v, ww, i, finished=0;
  148.   long l1, l2, l3, l4, l5, l6, bl1, bl2;
  149.   struct IntuiMessage *message;
  150.   struct Gadget *gadget;
  151.  
  152.  
  153.   if (argc < 3)
  154.     {
  155.       printf("USAGE:\n  %s fontname.font size\n", argv[0] );
  156.       exit(0);
  157.     }
  158.  
  159.   if ((IntuitionBase = (struct IntuitionBase *)
  160.        OpenLibrary("intuition.library",33L))==NULL)
  161.     {
  162.       CloseStuff();
  163.     }
  164.  
  165.   if ((GfxBase = (struct GfxBase *)
  166.        OpenLibrary("graphics.library" , 0L))==NULL)
  167.     {
  168.       CloseStuff();
  169.     }
  170.  
  171.   /* Open the hame library */
  172.   if ((HameBase = (struct Library *) 
  173.        OpenLibrary("hame.library",HAMELIB_VERSION)) == NULL)
  174.     {
  175.       printf("No hame.library\n");
  176.       CloseStuff();
  177.     }
  178.  
  179.  
  180.   /* Get a screen to play with */
  181.   if ((screen = OpenScreen(&my_screen))     ==NULL)
  182.     {
  183.       CloseStuff();
  184.     }
  185.  
  186.   my_window.Screen = screen;
  187.  
  188.   /* Get a window to play with */
  189.   if ((window = OpenWindow(&my_window)) == NULL)
  190.     {
  191.       CloseStuff();
  192.     }
  193.  
  194.   /* turn off the title bar */
  195.   ShowTitle(screen, 0L);
  196.  
  197.   /* Tell the library where to put pixels */
  198.   if ((HamePort = HAME_Init(screen, 640, 400, 400, 0, 10, 4, 186)) == NULL)
  199.     {
  200.       CloseStuff();
  201.     }
  202.  
  203.   /* Set the pallette */
  204.   palette1();
  205.  
  206.   /* Keep hame from turning off because of screen wide color 0 */
  207.   DrawLeftMargin();
  208.  
  209.   fh = atoi(argv[2]);
  210.   /* Open user font */
  211.   f1 = HAME_OpenFont(argv[1], fh);
  212.   if (!f1)
  213.     {
  214.       printf("No %s size %ld!\n", argv[1], fh);
  215.       CloseStuff();
  216.     }
  217.  
  218.   /* Open topaz 8 */
  219.   f2 = HAME_OpenFont("topaz.font", 8);
  220.   if (!f2)
  221.     {
  222.       printf("No topaz.font size 8!\n");
  223.       CloseStuff();
  224.     }
  225.  
  226.   /* Set replaceable pen to color 16 (black) */
  227.   HAME_SetFontPen( f1, 16 );
  228.   /* Read the font's palette into the HamePorts cookie at color 16 */
  229.   HAME_GetFontPallete( HamePort, f1 );
  230.  
  231.   HAME_SetFontPen( f1, 32 );
  232.   /* Read the font's palette into the HamePorts cookie at color 32 */
  233.   HAME_GetFontPallete( HamePort, f1 );
  234.  
  235.   HAME_SetFontPen( f1, 48 );
  236.   /* Read the font's palette into the HamePorts cookie at color 48 */
  237.   HAME_GetFontPallete( HamePort, f1 );
  238.  
  239.   HAME_SetFontPen( f1, 64 );
  240.   /* Read the font's palette into the HamePorts cookie at color 64 */
  241.   HAME_GetFontPallete( HamePort, f1 );
  242.  
  243.   /* Modify this fonts palette for an alternate appearance at color 32*/
  244.   for (i = 32; i < (32 + f1->NColors); i++)
  245.     {
  246.       HAME_GetRGB8(HamePort, i);
  247.       HAME_SetRGB8(HamePort, i, HamePort->b, HamePort->g, HamePort->r );
  248.     }
  249.  
  250.   /* Modify this fonts palette for an alternate appearance at color 48*/
  251.   for (i = 48; i < (48 + f1->NColors); i++)
  252.     {
  253.       HAME_GetRGB8(HamePort, i);
  254.       HAME_SetRGB8(HamePort, i, HamePort->g, HamePort->b, HamePort->r );
  255.     }
  256.  
  257.   /* Modify this fonts palette for an alternate appearance at color 64*/
  258.   for (i = 64; i < (64 + f1->NColors); i++)
  259.     {
  260.       HAME_GetRGB8(HamePort, i);
  261.       HAME_SetRGB8(HamePort, i, HamePort->r, HamePort->g, HamePort->b );
  262.     }
  263.  
  264.   /* Now set the fonts original palette to a grey scale */
  265.   for (i = 16; i < (16 + f1->NColors); i++)
  266.     {
  267.       HAME_GetRGB8(HamePort, i);
  268.       HAME_SetRGB8(HamePort, i, HamePort->r, HamePort->r, HamePort->r );
  269.     }
  270.  
  271.   /* Prepare this font's quick tables for colorbase of 16 */
  272.   HAME_MakeFTables(f1, 16);
  273.  
  274.   /* See above for description */
  275.   HAME_SetFontPen( f2, 1 );
  276.   HAME_GetFontPallete( HamePort, f2 );
  277.   HAME_MakeFTables(f2, 0);
  278.  
  279.   /* Render using quick font method */
  280.   l1 = HAME_QText(HamePort, f1, "HAME.LIBRARY", 16, 35 );
  281.   l2 = HAME_QText(HamePort, f2, "(c) 1991", 5, 55 );
  282.   l3 = HAME_QText(HamePort, f1, "BLACK", 19, 95 );
  283.   l4 = HAME_QText(HamePort, f1, "BELT", 23, 125 );
  284.   l5 = HAME_QText(HamePort, f1, "SYSTEMS", 27, 155 );
  285.   l6 = HAME_QText(HamePort, f2, "programming : Pete Patterson", 2, 175 );
  286.  
  287.   /* Cycle the grey scale */
  288.   for ( i = 0; i < 150; i++ )
  289.     {
  290.       HAME_CycleLeft(HamePort, 17, 31 );
  291.     }
  292.  
  293. /* This simply tests font clipping - uncomment to check it out
  294. */
  295. /*
  296.   for ( i = 0; i < 75; i++ )
  297.     {
  298.       ww = HAME_QText(HamePort, f1, "HAME", -30-i, 25-i );
  299.       ww = HAME_QText(HamePort, f1, "HAME", -30-i, 175+i );
  300.       ww = HAME_QText(HamePort, f1, "HAME", 290+i, 25-i );
  301.       ww = HAME_QText(HamePort, f1, "HAME", 290+i, 175+i );
  302.     }
  303.  
  304.   Delay(250);
  305. */
  306.  
  307.   HAME_SetAPen( HamePort, 0 );
  308.  
  309.   /* Render using slow font method */
  310.   /* Use all four font palletes */
  311.   /* Erase before drawing if font is less than thirty points */
  312.  
  313.   bl1 = f1->BaseLine;
  314.   bl2 = f2->BaseLine;
  315.  
  316.   HAME_SetFontPen( f1, 16 );
  317.   HAME_Box(HamePort, 16, 35-bl1, 16+l1, 35-bl1+fh, 1 );
  318.   ww = HAME_Text(HamePort, f1, "HAME.LIBRARY", 16, 35 );
  319.  
  320.   HAME_SetFontPen( f2, 1 );
  321.   HAME_Box(HamePort, 5, 55-bl2, 5+l2, 63-bl2, 1 );
  322.   ww = HAME_Text(HamePort, f2, "(c) 1991", 5, 55 );
  323.  
  324.   HAME_SetFontPen( f1, 32 );
  325.   HAME_Box(HamePort, 19, 95-bl1, 19+l3, 95-bl1+fh, 1 );
  326.   ww = HAME_Text(HamePort, f1, "BLACK", 19, 95 );
  327.  
  328.   HAME_SetFontPen( f1, 48 );
  329.   HAME_Box(HamePort, 23, 125-bl1, 23+l4, 125-bl1+fh, 1 );
  330.   ww = HAME_Text(HamePort, f1, "BELT", 23, 125 );
  331.  
  332.   HAME_SetFontPen( f1, 64 );
  333.   HAME_Box(HamePort, 27, 155-bl1, 27+l5, 155-bl1+fh, 1 );
  334.   ww = HAME_Text(HamePort, f1, "SYSTEMS", 27, 155 );
  335.  
  336.   HAME_SetFontPen( f2, 3 );
  337.   HAME_Box(HamePort, 2, 175-bl2, 2+l6, 183-bl2, 1 );
  338.   ww = HAME_Text(HamePort, f2, "programming : Pete Patterson", 2, 175 );
  339.  
  340.   /* Cycle all four palettes */
  341.   for ( i = 0; i < 150; i++ )
  342.     {
  343.       HAME_CycleLeft(HamePort, 17, 31 );
  344.       HAME_CycleRight(HamePort, 33, 47 );
  345.       HAME_CycleRight(HamePort, 49, 63 );
  346.       HAME_CycleRight(HamePort, 65, 79 );
  347.     }
  348.  
  349.   v = 0;
  350.   /* Wait for mouse... GLOW color 1 & 3 */
  351.   while( !finished )
  352.     {
  353.       message = (struct IntuiMessage *) GetMsg(window->UserPort);
  354.       if (message)
  355.         {
  356.           msg_class = message->Class;
  357.           msg_code = message->Code;
  358.           gadget = (struct Gadget *) message->IAddress;
  359.           ReplyMsg((struct Message *)message);
  360.  
  361.           switch( msg_class )
  362.             {
  363.               case MOUSEBUTTONS:
  364.                 finished = 1;
  365.               break;
  366.  
  367.               case MENUPICK:
  368.               break;
  369.  
  370.               case GADGETUP:
  371.               break;
  372.  
  373.               case INTUITICKS:
  374.               break;
  375.  
  376.               case CLOSEWINDOW:
  377.                 finished = 1;
  378.               break;
  379.  
  380.               default:
  381.                 printf("?");
  382.             }
  383.         }
  384.       else
  385.         {
  386.           /* Make the single color text glow */
  387.           v++;
  388.           v %= 256;
  389.           HAME_SetRGB8(HamePort, 1, v, v, v);
  390.           HAME_SetRGB8(HamePort, 3, v, v, v);
  391.         }
  392.      }
  393.  
  394.   while ((message = (struct IntuiMessage *)
  395.           GetMsg( window->UserPort )) != NULL)
  396.     {
  397.       ReplyMsg((struct Message *)message);
  398.     }
  399.  
  400.   CloseStuff();
  401. }
  402.