PDA

View Full Version : A simple .SBP viewer in C



educoop@edu-coop.org
04-27-2002, 11:59 PM
I started writing a small (50k) viewer for shopbot files this week. The code included below is my current effort.

There is much left to do including the following.

1. implement CG command using BGI arc command

2. write autoscale and autocenter routines

If anyone has any suggestions about how to wirte this, please let me know.

/*start of viewer code */

#include
#include
#include
#include
#include
#include
#include
#include

#define ESC 0x1b
#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0


struct PTS {
int x, y;
};

int GraphDriver;
int GraphMode;
double AspectRatio;
int MaxX, MaxY;
int MaxColors;
int ErrorCode;
struct palettetype palette;


void Initialize(void);
void plot(char *);
void Pause(void);
void MainWindow(char *header);
void DrawBorder(void);


int main(int argc, char* argv[])
{
Initialize();
plot(argv[1]);
closegraph();
return(0);
}


void Initialize(void)
{
int xasp, yasp;

GraphDriver = DETECT;
initgraph( &GraphDriver, &GraphMode, "c:\\tc\\bgi" );
ErrorCode = graphresult();
if( ErrorCode != grOk ){
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
exit( 1 );
}

getpalette( &palette );
MaxColors = getmaxcolor() + 1;

MaxX = getmaxx();
MaxY = getmaxy();

getaspectratio( &xasp, &yasp );
AspectRatio = (double)xasp / (double)yasp;

}


void plot(char* F)
{
MainWindow(F);
char *input;
char *cmd;
int oldx;
int oldy;
int newx;
int newy;
char *x;
char *y;
int s = 6;
int a = 100;
ifstream infile(F);

while (infile)
{
infile >> input;
cmd = strtok(input, ",");


if (cmd[0]=='M')
{
setcolor(2);
x = strtok(NULL, ",");
y = strtok(NULL, ",");
newx = atoi(x)*s+a;
newy = atoi(y)*s+a;
moveto(oldx,MaxY-oldy);
lineto(newx,MaxY-newy);
};

if (cmd[0]=='J')
{
setcolor(0);
x = strtok(NULL, ",");
y = strtok(NULL, ",");
newx = atoi(x)*s+a;
newy = atoi(y)*s+a;
moveto(oldx,MaxY-oldy);
lineto(newx,MaxY-newy);

};

oldx = newx;
oldy = newy;

};
Pause();
};


void Pause(void)
{
int c;

c = getch();

if( ESC == c ){
closegraph();
exit( 1 );
}

if( 0 == c ){
c = getch();
}

cleardevice();

}

void MainWindow( char *header )
{
int height;

cleardevice();
setcolor( MaxColors - 1 );
setviewport( 0, 0, MaxX, MaxY, 1 );

height = textheight( "H" );

settextjustify( CENTER_TEXT, TOP_TEXT );
outtextxy( MaxX/2, 2, header );
setviewport( 0, height+4, MaxX, MaxY-(height+4), 1 );
DrawBorder();
setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );

}


void DrawBorder(void)
{
struct viewporttype vp;

setcolor( MaxColors - 1 );

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );

getviewsettings( &vp );
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );

}


/*end of viewer code */