
lafore_robert_objectoriented_programming_in_c
.pdf

Appendix E
886
Listing for MSOFTCON.H
//msoftcon.h
//declarations for Lafore’s console graphics functions //uses Window’s console functions
#ifndef |
_INC_WCONSOLE |
//don’t |
let this file be included |
|||
#define |
_INC_WCONSOLE |
//twice |
in |
the |
same source file |
|
#include <windows.h> |
//for Windows console functions |
|||||
#include |
<conio.h> |
//for kbhit(), |
getche() |
|||
#include |
<math.h> |
//for sin, |
cos |
|
enum fstyle { SOLID_FILL, X_FILL, |
O_FILL, |
|
|
LIGHT_FILL, MEDIUM_FILL, DARK_FILL |
}; |
||
enum color { |
|
|
|
cBLACK=0, |
cDARK_BLUE=1, |
cDARK_GREEN=2, |
cDARK_CYAN=3, |
cDARK_RED=4, |
cDARK_MAGENTA=5, |
cBROWN=6, |
cLIGHT_GRAY=7, |
cDARK_GRAY=8, |
cBLUE=9, |
cGREEN=10, |
cCYAN=11, |
cRED=12, |
cMAGENTA=13, |
cYELLOW=14, |
cWHITE=15 }; |
//-------------------------------------------------------------- |
|
|
|
void init_graphics();
void set_color(color fg, color bg = cBLACK); void set_cursor_pos(int x, int y);
void clear_screen();
void wait(int milliseconds); void clear_line();
void draw_rectangle(int left, int top, int right, int bottom); void draw_circle(int x, int y, int rad);
void draw_line(int x1, int y1, int x2, int y2); void draw_pyramid(int x1, int y1, int height); void set_fill_style(fstyle);
#endif /* _INC_WCONSOLE */
Listing for MSOFTCON.CPP
//msoftcon.cpp
//provides routines to access Windows console functions
//compiler needs to be able to find this file
//in MCV++, /Tools/Options/Directories/Include/type path name
#include “msoftcon.h” |
|
|
HANDLE hConsole; |
//console handle |
|
char fill_char; |
//character used |
for fill |
//-------------------------------------------------------------- |
|
|


888 |
Appendix E |
|
//-------------------------------------------------------------- |
|
|
|
|
|
void wait(int milliseconds) |
|
|
|
||
{ |
|
|
|
|
|
Sleep(milliseconds); |
|
|
|
||
} |
|
|
|
|
|
//-------------------------------------------------------------- |
|
|
|
|
|
void clear_line() |
|
|
//clear to end of line |
||
{ |
|
|
|
//80 spaces |
|
//..... |
1234567890123456789012345678901234567890 |
||||
//..... |
0........ |
1......... |
2 |
.........3......... |
4 |
cputs(“ |
|
|
|
|
“); |
cputs(“ |
|
|
|
|
“); |
} |
|
|
|
|
|
//-------------------------------------------------------------- |
|
|
|
|
|
void draw_rectangle(int left, int top, int right, int bottom)
{
char temp[80];
int width = right - left + 1;
for(int j=0; j<width; j++) |
//string of squares |
temp[j] = fill_char; |
|
temp[j] = 0; |
//null |
for(int y=top; y<=bottom; y++) |
//stack of strings |
{ |
|
set_cursor_pos(left, y); |
|
cputs(temp); |
|
} |
|
} |
|
//-------------------------------------------------------------- |
|
void draw_circle(int xC, int yC, int radius)
{
double theta, increment, xF, pi=3.14159; int x, xN, yN;
increment = 0.8 / static_cast<double>(radius);
for(theta=0; theta<=pi/2; theta+=increment) //quarter circle
{
xF = radius * cos(theta);
xN = static_cast<int>(xF * 2 / 1); //pixels not square
yN = static_cast<int>(radius * sin(theta) + 0.5); |
|
|
x = xC-xN; |
|
|
while(x <= xC+xN) |
//fill two horizontal lines |
|
{ |
//one for each half circle |
|
set_cursor_pos(x, |
yC-yN); putch(fill_char); |
//top |
set_cursor_pos(x++, yC+yN); putch(fill_char); |
//bottom |
|
} |
|
|


890 |
Appendix E |
|
int incr = y - y1; |
|
|
|
for(x=x1-incr; x<=x1+incr; x++) |
|
||
{ |
|
|
|
set_cursor_pos(x, y); |
|
||
putch(fill_char); |
|
||
} |
|
|
|
} |
|
|
|
} |
|
|
|
//-------------------------------------------------------------- |
|
|
|
void set_fill_style(fstyle fs) |
|
||
{ |
|
|
|
switch(fs) |
|
|
|
{ |
|
|
|
case |
SOLID_FILL: |
fill_char = ‘\xDB’; break; |
|
case |
DARK_FILL: |
fill_char = ‘\xB0’; break; |
|
case |
MEDIUM_FILL: |
fill_char = ‘\xB1’; break; |
|
case |
LIGHT_FILL: |
fill_char = ‘\xB2’; |
break; |
case |
X_FILL: |
fill_char = ‘X’; |
break; |
case |
O_FILL: |
fill_char = ‘O’; |
break; |
} |
|
|
|
} |
|
|
|
//-------------------------------------------------------------- |
|
|
|
Listing for BORLACON.H
//borlacon.h
//declarations for Console Graphics Lite functions //uses Borland’s console functions
#ifndef _INC_WCONSOLE //don’t let this file be included #define _INC_WCONSOLE //twice in the same source file
#include <windows.h> |
//for Sleep() |
||
#include |
<conio.h> |
//for |
kbhit(), getche() |
#include |
<math.h> |
//for |
sin, cos |
enum fstyle { SOLID_FILL, X_FILL, O_FILL, |
|
||
LIGHT_FILL, MEDIUM_FILL, DARK_FILL |
}; |
||
enum color { |
|
|
|
cBLACK=0, |
cDARK_BLUE=1, |
cDARK_GREEN=2, |
cDARK_CYAN=3, |
cDARK_RED=4, |
cDARK_MAGENTA=5, |
cBROWN=6, |
cLIGHT_GRAY=7, |
cDARK_GRAY=8, |
cBLUE=9, |
cGREEN=10, |
cCYAN=11, |
cRED=12, |
cMAGENTA=13, |
cYELLOW=14, |
cWHITE=15 }; |
//-------------------------------------------------------------- |
|
|
|
void init_graphics();
void set_color(color fg, color bg = cBLACK); void set_cursor_pos(int x, int y);


