home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DDJ0190.ZIP / ZG_LWLVL.DOC < prev    next >
Text File  |  1989-12-26  |  15KB  |  319 lines

  1.              ---------------------------------
  2.              ZipGraph: The Low-Level Routines!
  3.              ---------------------------------
  4.  
  5.                         version 1.20
  6.                       1 November, 1989
  7.  
  8.                               
  9.                         written by:
  10.                         -----------
  11.                      Scott Robert Ladd
  12.                      705 West Virginia
  13.                      Gunnison CO 81230
  14.  
  15.                      BBS (303)641-5125
  16.                      FidoNet 1:104/708
  17.  
  18.                   Legal Disclaimer (Yuck)
  19.                   =======================
  20.  
  21. The software presented in this package is public domain. You
  22. may do anything you please with this software. As such,
  23. there are no warranties or guarantees whatsoever as to the
  24. quality, suitability, or general functionality of this
  25. software. If you use this software, you accept full
  26. responsibility for what it does or doesn't do.
  27.  
  28. Personally, I hate these disclaimers; however, in the
  29. litigious world we live in, you can't even give something
  30. away for free without covering yourself. Frankly, I suspect
  31. most people are generally appreciative that I've made this
  32. stuff available to them.
  33.  
  34. This software was developed using Zortech's C 2.01 on a
  35. 20Mhz 80386-based PC running MS-DOS 3.30. It's also been
  36. tested with Mcirosft C 5.10 and QuickC 2.01, Borland Turbo
  37. C 2.0, and Lattice C 6.01. I suspect other compilers will
  38. work, too, but I haven't had time to try them.
  39.  
  40. One last request: if you do use this module, I'd like to hear
  41. from you. I'm interested in any changes you might make. If
  42. you find any bugs (!), drop me a line, and I'll see what I
  43. can do. Again, since this is free, I can't make any
  44. guarantees about how quickly any bug will get fixed. But I
  45. will do my best!
  46.  
  47.                         Introduction
  48.                         ============
  49.  
  50. This document should be part of an archived package, which
  51. contains a complete C-language module for the low-level portion
  52. of the ZipGraph graphics library for PCs using MS-DOS. Included
  53. in the archive should be:
  54.  
  55.         ZG_LWLVL.H   -- the required header file
  56.         ZG_LWLVL.C   -- C language cource code
  57.         ZGTEST.C     -- a program to test the above
  58.         ZG_LWLVL.DOC -- This document!
  59.  
  60.  
  61. If you do redistribute this code, please do so with all of
  62. the files together in an archive. While I use LHARC to
  63. distribute this package, you can use any archiver you want.
  64. Just keep these three files together, since that's how they
  65. belong.
  66.  
  67.                    General Documentation
  68.                    =====================
  69.  
  70. The following is an excerpt reprint from my Januray-February 1990
  71. Cing Clearly column in Micro Cornucopia Magazine, where this module
  72. first appeared.
  73.  
  74.  
  75.  
  76. The files in this archive are a part of a library called ZipGraph,
  77. which provides several levels of functionality. These are
  78. the low-level routines, which handle basic tasks such as the
  79. determination of the installed adapter type and the plotting
  80. of pixels. Future segments will cover drawing primitives,
  81. clipping, region filling, and other basic tasks. In the
  82. highest level of this library, I will present a series of
  83. C++ classes, built on the lower-level C code, which will
  84. handle advanced routines to handle ray tracing, 3D
  85. modelling, and animation.
  86.  
  87. An obvious question might be: why write a graphics module?
  88. Not only are there dozens of commercial graphics libraries
  89. for C, but almost every compiler vendor now includes their
  90. own graphics routines. What does ZipGraph offer that others
  91. don't?
  92.  
  93. I had several goal in mind when building ZipGraph. To begin
  94. with, I wanted it to be fast. The current version is more
  95. than twice as fast as any other graphics library I have
  96. tried. I was surprised to find that my C-language version of
  97. the low-level graphics routines was nearly as fast as the
  98. one I had built in assembler language. The advantages of
  99. having easily maintainable C source far outweighed the few
  100. percentage points loss in speed when compared to my
  101. assembler language implementation.
  102.  
  103. My second goal was to make ZipGraph portable. As time
  104. passes, more and more of my article involve programs which
  105. do graphics. Alas, C compiler vendors do not have any
  106. interest in making their proprietary graphics libraries
  107. compatible. If I write a program using the Borland Graphic
  108. Interface (BGI) included with Turbo C, that program won't
  109. compile with any other C compiler. Rather than shut people
  110. out, I decided to build a library which compiled with all of
  111. the popular compilers. The version of ZipGraph presented
  112. here compiles with Borland Turbo C 2.0, Lattice C 6.01,
  113. Microsoft C 5.10 and QuickC 2.01, and Zortech C/C++ 2.01. In
  114. addition, the resulting object modules can be linked to
  115. Microsoft Fortran 5.0 and Stony Brook Modula-2 2.01.
  116.  
  117. My final reason for writing ZipGraph is that I find
  118. commercial libraries limited. For example, most do not
  119. include printer routines, and some do not support certain
  120. graphics adapters. On top of that they lack fundamental
  121. capabilities, such as a function to generate non-orthagonal
  122. ellipses. No graphics library I know of completely supports
  123. ray tracing, animation, object rotation, and 3D plotting.
  124. Additionally, commercial libraries are written using C and
  125. assembler language only, without utilizing the object-
  126. oriented features of C++. As you'll see in future columns,
  127. C++ provides the ability to do some fantastic things.
  128.  
  129. The ZG_LWLVL module, presented here, is the basis of all the
  130. other modules in the ZipGraph system. It uses some of the
  131. more powerful features of C, and provides the basic
  132. functions for detecting the type of graphics adapter
  133. installed in your machine, and supports the plotting and
  134. reading pixels on all common IBM adapters. As mentioned
  135. earlier, this module is almost 850 lines long, making it the
  136. longest piece of code ever published in this column.
  137. Subsequently, I'll have to forego a long detailed discussion
  138. of ZG_LWLVL. Instead, I'll be focusing on the techniques
  139. used to make it work.
  140.  
  141. IBM PCs and their compatibles were originally designed to be
  142. modular; in spite of IBM's recent move toward building video
  143. hardware into the computer's motherboard, most vendors have
  144. maintained the ability to install whatever kind of video
  145. adapter you want. Currently, there are six standard video
  146. adapters in common use in PCs: the Monochrome Display
  147. Adapter (MDA), the Color Graphics Adapter (CGA), the
  148. Hercules Graphics Card (HGC), the Enhanced Graphics Adapter
  149. (EGA), the Multi-Colored Graphics Array (MCGA), and the
  150. Video Graphics Array (VGA).
  151.  
  152. One problem PC software has always faced is that any one of
  153. these video adapters can be installed in a PC. While most of
  154. the adapters are supported by the PC's BIOS, the Hercules
  155. card is not. I'm sure most of you have had the experience of
  156. finding a pieces of graphics software which will not run on
  157. your computer.
  158.  
  159. Some graphics libraries allow you to "detect" which kind of
  160. video adapter is installed. Borland's Graphic Interface
  161. (BGI) has this capability. However, the BGI uses external
  162. drivers, which are loaded at run time. While you can convert
  163. the BGI drivers to object modules, you then have to go
  164. through a clumsy procedure to link them in and make the
  165. program aware of their resident status. Other "auto-sensing
  166. packages" fail to support certain adapters, or are just
  167. simply clumsy.
  168.  
  169. I've always been a believer that computers should do work
  170. for you. ZipGraph not only detects the presence of all of
  171. the above adapters, it also automatically sets up its
  172. routines so that you can write one program with which works
  173. with all of those adapters. The type of adapter you're
  174. working with is as transparent as possible.
  175.  
  176. Listing 1 shows ZG_LWLVL.H, the header file which must be
  177. included in any source program which uses the LG_LWLVL
  178. module. Listing 2 is ZG_LWLVL.C, the implementation of the
  179. module. Finally, Listing 3 shows one of my test programs,
  180. ZGTEST.C, which will give you an example of how to use the
  181. ZG_LWLVL module.
  182.  
  183. We'll start with a quick synopsis of how to use the ZG_LWLVL
  184. functions. First, you need to call ZG_Init to initialize
  185. the module. ZG_Init detects the adapter installed in the
  186. PC, and saves its initial status. Information on the adapter
  187. will be returned in the public ZG_VideoInfo structure.
  188. Then you need to set a graphics mode using the
  189. ZG_SetMode function. It sets the video mode you request,
  190. assigns the proper pixel plotting and reading function to
  191. the function pointers ZG_PlotPixel and ZG_ReadPixel,
  192. and returns information on the new graphics mode in
  193. ZG_VideoInfo. You can then plot pixels by calling the
  194. function pointed to by ZG_PlotPixel, a read pixels via
  195. the function pointer ZG_ReadPixel. When your program is
  196. done, it should call ZG_Done to restored the video
  197. adapter to its pre-program state. Examining ZGTEST.C will
  198. show you the details of how this all fits together. No let's
  199. examine the above process in detail.
  200.  
  201. When ZG_Init is called, it attempts to identify the
  202. video adapter installed in your computer. There isn't an
  203. built-in way to determine the adapter type, but we can use
  204. the process of elimination. ZG_Init() begins be calling
  205. an MCGA and VGA BIOS function which returns the adapter
  206. type. If an MCGA or VGA BIOS is installed, this call will
  207. tell us which one of those it is. If the adapter installed
  208. is not a VGA, the call to this BIOS function will fail. Once
  209. we've eliminated the VGA, we call an EGA BIOS function.
  210. Again, if the EGA BIOS is not present, the function will not
  211. return expected values, and we know that an EGA is not
  212. present. If no EGA is found, we ask the BIOS for the
  213. hardware information word. We check the appropriate bits to
  214. see if a color or monochrome adapter is installed. A color
  215. adapter will be a CGA at this point (since we have
  216. eliminated the other color adapters). If a monochrome
  217. adapter is installed, our final task is to differentiate
  218. between an MDA and a HGC. This is done by monitoring the
  219. vertical synch bit of the monochrome card's status register;
  220. if the bit changes, we have a Hercules card.
  221.  
  222. Information on the adapter's type and its installed monitor
  223. are placed into the ZG_VideoInfo structure. Constants
  224. for these values are defined in the ZG_LWLVL.H files can be
  225. used to make you code a bit clearer. At this point, all
  226. ZipGraph has done is determine what kind of video adapter
  227. you have. I've tested the detection routine on several
  228. computers with a variety of adapters installed. So far, it
  229. has worked flawlessly with VGA, EGA, MDA, and Hercules
  230. adapters. I do not have a CGA or MCGA adapter available to
  231. me, but I was able to test those routines on my Paradise 16-
  232. bit VGA card, which emulates the CGA and MCGA. I'd
  233. appreciate hearing from you if you have problems.
  234.  
  235. In order to display graphics, you now need to set a graphics
  236. mode. You do this by calling the ZG_SetMode function,
  237. passing it as its first parameter one of the ZG_MOD
  238. constants defined in ZG_LWLVL.H. ZG_SetMode will return
  239. 1 if the requested mode is not valid for the adapter
  240. detected. Information on the mode will again be returned in
  241. the public global variable ZG_VideoInfo. That
  242. information includes the x and y dimensions of the new
  243. graphics mode, and the number of colors available. If all
  244. goes well, ZG_SetMode will return 0 to indicate that
  245. success.
  246.  
  247. ZG_SetMode also does some automagical work for you. Two
  248. special graphics modes are ZG_MOD_BESTRES and
  249. ZG_MOD_MOSTCOLOR. Respectively, they represent -- for
  250. the detected adapter -- the best possible resolution, and
  251. the resolution providing the greatest selection of colors.
  252. The global static array VideoTable contains the actual
  253. modes for both of these modes, for each adapter. In
  254. addition, VideoTable also contains a bit mask which
  255. indicates the valid modes for a given adapter. The requested
  256. graphics mode is compared against the ModeList value for
  257. a given adapter type, to be sure it is valid. If it isn't,
  258. ZG_SetMode returns an error.
  259.  
  260. Once it has determined the validity of the requested
  261. graphics mode, ZG_SetMode uses the information stored in
  262. the global static array ModeData to do the actual mode
  263. set-up. ModeData contains the equivalent BIOS mode for
  264. the mode requested, the addresses of the appropriate pixel
  265. plotting and reading functions, and the dimensions and color
  266. counts for each mode. When the mode is set, ZG_SetMode
  267. assigns the appropriate values in the ModeData table to
  268. the function pointers ZG_PlotPixel and ZG_ReadPixel.
  269. We are ensured that for each graphics mode, the correct
  270. pixel plotting and reading routines are set -- all
  271. automatically. Once that is done, ZG_SetMode assigns
  272. values from the ModeData table to the width, height, and
  273. color count values of the ZG_VideoInfo structure.
  274.  
  275. It should be noted here that the Hercules card is handled in
  276. a special manner by ZG_LWLVL.C. Since the HGC is not
  277. supported by the PC BIOS in any way, it's graphics and text
  278. modes must be set via special functions. All the ZipGraph
  279. functions do is make exceptions to their normal set-up by
  280. calling the special Hercules functions rather than the BIOS
  281. (as used for the other graphics cards).
  282.  
  283. And we're ready to plot pixels! Different video modes
  284. require different pixel plotting and reading routines, which
  285. is why I use pointers to the functions for these tasks.
  286. ZG_PlotPixel and ZG_ReadPixel can be used exactly as
  287. if they were regular functions. However, they do different
  288. things based on the specific type of graphics adapter
  289. installed. Their value is set based on the graphics mode you
  290. requested via ZG_SetMode. In a way, you can look upon
  291. this as a form of polymorphism (an object-oriented
  292. programming concept) for C.
  293.  
  294. This document has already run far longer than it should, so
  295. I won't go into the specifics of how the individual pixel
  296. plotting and reading functions work. If you're dying to
  297. know, pick up a copy of Richard Wilton's Programmer's
  298. Guide to PC & PS/2 Video Systems (ISBN 1-55615-103-9), by
  299. far the best reference ever published about the nuts and
  300. bolts of PC graphics programming.
  301.  
  302. I doubt ZG_LWLVL will remain static. The most obvious
  303. enhancement is to include support for the "Super VGA modes,
  304. such as 800 x 600 graphics. Additionally, it would be nice
  305. to add the capability to use some of the non-documented VGA
  306. modes, such as 320 by 400 pixels with 256 colors. But all in
  307. good time. I suspect this issue's source code is more than
  308. enough for most of you to "chew" on for a while.
  309.  
  310.                     VERSION HISTORY!
  311.                     ----------------
  312.  
  313. Version 1.00 was built and released quickly because of a magazine
  314. article deadline. Later, as I looked at it, I saw that the performance
  315. could be increased dramatically. So, that's what I did, generating
  316. version 1.10. 1.20 corrected some minor bugs in the handling of
  317. modes which are only rarely used, such as 320 by 200 by 16 color
  318. and EGA monochrome mode.
  319.