home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / MS_ARCAD.ZIP / ARC6_3.CHP < prev    next >
Text File  |  1993-06-15  |  10KB  |  296 lines

  1. %
  2. #EF
  3. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 3
  4. #HS,1,4,80,25,11,1
  5. #C4,R5
  6.  
  7.                          ~W~IInitializing The BGI~Y~I
  8.  
  9. Setting up the BGI is basically accomplished by using the BGI ~W~Iinitgraph()~Y~I
  10. function. Custom initializations can be done by also using ~W~Idetectgraph()~Y~I,
  11. ~W~Iregisterbgidriver()~Y~I, and ~W~Iinstalluserdriver()~Y~I.
  12.  
  13.  
  14. #WN
  15.                                ~W~Iinitgraph()~Y~I
  16.  
  17. This function sets up the BGI and loads a graphics driver into memory. A
  18. graphics driver must be loaded before the BGI can be used. There are
  19. actually two ways provided by the BGI to link graphics drivers to your
  20. program. The first is to let ~W~Iinitgraph() ~Y~Iload the appropriate driver
  21. whenever the program runs. If we choose this option, then the correct
  22. driver must be available for the program to load. That usually means
  23. distributing the BGI drivers on our program diskette and copying them onto
  24. the user's hard disk.
  25.  
  26. #WN
  27. %
  28. #EF
  29. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 4
  30. #HS,1,4,80,25,11,1
  31. #C4,R5
  32. ~Y~I
  33. This approach saves memory because only the appropriate driver is actually
  34. loaded into memory. However, it can be the cause of some potential problems
  35. if the drivers are not readily available. The user can easily delete the
  36. driver, then call your telephone support line (if you have one) in great
  37. frustration because your program won't run.
  38.  
  39. #WN
  40. The alternative is to convert the driver to an object file, link it to
  41. your program, and use ~W~Iregisterbgidriver()~Y~I, which is discussed later, to
  42. access it.
  43.  
  44. #WN
  45. The ~W~Iinitgraph() ~Y~Ifunction takes three parameters. The first is a pointer to
  46. an |int| that indicates that driver to be used. If the first parameter is
  47. set to ~W~IDETECT~Y~I, then ~W~Iinitgraph()~Y~I will determine what video adaptor is
  48. currently installed in the system, load the driver, and select the highest
  49. video mode that the adaptor supports. ~W~IDETECT~Y~I is one of a group of constants
  50. is provided in ~G~IGRAPHICS.H~Y~I that can be used to set the driver.
  51.  
  52. #WP
  53. %
  54. #EF
  55. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 5
  56. #HS,1,4,80,25,11,1
  57. #C4,R5
  58. ~Y~I
  59. If the first parameter is anything other than ~W~IDETECT~Y~I, ~W~Iinitgraph()
  60. ~Y~Iwill attempt to load the |appropriate driver|. If it can't, an error message
  61. will be written to the screen. If DETECT is used, initgraph() will store in
  62. the first parameter the number corresponding to the driver that it loaded.
  63. That number can then be tested to see which driver the program is using.
  64.  
  65. #WN
  66. #C4,R12
  67. The second parameter to ~W~Iinitgraph()~Y~I is a pointer to an int that tells the
  68. BGI what |video mode| to use. If ~W~IDETECT ~Y~Iwas used for the first parameter,
  69. then the mode that the BGI selected will be stored in this parameter. If
  70. anything other than ~W~IDETECT ~Y~Iwas used for the first parameter, then the
  71. second parameter must be set to a value that corresponds to the video mode
  72. that the adaptor ~W~Iinitgraph()~Y~I selected. The available modes are listed as
  73. named constants in ~G~IGRAPHICS.H~Y~I. They are also listed in the Turbo C library
  74. reference under the section on ~W~Iinitgraph()~Y~I.
  75.  
  76. #WN
  77. #C4,R21
  78. The last parameter to ~W~Iinitgraph()~Y~I is the |path| of the BGI driver. If a null
  79. string is passed, then ~W~Iinitgraph()~Y~I will search for the driver in the
  80. current directory. ~SIf it is not found, the BGI will fail to initialize.~s
  81.  
  82. #WP
  83. %
  84. #EF
  85. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 6
  86. #HS,1,4,80,25,11,1
  87. #C4,R5
  88. ~Y~I
  89.                           ~W~Idetectgraph()~Y~I
  90.  
  91. This function is useful when you want to set the graphics system into
  92. something other than the BGI default. Normally the BGI puts the graphics
  93. adaptor into the highest mode that it supports. If for some reason we want
  94. to select another mode, we can use ~W~Idetectgraph()~Y~I to help us.
  95.  
  96. #WN
  97. Figure 6.1 shows come code that might be used to set the graphics mode
  98. based on which adaptor is being used. In this example, the video subsystem
  99. is set to the lowest resolution mode if the adaptor is an EGA or VGA. It
  100. is set to the highest resolution on a CGA. You might ask yourself why in
  101. the world anyone would want to do that. Go ahead, ask yourself. I'll wait.
  102.  
  103.  
  104. #WP
  105. %
  106. #EF
  107. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 7
  108. #HS,1,4,80,25,11,1
  109. #C2,R5
  110. The answer is that ~C~Ithe lower the resolution, the more colors can be used~Y~I.
  111. Having lots of pixels means using lots of memory. So does having lots of
  112. colors. On PC adaptors, only a limited amount of memory is available, so we
  113. can either choose lots of pixels or lots of colors, but not both. Therefore,
  114. this example assumes that the game uses lots of colors in EGA and VGA, but
  115. that the low resolution mode of CGA is just too low resolution. In that case
  116. color is sacrificed for resolution.
  117.  
  118. #WP
  119. %
  120. #EF
  121. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 8
  122. #HS,1,4,80,25,11,1
  123. #C2,R5
  124.                               ~W~IFigure 6.1~Y~I
  125.                           Using detectgraph()
  126.  
  127.  #include <graphics.h>
  128.  
  129.  main()
  130.  {   /* Start of main() */
  131.      /* Program code goes here. */
  132.  
  133.      /*Now it's time to initialize the BGI. */
  134.      set_screen();
  135.  
  136.      /* More program code. */
  137.  }   /* End of main() */
  138.  
  139. #C28,R23
  140. ~C~IContinued On Next Page~Y~I
  141.  
  142. #WN
  143. #EL,15
  144. #C2,R15
  145. ~W~I~F     set_screen();~Y~I~N
  146. #BO,4,17,78,21,7,1,0,3,15,2
  147. We're ignoring most of the program. In this example, all we care about
  148. is the initialization of the screen. This is done by the set_screen()
  149. function which we will write. It's shown on the next page.
  150.  
  151. #WP
  152. %
  153. #EF
  154. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 9
  155. #HS,1,4,80,25,11,1
  156. #C2,R5
  157.                            ~W~IFigure 6.1 (cont)~Y~I
  158.                           Using detectgraph()
  159.  
  160.  void set_screen(void)
  161.  {
  162.      int driver,mode;
  163.      ~W~Idriver=DETECT;
  164.      detectgraph(&driver,&mode);~Y~I
  165.      switch(driver)
  166.      {
  167.          case CGA:
  168.              mode=CGAHI;
  169.  
  170.              initgraph(&driver,&mode,"");
  171.              break;
  172.  
  173. #C28,R23
  174. ~C~IContinued On Next Page~Y~I
  175.  
  176.  
  177. #D1
  178. #BO,4,17,78,20,7,1,0,2,15,2
  179. We start by using detectgraph() to see what kind of video adaptor is
  180. installed.
  181.  
  182. #ES,1,12,80,13
  183. #C2,R11
  184. ~Y~I     driver=DETECT;
  185.      detectgraph(&driver,&mode);
  186.  
  187. #ES,1,17,80,22
  188. #C2,R15
  189. ~W~I         case CGA:
  190.              mode=CGAHI;
  191.  
  192.              initgraph(&driver,&mode,"");
  193.              break;~Y~I
  194.  
  195. #BO,4,9,78,13,7,1,0,3,15,2
  196. Here is the case that handles CGA monitors. Notice that the path (the
  197. third parameter to the initgraph() function) is just an empty string.
  198. For this to work, the BGI drivers must be in the current directory.
  199.  
  200. #BO,4,7,78,13,7,1,0,5,15,2
  201. Since we wanted the CGA in its highest mode, we could just set the
  202. variable "driver" to DETECT, then call initgraph(). It would
  203. automatically set the screen to the highest mode possible. However,
  204. since we wanted to do something different for EGA and VGA monitors,
  205. we need to use both detectgraph() and initgraph().
  206.  
  207. #WP
  208. %
  209. #EF
  210. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 10
  211. #HS,1,4,80,25,11,1
  212. #C2,R5
  213.                            ~W~IFigure 6.1 (cont)~Y~I
  214.                           Using detectgraph()
  215.  
  216.          case EGA:
  217.          case VGA:
  218.              /* EGALO and VGALO both equal 0. */
  219.              mode=0;
  220.  
  221.              /* The assumption is that the BGI drivers */
  222.              /* are in the current directory. */
  223.              initgraph(&driver,&mode,"");
  224.              break;
  225.  
  226.          default:
  227.              printf("Hold it! I don't support your screen.\n");
  228.              break;
  229.      }
  230.  }
  231.  
  232. #D1
  233. #C26,R15
  234. ~W~I~Fdriver~Y~I~N
  235. #BO,4,17,78,21,7,1,0,3,15,2
  236. The variable "driver" received a value during the call to the
  237. detectgraph() function back on page 9. So we don't need to initialize
  238. it.
  239.  
  240. #C26,R15
  241. ~Y~I~Ndriver
  242. #C15,R11
  243. ~W~I~Fmode~Y~I~N
  244. #BO,4,17,78,22,7,1,0,4,15,2
  245. The variable "mode" also received a value when we called detectgraph()
  246. on page 8. However, we want to override that value, so we store a 0
  247. in it. We could use the named constants EGALO, or VGALO. They are
  248. defined in GRAPHICS.H. They both equal 0.
  249. #WP
  250. %
  251. #EF
  252. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 11
  253. #HS,1,4,80,25,11,1
  254. #C4,R5
  255.                          ~W~Iregisterbgidriver()~Y~I
  256.  
  257. If you want all of the drivers that your program supports to be part of
  258. the program itself, you can convert the bgi driver to an object file with
  259. ~G~IBINOBJ.EXE~Y~I. This is a tool that comes with Turbo C.
  260.  
  261. #WN
  262. Once the driver is in object format, it can be linked into your program
  263. just like any other object file. Simply insert the name of the driver,
  264. which should now have a .OBJ extension on the file name, into your project
  265. file. When your program is compiled and linked, any driver listed in your
  266. project file will be linked in.
  267.  
  268. #WN
  269. However, the BGI doesn't know the drivers are linked in unless you tell it.
  270. The function ~W~Iregisterbgidriver() ~Y~Ipasses this information to the BGI. Before
  271. ~W~Iinitgraph() ~Y~Iis called, ~W~Iregisterbgidriver() ~Y~Imust be called once for
  272. each driver that you linked into your program. A constant is passed to this
  273. function that tells the BGI which driver to register. The constants are
  274. defined in GRAPHICS.H.
  275.  
  276. #WP
  277. %
  278. #EF
  279. #T15,1,Chapter 6     The Borland Graphics Interface     Pg. 12
  280. #HS,1,4,80,25,11,1
  281. #C4,R5
  282. ~Y~I
  283. If, for example, you had linked the CGA, EGA and VGA drivers into your
  284. program and wanted to register them, you would make the following two calls.
  285. ~W~I
  286. registerbgidriver(CGA_driver);
  287. registerbgidriver(EGAVGA_driver);
  288. ~Y~I
  289.  
  290. #WN
  291. The ~W~Iinitgraph() ~Y~Ifunction could then be called to start the graphics system
  292. with whichever of these drivers was appropriate.
  293.  
  294. #WP
  295. #X
  296.