892 |
Appendix E |
|
void clear_line() |
|
|
// clear to end of line |
||
{ |
|
|
|
// 80 spaces |
|
// |
.....1234567890123456789012345678901234567890 |
||||
//..... |
0........ |
1......... |
2 |
.........3......... |
4 |
cputs(“ |
|
|
|
“); |
|
cputs(“ |
|
|
|
“); |
|
} |
//end clreol() |
|
|
|
|
//-------------------------------------------------------------- |
|
|
|
|
|
void draw_rectangle(int left, int top, int right, int bottom)
{
int j;
char temp[80];
int width = right - left + 1;
for(j=0; j<width; j++) |
//string of squares |
temp[j] = fill_char; |
|
temp[j] = 0; |
//null |
for(int y=top; y<=bottom; y++) |
//stack of strings |
{ |
|
set_cursor_pos(left, y); |
|
cputs(temp); |
|
} |
|
} //end rectangle |
|
//-------------------------------------------------------------- |
|
void draw_circle(int xC, int yC, int radius)
{
double theta, increment, xF, pi=3.14159; int x, xN, yN;
increment = 0.8 / static_cast<double>(radius);
for(theta=0; theta<=pi/2; theta+=increment) //quarter circle
{ |
|
|
|
xF |
= radius * cos(theta); |
|
|
xN |
= static_cast<int>(xF * 2 / 1); // pixels not square |
||
yN |
= static_cast<int>(radius * sin(theta) + 0.5); |
|
|
x = xC-xN; |
|
|
|
while(x <= xC+xN) |
//fill two horizontal lines |
||
|
{ |
//one for each half circle |
|
|
set_cursor_pos(x, |
yC-yN); putch(fill_char); |
//top |
|
set_cursor_pos(x++, yC+yN); putch(fill_char); |
//bottom |
|
|
} |
|
|
} |
//end for |
|
|
} //end circle() //--------------------------------------------------------------
void draw_line(int x1, int y1, int x2, int y2)


894 |
Appendix E |
|
set_cursor_pos(x, y); |
|
|
putch(fill_char); |
|
|
} |
|
|
} |
|
|
} |
|
|
//-------------------------------------------------------------- |
|
|
void set_fill_style(fstyle fs) |
|
|
{ |
|
|
switch(fs) |
|
|
{ |
|
|
case SOLID_FILL: |
fill_char = ‘\xDB’; break; |
|
case DARK_FILL: |
fill_char = ‘\xB0’; break; |
|
case MEDIUM_FILL: |
fill_char = ‘\xB1’; break; |
|
case LIGHT_FILL: |
fill_char = ‘\xB2’; |
break; |
case X_FILL: |
fill_char = ‘X’; |
break; |
case O_FILL: |
fill_char = ‘O’; |
break; |
} |
|
|
} |
|
|
//-------------------------------------------------------------- |
|
|