home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / PROGRAMM / FGL112B.ZIP / USER07.DOC < prev    next >
Text File  |  1992-10-05  |  43KB  |  917 lines

  1.  
  2.  
  3. Chapter 7
  4.  
  5. Character Display Routines
  6. 100  Fastgraph User's Guide
  7.  
  8. Overview
  9.  
  10.      An important part of any program is the capability to display text or
  11. other characters on the screen.  Fastgraph supports two character sets:  the
  12. hardware or BIOS character set available with each video mode, and
  13. Fastgraph's own software character set for graphics video modes.
  14. Fastgraph/Light does not support the software character set.
  15.  
  16.      We'll begin this chapter with a review of character space, and then
  17. discuss the specifics about hardware and software characters.  At the end of
  18. the chapter, we'll briefly explain how to implement bit-mapped characters
  19. into a program.  To simplify things, the example programs presented in this
  20. chapter are mode-specific examples, and no testing is done to check if the
  21. video mode is available on the user's system.
  22.  
  23.  
  24. Character Space
  25.  
  26.      The coordinate system used for displaying hardware characters is called
  27. character space.  It is the only coordinate system available in text video
  28. modes, but it is a supplementary coordinate system you can use with either
  29. screen space or world space in graphics video modes.  Character space can be
  30. thought of as a grid of rows and columns, with each cell in the grid holding
  31. one character.  Each cell is identified by its unique (row,column) integer
  32. coordinates.  The rows and columns are numbered starting at zero; the origin
  33. is always the upper left corner of the screen.  For example, in the 80-column
  34. by 25-row video modes, the (row,column) coordinates of the screen corners are
  35. shown in the following diagram.
  36.  
  37.                             (0,0)           (0,79)
  38.  
  39.                             (24,0)         (24,79)
  40.  
  41. The number of rows and columns depends on the video mode, as shown in the
  42. following table.  For graphics modes, the table also includes the width and
  43. height in pixels of a character cell.
  44.  
  45.                        Mode  No. of  No. of Char. Char.
  46.                       Number Columns Rows   Width Height
  47.  
  48.                          4     40     25    8     8
  49.                          5     40     25    8     8
  50.                          6     80     25    8     8
  51.                          9     40     25    8     8
  52.                         11     80     25    9     14
  53.                         12     40     25    8     8
  54.                         13     40     25    8     8
  55.                         14     80     25    8     8
  56.                         15     80     25    8     14
  57.                         16     80     25    8     14
  58.                         17     80     30    8     16
  59.                         18     80     30    8     16
  60.                         19     40     25    8     8
  61.                         20     40     25    8     8
  62.                         21     40     50    8     8
  63.                         22     40     30    8     8
  64.                         23     40     60    8     8
  65.                                    Chapter 7:  Character Display Routines  101
  66.  
  67.  
  68. Hardware Characters
  69.  
  70.      Hardware characters are available in all of Fastgraph's supported video
  71. modes.  As explained in Chapter 5, text mode characters have a display
  72. attribute that defines their foreground color, their background color, and
  73. whether or not they blink.  Graphics mode characters appear in a single
  74. color, as determined by the current color index.  Chapter 5 also explained
  75. how Fastgraph's fg_setattr and fg_setcolor routines define the attribute or
  76. color index in which subsequent hardware characters appear.
  77.  
  78.      It is obviously important to define the color or attribute for hardware
  79. characters, but it is equally important to define their location on the
  80. screen.  Fastgraph draws hardware characters at the position defined by the
  81. text cursor.  Like the graphics cursor, the text cursor is not a cursor in
  82. the true sense, but is simply a pair of character space (row,column)
  83. coordinates with a special meaning.  The fg_setmode routine sets the text
  84. cursor position to the character space coordinates (0,0), which of course is
  85. the upper left corner of the screen.(2)
  86.  
  87.      The Fastgraph routine fg_locate changes the text cursor position.  It
  88. has two integer arguments that specify the (row,column) character space
  89. coordinates of the new position.  The row values must be between 0 and one
  90. less than the number of character rows available.  The column values must be
  91. between 0 and one less than the number of character columns available.
  92.  
  93.      The fg_text routine is Fastgraph's basic character display routine.  It
  94. displays a string of hardware characters, starting at the text cursor
  95. position, using the current color attribute (for text modes) or color index
  96. (for graphics modes).  If the string reaches the last column in a row,
  97. fg_text will wrap the string to the first column of the next row.
  98. Additionally, fg_text leaves the cursor one column to the right of the last
  99. character displayed (or the first column of the next row if the last
  100. character appears at the end of a row).  This feature makes it possible for
  101. successive calls to fg_text to display adjacent strings.  The first argument
  102. for the fg_text routine is a character string of arbitrary length, and the
  103. second argument is an integer value that specifies the number of characters
  104. to display from that string.
  105.  
  106.  
  107.  
  108. ____________________
  109.  
  110.      (2) In reality there are eight text cursors, one for each video page.
  111. The fg_setmode routine initializes each text cursor position to (0,0).  The
  112. next chapter describes this in more detail.
  113. 102  Fastgraph User's Guide
  114.  
  115.      Example 7-1 illustrates the use of the fg_locate and fg_text routines in
  116. the 80 by 25 color text mode (mode 3).  After establishing the video mode and
  117. making the BIOS cursor invisible, the program displays four strings with
  118. different attributes.  The attributes are selected using the fg_setattr
  119. routine, and the strings are displayed by the fg_text routine.  The first
  120. string appears in yellow (attributes 14,0,0) in the upper left corner of the
  121. screen; the fg_locate routine is not necessary because (0,0) is the default
  122. text cursor position established by fg_setmode.  The second string appears in
  123. light green (10,0,0) one space to the right of the first string.  Its
  124. position relies on the fact fg_text sets the new text cursor position one
  125. space to the right of the last character displayed (following the "w" of
  126. "yellow" in this case).  The leading space in " green" leaves a space between
  127. the first and second strings.  Similarly, the third string appears in
  128. blinking light red (12,0,1) one space to the right of the second string.
  129.  
  130.      The program then uses the fg_locate routine to move the text cursor to
  131. the lower left corner of the screen and displays the "Press any key" string.
  132. This string is displayed with a light red foreground against a gray
  133. background (12,7,0).  The extra spaces surrounding the string extend the
  134. background color one character position to the left and right and make the
  135. string more visually appealing.  Finally, once you press any key, the program
  136. restores the original video mode and screen attributes before returning to
  137. DOS.
  138.  
  139.                                  Example 7-1.
  140.  
  141.                       #include <fastgraf.h>
  142.                       void main(void);
  143.  
  144.                       void main()
  145.                       {
  146.                          int old_mode;
  147.  
  148.                          old_mode = fg_getmode();
  149.                          fg_setmode(3);
  150.                          fg_cursor(0);
  151.  
  152.                          fg_setattr(14,0,0);
  153.                          fg_text("yellow",6);
  154.  
  155.                          fg_setattr(10,0,0);
  156.                          fg_text(" green",6);
  157.  
  158.                          fg_setattr(12,0,1);
  159.                          fg_text(" blinking",9);
  160.  
  161.                          fg_setattr(12,7,0);
  162.                          fg_locate(24,0);
  163.                          fg_text(" Press any key. ",16);
  164.                          fg_waitkey();
  165.  
  166.                          fg_setmode(old_mode);
  167.                          fg_reset();
  168.                       }
  169.  
  170.      The fg_where routine retrieves the text cursor position in its two
  171. integer arguments.  This routine is not used as frequently as the fg_locate
  172.                                    Chapter 7:  Character Display Routines  103
  173.  
  174. and fg_text routines because more often than not your program will know the
  175. text cursor position implicitly, or you'll know in advance the locations at
  176. which text will be displayed.  The fg_where routine takes two integer
  177. arguments passed as pointers (that is, by reference), and these two arguments
  178. respectively receive the text cursor's current row and column position.
  179.  
  180.      Example 7-2 produces the same results as example 7-1, but it does so a
  181. bit differently.  It uses its own routine, put_string, to display a string at
  182. a specified row and column.  The put_string routine simply calls fg_locate to
  183. establish the text cursor position and then calls fg_text to display the
  184. string.  Note the use of the C library function strlen to determine the
  185. string length passed to the fg_text routine.  Example 7-2 also uses the
  186. fg_where routine to retrieve the new text cursor positions, which are then
  187. passed to the put_string routine.
  188.  
  189.                                  Example 7-2.
  190.  
  191.                    #include <fastgraf.h>
  192.                    #include <string.h>
  193.                    void main(void);
  194.                    void put_string(char*,int,int);
  195.  
  196.                    void main()
  197.                    {
  198.                       int old_mode;
  199.                       int row, column;
  200.  
  201.                       old_mode = fg_getmode();
  202.                       fg_setmode(3);
  203.                       fg_cursor(0);
  204.  
  205.                       fg_setattr(14,0,0);
  206.                       put_string("yellow",0,0);
  207.  
  208.                       fg_setattr(10,0,0);
  209.                       fg_where(&row,&column);
  210.                       put_string("green",row,column+1);
  211.                       fg_setattr(12,0,1);
  212.                       fg_where(&row,&column);
  213.                       put_string("blinking",row,column+1);
  214.  
  215.                       fg_setattr(12,7,0);
  216.                       put_string(" Press any key. ",24,0);
  217.                       fg_waitkey();
  218.  
  219.                       fg_setmode(old_mode);
  220.                       fg_reset();
  221.                    }
  222.  
  223.                    void put_string(string,row,column)
  224.                    char *string;
  225.                    int row, column;
  226.                    {
  227.                       fg_locate(row,column);
  228.                       fg_text(string,strlen(string));
  229.                    }
  230.  
  231. 104  Fastgraph User's Guide
  232.  
  233.      Sometimes you may wish to change the display attribute of existing text,
  234. such as when creating a shadow around the edges of a pop-up window.  The
  235. Fastgraph routine fg_chgattr performs this function.  It applies the current
  236. text display attribute (as defined in the most recent call to fg_setattr or
  237. fg_setcolor) to a given number of characters, starting at the text cursor
  238. position.  It leaves the text cursor one column to the right of the last
  239. character changed (or the first column of the next row if the last character
  240. is at the end of a row).  The fg_chgattr routine's argument specifies the
  241. number of characters to change.  This routine has no effect in graphics video
  242. modes.
  243.  
  244.      The Fastgraph routine fg_chgtext performs somewhat the opposite function
  245. of fg_chgattr.  It displays new text but uses the display attributes already
  246. assigned to the character cells where the text will appear.  The fg_chgtext
  247. routine takes the same two arguments as the fg_text routine, displays the
  248. characters starting at the text cursor position, and leaves the cursor one
  249. column to the right of the last character displayed.  Like fg_chgattr,
  250. fg_chgtext has no effect in graphics video modes.
  251.  
  252.      Example 7-3 illustrates the fg_chgattr and fg_chgtext routines.  It runs
  253. in the 80-column color text mode (mode 3), but if we change the fg_setmode
  254. argument it also would run in the monochrome text mode (mode 7).  The program
  255. first displays the word "hello" in the upper left corner of the screen, using
  256. a gray foreground and black background attribute.  After waiting for a
  257. keystroke, the program calls fg_chgattr to make the word "hello" appear in
  258. reverse video (that is, a black foreground and gray background attribute).
  259. After a second keystroke, the program uses fg_chgtext to change the "h" of
  260. "hello" to upper case.  Following this, the program returns to DOS.
  261.  
  262.                                  Example 7-3.
  263.  
  264.                          #include <fastgraf.h>
  265.                          void main(void);
  266.  
  267.                          void main()
  268.                          {
  269.                             int old_mode;
  270.  
  271.                             old_mode = fg_getmode();
  272.                             fg_setmode(3);
  273.                             fg_cursor(0);
  274.                             fg_setattr(7,0,0);
  275.                             fg_text("hello",5);
  276.                             fg_waitkey();
  277.  
  278.                             fg_locate(0,0);
  279.                             fg_setattr(0,7,0);
  280.                             fg_chgattr(5);
  281.                             fg_waitkey();
  282.                             fg_locate(0,0);
  283.                             fg_chgtext("H",1);
  284.                             fg_waitkey();
  285.  
  286.                             fg_setmode(old_mode);
  287.                             fg_reset();
  288.                          }
  289.  
  290.                                    Chapter 7:  Character Display Routines  105
  291.  
  292.  
  293.      You also can retrieve the character or attribute stored in a specific
  294. character cell.  The Fastgraph routine fg_getchar retrieves character values,
  295. while fg_getattr retrieves character attributes.  Both routines have two
  296. integer arguments that specify the (row,column) coordinates for the character
  297. cell of interest.  Example 7-4 uses fg_getchar and fg_getattr to obtain the
  298. character and attribute stored at row 24, column 0.  Just before the program
  299. exits, it displays these values.
  300.  
  301.                                  Example 7-4.
  302.  
  303.                      #include <fastgraf.h>
  304.                      #include <stdio.h>
  305.                      void main(void);
  306.  
  307.                      void main()
  308.                      {
  309.                         int attr, value;
  310.                         int old_mode;
  311.  
  312.                         old_mode = fg_getmode();
  313.                         fg_setmode(3);
  314.                         fg_cursor(0);
  315.  
  316.                         fg_setattr(9,7,0);
  317.                         fg_locate(24,0);
  318.                         fg_text("Test",4);
  319.                         value = fg_getchar(24,0);
  320.                         attr  = fg_getattr(24,0);
  321.                         fg_waitkey();
  322.  
  323.                         fg_setmode(old_mode);
  324.                         fg_reset();
  325.                         printf("%c %2.2X\n",value,attr);
  326.                      }
  327.  
  328.  
  329.      If you need to retrieve characters and attributes from a rectangular
  330. area, it's more efficient to use the fg_getimage routine (described in
  331. Chapter 9) than to call fg_getchar and fg_getattr repeatedly.
  332.  
  333.      Displaying hardware characters in graphics video modes is quite
  334. different from doing so in text modes, although we use the same Fastgraph
  335. routines in both cases.  Example 7-5 is similar to example 7-1, but it runs
  336. in the EGA enhanced graphics mode (mode 16) instead of a text mode.  In
  337. graphics modes, the fg_cursor routine has no effect, so we have omitted it
  338. from the program.  Furthermore, characters cannot be displayed with a
  339. blinking attribute, so we have omitted the blinking characters (we could
  340. simulate blinking by repetitively displaying and erasing them, but that is
  341. beyond the scope of this example).  Because graphics mode characters only
  342. have a foreground color, we had to simulate the gray background of the "Press
  343. any key" string by first drawing a rectangle where that string appears.  The
  344. differences between examples 7-5 and 7-1 hold for any graphics video mode,
  345. not just mode 16.
  346. 106  Fastgraph User's Guide
  347.  
  348.                                  Example 7-5.
  349.  
  350.                       #include <fastgraf.h>
  351.                       void main(void);
  352.  
  353.                       void main()
  354.                       {
  355.                          int old_mode;
  356.  
  357.                          old_mode = fg_getmode();
  358.                          fg_setmode(16);
  359.  
  360.                          fg_setcolor(14);
  361.                          fg_text("yellow",6);
  362.  
  363.                          fg_setcolor(10);
  364.                          fg_text(" green",6);
  365.  
  366.                          fg_setcolor(7);
  367.                          fg_rect(0,127,336,349);
  368.                          fg_setcolor(12);
  369.                          fg_locate(24,0);
  370.                          fg_text(" Press any key. ",16);
  371.                          fg_waitkey();
  372.  
  373.                          fg_setmode(old_mode);
  374.                          fg_reset();
  375.                       }
  376.  
  377.      Example 7-6 demonstrates a side effect that occurs when displaying
  378. characters in graphics modes.  This example uses the MCGA graphics mode (mode
  379. 19) and displays two character strings at the same location.  If we were to
  380. do this in a text mode, the first string would disappear once we displayed
  381. the second string.  In graphics modes, however, the portions of the first
  382. string not covered by characters from the second string are still visible.
  383. The reason for this may not be clear at first, but remember when we display
  384. characters in graphics modes, we aren't really displaying characters but
  385. merely a pixel representation of the characters.  Fastgraph has no way to
  386. distinguish such pixels from any other pixels.
  387.  
  388.                                  Example 7-6.
  389.  
  390.                          #include <fastgraf.h>
  391.                          void main(void);
  392.  
  393.                          void main()
  394.                          {
  395.                             int old_mode;
  396.  
  397.                             old_mode = fg_getmode();
  398.                             fg_setmode(19);
  399.  
  400.                             fg_setcolor(14);
  401.                             fg_text("yellow",6);
  402.                             fg_locate(0,0);
  403.                             fg_setcolor(10);
  404.  
  405.                                    Chapter 7:  Character Display Routines  107
  406.  
  407.                             fg_text(" green",6);
  408.                             fg_waitkey();
  409.                             fg_setmode(old_mode);
  410.                             fg_reset();
  411.                          }
  412.  
  413.  
  414.      To avoid this problem, the recommended procedure for displaying
  415. characters in graphics modes is to first erase the area where the text will
  416. appear.  The easiest way to do this is to use the fg_rect routine to draw a
  417. rectangle in the background color.  In example 7-6, we could do this by
  418. inserting the statements
  419.  
  420.                               fg_setcolor(0);
  421.                               fg_rect(0,47,0,7);
  422.  
  423. immediately before the call to fg_locate.  The parameters passed to the
  424. fg_rect routine represent the 48 by 8 pixel region that corresponds to the
  425. first six character cells of row 0 in the 320 by 200 graphics modes.
  426.  
  427.  
  428. Conversion Routines
  429.  
  430.      We have already mentioned Fastgraph includes routines for converting
  431. coordinates between character space and screen space (see page 44).  In this
  432. section, we will review these routines and then present an example that uses
  433. some of them.
  434.  
  435.      The fg_xalpha and fg_yalpha routines convert screen space coordinates to
  436. character space.  The fg_xalpha routine converts a screen space x coordinate
  437. to the character space column that contains the coordinate.  Similarly, the
  438. fg_yalpha routine converts a screen space y coordinate to the character space
  439. row that contains the coordinate.
  440.  
  441.      The fg_xconvert and fg_yconvert routines convert character space
  442. coordinates to screen space.  The fg_xconvert routine converts a character
  443. space column to the screen space coordinate of its leftmost pixel.
  444. Similarly, the fg_yconvert routine converts a character space row to the
  445. screen space coordinate of its top (lowest-numbered) pixel.
  446.  
  447.      On page 105, example 7-5 demonstrated how to display characters in a
  448. graphics mode.  Because characters do not have a background color in graphics
  449. modes, example 7-5 used the fg_rect routine to simulate a background color by
  450. drawing a gray rectangle before displaying the text.  It was necessary to
  451. determine the screen coordinates of the character cells so we could pass the
  452. correct parameters to fg_rect.  By using the fg_xconvert and fg_yconvert
  453. routines, we can let Fastgraph calculate the required screen coordinates.
  454. This method has the additional benefit of working in any graphics mode,
  455. whereas the coordinates passed to fg_rect in example 7-5 would only work
  456. properly in a 640 by 350 graphics mode.  Example 7-7 shows how we could
  457. extend example 7-5 to use the fg_xconvert and fg_yconvert routines.
  458.  
  459.                                  Example 7-7.
  460.  
  461.                       #include <fastgraf.h>
  462.                       void main(void);
  463.  
  464. 108  Fastgraph User's Guide
  465.  
  466.                       void main()
  467.                       {
  468.                          int old_mode;
  469.                          int minx, maxx, miny, maxy;
  470.  
  471.                          fg_old_mode = fg_getmode();
  472.                          fg_setmode(16);
  473.  
  474.                          fg_setcolor(14);
  475.                          fg_text("yellow",6);
  476.  
  477.                          fg_setcolor(10);
  478.                          fg_text(" green",6);
  479.  
  480.                          fg_setcolor(7);
  481.                          minx = fg_xconvert(0);
  482.                          maxx = fg_xconvert(16) - 1;
  483.                          miny = fg_yconvert(24);
  484.                          maxy = fg_yconvert(25) - 1;
  485.                          fg_rect(minx,maxx,miny,maxy);
  486.                          fg_setcolor(12);
  487.                          fg_locate(24,0);
  488.                          fg_text(" Press any key. ",16);
  489.                          fg_waitkey();
  490.  
  491.                          fg_setmode(old_mode);
  492.                          fg_reset();
  493.                       }
  494.  
  495.  
  496. Software Characters
  497.  
  498.      Software characters, also called stroke characters or vector characters
  499. in other literature, are only are available in graphics video modes.  Unlike
  500. the fixed-size hardware characters, you can display software characters in
  501. any size, at any angle, and at any position.  In addition, software
  502. characters are proportionally spaced.  However, software characters take
  503. longer to draw than do hardware characters.
  504.  
  505.      Fastgraph includes two software character fonts, called the primary font
  506. and the alternate font.  The primary font contains upper and lower case
  507. letters, numbers, punctuation, and most of the other printable ASCII
  508. characters.  The alternate font contains upper and lower case Greek letters
  509. and other mathematical and scientific symbols.
  510.  
  511.      The Fastgraph routine fg_swchar displays a string of software characters
  512. in the current color index (as defined by the most recent call to
  513. fg_setcolor).  The string may contain any characters from the primary font,
  514. the alternate font, or both.  You can display the characters left justified,
  515. centered, or right justified relative to the graphics cursor position.  Just
  516. as the fg_text routine updates the text cursor position, fg_swchar sets the
  517. graphics cursor position just to the right of the last character drawn.  The
  518. characters are clipped according to the current clipping region.  In addition
  519. to the characters, the string passed to fg_swchar also may contain operators
  520. for switching fonts, underlining, subscripting, or superscripting characters.
  521. Because fg_swchar internally uses world space coordinates, you must call the
  522. fg_initw routine at some point in your program before the first call to
  523.                                    Chapter 7:  Character Display Routines  109
  524.  
  525. fg_swchar.  You also must establish a world space coordinate system with the
  526. fg_setworld routine.
  527.  
  528.      The fg_swchar routine has three arguments.  The first argument is the
  529. character string to display.  The second argument is an integer value that
  530. specifies the number of characters in the string, including any characters
  531. used as special operators.  The third argument is an integer value that
  532. determines the position of the string relative to the graphics cursor
  533. position.  If this value is negative, the lower left corner of the first
  534. character will be at the graphics cursor position.  If it is positive, the
  535. lower right corner of the last character will be at the graphics cursor
  536. position.  If it is zero, the string will be horizontally centered at the
  537. graphics cursor position.
  538.  
  539.      The size of software characters is determined by the values passed to
  540. the fg_setsize, fg_setsizew, and fg_setratio routines.  The fg_setsize
  541. routine has a single integer argument that defines the height of software
  542. characters in screen space units, while the fg_setsizew routine has a single
  543. floating point argument that defines the height in world space units.  If
  544. neither of these routines is called, Fastgraph will use its default character
  545. height of one world space unit.  The fg_setratio routine has a single
  546. floating point argument that defines the aspect ratio for software
  547. characters.  The aspect ratio is the ratio of character width to character
  548. height.  For example, an aspect ratio of 2.0 means characters are twice as
  549. wide as they are high.  If the fg_setratio routine is not called, Fastgraph
  550. uses its default aspect ratio of 1.
  551.  
  552.      Example 7-8 displays both of the software character fonts.  The program
  553. uses the enhanced EGA graphics mode (mode 16), but it could run in any
  554. graphics mode by changing the fg_setmode argument.  After establishing the
  555. video mode, the program calls the fg_initw routine to initialize Fastgraph's
  556. world space parameters; this is required since the software character drawing
  557. routines internally use world space coordinates.  The next statement is a
  558. call to fg_setworld that establishes a world space coordinate system with
  559. 0.01 world space units per pixel.  Following this is a call to fg_setsizew
  560. that defines the character height as 0.21 world space units, or 21 pixels.
  561. Note we could have instead used the fg_setsize routine here with an integer
  562. argument of 21.
  563.  
  564.      The next part of the program draws the characters in the primary font on
  565. the upper half of the screen.  After doing this, the program draws the
  566. alternate font characters on the lower half.  In each case it does this with
  567. the fg_swchar routine.  By default, the string passed to fg_swchar will
  568. produce characters from the primary font.  However, you can insert a back
  569. slash character (\) in the string to toggle between the two fonts.  Don't
  570. forget the C language applies a special meaning to the back slash character
  571. within strings, so you must use two consecutive back slashes to insert a
  572. single back slash in the string.
  573.  
  574.                                  Example 7-8.
  575.  
  576.             #include <fastgraf.h>
  577.             void main(void);
  578.  
  579.             void main()
  580.             {
  581.  
  582. 110  Fastgraph User's Guide
  583.  
  584.                int old_mode;
  585.  
  586.                old_mode = fg_getmode();
  587.                fg_setmode(16);
  588.                fg_initw();
  589.                fg_setworld(0.0,6.39,0.0,3.49);
  590.                fg_setsizew(0.21);
  591.  
  592.                fg_setcolor(15);
  593.                fg_locate(0,26);
  594.                fg_text("Software characters - font 1",28);
  595.                fg_setcolor(10);
  596.                fg_movew(0.0,3.1);
  597.                fg_swchar("ABCDEFGHIJKLMNOPQRSTUVWXYZ",26,-1);
  598.                fg_movew(0.0,2.8);
  599.                fg_swchar("abcdefghijklmnopqrstuvwxyz",26,-1);
  600.                fg_movew(0.0,2.5);
  601.                fg_swchar("0123456789",10,-1);
  602.                fg_movew(0.0,2.2);
  603.                fg_swchar("!\"#$%&'()*+,-./:;<=>?[]^`{|}~",29,-1);
  604.  
  605.                fg_setcolor(15);
  606.                fg_locate(12,26);
  607.                fg_text("Software characters - font 2",28);
  608.                fg_setcolor(10);
  609.                fg_movew(0.0,1.4);
  610.                fg_swchar("\\ABCDEFGHIJKLMNOPRSTUWXYZ",25,-1);
  611.                fg_movew(0.0,1.1);
  612.                fg_swchar("\\abcdefghijklmnoprstuwxyz",25,-1);
  613.                fg_movew(0.0,0.4);
  614.                fg_swchar("\\012345678#$%&()*+/<=>?[]{}",27,-1);
  615.                fg_waitkey();
  616.  
  617.                fg_setmode(old_mode);
  618.                fg_reset();
  619.             }
  620.  
  621.      Example 7-8 displays all characters in each font.  If you compare the
  622. primary font strings with the alternate font strings, you'll see the
  623. alternate font contains fewer characters.  For example, the letters Q and V
  624. (either upper or lower case) have no corresponding character in the alternate
  625. font.  You might have also noticed the primary font does not support the full
  626. printable ASCII character set.  Any character in a string passed to the
  627. fg_swchar routine that does not have a corresponding character in the current
  628. font will display a blank character.
  629.  
  630.      In addition to the font change operator (the back slash character),
  631. fg_swchar recognizes three other operators.  The superscript operator is a
  632. back slash followed by a caret (\^).  It causes the next character to appear
  633. as a superscript.  Similarly, the subscript operator is a back slash followed
  634. by a lower case v (\v); it causes the next character to appear as a
  635. subscript.  The size of superscripted and subscripted characters is one half
  636. the height of the other characters.  The underline operator is the underscore
  637. character (_).  It causes all subsequent characters in the string to be
  638. underlined until another underscore character is found, or until the end of
  639. the string.  When using these operators, be sure to include them as part of
  640. the string length count passed to fg_swchar.
  641.                                    Chapter 7:  Character Display Routines  111
  642.  
  643.      Example 7-9 illustrates the use of the font selection, superscript,
  644. subscript, and underline operators with the fg_swchar routine.  Again,
  645. because the back slash character has a special meaning to the C programming
  646. language, we must use two consecutive back slashes to represent a single back
  647. slash within the string.  The program displays four strings:
  648.  
  649.                               cos2o + sin2o = 1
  650.                                      H2O
  651.                                      U232
  652.                            One word is underlined.
  653.  
  654. The theta symbol (o) in the first string is produced by displaying the
  655. character "h" in the alternate font.  Note another font selection operator
  656. (\) appears immediately after the "h" to revert to the primary font.  The
  657. first string also includes superscript operators (\^) to display the
  658. exponents in the equation.  The second string includes a single subscripted
  659. character, while the third string shows how to display three consecutive
  660. subscripted characters.  Finally, the fourth string illustrates how to
  661. underline characters.
  662.  
  663.      Note example 7-9 also uses the fg_setratio routine.  The first three
  664. strings are drawn with an aspect ratio of 2, making them twice as wide as
  665. they are high.  The fourth string is drawn with an aspect ratio of 1
  666. (Fastgraph's default aspect ratio for software characters), so the character
  667. height is the same as the character width.  Also, the strings are centered
  668. instead of left justified as in the previous example.
  669.  
  670.                                  Example 7-9.
  671.  
  672.             #include <fastgraf.h>
  673.             void main(void);
  674.  
  675.             void main()
  676.             {
  677.                int old_mode;
  678.  
  679.                old_mode = fg_getmode();
  680.                fg_setmode(16);
  681.                fg_setcolor(10);
  682.                fg_initw();
  683.                fg_setworld(0.0,6.39,0.0,3.49);
  684.                fg_setratio(2.0);
  685.                fg_setsizew(0.21);
  686.  
  687.                fg_movew(3.2,3.0);
  688.                fg_swchar("cos\\^2\\h\\ + sin\\^2\\h\\ = 1",25,0);
  689.  
  690.                fg_movew(3.2,2.0);
  691.                fg_swchar("H\\v2O   U\\v2\\v3\\v2",18,0);
  692.  
  693.                fg_movew(3.2,1.0);
  694.                fg_setratio(1.0);
  695.                fg_swchar("One _word_ is underlined.",25,0);
  696.                fg_waitkey();
  697.  
  698. 112  Fastgraph User's Guide
  699.  
  700.                fg_setmode(old_mode);
  701.                fg_reset();
  702.             }
  703.  
  704.      The fg_setangle routine defines the angle or orientation at which
  705. software characters are displayed.  Its only argument is a floating point
  706. value that specifies the angle, measured in degrees counterclockwise from the
  707. positive x axis.  If a program draws software characters before calling
  708. fg_setangle, Fastgraph will use its default angle of zero degrees (that is,
  709. the characters will be oriented horizontally).
  710.  
  711.      In most programs, the alternate font is not needed.  However, if you use
  712. the fg_swchar routine, Fastgraph will include the definitions of these
  713. characters in your program's data segment.  To prevent wasting this space,
  714. Fastgraph includes the fg_swtext routine.  The fg_swtext routine is same as
  715. the fg_swchar routine, except it does not include the alternate font.  Since
  716. the font selection operator does not apply when using fg_swtext, the routine
  717. simply ignores it.  You should only use fg_swtext if do not use fg_swchar.
  718. If you use both routines, your program will still work correctly, but its
  719. data segment will contain an extra copy of the primary font definitions.
  720.  
  721.      Example 7-10 demonstrates the use of the fg_setangle and fg_swtext
  722. routines.  The program draws a series of strings of the form "nnn degrees",
  723. where nnn is a multiple of 15, radiating from the screen center.  Each string
  724. appears at the specified angle.  For example, the string "15 degrees" is
  725. drawn at an angle of 15 degrees.
  726.  
  727.                                 Example 7-10.
  728.  
  729.                #include <fastgraf.h>
  730.                void main(void);
  731.  
  732.                void main()
  733.                {
  734.                   char string[24];
  735.                   int angle;
  736.                   int old_mode;
  737.  
  738.                   old_mode = fg_getmode();
  739.                   fg_setmode(16);
  740.                   fg_setcolor(10);
  741.                   fg_initw();
  742.                   fg_setworld(0.0,6.39,0.0,3.49);
  743.                   fg_setsizew(0.21);
  744.  
  745.                   for (angle = 0; angle < 360; angle += 15) {
  746.                      fg_movew(3.2,1.75);
  747.                      fg_setangle((double)angle);
  748.                      sprintf(string,"     %3d degrees",angle);
  749.                      fg_swtext(string,16,-1);
  750.                      }
  751.                   fg_waitkey();
  752.  
  753.                   fg_setmode(old_mode);
  754.                   fg_reset();
  755.                }
  756.  
  757.                                    Chapter 7:  Character Display Routines  113
  758.  
  759.      The final routine pertaining to software characters is fg_swlength,
  760. which returns the length of a specified string of software characters in
  761. world space units.  The length is returned as the routine's floating point
  762. function value.  The fg_swlength routine has two arguments -- a string of
  763. software characters, and an integer value specifying the number of characters
  764. in the string.  As with fg_swchar and fg_swtext, the count includes any of
  765. the special operator characters.
  766.  
  767.      Example 7-11 demonstrates a typical use of the fg_swlength routine.  The
  768. program displays the string "hello there." in light green against a gray
  769. background in the middle of the screen.  As in our previous software
  770. character examples, the program uses mode 16 and first performs the necessary
  771. initializations to use software characters.  Following this, the program uses
  772. the fg_swlength routine to compute the length in world space units of the
  773. string.  Note we have added blank characters to each end of the string passed
  774. to fg_swlength; this increases the length of the actual string and will
  775. effectively give the gray rectangle an extended border on its left and right
  776. sides.  The string length returned by fg_swlength is multiplied by 0.5,
  777. giving the distance from the middle of the screen to either side of the
  778. rectangle.  The program then uses this value to compute the minimum and
  779. maximum x coordinates passed to fg_rectw.  After drawing the gray rectangle,
  780. the program uses fg_swtext to draw the string of software characters in the
  781. middle of the screen.  It then waits for a keystroke before restoring the
  782. original video mode and screen attributes and returning to DOS.
  783.  
  784.                                 Example 7-11.
  785.  
  786.               #include <fastgraf.h>
  787.               void main(void);
  788.  
  789.               void main()
  790.               {
  791.                  int old_mode;
  792.                  double half;
  793.                  double fg_swlength();
  794.  
  795.                  old_mode = fg_getmode();
  796.                  fg_setmode(16);
  797.                  fg_initw();
  798.                  fg_setworld(0.0,6.39,0.0,3.49);
  799.                  fg_setsizew(0.21);
  800.  
  801.                  fg_setcolor(7);
  802.                  half = fg_swlength(" Hello there. ",14) * 0.5;
  803.                  fg_rectw(3.2-half,3.2+half,1.6,1.9);
  804.  
  805.                  fg_setcolor(10);
  806.                  fg_movew(3.2,1.65);
  807.                  fg_swtext("Hello there.",12,0);
  808.                  fg_waitkey();
  809.  
  810.                  fg_setmode(old_mode);
  811.                  fg_reset();
  812.               }
  813.  
  814. 114  Fastgraph User's Guide
  815.  
  816.  
  817. Bit-Mapped Characters
  818.  
  819.      Bit-mapped characters combine the properties of hardware and software
  820. characters.  They are a fixed size, as hardware characters are, but they can
  821. be positioned anywhere instead of just in character cells.  Because they are
  822. not scalable, they do not require floating point arithmetic, and therefore
  823. they are much faster to display than software characters.
  824.  
  825.      Fastgraph makes no special provision for bit-mapped characters because
  826. it treats them as if they were any other bit-mapped image.  For example, to
  827. use a five-pixel by five-pixel bit-mapped font, you can construct characters
  828. as shown below and then store these representations in an image array.
  829.  
  830.                           *       * * * *       * * * *
  831.                         *   *     *       *   *
  832.                       * * * * *   * * * *     *
  833.                       *       *   *       *   *
  834.                       *       *   * * * *       * * * *
  835.  
  836. The image display routines fg_drawmap and fg_drwimage, discussed in Chapter
  837. 9, could then be used to display specific characters from the image array.
  838.  
  839.  
  840. Summary of Character Display Routines
  841.  
  842.      This section summarizes the functional descriptions of the Fastgraph
  843. routines presented in this chapter.  More detailed information about these
  844. routines, including their arguments and return values, may be found in the
  845. Fastgraph Reference Manual.
  846.  
  847.      FG_CHGATTR applies the current text display attribute to a given number
  848. of characters, starting at the text cursor position.  This routine leaves the
  849. text cursor one column to the right of the last character changed (or the
  850. first column of the next row if the last character is at the end of a row).
  851. It has no effect in graphics video modes.
  852.  
  853.      FG_CHGTEXT displays a string of hardware characters, starting at the
  854. text cursor position, using the existing text display attributes.  This
  855. routine leaves the text cursor one column to the right of the last character
  856. displayed (or the first column of the next row if the last character is at
  857. the end of a row).  It has no effect in graphics video modes.
  858.  
  859.      FG_GETATTR returns the character attribute stored at the specified
  860. position on the active video page.  It has no effect in graphics video modes.
  861.  
  862.      FG_GETCHAR returns the character value stored at the specified position
  863. on the active video page.  It has no effect in graphics video modes.
  864.  
  865.      FG_LOCATE changes the text cursor position for the active video page.
  866.  
  867.      FG_SETANGLE defines the angle or orientation at which software
  868. characters are displayed.  The angle is measured in degrees counterclockwise
  869. from the positive x axis.
  870.  
  871.      FG_SETATTR establishes the current text display attribute in text video
  872. modes.  This routine has no effect in graphics video modes.
  873.                                    Chapter 7:  Character Display Routines  115
  874.  
  875.      FG_SETCOLOR establishes the current color index (which may be a virtual
  876. color index in graphics modes).  In text modes, the fg_setcolor routine
  877. provides an alternate method of establishing the current text display
  878. attribute.
  879.  
  880.      FG_SETRATIO defines the aspect ratio for software characters.  The
  881. aspect ratio is the ratio of character width to character height.
  882.  
  883.      FG_SETSIZE defines the height of software characters in screen space
  884. units.
  885.  
  886.      FG_SETSIZEW defines the height of software characters in world space
  887. units.
  888.  
  889.      FG_SWCHAR displays a string of software characters using the current
  890. color index.  The string may be left justified, centered, or right justified
  891. relative to the graphics cursor position.  The string passed to fg_swchar may
  892. contain special operators that allow switching between fonts, underlining,
  893. superscripting, or subscripting.  This routine has no effect in text video
  894. modes.
  895.  
  896.      FG_SWLENGTH returns the length in world space units of a string of
  897. software characters.
  898.  
  899.      FG_SWTEXT is a scaled down version of the fg_swchar routine.  It does
  900. not include the alternate font character definitions and thus requires less
  901. memory than fg_swchar.
  902.  
  903.      FG_TEXT displays a string of hardware characters, starting at the text
  904. cursor position, using the current color attribute (for text modes) or color
  905. index (for graphics modes).  This routine leaves the text cursor one column
  906. to the right of the last character displayed (or the first column of the next
  907. row if the last character is at the end of a row).
  908.  
  909.      FG_WHERE retrieves the row and column numbers of the text cursor
  910. position.
  911.  
  912.      FG_XALPHA and FG_YALPHA convert screen space coordinates to character
  913. space.
  914.  
  915.      FG_XCONVERT and FG_YCONVERT convert character space coordinates to
  916. screen space.
  917. 116  Fastgraph User's Guide