home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 517a.lha / VFont_v2.0 / examples / bmore.c next >
C/C++ Source or Header  |  1991-06-09  |  8KB  |  310 lines

  1. /* Copyright 1991 by Michael Jansson, All rights reserved.
  2.  
  3.  NAME:  BMore
  4.  USAGE: BMore [-v][-q] <font name> <width> <height> <file name>
  5.  VERSION: 1.1
  6.  
  7. This is a small benchmark program that will display a text file as fast
  8. as it can. It uses both normal amiga bitmap fonts and the new vfont vector
  9. fonts.
  10.  
  11. [See comments about the -q flag in the rotate.c file]
  12.  
  13. */
  14.  
  15.  
  16. #include <intuition/intuition.h>
  17. #include <intuition/screens.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/vfontlib.pro>
  23. #include <pragmas/dos.h>
  24. #include <pragmas/exec.h>
  25. #include <pragmas/intuition.h>
  26. #include <pragmas/graphics.h>
  27. #include <pragmas/vfontlib.pra>
  28. #include <vfont/curve.h>
  29. #include <vfont/vfont.h>
  30. #include <stdio.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33.  
  34. /* Some macros. (It's done this way to improve the readability of the code). */
  35. #define MyPutS(str)     Write(Output(), str "\n", (long)strlen(str)+1L)
  36.  
  37. /* A description of the font that we are interested in. */
  38. TextVAttr tattr = {
  39.     (UBYTE *)"fixwire.vfont",   /* The name of the class. */
  40.     32,                         /* The height */
  41.     1,                          /* It is a vector font. */
  42.     FS_NORMAL,                  /* The style. */
  43.     FPF_DESIGNED|FPF_DISKFONT,  /* The flags. */
  44.     (UBYTE *)"John",            /* Our name for this font. */
  45.     0,                          /* X Size. */
  46.     VFB_EXCLUSIVE|VFB_ANYSIZE,  /* VFlags. */
  47.     0                           /* Desired File format. */
  48. };
  49.  
  50.  
  51. /* Some intuition structure used when opening the screen and the window. */
  52.  
  53. #define ProgramName (UBYTE *)"BMore v1.1 - Copyright 1991 by Michael Jansson"
  54. #define width 640L
  55. #define height 512L            /* Change this to 400 for NTSC. */
  56.  
  57. struct NewScreen newscreen = {
  58.     0,0,width,height,1,         /* Screen dimensions. */
  59.     0,-1,                       /* Pen colors. */
  60.     LACE|HIRES,                 /* View modes. */
  61.     CUSTOMSCREEN,               /* Screen type. */
  62.     NULL,                       /* Default screen font (none). */
  63.     ProgramName,                /* Screen title. */
  64.     NULL,                       /* Gadgets (none). */
  65.     NULL                        /* CustomBitMap (none). */
  66. };
  67.  
  68. struct NewWindow newwindow = {
  69.     0,0,width,height,           /* Window dimensions. */
  70.     0,-1,                       /* Pen colors. */
  71.     0UL,                        /* IDCMPFlags (none). */
  72.         SIMPLE_REFRESH |        /* Flags. (We want a really simple window). */
  73.         NOCAREREFRESH |
  74.         BORDERLESS,
  75.     NULL,                       /* Gadgets. (none). */
  76.     NULL,                       /* Check mark. (none). */
  77.     ProgramName,                /* Title. */
  78.     NULL,                       /* Screen (set it to our screen when it is open). */
  79.     NULL,                       /* BitMap. (none). */
  80.     0,0,width,height,           /* Min/Max dimensions. */
  81.     CUSTOMSCREEN                /* Window type. */
  82. };
  83.  
  84. /* Library names. */
  85. #define INTUITION   (UBYTE *)"intuition.library"
  86. #define GRAPHICS    (UBYTE *)"graphics.library"
  87. #define VFONTS      (UBYTE *)"vfont.library"
  88.  
  89. #define AREABUFFSIZE   2000L
  90. #define AREABUFFSIZE5 10000L
  91.  
  92. /* Global data. */
  93. UBYTE *myBuffer;
  94. struct AreaInfo myArea;
  95. struct TmpRas myTmpRas;
  96. BOOL vector_flg = FALSE;
  97. long size = 0;
  98.  
  99. /* Global data that is allocated/opened. */
  100. struct Library *IntuitionBase = NULL;
  101. struct Library *vFontBase = NULL;
  102. struct Library *GfxBase = NULL;
  103. struct Screen *screen = NULL;
  104. struct Window *window = NULL;
  105. struct TextFont *font = NULL;
  106. PLANEPTR myPlane = NULL;
  107. UBYTE *buffer = NULL;
  108. VFont *vfont = NULL;
  109. BPTR file = 0L;
  110.  
  111.  
  112. void
  113. _abort(void)
  114. {
  115.     if (file)
  116.         Close(file);
  117.     if (buffer)
  118.         FreeMem(buffer, size);
  119.     if (vector_flg) {
  120.         if (vfont)
  121.             CloseVFont(vfont);
  122.     } else {
  123.         if (font)
  124.             CloseFont(font);
  125.     }
  126.     if (myPlane)
  127.         FreeRaster(myPlane, width, height);
  128.     if (myBuffer)
  129.         FreeMem(myBuffer, AREABUFFSIZE5);
  130.     if (window)
  131.         CloseWindow(window);
  132.     if (screen)
  133.         CloseScreen(screen);
  134.     if (vFontBase)
  135.         CloseLibrary(vFontBase);
  136.     if (IntuitionBase)
  137.         CloseLibrary(IntuitionBase);
  138.     if (GfxBase)
  139.         CloseLibrary(GfxBase);
  140.     exit();
  141. }
  142.  
  143. void
  144. ClearMem(void *data, int size)
  145. {
  146.     while(size)
  147.         ((char *)data)[--size] = '\0';
  148. }
  149.  
  150. void
  151. main(int argc, char **argv)
  152. {
  153.     struct RastPort *RPort;
  154.     LONG t1[3], t2[3];
  155.     char line[256];
  156.     long Args = 0;
  157.     long row;
  158.     long tot = 0;
  159.     long ysize;
  160.     long count = 0;
  161.     BOOL quick_flg = FALSE;
  162.     long i;
  163.  
  164.     /* Process the CLI parameters. */
  165.     if (argc<4) {
  166.         MyPutS("Usage: bmore [-v][-q] font xsize ysize filename");
  167.         return;
  168.     }
  169.     while (argv[++Args][0]=='-') {
  170.         if (tolower(argv[Args][1])=='v')
  171.             vector_flg = TRUE;
  172.         if (tolower(argv[Args][1])=='q')
  173.             quick_flg = TRUE;
  174.     }
  175.     
  176.     /* Set up the requirements for the font. */
  177.     tattr.Name = (UBYTE *)argv[Args++];
  178.     tattr.XSize = atoi(argv[Args++]);
  179.     tattr.YSize = atoi(argv[Args++]);
  180.     if ((tattr.XSize<0) || (tattr.YSize<=0)) {
  181.         MyPutS("Invalid font size.");
  182.         _abort();
  183.     }
  184.  
  185.     /* Open the needed libraries. */
  186.     if (!(IntuitionBase = OpenLibrary(INTUITION, 0L))
  187.     || !(GfxBase = OpenLibrary(GRAPHICS, 0L))
  188.     || !(vFontBase = OpenLibrary(VFONTS, 0L))) {
  189.         MyPutS("Can't open the vfont libarary.");
  190.         _abort();
  191.     }
  192.  
  193.     /* Open the file. */
  194.     if ((file = Open((UBYTE *)argv[Args], (long)MODE_OLDFILE))==NULL) {
  195.         MyPutS("Can't open the that file.");
  196.         _abort();
  197.     }
  198.     Seek(file, 0L, (long)OFFSET_END);
  199.     size = Seek(file, 0L, (long)OFFSET_BEGINNING);
  200.     if (!(buffer=AllocMem(size, 0L))) {
  201.         MyPutS("File to big to be loaded at once!");
  202.         _abort();
  203.     }
  204.     if (size!=Read(file, buffer, size)) {
  205.         MyPutS("DOS error while reading file.");
  206.         _abort();
  207.     }
  208.     MyPutS("Text file loaded!");
  209.     Close(file);
  210.     file = 0L;
  211.  
  212.     /* Book keep the time. */
  213.     DateStamp(t1);
  214.  
  215.     /* Open the font. */
  216.     if (vector_flg) {
  217.         /* Open a vector font. */
  218.         if ((vfont = OpenVFont(&tattr))==NULL) {
  219.             MyPutS("Didn't manage to open the font.");
  220.             _abort();
  221.         }
  222.     } else {
  223.         /* Open a font that is both a vector font and a true amiga bitmap font. */
  224.         if ((font = OpenBFont(&tattr))==NULL) {
  225.             MyPutS("Didn't manage to open the font.");
  226.             _abort();
  227.         }
  228.     }
  229.  
  230.     DateStamp(t2);
  231.     printf("Time to load the font: %ld msec\n",(long)(t2[2]-t1[2])*20);
  232.  
  233.     /* Open a screen. */
  234.     if (!(screen = OpenScreen(&newscreen))) {
  235.         MyPutS("Couldn't open screen.");
  236.         _abort();
  237.     }
  238.  
  239.     if (quick_flg)
  240.          RPort = &screen->RastPort;
  241.     else {
  242.  
  243.         /* Open a window. */
  244.         newwindow.Screen = screen;
  245.         if (!(window=OpenWindow(&newwindow))) {
  246.             MyPutS("Failed to open a window.");
  247.             _abort();
  248.         }
  249.          RPort = window->RPort;
  250.     }
  251.  
  252.     SetDrMd(RPort, (long)JAM1);
  253.  
  254.  
  255.     /* Add a TmpRas. */
  256.     if (!(myPlane=AllocRaster(width, height)))
  257.         _abort();
  258.     InitTmpRas(&myTmpRas, myPlane, (long)RASSIZE(width, height));
  259.     RPort->TmpRas = &myTmpRas;
  260.  
  261.     /* Add an AreaInfo. */
  262.     if (!(myBuffer=AllocMem(AREABUFFSIZE5, 0L)))
  263.         _abort();
  264.     InitArea(&myArea, myBuffer, AREABUFFSIZE);
  265.     RPort->AreaInfo = &myArea;
  266.  
  267.     /* Let the library know that it should use this font in our rastport. */
  268.     if (vector_flg) {
  269.         SetVFont(RPort, vfont);
  270.         ysize = vfont->YSize;
  271.     } else {
  272.         SetFont(RPort, font);
  273.         ysize = font->tf_YSize;
  274.     }
  275.  
  276.     /* Book keep the time. */
  277.     DateStamp(t1);
  278.  
  279.     SetAPen(RPort, 1L);
  280.     SetDrMd(RPort, (long)JAM1);
  281.     ScreenToFront(screen);
  282.     row = ysize+12;
  283.     while (count!=size) {
  284.         i = 0;
  285.         do {
  286.             line[i++] = buffer[count++];
  287.         } while (line[i-1]!='\n' && i<256 && count<size);
  288.         Move (RPort, 4L, row);
  289. /*
  290.     VText is a function that will use the vector font that is assigned to 
  291.     the given rastport, or else use the default TextFont font if no
  292.     vector font is given.
  293. */
  294.         VText(RPort, (UBYTE *)line, i-1);
  295.         tot += i;
  296.         row += ysize;
  297.         if (row>=500) {
  298.             SetRast(RPort, 0L); 
  299.             row = ysize+12L;
  300.         }
  301.         Chk_Abort();
  302.     }
  303.  
  304.     /* Report the elapsed time. */
  305.     DateStamp(t2);
  306.     printf("Chars/sec %ld\n",(long)tot/((t2[1]-t1[1])*60+(t2[2]-t1[2])/50));
  307.  
  308.     _abort();
  309. }
  310.