home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 517a.lha / VFont_v2.0 / examples / spiral.c < prev   
C/C++ Source or Header  |  1991-06-09  |  10KB  |  303 lines

  1. /* Copyright 1991 by Michael Jansson, All rights reserved.
  2.  
  3.  NAME:  Spiral 
  4.  USAGE: Spiral <font name> <text string>
  5.  VERSION: 1.0
  6.  
  7. This is a small program that will display a text file in a corkscrew
  8. fashion. It is a typical example of the use of the vfont.library functions:  
  9. OpenVFont, ChangeVFont, VText, Rotate, SetVFont, FixToInt etc.
  10.  
  11. The program opens a screen and prints a file, character by character.
  12. The font is never bitmap cached since that font has to be re-mapped, 
  13. i.e. re-calculated to reflect a new size and orientation, after
  14. each character.
  15.  
  16. */
  17.  
  18.  
  19. #include <intuition/intuition.h>
  20. #include <intuition/screens.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/intuition_protos.h>
  24. #include <clib/graphics_protos.h>
  25. #include <pragmas/dos.h>
  26. #include <pragmas/exec.h>
  27. #include <pragmas/intuition.h>
  28. #include <pragmas/graphics.h>
  29. #include <vfont/curve.h>
  30. #include <vfont/vfont.h>
  31. #include <clib/vfontlib.pro>
  32. #include <pragmas/vfontlib.pra>
  33. #include <math.h>
  34. #include <stdio.h>
  35. #include <time.h>
  36.  
  37.  
  38. /* Some constans. */
  39. #define Deg10   0.174532925
  40. #define FIXPI   205887L
  41. #define TWO_PI    ((double)    3.141592653589793))*2.0
  42.  
  43. /* Some macros. (It's done this way to improve the readability of the code). */
  44. #define RotateFont(font, angle, height)     {VFont req; req.YSize = height; req.XSize = 0; req.AlfaPath = req.UpVector = (FIX)(FIXOFFSET*angle)-FIXPI; ChangeVFont(font, (UBYTE)(VCM_PATH|VCM_UP|VCM_SIZE), &req, (long)FALSE);}
  45. #define MyPutS(str)     Write(Output(), str "\n", (long)strlen(str)+1L)
  46.  
  47.  
  48. /* This is the paramater that is used when the font is opened.
  49.    It is very similar to the intuition.library structure TextAttr.
  50.    The TextAttr is distinguished from a TextVAttr (notice the 
  51.    extra "V" in the name) by the one bit flag  VectorFont, which
  52.    corresponds to the sign bit of the ta_YSize field of the TextAttr 
  53.    structure. You may use a plain TextAttr if you are satisfied with
  54.    the default values for the vector font exxtension of the structure
  55.    when you open a font with the OpenVFont function.
  56.  
  57.    The width or the height may be set to zero to indicate that you
  58.    want the default width or height. The library will then set it so 
  59.    that the proportions will be preseved, e.g. a 32x16 large font class
  60.    opened as a 64x0 large font will be set to be 64x32.
  61. */
  62. TextVAttr tattr = {
  63.     (UBYTE *)"vwire.font",   /* The name of the class. */
  64.     32,                         /* The height */
  65.     1,                          /* It is a vector font. */
  66.     FS_NORMAL,                  /* The style. */
  67.     FPF_DESIGNED|FPF_DISKFONT,  /* The flags. */
  68.     (UBYTE *)"John",            /* Our name for this font. */
  69.     0,                          /* Use default width. */
  70. /* 
  71.     The next field in this structure is VFlags. It is used to control
  72.     the way the vfont library is managing the desired font. The possible 
  73.     bits are the same that are used by other functions than the OpenVFont
  74.     function. The followilg flags may be used:
  75.  
  76.     Name                Meaning
  77.     ====                =======
  78.     VFB_MUTE            NOT VNB_DEBUG and NOT VFB_VERBOSE
  79.     VFB_DEBUG           Used for debuging purpose. It will trace all 
  80.                         function calls in the vfont lmbrary on the 
  81.                         serial port. It is not usefull nor recomended
  82.                         use this flag.
  83.     VFB_VERBOSE         Alert the user through a requester when errors
  84.                         occurs. Usefull for small programs that don't
  85.                         want to deal with a lot of user interface stuff.
  86.     VFB_FLUSHCLASS      Indicates that un-used font classes should be 
  87.                         purged from the library. Used by the FlushWhat
  88.                         function.
  89.     VFB_FLUSHFONTS      Same as VFB_FLUSHCLASS, but for font instances. 
  90.                         Note that a lacked font (instance) or a locked 
  91.                         class can not be flushed.
  92.     VFB_MAPPED          Used internally (keep your hands off this bit).
  93.     VFB_EXCLUSIVE       Indicates that font for exlusive use is desired.
  94.                         Opening a non-exlusive font means that it may be 
  95.                         re-used by other clients (programs, task etc). 
  96.                         Changes done to a non-exclusive font affects all 
  97.                         clients since they all use the same font instance.
  98.     VFB_FORCED          Exlusive fonts can be re-opened if this flag is 
  99.                         used. You should have a good reason for re-opening
  100.                         other clients exlusive fonts.
  101.     VFB_NOCONVERT       The library will attempt to build the desired font
  102.                         from what ever class that is available, unless
  103.                         this flag is used.
  104.     VFB_ANYSIZE         The library will look for a class that has exactly
  105.                         the same size as desired, unless this flags is used.
  106.     VFB_NOBITCACHE      A bitmap cache will be devoted to the opened font, 
  107.                         unless this font is used.
  108. */
  109.  
  110.     VFB_VERBOSE |               /* Let the library inform the user when error occures. */
  111.     VFB_EXCLUSIVE |             /* Don't let others use the font. */
  112.     VFB_NOBITCACHE |            /* Don't use the bitmap cache. */
  113.     VFB_ANYSIZE                 /* It is okay to load a class at any size. */
  114. };
  115.  
  116.  
  117. /* Some intuition structure that is used when opening a screen. */
  118. #define ProgramName (UBYTE *)"Spiral v1.0 - Copyright 1991 by Michael Jansson"
  119. #define width 640L
  120. #define height 512L            /* Change this to 400 for NTSC. */
  121.  
  122. struct NewScreen newscreen = {
  123.     0,0,width,height,1,         /* Screen dimensions. */
  124.     0,-1,                       /* Pen colors. */
  125.     LACE|HIRES,                 /* View modes. */
  126.     CUSTOMSCREEN,               /* Screen type. */
  127.     NULL,                       /* Default screen font (none). */
  128.     ProgramName,                /* Screen title. */
  129.     NULL,                       /* Gadgets (none). */
  130.     NULL                        /* CustomBitMap (none). */
  131. };
  132.  
  133. /* Library names. */
  134. #define INTUITION   (UBYTE *)"intuition.library"
  135. #define GRAPHICS    (UBYTE *)"graphics.library"
  136. #define VFONTS      (UBYTE *)"vfont.library"
  137.  
  138. #define AREABUFFSIZE 1000L
  139. #define AREABUFFSIZE5 5000L
  140.  
  141. /* Global data. */
  142. UBYTE myBuffer[AREABUFFSIZE5];
  143. struct AreaInfo myArea;
  144. struct TmpRas myTmpRas;
  145. struct NewScreen newscreen;
  146.  
  147. /* Global data that is allocated/opened. */
  148. struct Library *IntuitionBase = NULL;
  149. struct Library *vFontBase = NULL;
  150. struct Library *GfxBase = NULL;
  151. struct Screen *screen = NULL;
  152. PLANEPTR myPlane = NULL;
  153. UBYTE *buffer = NULL;
  154. VFont *font = NULL;
  155. BPTR file = 0L;
  156. long size = 0;
  157.  
  158.  
  159. /*
  160.     This is the function that will release the allocated resources.
  161. */
  162. void
  163. _abort(void)
  164. {
  165.     if (file)
  166.         Close(file);
  167.     if (buffer)
  168.         FreeMem(buffer, size);
  169.     if (font)
  170.            CloseVFont(font);
  171.     if (myPlane)
  172.         FreeRaster(myPlane, width, height);
  173.     if (screen)
  174.         CloseScreen(screen);
  175.     if (vFontBase)
  176.         CloseLibrary(vFontBase);
  177.     if (IntuitionBase)
  178.         CloseLibrary(IntuitionBase);
  179.     if (GfxBase)
  180.         CloseLibrary(GfxBase);
  181.     exit(0L);
  182. }
  183.  
  184.  
  185. /*
  186.         The main part of the program.
  187. */
  188. void
  189. main(int argc, UBYTE **argv)
  190. {
  191.     struct RastPort *RPort;
  192.     double angle = 0.0;
  193.     FIX radius = IntToFix(3);
  194.     LONG t1[3], t2[3];
  195.     Point pos;
  196.     int i = 0;
  197.  
  198.     /* Process the CLI parameters. */
  199.     if (argc<2) {
  200.         MyPutS("Usage: spiral font file");
  201.         MyPutS("Example: spiral emerald hakdic.txt");
  202.         return;
  203.     }
  204.  
  205.     /* Open the file. */
  206.     if ((file = Open((UBYTE *)argv[2], (long)MODE_OLDFILE))==NULL) {
  207.         MyPutS("Can't open the that file.");
  208.         _abort();
  209.     }
  210.  
  211.     /* Check the size of the file. */
  212.     Seek(file, 0L, (long)OFFSET_END);
  213.     size = Seek(file, 0L, (long)OFFSET_BEGINNING);
  214.  
  215.     /* Allocate a buffer that will fit the whole file. */
  216.     if (!(buffer=AllocMem(size, 0L))) {
  217.         MyPutS("That file is to big to be loaded all at once!");
  218.         _abort();
  219.     }
  220.  
  221.     /* Read the file into the buffer. */
  222.     if (size!=Read(file, buffer, size)) {
  223.         MyPutS("DOS error while reading file.");
  224.         _abort();
  225.     }
  226.     MyPutS("Text file loaded!");
  227.     Close(file);
  228.     file = 0L;
  229.  
  230.     /* Set up the requirements for the font. */
  231.     tattr.Name = (UBYTE *)argv[1];
  232.  
  233.     /* Open the needed libraries. */
  234.     if (!(IntuitionBase = OpenLibrary(INTUITION, 0L))
  235.     || !(GfxBase = OpenLibrary(GRAPHICS, 0L))
  236.     || !(vFontBase = OpenLibrary(VFONTS, 0L))) {
  237.         MyPutS("Can't open the vfont libarary.");
  238.         _abort();
  239.     }
  240.  
  241.     /* Book keep the starting time. */
  242.     DateStamp(t1);
  243.  
  244.     /* Open the font. */
  245.     if ((font = OpenVFont(&tattr))==NULL) {
  246.         MyPutS("Didn't manage to load the font.");
  247.         _abort();
  248.     }
  249.  
  250.     DateStamp(t2);
  251.     printf("Time to load the font: %ld msec\n",(long)(t2[2]-t1[2])*20);
  252.  
  253.     /* Open a screen. */
  254.     if (!(screen = OpenScreen(&newscreen))) {
  255.         MyPutS("Couldn't open screen.");
  256.         _abort();
  257.     }
  258.     RPort = &screen->RastPort;
  259.     SetDrMd(RPort, (long)JAM1);
  260.  
  261.     /* Add a TmpRas. */
  262.     if (!(myPlane=AllocRaster(width, height)))
  263.         _abort();
  264.     InitTmpRas(&myTmpRas, myPlane, (long)RASSIZE(width, height));
  265.     RPort->TmpRas = &myTmpRas;
  266.  
  267.     /* Add a AreaInfo. */
  268.     InitArea(&myArea, myBuffer, AREABUFFSIZE);
  269.     RPort->AreaInfo = &myArea;
  270.  
  271.     SetVFont(RPort, font);
  272.     ScreenToFront(screen);
  273.  
  274.     /* Book keep the time. */
  275.     DateStamp(t1);
  276.  
  277.     /* Do the rendering. */
  278.     while (i<size) {
  279.  
  280.         /* Clear the screen if we are reaching the edges of the screen. */
  281.         if ((FixToInt(radius)+font->YSize)>(screen->Height/2)) {
  282.             radius = IntToFix(3);
  283.             angle = 0.0;
  284.             SetRast(RPort, 0L);
  285.         }
  286.         radius = radius*101/100;
  287.         angle += Deg10;
  288.         pos.x = 0;
  289.         pos.y = FixToInt(radius);
  290.         Rotate(&pos, 1L, FloatToFix(sin(angle)), FloatToFix(cos(angle)), 0, 0);
  291.         RotateFont(font, angle, FixToInt(radius)/5+2);
  292.         Move(RPort, (long)(pos.x+screen->Width/2), (long)(pos.y+screen->Height/2));
  293.         VText(RPort, &buffer[i++], 1L);
  294.         Chk_Abort();
  295.     }
  296.  
  297.     /* Report the elapsed time. */
  298.     DateStamp(t2);
  299.     printf("Chars/sec %ld\n",(long)size/((t2[1]-t1[1])*60+(t2[2]-t1[2])/50));
  300.  
  301.     _abort();
  302. }
  303.