abrash-black-book/36-04.md
2013-12-30 17:51:31 +11:00

95 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

------------------------ --------------------------------- --------------------
[Previous](36-03.html) [Table of Contents](index.html) [Next](37-01.html)
------------------------ --------------------------------- --------------------
Notwithstanding that it’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’t be
surprised—in which case I suggest you *not* miss the next chapter.
**LISTING 36.2 L36-2.C**
/* Sample line-drawing program. Uses the optimized
line-drawing functions coded in LListing L36.1.C.
Tested with Borland C++ in the small model. */
#include <dos.h>
#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 < ( XCenter + XLength ); WorkingX++ )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
/* lines from center to right of rectangle */
WorkingX = XCenter + XLength - 1;
WorkingY = YCenter - YLength;
for ( ; WorkingY < ( YCenter + YLength ); WorkingY++ )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
/* lines from center to bottom of rectangle */
WorkingX = XCenter + XLength - 1;
WorkingY = YCenter + YLength - 1;
for ( ; WorkingX >= ( XCenter - XLength ); WorkingX-- )
{
LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
}
/* lines from center to left of rectangle */
WorkingX = XCenter - XLength;
WorkingY = YCenter + YLength - 1;
for ( ; WorkingY >= ( 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, &regs, &regs);
/* 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, &regs, &regs);
}
------------------------ --------------------------------- --------------------
[Previous](36-03.html) [Table of Contents](index.html) [Next](37-01.html)
------------------------ --------------------------------- --------------------
* * * * *
Graphics Programming Black Book © 2001 Michael Abrash