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

273 lines
9.3 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: Be It Resolved: 360x480</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=32//-->
<!--PAGES=615-618//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="32-02.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="32-04.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>LISTING 32.2 L32-2.C</B></P>
<!-- CODE //-->
<PRE>
* Sample program to illustrate VGA line drawing in 360&#215;480
* 256-color mode.
*
* Compiled with Borland C/C&#43;&#43;.
*
* Must be linked with Listing 32.1 with a command line like:
*
* bcc l10-2.c l10-1.asm
*
* By Michael Abrash
*/
#include &lt;dos.h&gt; /* contains geninterrupt */
#define TEXT_MODE 0&#215;03
#define BIOS_VIDEO_INT 0&#215;10
#define X_MAX 360 /* working screen width */
#define Y_MAX 480 /* working screen height */
extern void Draw360&#215;480Dot();
extern void Set360&#215;480Mode();
/*
* Draws a line in octant 0 or 3 ( |DeltaX| &gt;= DeltaY ).
* |DeltaX|&#43;1 points are drawn.
*/
void Octant0(X0, Y0, DeltaX, DeltaY, XDirection, Color)
unsigned int X0, Y0; /* coordinates of start of the line */
unsigned int DeltaX, DeltaY; /* length of the line */
int XDirection; /* 1 if line is drawn left to right,
-1 if drawn right to left */
int Color; /* color in which to draw line */
{
int DeltaYx2;
int DeltaYx2MinusDeltaXx2;
int ErrorTerm;
/* Set up initial error term and values used inside drawing loop */
DeltaYx2 = DeltaY * 2;
DeltaYx2MinusDeltaXx2 = DeltaYx2 - (int) ( DeltaX * 2 );
ErrorTerm = DeltaYx2 - (int) DeltaX;
/* Draw the line */
Draw360&#215;480Dot(X0, Y0, Color); /* draw the first pixel */
while ( DeltaX-- ) {
/* See if it&#146;s time to advance the Y coordinate */
if ( ErrorTerm &gt;= 0 ) {
/* Advance the Y coordinate &amp adjust the error term
back down */
Y0&#43;&#43;;
ErrorTerm &#43;= DeltaYx2MinusDeltaXx2;
} else {
/* Add to the error term */
ErrorTerm &#43;= DeltaYx2;
}
X0 &#43;= XDirection; /* advance the X coordinate */
Draw360&#215;480Dot(X0, Y0, Color); /* draw a pixel */
}
}
/*
* Draws a line in octant 1 or 2 ( |DeltaX| &lt; DeltaY ).
* |DeltaY|&#43;1 points are drawn.
*/
void Octant1(X0, Y0, DeltaX, DeltaY, XDirection, Color)
unsigned int X0, Y0; /* coordinates of start of the line */
unsigned int DeltaX, DeltaY; /* length of the line */
int XDirection; /* 1 if line is drawn left to right,
-1 if drawn right to left */
int Color; /* color in which to draw line */
{
int DeltaXx2;
int DeltaXx2MinusDeltaYx2;
int ErrorTerm;
/* Set up initial error term and values used inside drawing loop */
DeltaXx2 = DeltaX * 2;
DeltaXx2MinusDeltaYx2 = DeltaXx2 - (int) ( DeltaY * 2 );
ErrorTerm = DeltaXx2 - (int) DeltaY;
Draw360&#215;480Dot(X0, Y0, Color);/* draw the first pixel */
while ( DeltaY-- ) {
/* See if it&#146;s time to advance the X coordinate */
if ( ErrorTerm &gt;= 0 ) {
/* Advance the X coordinate &amp adjust the error term
back down */
X0 &#43;= XDirection;
ErrorTerm &#43;= DeltaXx2MinusDeltaYx2;
} else {
/* Add to the error term */
ErrorTerm &#43;= DeltaXx2;
}
Y0&#43;&#43;; /* advance the Y coordinate */
Draw360&#215;480Dot(X0, Y0,Color); /* draw a pixel */
}
}
/*
* Draws a line on the EGA or VGA.
*/
void EVGALine(X0, Y0, X1, Y1, Color)
int X0, Y0; /* coordinates of one end of the line */
int X1, Y1; /* coordinates of the other end of the line */
unsigned char Color; /* color in which to draw line */
{
int DeltaX, DeltaY;
int Temp;
/* Save half the line-drawing cases by swapping Y0 with Y1
and X0 with X1 if Y0 is greater than Y1. As a result, DeltaY
is always &gt; 0, and only the octant 0-3 cases need to be
handled. */
if ( Y0 &gt; Y1 ) {
Temp = Y0;
Y0 = Y1;
Y1 = Temp;
Temp = X0;
X0 = X1;
X1 = Temp;
}
/* Handle as four separate cases, for the four octants in which
Y1 is greater than Y0 */
DeltaX = X1 - X0; /* calculate the length of the line
in each coordinate */
DeltaY = Y1 - Y0;
if ( DeltaX &gt; 0 ) {
if ( DeltaX &gt; DeltaY ) {
Octant0(X0, Y0, DeltaX, DeltaY, 1, Color);
} else {
Octant1(X0, Y0, DeltaX, DeltaY, 1, Color);
}
} else {
DeltaX = -DeltaX; /* absolute value of DeltaX */
if ( DeltaX &gt; DeltaY ) {
Octant0(X0, Y0, DeltaX, DeltaY, -1, Color);
} else {
Octant1(X0, Y0, DeltaX, DeltaY, -1, Color);
}
}
}
/*
* Subroutine to draw a rectangle full of vectors, of the
* specified length and in varying colors, around the
* specified rectangle center.
*/
void VectorsUp(XCenter, YCenter, XLength, YLength)
int XCenter, YCenter; /* center of rectangle to fill */
int XLength, YLength; /* distance from center to edge
of rectangle */
{
int WorkingX, WorkingY, Color = 1;
/* Lines from center to top of rectangle */
WorkingX = XCenter - XLength;
WorkingY = YCenter - YLength;
for ( ; WorkingX &lt; ( XCenter &#43; XLength ); WorkingX&#43;&#43; )
EVGALine(XCenter, YCenter, WorkingX, WorkingY, Color&#43;&#43;);
/* Lines from center to right of rectangle */
WorkingX = XCenter &#43; XLength - 1;
WorkingY = YCenter - YLength;
for ( ; WorkingY &lt; ( YCenter &#43; YLength ); WorkingY&#43;&#43; )
EVGALine(XCenter, YCenter, WorkingX, WorkingY, Color&#43;&#43;);
/* Lines from center to bottom of rectangle */
WorkingX = XCenter &#43; XLength - 1;
WorkingY = YCenter &#43; YLength - 1;
for ( ; WorkingX &gt;= ( XCenter - XLength ); WorkingX-- )
EVGALine(XCenter, YCenter, WorkingX, WorkingY, Color&#43;&#43;);
/* Lines from center to left of rectangle */
WorkingX = XCenter - XLength;
WorkingY = YCenter &#43; YLength - 1;
for ( ; WorkingY &gt;= ( YCenter - YLength ); WorkingY-- )
EVGALine(XCenter, YCenter, WorkingX, WorkingY, Color&#43;&#43;);
}
/*
* Sample program to draw four rectangles full of lines.
*/
void main()
{
char temp;
Set360x480Mode();
/* 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 the enter key to be pressed */
scanf(&#147;%c&#148;, &amptemp);
/* Back to text mode */
_AX = TEXT_MODE;
geninterrupt(BIOS_VIDEO_INT);
}
</PRE>
<!-- END CODE //-->
<P>The first thing you&#146;ll notice when you run this code is that the speed of 360&#215;480 256-color mode is pretty good, especially considering that most of the program is im-plemented in C.
</P>
<TABLE WIDTH="100%"><TD VALIGN="TOP" ALIGN="LEFT" WIDTH="5%"><IMG SRC="images/32-01i.jpg"><TD VALIGN="TOP" ALIGN="LEFT" WIDTH="95%"><SMALL><I>Drawing in 360&#215;480 256-color mode can sometimes actually be faster than in the 16-color modes, because the byte-per-pixel display memory organization of 256-color mode eliminates the need to read display memory before writing to it in order to isolate individual pixels coexisting within a single byte. In addition, 360&#215;480 256-color mode is a variant of Mode X, which we&#146;ll encounter in detail in Chapter 47, and supports all the high-performance features of Mode X.</I></SMALL>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="32-02.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="32-04.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 -->