home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / animutil / fastgfx / fg303b / manuals.arj / USER13.DOC < prev    next >
Text File  |  1993-10-02  |  22KB  |  519 lines

  1. Chapter 13
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Special Effects
  8. 250   Fastgraph User's Guide
  9.  
  10.  
  11. Overview
  12.  
  13.      This chapter will discuss the Fastgraph routines that help produce
  14. special visual effects.  These include the ability to dissolve the screen
  15. contents in small increments, scroll areas of the screen, and change the
  16. physical origin of the screen.  The accompanying example programs illustrate
  17. how to use these routines to produce some interesting effects.
  18.  
  19.  
  20. Screen Dissolving
  21.  
  22.      Screen dissolving is the process of replacing the entire screen contents
  23. in random small increments instead of all at once.  Fastgraph includes two
  24. routines, fg_fadeout and fg_fadein, for this purpose.  The fg_fadeout routine
  25. incrementally replaces the visual page contents with pixels of the current
  26. color, while fg_fadein incrementally replaces the visual page contents with
  27. the hidden page contents (that is, the page defined in the most recent call
  28. to fg_sethpage).  Both routines accept an integer argument that defines the
  29. delay between each incremental replacement.  A value of zero means to perform
  30. the replacement as quickly as possible, while 1 is slightly slower, 2 is
  31. slower yet, and so forth.  The fg_fadeout and fg_fadein routines have no
  32. effect in text video modes.
  33.  
  34.      Example 13-1 shows how to use the fg_fadeout routine.  The program,
  35. which runs in any graphics video mode, first fills the screen with a
  36. rectangle of color 2.  After waiting for a keystroke, the program
  37. incrementally replaces the screen contents with pixels of color 15 (the
  38. current color index when fg_fadeout is called).  After another keystroke, the
  39. program exits gracefully.
  40.  
  41.                                 Example 13-1.
  42.  
  43.                   #include <fastgraf.h>
  44.                   void main(void);
  45.  
  46.                   void main()
  47.                   {
  48.                      int old_mode;
  49.  
  50.                      old_mode = fg_getmode();
  51.                      fg_setmode(fg_automode());
  52.  
  53.                      fg_setcolor(2);
  54.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  55.                      fg_waitkey();
  56.  
  57.                      fg_setcolor(15);
  58.                      fg_fadeout(0);
  59.                      fg_waitkey();
  60.  
  61.                      fg_setmode(old_mode);
  62.                      fg_reset();
  63.                   }
  64.  
  65.                                             Chapter 13:  Special Effects   251
  66.  
  67.      Example 13-2 shows how to use the fg_fadein routine in any 320 by 200
  68. color graphics video mode.  The program first fills the screen with a
  69. rectangle of color 2 and then fills video page 1 with a rectangle of color 1.
  70. After waiting for a keystroke, the program incrementally transfers the
  71. contents of page 1 to the visual page.  After the call to fg_fadein, both
  72. page 0 (the visual page) and page 1 (the hidden page) will contain rectangles
  73. of color 1 that fill the entire video page.  Finally, the program waits for
  74. another keystroke before returning to DOS.
  75.  
  76.                                 Example 13-2.
  77.  
  78.                 #include <fastgraf.h>
  79.                 #include <stdio.h>
  80.                 #include <stdlib.h>
  81.                 void main(void);
  82.  
  83.                 void main()
  84.                 {
  85.                    int new_mode, old_mode;
  86.  
  87.                    new_mode = fg_bestmode(320,200,2);
  88.                    if (new_mode < 0 || new_mode == 12) {
  89.                       printf("This program requires a 320 ");
  90.                       printf("x 200 color graphics mode.\n");
  91.                       exit(1);
  92.                       }
  93.                    old_mode = fg_getmode();
  94.                    fg_setmode(new_mode);
  95.                    fg_allocate(1);
  96.                    fg_sethpage(1);
  97.  
  98.                    fg_setcolor(2);
  99.                    fg_rect(0,319,0,199);
  100.                    fg_setpage(1);
  101.                    fg_setcolor(1);
  102.                    fg_rect(0,319,0,199);
  103.                    fg_waitkey();
  104.  
  105.                    fg_fadein(0);
  106.                    fg_waitkey();
  107.  
  108.                    fg_freepage(1);
  109.                    fg_setmode(old_mode);
  110.                    fg_reset();
  111.                 }
  112.  
  113.  
  114.      You also can produce some appealing visual effects by replacing the
  115. screen contents in a non-random fashion using the fg_restore or fg_transfer
  116. routines.  For example, you could copy the hidden page contents to the visual
  117. page through a series of concentric rectangular areas, each slightly larger
  118. than the previous, until the entire screen is copied.  Another interesting
  119. effect is to start around the screen perimeter and proceed toward the screen
  120. center, thus producing a "snake-like" effect.  Experimenting with such
  121. techniques may reveal other effects that suit your application.
  122. 252   Fastgraph User's Guide
  123.  
  124. Scrolling
  125.  
  126.      Another useful effect is scrolling, and Fastgraph provides a routine
  127. that performs vertical scrolling within a given region of the active video
  128. page.  The fg_scroll routine scrolls a region defined in screen space or
  129. character space.  It can scroll up or down and offers two types of scrolling:
  130. circular and end-off.  In circular scrolling, rows that scroll off one edge
  131. of the defined region appear at its opposite edge.  In end-off scrolling,
  132. such rows are simply wind up above or below the scrolling area.  The
  133. following diagrams illustrate the two types of scrolling.
  134.  
  135.                 end-off scrolling            circular scrolling
  136.  
  137.               before         after          before         after
  138.  
  139.                                C                             B
  140.                  A                             A
  141.  
  142.                                A                             A
  143.                  B                             B
  144.  
  145.  
  146.      In these diagrams, the area bounded by the double lines is the scrolling
  147. region, as specified in the call to fg_scroll.  Also, the scrolling direction
  148. is assumed to be down (that is, toward the bottom of the screen), and the
  149. number of rows to scroll is the height of the area designated B.  The number
  150. of rows to scroll is often called the scrolling increment.
  151.  
  152.      For the end-off scrolling example, the scrolling operation transfers
  153. region A downward so part of it is copied into area B.  The area C (which is
  154. the same size as area B) at the top of the scrolling region is filled with
  155. pixels of the current color index (as defined in the most recent call to
  156. fg_setcolor), and the original contents of area B are lost.  The circular
  157. scrolling example also copies region A downward into the original area B.
  158. Unlike end-off scrolling, however, circular scrolling preserves the area B by
  159. copying it to the opposite edge of the scrolling region.
  160.  
  161.      The fg_scroll routine takes six arguments.  The first four define the
  162. scrolling region in the order minimum x coordinate, maximum x coordinate,
  163. minimum y coordinate, and maximum y coordinate.  In graphics video modes, the
  164. x coordinates are extended by byte boundaries if needed.  The fifth argument
  165. is the scrolling increment.  It specifies the number of rows to scroll.  If
  166. it is positive, the scrolling direction is toward the bottom of the screen;
  167. if it is negative, the scrolling direction is toward the top of the screen.
  168. The sixth and final argument specifies the scroll type.  If this value is
  169. zero, the scroll will be circular; if it is any other value, the scroll will
  170. be end-off.  If the scroll type is circular, Fastgraph will use the hidden
  171. page (as defined in the most recent call to fg_sethpage) as a workspace (more
  172. specifically, the area bounded by the scrolling region extremes on the hidden
  173. page will be used).  We'll now present three example programs that use the
  174. fg_scroll routine.
  175.  
  176.      Example 13-3 runs in any 320 by 200 graphics video mode.  The program
  177. displays two lines of text ("line one" and "line two") in the upper left
  178. corner of the screen against a white background.  It then uses the fg_scroll
  179. routine to move the second line down four pixel rows using an end-off scroll.
  180.                                             Chapter 13:  Special Effects   253
  181. After waiting for a keystroke, the program again uses fg_scroll to move the
  182. text back to its original position.  Note especially how the fg_setcolor
  183. routine appears before the first call to fg_scroll to replace the "scrolled
  184. off" rows with pixels of color 15, thus preserving the white background.
  185.  
  186.                                 Example 13-3.
  187.  
  188.                 #include <fastgraf.h>
  189.                 #include <stdio.h>
  190.                 #include <stdlib.h>
  191.                 void main(void);
  192.  
  193.                 void main()
  194.                 {
  195.                    int new_mode, old_mode;
  196.  
  197.                    new_mode = fg_bestmode(320,200,1);
  198.                    if (new_mode < 0 || new_mode == 12) {
  199.                       printf("This program requires a 320 ");
  200.                       printf("x 200 color graphics mode.\n");
  201.                       exit(1);
  202.                       }
  203.                    old_mode = fg_getmode();
  204.                    fg_setmode(new_mode);
  205.  
  206.                    fg_setcolor(15);
  207.                    fg_rect(0,319,0,199);
  208.                    fg_setcolor(10);
  209.                    fg_text("line one",8);
  210.                    fg_locate(1,0);
  211.                    fg_text("line two",8);
  212.                    fg_waitkey();
  213.  
  214.                    fg_setcolor(15);
  215.                    fg_scroll(0,63,8,15,4,1);
  216.                    fg_waitkey();
  217.                    fg_scroll(0,63,12,19,-4,1);
  218.                    fg_waitkey();
  219.  
  220.                    fg_setmode(old_mode);
  221.                    fg_reset();
  222.                 }
  223.  
  224.      Example 13-4 is similar to example 13-3, but it runs in the 80-column
  225. color text mode (mode 3).  In text modes, we cannot scroll half a character
  226. row (four pixels) as in example 13-3, so the program scrolls the minimum one
  227. row instead.
  228.  
  229.                                 Example 13-4.
  230.  
  231.                          #include <fastgraf.h>
  232.                          void main(void);
  233.  
  234.                          void main()
  235.                          {
  236.                             int old_mode;
  237.  
  238. 254   Fastgraph User's Guide
  239.  
  240.                             old_mode = fg_getmode();
  241.                             fg_setmode(3);
  242.                             fg_cursor(0);
  243.  
  244.                             fg_setcolor(7);
  245.                             fg_rect(0,79,0,24);
  246.                             fg_setattr(10,7,0);
  247.                             fg_text("line one",8);
  248.                             fg_locate(1,0);
  249.                             fg_text("line two",8);
  250.                             fg_waitkey();
  251.  
  252.                             fg_setcolor(7);
  253.                             fg_scroll(0,7,1,1,1,1);
  254.                             fg_waitkey();
  255.                             fg_scroll(0,7,2,2,-1,1);
  256.                             fg_waitkey();
  257.  
  258.                             fg_setmode(old_mode);
  259.                             fg_reset();
  260.                          }
  261.  
  262.      Example 13-5, the final scrolling example, demonstrates a circular
  263. scroll.  The program runs in any 320 by 200 color graphics video mode; note
  264. the use of video page 1 for the workspace required when the fg_scroll routine
  265. performs a circular scroll.  The program first fills the screen with a light
  266. blue rectangle (cyan in CGA), displays a smaller white rectangle in the
  267. center of the screen, and then uses fg_move, fg_draw, and fg_paint to display
  268. a light green star (magenta in CGA) within the white rectangle.  The program
  269. executes a while loop to scroll the star upward in four pixel increments.
  270. Because the scroll is circular, rows of the star that "scroll off" the top
  271. edge of the white rectangle (whose height is the same as the scrolling
  272. region) reappear at its bottom edge.  The use of fg_waitfor within the loop
  273. simply slows down the scroll.  The scrolling continues until any key is
  274. pressed.
  275.  
  276.                                 Example 13-5.
  277.  
  278.                 #include <conio.h>
  279.                 #include <fastgraf.h>
  280.                 #include <stdio.h>
  281.                 #include <stdlib.h>
  282.                 void main(void);
  283.  
  284.                 void main()
  285.                 {
  286.                    int new_mode, old_mode;
  287.  
  288.                    new_mode = fg_bestmode(320,200,2);
  289.                    if (new_mode < 0 || new_mode == 12) {
  290.                       printf("This program requires a 320 ");
  291.                       printf("x 200 color graphics mode.\n");
  292.                       exit(1);
  293.                       }
  294.                    old_mode = fg_getmode();
  295.  
  296.                                             Chapter 13:  Special Effects   255
  297.  
  298.                    fg_setmode(new_mode);
  299.                    fg_allocate(1);
  300.                    fg_sethpage(1);
  301.  
  302.                    fg_setcolor(9);
  303.                    fg_rect(0,319,0,199);
  304.                    fg_setcolor(15);
  305.                    fg_rect(132,188,50,150);
  306.  
  307.                    fg_setcolor(10);
  308.                    fg_move(160,67);
  309.                    fg_draw(175,107);
  310.                    fg_draw(140,82);
  311.                    fg_draw(180,82);
  312.                    fg_draw(145,107);
  313.                    fg_draw(160,67);
  314.                    fg_paint(160,77);
  315.                    fg_paint(150,87);
  316.                    fg_paint(160,87);
  317.                    fg_paint(170,87);
  318.                    fg_paint(155,97);
  319.                    fg_paint(165,97);
  320.  
  321.                    while (kbhit() == 0) {
  322.                       fg_waitfor(1);
  323.                       fg_scroll(136,184,50,150,-4,0);
  324.                       }
  325.                    fg_waitkey();
  326.  
  327.                    fg_freepage(1);
  328.                    fg_setmode(old_mode);
  329.                    fg_reset();
  330.                 }
  331.  
  332.  
  333. Changing the Screen Origin
  334.  
  335.      Fastgraph includes two routines for changing the screen origin.  By
  336. changing the screen origin, we simply mean defining the (x,y) coordinate of
  337. the upper left corner of the display area.  The fg_pan routine performs this
  338. function in screen space, while the fg_panw routine does in world space.
  339. Neither routine changes the graphics cursor position.
  340.  
  341.      Each of these routines has two arguments that specify the x and y
  342. coordinates of the screen origin.  For the fg_pan routine, the arguments are
  343. integer quantities.  For the fg_panw routine, they are floating point
  344. quantities.
  345.  
  346.      In the EGA, VGA, MCGA, XVGA, and SVGA graphics modes (modes 13 to 29),
  347. you can set the screen origin to any (x,y) coordinate position (that is, to
  348. any pixel).  In the other graphics modes, certain restrictions exist, as
  349. imposed by specific video hardware.  These constraints limit the coordinate
  350. positions that can be used as the screen origin.  Fastgraph compensates for
  351. these restrictions by reducing the specified x and y coordinates to values
  352. that are acceptable to the current video mode, as shown in the following
  353. table.
  354. 256   Fastgraph User's Guide
  355.  
  356.                            x will be reduced   y will be reduced
  357.           video mode       to a multiple of:   to a multiple of:
  358.  
  359.               4-5                  8                   2
  360.                6                  16                   2
  361.                9                   4                   4
  362.               11                   8                   4
  363.               12                   4                2 or 3
  364.  
  365. In modes 4 and 5, for instance, the x coordinate will be reduced to a
  366. multiple of 8 pixels, and the y coordinate will be reduced to a multiple of 2
  367. pixels.  In the Hercules low resolution mode (mode 12), the y coordinate
  368. reduction depends on whether or not the specified pixel row is scan doubled.
  369.  
  370.      Example 13-6 shows a useful effect that can be made with the fg_pan or
  371. fg_panw routines.  This program uses the fg_automode routine to select a
  372. video mode and then draws an unfilled white rectangle.  The top and bottom
  373. sides of the rectangle are intentionally drawn just smaller than the physical
  374. screen size.  After waiting for a keystroke, the program uses a for loop to
  375. make the rectangle jiggle up and down.  The rectangle moves because the
  376. fg_pan routine is called inside the loop to switch the screen origin between
  377. the rectangle's upper left corner and the original origin.  Note also the use
  378. of the fg_waitfor routine to cause slight delays after each call to fg_pan.
  379. If we didn't use fg_waitfor, the changing of the origin would occur so
  380. rapidly we wouldn't notice the effect.  Finally, the program restores the
  381. original video mode and screen attributes before returning to DOS.
  382.  
  383.                                 Example 13-6.
  384.  
  385.                  #include <fastgraf.h>
  386.                  #include <stdio.h>
  387.                  #include <stdlib.h>
  388.                  void main(void);
  389.  
  390.                  #define DELAY 2
  391.                  #define JUMP  4
  392.  
  393.                  void main()
  394.                  {
  395.                     int i;
  396.                     int old_mode;
  397.  
  398.                     old_mode = fg_getmode();
  399.                     fg_setmode(fg_automode());
  400.  
  401.                     fg_setcolor(15);
  402.                     fg_move(0,JUMP);
  403.                     fg_draw(fg_getmaxx(),JUMP);
  404.                     fg_draw(fg_getmaxx(),fg_getmaxy()-JUMP);
  405.                     fg_draw(0,fg_getmaxy()-JUMP);
  406.                     fg_draw(0,JUMP);
  407.                     fg_waitkey();
  408.  
  409.                     for (i = 0; i < 6; i++) {
  410.                        fg_pan(0,JUMP);
  411.                        fg_waitfor(DELAY);
  412.                                             Chapter 13:  Special Effects   257
  413.  
  414.                        fg_pan(0,0);
  415.                        fg_waitfor(DELAY);
  416.                        }
  417.  
  418.                     fg_setmode(old_mode);
  419.                     fg_reset();
  420.                  }
  421.  
  422.  
  423.      The real power of the fg_pan routine becomes clear when it is used with
  424. the fg_resize routine to perform smooth panning.  Recall from Chapter 8 that
  425. fg_resize changes the video page dimensions in native EGA and VGA graphics
  426. modes (modes 13 to 18), the extended VGA graphics modes (20 to 23), and the
  427. SVGA graphics modes (24 to 29).  We'll now present an example that shows how
  428. to use these two routines to perform panning in the low-resolution EGA
  429. graphics mode (mode 13).  The method it uses also would work in any mode that
  430. supports video page resizing.
  431.  
  432.      Example 13-7 begins by establishing the video mode and then immediately
  433. calls fg_resize to increase the video page size to 640 by 400 pixels.  Thus,
  434. the video page is now four times its original size.  Following this, the
  435. program fills the page (the entire page, not just what is displayed) with a
  436. bright green rectangle with a white border around it.  It then displays the
  437. message "Press arrow keys to pan" as close as possible to the center of the
  438. page.
  439.  
  440.      The main part of the program is a loop that accepts keystrokes and then
  441. calls fg_pan to perform the panning one pixel at a time.  When you press any
  442. of the four arrow keys, the program adjusts the x and y coordinates for the
  443. screen origin as directed.  For example, pressing the up arrow key scrolls
  444. the screen upward one pixel.  Note that when we reach the edge of the video
  445. page, the program prevents further scrolling in that direction.  This process
  446. continues until you press the Esc key, at which time the program restores the
  447. original video mode and screen attributes before exiting.
  448.  
  449.                                 Example 13-7.
  450.  
  451.                   #include <fastgraf.h>
  452.                   void main(void);
  453.  
  454.                   void main()
  455.                   {
  456.                      unsigned char key, aux;
  457.                      int old_mode;
  458.                      int x, y;
  459.  
  460.                      old_mode = fg_getmode();
  461.                      fg_setmode(13);
  462.                      fg_resize(640,400);
  463.  
  464.                      fg_setcolor(2);
  465.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  466.                      fg_setcolor(15);
  467.                      fg_box(0,fg_getmaxx(),0,fg_getmaxy());
  468.                      fg_locate(24,28);
  469.                      fg_text("Press arrow keys to pan.",24);
  470. 258   Fastgraph User's Guide
  471.  
  472.                      x = 0;
  473.                      y = 0;
  474.  
  475.                      do {
  476.                         fg_getkey(&key,&aux);
  477.                         if (aux == 72 && y < 200)
  478.                            y++;
  479.                         else if (aux == 75 && x < 320)
  480.                            x++;
  481.                         else if (aux == 77 && x > 0)
  482.                            x--;
  483.                         else if (aux == 80 && y > 0)
  484.                            y--;
  485.                         fg_pan(x,y);
  486.                      } while (key != 27);
  487.  
  488.                      fg_setmode(old_mode);
  489.                      fg_reset();
  490.                   }
  491.  
  492.  
  493.  
  494. Summary of Special Effects Routines
  495.  
  496.      This section summarizes the functional descriptions of the Fastgraph
  497. routines presented in this chapter.  More detailed information about these
  498. routines, including their arguments and return values, may be found in the
  499. Fastgraph Reference Manual.
  500.  
  501.      FG_FADEIN incrementally replaces the visual page contents with the
  502. hidden page contents.  This routine has no effect in text video modes.
  503.  
  504.      FG_FADEOUT incrementally replaces the visual page contents with pixels
  505. of the current color.  This routine has no effect in text video modes.
  506.  
  507.      FG_PAN changes the screen origin (the upper left corner of the screen)
  508. to the specified screen space coordinates.  This routine has no effect in
  509. text video modes.
  510.  
  511.      FG_PANW is the world space version of the fg_pan routine.
  512.  
  513.      FG_RESIZE changes the dimensions of a video page in EGA and VGA graphics
  514. modes.
  515.  
  516.      FG_SCROLL vertically scrolls a region of the active video page.  The
  517. scrolling may be done either up or down, using either an end-off or circular
  518. method.  Circular scrolling uses part of the hidden page as a temporary
  519. workspace.