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

  1.  
  2.  
  3. Chapter 8
  4.  
  5. Video Page Management
  6. 118  Fastgraph User's Guide
  7.  
  8.  
  9. Overview
  10.  
  11.      The amount of memory required to store one full screen of information is
  12. called a video page.  This chapter will discuss video pages in detail, along
  13. with the Fastgraph routines you can use to manage video pages.
  14.  
  15.  
  16. Physical Pages and Virtual Pages
  17.  
  18.      Pages that use the memory that resides on the video adapter are called
  19. physical pages or true pages.  The number of physical pages available depends
  20. on the video mode and the amount of memory resident on the user's video
  21. adapter.  All video modes have at least one physical page.
  22.  
  23.      In certain video modes, Fastgraph can allocate available random-access
  24. memory (RAM) and treat this memory as a video page.  Pages that use standard
  25. RAM in this sense are called virtual pages.  From a programmer's perspective,
  26. virtual pages are essentially identical to physical pages.
  27.  
  28.      The following table shows the number of physical pages in each video
  29. mode.  It also indicates whether or not specific video modes support virtual
  30. pages.
  31.  
  32.         Mode                              Page Size  Physical  Virtual
  33.        Number Description                 in Bytes   Pages     Pages
  34.  
  35.          0    40 column color text          2,000    8         no
  36.          1    40 column color text          2,000    8         no
  37.          2    80 column color text          4,000    4         no
  38.          3    80 column color text          4,000    4         no
  39.          4    320 x 200 CGA graphics       16,000    1         yes
  40.          5    320 x 200 CGA graphics       16,000    1         yes
  41.          6    640 x 200 CGA graphics       16,000    1         yes
  42.          7    80 column monochrome text     4,000    1         yes
  43.          9    320 x 200 Tandy graphics     32,000    1         yes
  44.          11   720 x 348 Hercules graphics  31,320    2         yes
  45.          12   320 x 200 Hercules graphics  31,320    2         yes
  46.          13   320 x 200 EGA graphics       32,000    8         no
  47.          14   640 x 200 EGA graphics       64,000    4         no
  48.          15   640 x 350 EGA mono graphics  56,000    2         no
  49.          16   640 x 350 EGA graphics      112,000    2         no
  50.          17   640 x 480 MCGA/VGA graphics  38,400    1+        no
  51.          18   640 x 480 VGA graphics      153,600    1+        no
  52.          19   320 x 200 MCGA graphics      64,000    1         yes
  53.          20   320 x 200 VGA graphics       64,000    4         no
  54.          21   320 x 400 VGA graphics      128,000    2         no
  55.          22   320 x 240 VGA graphics       76,800    3+        no
  56.          23   320 x 480 VGA graphics      153,600    1+        no
  57.  
  58. The preceding table assumes the video adapter for EGA and VGA modes contains
  59. 256K bytes of video memory.  For EGA adapters with less video memory, the
  60. number of physical pages is reduced proportionately.  In other words, a 64K
  61. EGA has two video pages available instead of eight pages in mode 13.
  62.                                         Chapter 8:  Video Page Management  119
  63.  
  64.      Note that the number of physical pages in some video modes is followed
  65. by a plus symbol.  In these modes, there is an additional partial video page
  66. available.  For modes 17, 18, and 23, there is one full page (page 0) plus
  67. one partial page of 320 pixel rows (page 1).  For mode 22, there are three
  68. full physical pages (numbered 0 to 2) plus one partial page of 80 pixel rows
  69. (page 3).  You can safely use the partial pages as long as you don't
  70. reference pixel rows beyond the last available row.  However, you cannot use
  71. the fg_setvpage or the fg_text routines on the partial video page.
  72.  
  73.      Physical pages are numbered starting at zero.  For example, there are
  74. four physical video pages available in mode 3, and they are numbered 0 to 3.
  75. Virtual pages are numbered n to 63, where n is the number of physical pages
  76. in that mode.  For example, there are two physical pages (numbered 0 and 1)
  77. and 62 virtual pages (numbered 2 to 63) in mode 11.  Note only modes 4
  78. through 12 and mode 19 offer virtual pages, and the amount of conventional
  79. memory in the user's system usually limits the number of virtual pages
  80. available (this is especially true in mode 19 because of the large page
  81. size).
  82.  
  83.  
  84. Pages With Special Meanings
  85.  
  86.      There are three video pages that have special meanings to Fastgraph.
  87. The visual page, as one might guess, is the video page currently visible on
  88. the user's display.  The active page is the video page to which Fastgraph
  89. writes text or graphics information.  The hidden page is meaningful only to a
  90. few Fastgraph routines and will be discussed specifically within the context
  91. of those routines.  The fg_setmode routine sets all three of these pages to
  92. page 0, and it does not matter if these pages are physical or virtual.
  93.  
  94.      One of the most useful features of multiple video pages (either physical
  95. or virtual) is the ability to build a text or graphics image off screen (that
  96. is, on some video page besides the visual page).  Then, once the image is
  97. ready, we can either transfer it to the visual page, or make the page on
  98. which the image resides the visual page.  This feature is especially useful
  99. in animation, for it displays an image instantaneously instead of visibly
  100. updating the screen while producing the image.
  101.  
  102.  
  103. Some Simple Examples
  104.  
  105.      In this section, we will present six variations of a simple program that
  106. uses four video pages.  The program fills each video page with a rectangle
  107. and then displays text containing the video page number in the center of each
  108. page.  The first two examples run in a specific text or graphics video mode
  109. and only use physical pages.  The next two examples also run in a specific
  110. text or graphics video mode, but they also use virtual pages.  The final two
  111. examples are more general and run in several video modes.  You could of
  112. course write a program that essentially does the same thing as the examples
  113. in this section without using multiple video pages.  However, to use
  114. Fastgraph's image display and animation routines effectively, you must first
  115. understand the concept of video pages.
  116.  
  117.      Before proceeding, we must introduce the Fastgraph routines fg_setpage
  118. and fg_setvpage.  The fg_setpage routine defines the active video page, which
  119. causes Fastgraph to put subsequent text and graphics output on that page.
  120. The fg_setvpage routine defines the visual video page displayed on the
  121. 120  Fastgraph User's Guide
  122.  
  123. screen.  Both routines take a single integer argument between 0 and 63 that
  124. specifies the video page number.  It does not matter if the referenced video
  125. page is a physical page or a virtual page.  As mentioned earlier, fg_setmode
  126. makes page 0 the active and visual video page.
  127.  
  128.      Example 8-1 uses four video pages (numbered 0 to 3) in the 40-column
  129. color text mode (mode 1).  The program first calls fg_testmode to check the
  130. availability of the requested video mode when used with four video pages.  If
  131. it is available, the program calls fg_setmode to establish that video mode.
  132. The first for loop fills each of the four pages with different color
  133. rectangles and then displays black text containing the video page number in
  134. the center of each page.  It does this by calling fg_setpage to define the
  135. active video page, fg_setcolor and fg_rect to draw the colored rectangles,
  136. and finally fg_setattr, fg_locate, and fg_text to display the text.  The
  137. program must call fg_locate inside the loop because each video page has its
  138. own text cursor position.  The second for loop successively makes each video
  139. page the visual page; the page remains displayed until you press a key.
  140. After displaying all four video pages, the program restores the original
  141. video mode and screen attributes before returning to DOS.
  142.  
  143.                                  Example 8-1.
  144.  
  145.                #include <fastgraf.h>
  146.                #include <stdio.h>
  147.                #include <stdlib.h>
  148.                void main(void);
  149.  
  150.                #define PAGES 4
  151.  
  152.                void main()
  153.                {
  154.                   int color;
  155.                   int old_mode;
  156.                   int page;
  157.                   char string[8];
  158.  
  159.                   if (fg_testmode(1,PAGES) == 0) {
  160.                      printf("This program requires color.\n");
  161.                      exit(1);
  162.                      }
  163.  
  164.                   old_mode = fg_getmode();
  165.                   fg_setmode(1);
  166.  
  167.                   for (page = 0; page < PAGES; page++) {
  168.                      fg_setpage(page);
  169.                      color = page + 1;
  170.                      fg_setcolor(color);
  171.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  172.                      fg_setattr(0,color,0);
  173.                      fg_locate(12,17);
  174.                      sprintf(string,"page %d",page);
  175.                      fg_text(string,6);
  176.                      }
  177.  
  178.                   for (page = 0; page < PAGES; page++) {
  179.  
  180.                                         Chapter 8:  Video Page Management  121
  181.  
  182.                      fg_setvpage(page);
  183.                      fg_waitkey();
  184.                      }
  185.                   fg_setmode(old_mode);
  186.                   fg_reset();
  187.                }
  188.  
  189.      Example 8-2 is similar to example 8-1, but it uses the 320 by 200 EGA
  190. graphics mode (mode 13) instead of a text mode.  Note the only real
  191. difference between this program and the text mode version is the use of
  192. fg_setcolor instead of fg_setattr to make the text appear in black.
  193.  
  194.                                  Example 8-2.
  195.  
  196.                #include <fastgraf.h>
  197.                #include <stdio.h>
  198.                #include <stdlib.h>
  199.                void main(void);
  200.  
  201.                #define PAGES 4
  202.  
  203.                void main()
  204.                {
  205.                   int color;
  206.                   int old_mode;
  207.                   int page;
  208.                   char string[8];
  209.  
  210.                   if (fg_testmode(13,PAGES) == 0) {
  211.                      printf("This program requires a ");
  212.                      printf("320 x 200 EGA graphics mode.\n");
  213.                      exit(1);
  214.                      }
  215.  
  216.                   old_mode = fg_getmode();
  217.                   fg_setmode(13);
  218.  
  219.                   for (page = 0; page < PAGES; page++) {
  220.                      fg_setpage(page);
  221.                      color = page + 1;
  222.                      fg_setcolor(color);
  223.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  224.                      fg_setcolor(0);
  225.                      fg_locate(12,17);
  226.                      sprintf(string,"page %d",page);
  227.                      fg_text(string,6);
  228.                      }
  229.  
  230.                   for (page = 0; page < PAGES; page++) {
  231.                      fg_setvpage(page);
  232.                      fg_waitkey();
  233.                      }
  234.  
  235.                   fg_setmode(old_mode);
  236.                   fg_reset();
  237.                }
  238.  
  239. 122  Fastgraph User's Guide
  240.  
  241.      Virtual video pages are created with Fastgraph's fg_allocate routine.
  242. The fg_allocate routine reserves conventional random-access memory (RAM)
  243. which Fastgraph then treats as a video page.  The amount of memory required
  244. depends on the current video mode.  The fg_allocate routine takes a single
  245. integer argument that specifies the page number by which the virtual page
  246. will be referenced.  This value must be between 0 and 63.
  247.  
  248.      If you try to create a virtual page with a page number already assigned
  249. to a physical page, fg_allocate does nothing.  For example, in the Hercules
  250. graphics modes (modes 11 and 12) there are two physical pages numbered 0 and
  251. 1.  Virtual pages in the Hercules graphics modes must thus have page numbers
  252. between 2 and 63.  If you tell fg_allocate to create a Hercules virtual page
  253. numbered 0 or 1, it does nothing because those video pages are physical
  254. pages.  Similarly, if you use the fg_allocate routine in a video mode that
  255. does not support virtual video pages, it simply returns without doing
  256. anything.
  257.  
  258.      A possible problem with fg_allocate can occur when there is not enough
  259. memory available for creating a virtual page in the current video mode.  The
  260. fg_allocate routine returns as its function value a status code indicating
  261. whether or not it was successful.  The possible values of the status code
  262. are:
  263.  
  264.     value  meaning
  265.  
  266.       0    virtual page created
  267.       1    specified page number is a physical page
  268.       7   virtual page created, but memory control blocks were destroyed
  269.       8   insufficient memory to create the virtual page
  270.  
  271. If you use the fg_testmode or fg_bestmode routines to check if the required
  272. number of video pages are available when using the requested video mode, you
  273. should not need to monitor the status code returned by the fg_allocate
  274. routine.
  275.  
  276.      The fg_freepage routine releases the memory for a virtual page created
  277. with the fg_allocate routine.  It requires a single integer argument that
  278. specifies the virtual page number to release.  This value must be between 0
  279. and 63.  If you try to release a physical video page, or release a virtual
  280. page that was never created, fg_freepage does nothing.  It is a good idea to
  281. use fg_freepage to release all virtual video pages before a program returns
  282. control to DOS, or just before a program selects a new video mode.
  283.  
  284.      Example 8-3 is also similar to example 8-1, but it uses the monochrome
  285. text mode (mode 7).  Because the monochrome text mode only has one physical
  286. video page, we must use virtual video pages for page numbers 1, 2, and 3.
  287. Note how the fg_allocate and fg_freepage routines are used to create and
  288. release the virtual video pages in this example.
  289.  
  290.                                  Example 8-3.
  291.  
  292.              #include <fastgraf.h>
  293.              #include <stdio.h>
  294.  
  295.                                         Chapter 8:  Video Page Management  123
  296.  
  297.              #include <stdlib.h>
  298.              void main(void);
  299.  
  300.              #define PAGES 4
  301.  
  302.              void main()
  303.              {
  304.                 int old_mode;
  305.                 int page;
  306.                 char string[8];
  307.  
  308.                 if (fg_testmode(7,PAGES) == 0) {
  309.                    printf("This program requires monochrome.\n");
  310.                    exit(1);
  311.                    }
  312.  
  313.                 old_mode = fg_getmode();
  314.                 fg_setmode(7);
  315.                 fg_cursor(0);
  316.  
  317.                 for (page = 0; page < PAGES; page++) {
  318.                    fg_allocate(page);
  319.                    fg_setpage(page);
  320.                    fg_setcolor(7);
  321.                    fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  322.                    fg_setattr(0,7,0);
  323.                    fg_locate(12,37);
  324.                    sprintf(string,"page %d",page);
  325.                    fg_text(string,6);
  326.                    }
  327.  
  328.                 for (page = 0; page < PAGES; page++) {
  329.                    fg_setvpage(page);
  330.                    fg_waitkey();
  331.                    fg_freepage(page);
  332.                    }
  333.  
  334.                 fg_setmode(old_mode);
  335.                 fg_reset();
  336.              }
  337.  
  338.      Example 8-4 is similar to example 8-3, but it uses the standard Hercules
  339. graphics mode (mode 11) instead of the monochrome text mode.  Because the
  340. Hercules graphics modes have two physical video pages, we must use virtual
  341. video pages for page numbers 2 and 3.  Note the only real difference between
  342. this program and the text mode version is the use of fg_setcolor instead of
  343. fg_setattr to make the text appear in black.
  344.  
  345.                                  Example 8-4.
  346.  
  347.                #include <fastgraf.h>
  348.                #include <stdio.h>
  349.                #include <stdlib.h>
  350.                void main(void);
  351.  
  352.                #define PAGES 4
  353.  
  354. 124  Fastgraph User's Guide
  355.  
  356.                void main()
  357.                {
  358.                   int old_mode;
  359.                   int page;
  360.                   char string[8];
  361.  
  362.                   if (fg_testmode(11,PAGES) == 0) {
  363.                      printf("This program requires Hercules ");
  364.                      printf("monochrome graphics.\n");
  365.                      exit(1);
  366.                      }
  367.  
  368.                   old_mode = fg_getmode();
  369.                   fg_setmode(11);
  370.  
  371.                   for (page = 0; page < PAGES; page++) {
  372.                      fg_allocate(page);
  373.                      fg_setpage(page);
  374.                      fg_setcolor(7);
  375.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  376.                      fg_setcolor(0);
  377.                      fg_locate(12,37);
  378.                      sprintf(string,"page %d",page);
  379.                      fg_text(string,6);
  380.                      }
  381.  
  382.                   for (page = 0; page < PAGES; page++) {
  383.                      fg_setvpage(page);
  384.                      fg_waitkey();
  385.                      fg_freepage(page);
  386.                      }
  387.  
  388.                   fg_setmode(old_mode);
  389.                   fg_reset();
  390.                }
  391.  
  392.      Example 8-5 is a generalized version of examples 8-1 and 8-3 that runs
  393. in any 80-column text video mode.  To simplify the program, each video page
  394. is filled with rectangles of the same color.  Note that fg_allocate and
  395. fg_freepage are used to manage the virtual video pages in case fg_bestmode
  396. selects the monochrome text mode (mode 7).  If fg_bestmode selects one of the
  397. 80-column color text modes (which have four physical video pages),
  398. fg_allocate and fg_freepage will simply return without doing anything.
  399.  
  400.                                  Example 8-5.
  401.  
  402.                 #include <fastgraf.h>
  403.                 #include <stdio.h>
  404.                 #include <stdlib.h>
  405.                 void main(void);
  406.  
  407.                 #define PAGES 4
  408.  
  409.                 void main()
  410.                 {
  411.                    int old_mode, new_mode;
  412.  
  413.                                         Chapter 8:  Video Page Management  125
  414.                    int page;
  415.                    char string[8];
  416.  
  417.                    new_mode = fg_bestmode(80,25,PAGES);
  418.                    if (new_mode < 0) {
  419.                       printf("This program requires ");
  420.                       printf("an 80-column display.\n");
  421.                       exit(1);
  422.                       }
  423.                    old_mode = fg_getmode();
  424.                    fg_setmode(new_mode);
  425.                    fg_cursor(0);
  426.  
  427.                    for (page = 0; page < PAGES; page++) {
  428.                       fg_allocate(page);
  429.                       fg_setpage(page);
  430.                       fg_setcolor(7);
  431.                       fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  432.                       fg_setattr(0,7,0);
  433.                       fg_locate(12,37);
  434.                       sprintf(string,"page %d",page);
  435.                       fg_text(string,6);
  436.                       }
  437.  
  438.                    for (page = 0; page < PAGES; page++) {
  439.                       fg_setvpage(page);
  440.                       fg_waitkey();
  441.                       fg_freepage(page);
  442.                       }
  443.  
  444.                    fg_setmode(old_mode);
  445.                    fg_reset();
  446.                 }
  447.  
  448.      Example 8-6 is a generalized version of examples 8-2 and 8-4 that runs
  449. in any 320 by 200 graphics video mode.  To simplify the program, each video
  450. page is filled with rectangles of the same color.  As in example 8-5,
  451. fg_allocate and fg_freepage are used to manage the virtual video pages in
  452. case fg_bestmode selects a video mode with fewer than four physical video
  453. pages.  Note the only real difference between this program and the text mode
  454. version is the use of fg_setcolor instead of fg_setattr to make the text
  455. appear in black.
  456.  
  457.                                  Example 8-6.
  458.  
  459.                 #include <fastgraf.h>
  460.                 #include <stdio.h>
  461.                 #include <stdlib.h>
  462.                 void main(void);
  463.  
  464.                 #define PAGES 4
  465.  
  466.                 void main()
  467.                 {
  468.                    int old_mode, new_mode;
  469.                    int page;
  470.                    char string[8];
  471.  
  472. 126  Fastgraph User's Guide
  473.  
  474.  
  475.                    new_mode = fg_bestmode(320,200,PAGES);
  476.                    if (new_mode < 0) {
  477.                       printf("This program requires a ");
  478.                       printf("320 x 200 graphics mode.\n");
  479.                       exit(1);
  480.                       }
  481.  
  482.                    old_mode = fg_getmode();
  483.                    fg_setmode(new_mode);
  484.  
  485.                    for (page = 0; page < PAGES; page++) {
  486.                       fg_allocate(page);
  487.                       fg_setpage(page);
  488.                       fg_setcolor(15);
  489.                       fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  490.                       fg_setcolor(0);
  491.                       fg_locate(12,17);
  492.                       sprintf(string,"page %d",page);
  493.                       fg_text(string,6);
  494.                       }
  495.  
  496.                    for (page = 0; page < PAGES; page++) {
  497.                       fg_setvpage(page);
  498.                       fg_waitkey();
  499.                       fg_freepage(page);
  500.                       }
  501.  
  502.                    fg_setmode(old_mode);
  503.                    fg_reset();
  504.                 }
  505.  
  506.  
  507. Text Cursors
  508.  
  509.      As mentioned in the previous chapter, Fastgraph draws hardware
  510. characters at the position defined by the text cursor.  Like the graphics
  511. cursor, the text cursor is not a cursor in the true sense, but is simply a
  512. pair of character space (row,column) coordinates with a special meaning.  The
  513. first 8 video pages (that is, pages 0 through 7) each have their own text
  514. cursor.  Each subsequent group of 8 video pages (pages 8 through 15, pages 16
  515. to 23, and so forth) respectively share the same text cursor positions as the
  516. first 8 pages.  This means the fg_locate routine will update one of 8
  517. different text cursors depending on the active video page.  Similarly, the
  518. fg_where routine returns the text cursor position for the active page.  The
  519. fg_setmode routine sets all 8 text cursor positions to the character space
  520. coordinates (0,0).
  521.  
  522.      Example 8-7 demonstrates the use of different text cursors in an 80-
  523. column color text mode (mode 3).  The program first displays the text "Page "
  524. on video page 0 (the visible page) and waits for a keystroke.  It then makes
  525. page 1 the active video page, changes the text cursor location for that page,
  526. and displays the text "Page 1" on video page 1.  Next, it appends the
  527. character "0" to the text originally displayed on page 0.  Note it is not
  528. necessary to restore the text cursor position for page 0 because it is
  529. unaffected by changing the text cursor for page 1.  After waiting for another
  530.                                         Chapter 8:  Video Page Management  127
  531.  
  532. keystroke, the program makes video page 1 the visual page and then waits for
  533. yet another keystroke before returning to DOS.
  534.  
  535.                                  Example 8-7.
  536.  
  537.                          #include <fastgraf.h>
  538.                          void main(void);
  539.  
  540.                          void main()
  541.                          {
  542.                             int old_mode;
  543.  
  544.                             old_mode = fg_getmode();
  545.                             fg_setmode(3);
  546.                             fg_cursor(0);
  547.                             fg_setattr(10,0,0);
  548.  
  549.                             fg_locate(1,0);
  550.                             fg_text("Page ",5);
  551.                             fg_waitkey();
  552.  
  553.                             fg_setpage(1);
  554.                             fg_locate(23,0);
  555.                             fg_text("Page 1",6);
  556.  
  557.                             fg_setpage(0);
  558.                             fg_text("0",1);
  559.                             fg_waitkey();
  560.  
  561.                             fg_setvpage(1);
  562.                             fg_waitkey();
  563.  
  564.                             fg_setmode(old_mode);
  565.                             fg_reset();
  566.                          }
  567.  
  568.  
  569. Obtaining Video Page Information
  570.  
  571.      Fastgraph includes two routines, fg_getpage and fg_getvpage, that
  572. respectively return the current active or visual video page number.  Each
  573. routine returns the video page number as its function value, and neither
  574. routine requires any arguments.
  575.  
  576.      The fg_getaddr routine is sometimes useful when using virtual pages.  It
  577. returns as its function value the segment address for the start of the active
  578. video page.  It does not require any arguments.  Although fg_getaddr is more
  579. useful when using virtual video pages, it works equally well when using
  580. physical video pages.
  581.  
  582.      Example 8-8 illustrates the use of the fg_getpage, fg_getvpage, and
  583. fg_getaddr routines in the standard CGA color graphics mode (mode 4).  This
  584. video mode offers only one physical page, so the program uses fg_allocate to
  585. create a virtual video page (page 1).  After creating the virtual page, the
  586. program makes it the active video page; page 0 remains the visual video page.
  587. The fg_getpage routine then returns the active page number (1), followed by a
  588. call to fg_getvpage to return the visual page number (0).  Next, the program
  589. 128  Fastgraph User's Guide
  590.  
  591. uses fg_getaddr to return the segment address for video pages 0 and 1.
  592. Finally, it restores the original video mode and screen attributes, displays
  593. the returned values, and returns to DOS.
  594.  
  595.                                  Example 8-8.
  596.  
  597.                  #include <fastgraf.h>
  598.                  #include <stdio.h>
  599.                  void main(void);
  600.  
  601.                  void main()
  602.                  {
  603.                     int old_mode;
  604.                     int active, visual;
  605.                     int page0, page1;
  606.  
  607.                     old_mode = fg_getmode();
  608.                     fg_setmode(4);
  609.                     fg_allocate(1);
  610.                     fg_setpage(1);
  611.  
  612.                     active = fg_getpage();
  613.                     visual = fg_getvpage();
  614.  
  615.                     fg_setpage(0);
  616.                     page0 = fg_getaddr();
  617.                     fg_setpage(1);
  618.                     page1 = fg_getaddr();
  619.  
  620.                     fg_freepage(1);
  621.                     fg_setmode(old_mode);
  622.                     fg_reset();
  623.  
  624.                     printf("Active page is %d.\n",active);
  625.                     printf("Visual page is %d.\n",visual);
  626.                     printf("Page 0 address is %4X\n",page0);
  627.                     printf("Page 1 address is %4X\n",page1);
  628.                  }
  629.  
  630.  
  631. Considerations for Virtual Pages
  632.  
  633.      When you are using virtual pages, you should avoid using the fg_setvpage
  634. routine in sections of the program that require fast screen updates or
  635. animation sequences.  This is because the PC and PS/2 video BIOS are only
  636. capable of displaying physical pages.  To compensate for this restriction,
  637. Fastgraph exchanges the contents of a physical page with the requested
  638. virtual page.  In other words, if page 1 is a virtual page and you make it
  639. the visual page, Fastgraph will exchange the contents of page 1 with whatever
  640. page was previously the visual page.  This does not mean Fastgraph's page
  641. numbers change because Fastgraph also maintains an internal table containing
  642. video page addresses and exchanges the two corresponding table entries.  As
  643. before, you would make page 1 the active video page if you wanted to write
  644. something to the visual page.
  645.  
  646.      About the only other potential problem when using virtual pages is what
  647. happens when you try to write to a non-existent video page (for example, if
  648.                                         Chapter 8:  Video Page Management  129
  649.  
  650. you write to virtual video page 1 before creating it with fg_allocate).  In
  651. this case, Fastgraph simply redirects the video output to the visual page.
  652.  
  653.  
  654. Logical Pages
  655.  
  656.      In addition to physical and virtual video pages, Fastgraph offers
  657. another class of video pages, called logical pages.  You can create logical
  658. pages in any video mode.  They can exist in conventional memory, expanded
  659. memory (EMS), or extended memory (XMS).  However, they are not as versatile
  660. as physical or virtual pages because the only operations you can perform with
  661. logical pages are:
  662.  
  663.        Copy an entire physical or virtual page to a logical page
  664.        Copy an entire logical page to a physical or virtual page
  665.        Copy an entire logical page to another logical page
  666.  
  667. Three Fastgraph routines -- fg_alloccms, fg_allocems, and fg_allocxms --
  668. create logical pages in conventional memory, expanded memory, and extended
  669. memory, respectively.  All three routines have a single integer argument that
  670. specifies the page number by which the logical page will be referenced.  The
  671. page number must be between 1 and 63 and must not reference a physical or
  672. virtual page.  Their return value is 0 if the logical page is created, and
  673. negative otherwise (refer to the descriptions of these routines in the
  674. Fastgraph Reference Manual for a complete list of return values).  As with
  675. virtual pages, use the fg_freepage function to release a logical page.
  676.  
  677.      Before you can create logical pages in expanded or extended memory, you
  678. must initialize these resources for use with Fastgraph.  The fg_initems
  679. routine initializes expanded memory.  To use expanded memory, you must have
  680. an Expanded Memory Manager (EMM) that conforms to the Lotus/Intel/Microsoft
  681. Expanded Memory Specification (LIM-EMS) version 3.2 or later.  On 80386 and
  682. 80486 systems, the EMM386.EXE device driver supplied with DOS 5.0 can be used
  683. to treat some or all of extended memory as expanded memory.  The fg_initxms
  684. routine initializes extended memory for use with Fastgraph.  To use extended
  685. memory, you must have an XMS driver that conforms to the
  686. Lotus/Intel/Microsoft/AST eXtended Memory Specification version 2.0 or later,
  687. such as HIMEM.SYS.  XMS drivers require an 80286, 80386, or 80486 system.
  688. The fg_initems and fg_initxms routines have no arguments.  Their return value
  689. is 0 if successful, and -1 if the required driver and resources are not
  690. present.
  691.  
  692.      Example 8-9 illustrates the use of logical pages in a 320 x 200 color
  693. graphics mode.  The program first tries to create a logical page in extended
  694. memory by calling fg_initxms and fg_allocxms.  If the initialization or page
  695. creation fails, it then tries to create the page in expanded memory with
  696. fg_initems and fg_allocems.  Should that fail, the program calls fg_alloccms
  697. to try to create the page in conventional memory.  If it can't create the
  698. logical page at all, the program displays an error message and exits.
  699.  
  700.      Once the logical page is created, example 8-9 displays the word "test"
  701. in the middle of the visual page (page 0) and then uses fg_copypage to
  702. transfer the visual page contents to the logical page (page 8).  Because this
  703. program runs in one of several different graphics modes, we must use a
  704. logical page number greater than any possible physical page number.  We chose
  705. page 8 because mode 13 has physical pages numbered 0 through 7, and no mode
  706. has higher-numbered physical pages.  After waiting for a keystroke, the
  707. 130  Fastgraph User's Guide
  708.  
  709. program erases the visual page, waits for another keystroke, and copies the
  710. logical page contents back to the visual page.  It then releases the logical
  711. page and exits.
  712.  
  713.                                  Example 8-9.
  714.  
  715.               #include <fastgraf.h>
  716.               #include <stdio.h>
  717.               #include <stdlib.h>
  718.               void main(void);
  719.  
  720.               void main()
  721.               {
  722.                  int new_mode, old_mode;
  723.                  int status;
  724.  
  725.                  new_mode = fg_bestmode(320,200,1);
  726.                  if (new_mode < 0 || new_mode == 12) {
  727.                     printf("This program requires a 320 ");
  728.                     printf("x 200 color graphics mode.\n");
  729.                     exit(1);
  730.                     }
  731.                  old_mode = fg_getmode();
  732.                  fg_setmode(new_mode);
  733.  
  734.                  status = fg_initxms();
  735.                  if (status == 0) status = fg_allocxms(8);
  736.                  if (status < 0) {
  737.                     status = fg_initems();
  738.                     if (status == 0) status = fg_allocems(8);
  739.                     }
  740.                  if (status < 0) status = fg_alloccms(8);
  741.                  if (status < 0) {
  742.                     fg_setmode(old_mode);
  743.                     fg_reset();
  744.                     printf("Unable to create logical page.\n");
  745.                     exit(1);
  746.                     }
  747.  
  748.                  fg_setcolor(7);
  749.                  fg_rect(0,319,0,199);
  750.                  fg_setcolor(9);
  751.                  fg_locate(12,18);
  752.                  fg_text("test",4);
  753.                  fg_waitkey();
  754.  
  755.                  fg_copypage(0,8);
  756.                  fg_erase();
  757.                  fg_waitkey();
  758.                  fg_copypage(8,0);
  759.                  fg_waitkey();
  760.  
  761.                  fg_freepage(8);
  762.                  fg_setmode(old_mode);
  763.                  fg_reset();
  764.               }
  765.  
  766.                                         Chapter 8:  Video Page Management  131
  767.  
  768.      As mentioned before, the only function you can perform with logical
  769. pages is copying one video page to another.  The fg_copypage routine provides
  770. the only way to do this for logical pages.  See page 175 for a description of
  771. fg_copypage.
  772.  
  773.  
  774. Video Page Resizing
  775.  
  776.      Resizing is the process of changing the dimensions of a video page.  It
  777. is available only in the native EGA graphics modes (modes 13 to 16), native
  778. VGA graphics modes (modes 17 and 18), and extended VGA modes (modes 20 to
  779. 23).  Resizing does not change the screen resolution, but instead increases
  780. the video page size so that only part of the page is visible.  For now, we'll
  781. just introduce resizing with a simple example, but in Chapter 11 we'll see
  782. its real power when we perform smooth panning.
  783.  
  784.      The Fastgraph routine fg_resize changes the dimensions of a video page.
  785. Its two integer arguments define the page width and page height, both in
  786. pixels.  Example 8-10 runs in the 320 x 200 EGA graphics mode (mode 13).
  787. After establishing the video mode, it displays the word "resize" starting in
  788. column 38 of row 0.  Because the characters extend beyond the last column of
  789. the row, they wrap to the next row.  The program continues displaying this
  790. until you press a key.  Then, it clears the screen and calls fg_resize to
  791. make the page size 640 x 200 pixels.  Again the program displays the word
  792. "resize" starting in column 38 of row 0, but this time it does not wrap to
  793. the next row.  This is because the resizing doubled the page width, which
  794. increased the number of character cells per row from 40 to 80.  The
  795. characters that formerly wrapped to the next row now continue on an off-
  796. screen portion of the same row.
  797.  
  798.                                 Example 8-10.
  799.  
  800.                 #include <fastgraf.h>
  801.                 #include <stdio.h>
  802.                 #include <stdlib.h>
  803.                 void main(void);
  804.  
  805.                 void main()
  806.                 {
  807.                    int old_mode;
  808.  
  809.                    if (fg_testmode(13,1) == 0) {
  810.                       printf("This program requires a 320 ");
  811.                       printf("x 200 EGA graphics mode.\n");
  812.                       exit(1);
  813.                       }
  814.  
  815.                    old_mode = fg_getmode();
  816.                    fg_setmode(13);
  817.  
  818.                    fg_setcolor(9);
  819.                    fg_locate(0,38);
  820.                    fg_text("resize",6);
  821.                    fg_waitkey();
  822.  
  823.                    fg_erase();
  824.  
  825. 132  Fastgraph User's Guide
  826.  
  827.                    fg_resize(640,200);
  828.                    fg_setcolor(10);
  829.                    fg_locate(0,38);
  830.                    fg_text("resize",6);
  831.                    fg_waitkey();
  832.  
  833.                    fg_setmode(old_mode);
  834.                    fg_reset();
  835.                 }
  836.  
  837.      The size of a video page is constrained only by the amount of video
  838. memory available.  Increasing the video page size reduces the number of
  839. physical pages available proportionally.  In mode 13, for example, increasing
  840. the page size from 320 x 200 to 640 x 400 reduces the number of video pages
  841. from 8 to 2.  When you call fg_resize, the visual page must be page 0.  If
  842. you have created any logical video pages, you must release them with
  843. fg_freepage before calling fg_resize, and then create them again afterward.
  844. If you have initialized the mouse (with fg_mouseini), joysticks (with
  845. fg_initjoy), expanded memory (with fg_initems), or extended memory (with
  846. fg_initxms), you should re-initialize these resources after calling
  847. fg_resize.  Most mouse drivers expect a fixed video page width, so the mouse
  848. cursor may become distorted after resizing video pages.  When you call
  849. fg_resize, Fastgraph sets the clipping region to the new page limits.  The
  850. fg_setmode routine re-establishes the dimensions of a video page to the
  851. default screen resolution for the selected video mode.
  852.  
  853.       Depending on the dimensions passed to fg_resize, you may end up with a
  854. partial video page.  Again, suppose we're using mode 13 and have changed the
  855. page size to 960 x 400 (this is six times the default page size).  The
  856. original pages 0 to 5 now comprise page 0, and original pages 6 and 7 now
  857. comprise page 1.  However, there is not enough video memory left on page 1
  858. for a full 960 x 400 page.  In this case, the number of pixel rows available
  859. on page 1 would be one-third the full page size, or 133 rows.  This is
  860. because the total storage required by original pages 6 and 7 is one-third the
  861. total required for original pages 0 through 5.
  862.  
  863.  
  864. Summary of Video Page Management Routines
  865.  
  866.      This section summarizes the functional descriptions of the Fastgraph
  867. routines presented in this chapter.  More detailed information about these
  868. routines, including their arguments and return values, may be found in the
  869. Fastgraph Reference Manual.
  870.  
  871.      FG_ALLOCATE creates a virtual video page.  The amount of memory required
  872. depends on the current video mode.  This routine has no effect if it
  873. references a physical or logical video page.
  874.  
  875.      FG_ALLOCEMS creates a logical page in expanded memory (EMS).  The amount
  876. of memory required depends on the current video mode and video buffer
  877. dimensions.  This routine has no effect if it references a physical or
  878. virtual video page.
  879.  
  880.      FG_ALLOCXMS creates a logical page in extended memory (XMS).  The amount
  881. of memory required depends on the current video mode and video buffer
  882. dimensions.  This routine has no effect if it references a physical or
  883. virtual video page.
  884.                                         Chapter 8:  Video Page Management  133
  885.  
  886.      FG_COPYPAGE transfers the contents of one video page to another.  The
  887. pages may be physical, virtual, or logical video pages.  If both pages are
  888. logical pages, they must exist in the same type of memory.
  889.  
  890.      FG_FREEPAGE releases a virtual or logical video page created with the
  891. fg_allocate, fg_alloccms, fg_allocems, or fg_allocxms routines.  This routine
  892. has no effect if it references a physical video page, or a virtual page that
  893. was never created.
  894.  
  895.      FG_GETADDR returns the segment address of the active video page.
  896.  
  897.      FG_GETPAGE returns the active video page number.
  898.  
  899.      FG_GETVPAGE returns the visual video page number.
  900.  
  901.      FG_INITEMS initializes expanded memory for use with Fastgraph.
  902.  
  903.      FG_INITXMS initializes extended memory for use with Fastgraph.
  904.  
  905.      FG_RESIZE changes the dimensions of a video page in EGA and VGA graphics
  906. modes.
  907.  
  908.      FG_SETPAGE establishes the active video page.  It may be a physical or
  909. virtual page.
  910.  
  911.      FG_SETVPAGE establishes the visual video page.  It may be a physical or
  912. virtual page.
  913.