📄 grdemo.c
字号:
#include <graph.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include "turtle.h"
#include "menu.h"
int main( void );
void Circles( void );
void Sphere( void );
int Polygons( void );
int Spiral( int angle, double inc );
int InSpiral( double side, int angle, int inc );
void Bug( void );
void Adjust( void );
void Diamond( double xy );
/* Returns a random number between min and max, which must be in
* integer range.
*/
#define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
#define PI 3.141593
#define LASTATR 15
#define NLASTATR 14
/* 主菜单 */
ITEM mnuMain[] =
{
{ 0, "Quit" }, /* Q 0 */
{ 0, "Circles" }, /* C 0 */
{ 0, "Rotating Sphere" }, /* R 0 */
{ 0, "Tunnel" }, /* T 0 */
{ 0, "Spiral" }, /* S 0 */
{ 0, "Inverted Spiral" }, /* I 0 */
{ 0, "Bug" }, /* B 0 */
{ 0, "Adjust Window" }, /* A 0 */
{ 0, "Mode Change" }, /* M 0 */
{ 0, "" }
};
enum CHOICES
{
QUIT, CIRCLES, SPHERE, TUNNEL, SPIRAL, INSPIRAL, BUG, ADJUST, CHANGE
};
ITEM mnuModesT[] =
{ /* Highlight Char Pos */
{ 0, "ORESCOLOR " }, /* O 0 */
{ 4, "MRES4COLOR " }, /* 4 4 */
{ 4, "MRESNOCOLOR" }, /* N 4 */
{ 4, "HRESBW" }, /* B 4 */
{ 0, "MRES16COLOR" }, /* M 0 */
{ 0, "HRES16COLOR" }, /* H 0 */
{ 0, "ERESCOLOR" }, /* E 0 */
{ 4, "VRES2COLOR" }, /* 2 4 */
{ 0, "VRES16COLOR" }, /* V 0 */
{ 1, "MRES256COLOR" }, /* R 1 */
{ 6, "ORES256COLOR" }, /* 6 6 */
{ 7, "VRES256COLOR" }, /* C 7 */
{ 0, "" }
};
ITEM *mnuModes = &mnuModesT[1]; /* Default is no Olivetti mode */
//选择颜色
int aModesT[] =
{
_MRES16COLOR,
_HRES16COLOR,
_ERESCOLOR,
_VRES2COLOR,
_VRES16COLOR,
_MRES256COLOR,
_ORES256COLOR,
_VRES256COLOR,
_TEXTMONO,
_ERESNOCOLOR,
_HERCMONO
};
int *aModes = &aModesT[1]; /* Default is no Olivetti mode */
/* Global video configuration */
struct videoconfig vc;
int main()
{
short rowMid, colMid;
short fColor, fFirstTime = TRUE;
short iMode, iLastMode, iMainCur = 0, iModesCur = 0;
_displaycursor( _GCURSOROFF );
_getvideoconfig( &vc );
rowMid = vc.numtextrows / 2;
colMid = vc.numtextcols / 2;
/* Select best graphics mode, adjust menus, set color flag. Note
* that this requires checking both the adapter and the mode.
*/
switch( vc.adapter )
{
case _OCGA:
mnuModes = &mnuModesT[0]; /* Turn on Olivetti mode */
aModes = &aModesT[0];
case _CGA:
mnuModesT[4].achItem[0] = '\0'; /* Turn off EGA modes */
iMode = _MRES4COLOR;
break;
case _HGC:
mnuModesT[7].achItem[0] = '\0';
iMode = _HERCMONO;
break;
case _OEGA:
mnuModes = &mnuModesT[0]; /* Turn on Olivetti mode */
aModes = &aModesT[0];
case _EGA:
mnuModesT[7].achItem[0] = '\0'; /* Turn off VGA modes */
if( vc.memory > 64 )
iMode = _ERESCOLOR;
else
iMode = _HRES16COLOR;
break;
case _OVGA:
mnuModes = &mnuModesT[0]; /* Turn on Olivetti mode */
aModes = &aModesT[0];
case _VGA:
mnuModesT[10].achItem[0] = '\0'; /* Turn off SVGA modes */
iMode = _VRES16COLOR;
break;
case _SVGA:
iMode = _VRES16COLOR;
break;
case _MCGA:
iMode = _MRES256COLOR;
break;
case _MDPA:
default:
puts( "No graphics mode available.\n" );
return TRUE;
}
switch( vc.mode )
{
case _TEXTBW80:
case _TEXTBW40:
fColor = FALSE;
break;
case _TEXTMONO:
case _ERESNOCOLOR:
case _HERCMONO:
fColor = FALSE;
if( iMode != _HERCMONO )
iMode = _ERESNOCOLOR;
mnuMain[8].achItem[0] = '\0'; /* Turn off mode change */
break;
default:
fColor = TRUE;
break;
}
/* Find current mode in mode array. */
for( iModesCur = 0; aModes[iModesCur] != iMode; iModesCur++ )
;
iLastMode = iMode;
/* Seed randomizer with time. */
srand( (unsigned)time( NULL ) );
while( TRUE )
{
/* Set text mode and optionally clear the screen to blue. */
if( iMainCur != CHANGE )
_setvideomode(_DEFAULTMODE );
if( fColor )
_setbkcolor( (long)_TBLUE );
_clearscreen( _GCLEARSCREEN );
/* Select from menu. */
iMainCur = Menu( rowMid, colMid, mnuMain, iMainCur );
/* Set graphics mode and initialize turtle graphics. Put border
* on window.
*/
if( iMainCur != CHANGE )
{
if( !_setvideomode( iMode ) )
{
_settextposition( 1, 22 );
_outtext( "Mode not recognized" );
iMode = iLastMode;
continue;
}
iLastMode = iMode;
_displaycursor( _GCURSOROFF );
_getvideoconfig( &vc );
InitTurtle( &vc );
Rectangle( 2 * tc.xMax, 2 * tc.yMax );
/* After drawing border, reset drawing area inside border. */
tc.xsLeft++; tc.xsRight--; tc.ysTop++; tc.ysBot--;
Home();
}
/* Branch to menu choice. */
switch( iMainCur )
{
case QUIT:
_setvideomode( _DEFAULTMODE );
return FALSE;
case CIRCLES:
Circles();
break;
case SPHERE:
Sphere();
break;
case TUNNEL:
PenDown( FALSE );
MoveTo( -tc.xMax * .2, tc.yMax * .15 );
PenDown( TRUE );
Polygons();
//++++++++++
while( !ClickOrPress() )
NextColorValue( DEFAULT ); /* Rotate palette */
break;
//+++++++++++
case SPIRAL:
if( Spiral( getrandom( 30, 80 ), (double)getrandom( 1, 5 ) ) )
break;
//++++++++++
while( !ClickOrPress() )
NextColorValue( DEFAULT );
//++++++++++
break;
case INSPIRAL:
NextColorIndex( 0 );
if( InSpiral( (double)getrandom( 8, 20 ),
getrandom( 4, 22 ),
getrandom( 3, 31 ) ) )
break;
//++++++++++++++
while( !ClickOrPress() )
NextColorValue( DEFAULT );
//++++++++++++++
break;
case BUG:
Bug();
break;
case ADJUST:
Adjust();
continue;
case CHANGE:
_clearscreen( _GCLEARSCREEN );
iModesCur = Menu( rowMid, colMid, mnuModes, iModesCur );
iMode = aModes[iModesCur];
break;
}
}
}
void Circles()
{
double x, y, xyRadius;
int fFill, fPenDown;
/* Initialize and save pen and fill flags. */
if( tc.cci <= 4 )
fFill = SetFill( FALSE );
else
fFill = SetFill( TRUE );
fPenDown = PenDown( FALSE );
while( TRUE )
{
/* Draw circles. */
for( xyRadius = 10.0; xyRadius <= 130.0; xyRadius++ )
{
x = (tc.xMax - 30) * atan( sin( xyRadius / PI ) );
y = (tc.yMax - 30) * atan( cos( xyRadius / PI ) );
MoveTo( x, y );
PenColor( NextColorIndex( DEFAULT ) );
Circle( xyRadius );
if( ClickOrPress() )
{
PenDown( fPenDown );
SetFill( fFill );
return;
}
}
/* For palette modes (except 256 color), start over. */
if( tc.ccv == 64 || tc.ccv == 16 )
{
_clearscreen( _GCLEARSCREEN );
SetFill( FALSE );
MoveTo( 0.0, 0.0 );
PenColor( WHITE );
Rectangle( 2 * tc.xMax, 2 * tc.yMax );
SetFill( fFill );
NextColorValue( DEFAULT );
}
}
}
/* Sphere - Draw and fill slices of a sphere. Rotate colors in EGA+ modes
* with more than 4 color indexes.
*
* Params: None
*
* Return: None
*
* Uses: tc
*/
void Sphere()
{
double xCur, xSize, ySize, xInc;
short ciBorder, fFill;
ySize = xSize = tc.yMax * 0.9 * 2;
fFill = SetFill( FALSE );
NextColorIndex( 0 );
xInc = xSize / 14;
ciBorder = PenColor( DEFAULT );
BorderColor( ciBorder );
/* Draw slices. */
for( xCur = xInc; xCur <= xSize; xCur += xInc * 2 )
Ellipse( xCur, ySize );
SetFill( TRUE );
PenDown( FALSE );
Turn( 90 );
xSize /= 2;
MoveTo( xSize - xInc, 0.0 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -