abrash-black-book/36-04.html
2013-12-30 12:21:49 +11:00

144 lines
4.9 KiB
HTML

<HTML>
<HEAD>
<META name=vsisbn content="1576101746">
<META name=vstitle content="Michael Abrash's Graphics Programming Black Book, Special Edition">
<META name=vsauthor content="Michael Abrash">
<META name=vspublisher content="The Coriolis Group">
<META name=vspubdate content="07/01/97">
<META name=vscategory content="Web and Software Development: Game Development,Web and Software Development: Graphics and Multimedia Development">
<TITLE>Michael Abrash's Graphics Programming Black Book Special Edition: The Good, the Bad, and the Run-Sliced</TITLE>
<!-- HEADER -->
<!-- Empty Reference Subhead -->
<!--ISBN=1576101746//-->
<!--TITLE=Michael Abrash's Graphics Programming Black Book Special Edition//-->
<!--AUTHOR=Michael Abrash//-->
<!--PUBLISHER=The Coriolis Group, Inc.//-->
<!--CHAPTER=36//-->
<!--PAGES=692-693//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="36-03.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="37-01.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Notwithstanding that it&#146;s not optimized, Listing 36.1 is reasonably fast. If you run Listing 36.2 (a sample line-drawing program that you can use to test-drive Listing 36.1), you may be as surprised as I was at how quickly the screen fills with vectors, considering that Listing 36.1 is entirely in C and has some redundant divides. Or perhaps you won&#146;t be surprised&#151;in which case I suggest you <I>not</I> miss the next chapter.</P>
<P><B>LISTING 36.2 L36-2.C</B></P>
<!-- CODE //-->
<PRE>
/* Sample line-drawing program. Uses the optimized
line-drawing functions coded in LListing L36.1.C.
Tested with Borland C&#43;&#43; in the small model. */
#include &ltdos.h&gt
#define GRAPHICS_MODE 0x13
#define TEXT_MODE 0x03
#define BIOS_VIDEO_INT 0x10
#define X_MAX 320 /* working screen width */
#define Y_MAX 200 /* working screen height */
extern void LineDraw(int XStart, int YStart, int XEnd, int YEnd, int Color);
/* Subroutine to draw a rectangle full of vectors, of the specified
* length and color, around the specified rectangle center. */
void VectorsUp(XCenter, YCenter, XLength, YLength, Color)
int XCenter, YCenter; /* center of rectangle to fill */
int XLength, YLength; /* distance from center to edge of rectangle */
int Color; /* color to draw lines in */
{
int WorkingX, WorkingY;
/* lines from center to top of rectangle */
WorkingX = XCenter - XLength;
WorkingY = YCenter - YLength;
for ( ; WorkingX &lt ( XCenter &#43; XLength ); WorkingX&#43;&#43; )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
/* lines from center to right of rectangle */
WorkingX = XCenter &#43; XLength - 1;
WorkingY = YCenter - YLength;
for ( ; WorkingY &lt ( YCenter &#43; YLength ); WorkingY&#43;&#43; )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
/* lines from center to bottom of rectangle */
WorkingX = XCenter &#43; XLength - 1;
WorkingY = YCenter &#43; YLength - 1;
for ( ; WorkingX &gt= ( XCenter - XLength ); WorkingX-- )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
/* lines from center to left of rectangle */
WorkingX = XCenter - XLength;
WorkingY = YCenter &#43; YLength - 1;
for ( ; WorkingY &gt= ( YCenter - YLength ); WorkingY-- )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
}
/* Sample program to draw four rectangles full of lines. */
int main()
{
union REGS regs;
/* Set graphics mode */
regs.x.ax = GRAPHICS_MODE;
int86(BIOS_VIDEO_INT, &ampregs, &ampregs);
/* Draw each of four rectangles full of vectors */
VectorsUp(X_MAX / 4, Y_MAX / 4, X_MAX / 4, Y_MAX / 4, 1);
VectorsUp(X_MAX * 3 / 4, Y_MAX / 4, X_MAX / 4, Y_MAX / 4, 2);
VectorsUp(X_MAX / 4, Y_MAX * 3 / 4, X_MAX / 4, Y_MAX / 4, 3);
VectorsUp(X_MAX * 3 / 4, Y_MAX * 3 / 4, X_MAX / 4, Y_MAX / 4, 4);
/* Wait for a key to be pressed */
getch();
/* Return back to text mode */
regs.x.ax = TEXT_MODE;
int86(BIOS_VIDEO_INT, &ampregs, &ampregs);
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="36-03.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="37-01.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade>
<div align="center">
<font face="Verdana,sans-serif" size="1">Graphics Programming Black Book &copy; 2001 Michael Abrash</font>
</div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- BEGIN SUB FOOTER -->
</BODY>
</HTML>
<!-- END FOOTER -->