⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 microcad.c

📁 C++游戏开发书籍的实例非常适合初学但又又想往游戏开发方面发展的人学习哦
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <stdio.h>

#define	VERTICAL		480		
#define	HORIZONTAL		640		

#define	ARC_RES			64		

#define	SET_PIXEL		0x0F	
#define	CLEAR_PIXEL		0x00	
#define	FLIP_PIXEL		0x8F	

#define	MOUSE_LEFT		0x01	
#define	MOUSE_RIGHT		0x02	
#define	MOUSE_CENTER	0x04	

#define	LINE			0x01	
#define	BOX				0x02	
#define	CIRCLE			0x03	
#define	TEXT			0x04	
#define	ARC				0x05	
#define	GROUP			0x06	
#define	ACOPY			0x07	
#define	RCOPY			0x08	

unsigned char drawing[32000];	
unsigned dtop = 0, dpos;		


#define	PBYTES	11				
int grid = 0,					
	snap = 0,					
	tscale = 100,				
	xbase = 0,					
	ybase = 0;					
char tick = 1;					

char pixel = SET_PIXEL,			
	cursor_flag = 0,			
	edit = -1,					
	font[4608],					
	dfile[65],					
	ffile[65],					
	ifile[65],					
	vmode;						

unsigned mousex = -1, mousey = -1;	


unsigned sine[] = {
	    0,  1608,  3216,  4821,  6424,  8022,  9616, 11204,
	12785, 14359, 15924, 17479, 19024, 20557, 22078, 23586,
	25079, 26557, 28020, 29465, 30893, 32302, 33692, 35062,
	36410, 37736, 39040, 40320, 41575, 42806, 44011, 45190,
	46341, 47464, 48559, 49624, 50660, 51665, 52639, 53581,
	54491, 55368, 56212, 57022, 57797, 58538, 59243, 59913,
	60547, 61144, 61705, 62228, 62714, 63162, 63571, 63943,
	64276, 64571, 64826, 65043, 65220, 65358, 65457, 65516 };

extern FILE *get_file();	

main(argc, argv)
	int argc;
	char *argv[];
{
	int i, j, select;
	char *ptr;
	FILE *fp;

	concat(dfile, "MICROCAD.DWG");
	concat(ffile, "MICROCAD.FNT");
	*ifile = 0;

	for(i=1; i < argc; ++i) {
		ptr = argv[i];
		switch((toupper(*ptr++) << 8) | toupper(*ptr++)) {
			case 'F=' :		
				concat(ffile, ptr, ".FNT");
				break;
			case '/D' :		
			case '-D' :
				edit = 0;
				break;
			case '?' << 8:	
			case '/?' :
			case '-?' :
				abort("\nUse: MICROCAD [drawing file] [F=font file] [/Display]\n\nCopyright 1992-1993 Dave Dunfield\nAll rights reserved.\n");
			default:		/* Assume drawing filename */
				concat(dfile, argv[i], ".DWG"); } }


	fp = fopen(ffile, "qrbv");
	fread(font, sizeof(font), 1,fp);
	fclose(fp);

	if(fp = fopen(dfile, "rbv")) {
		fread(&grid, PBYTES,1, fp);
		dtop = fread(drawing, sizeof(drawing),1, fp);
		fclose(fp); }

	zero_drawing(dtop);

	if(!init_video())
		abort("VGA required!");

	if(!edit) {
		grid = 0;
		tick = 1;
		redraw();
		get_key();
		video_mode(vmode);
		return; }

	if(!init_mouse())
		abort("MOUSE required!");

	redraw();

	if(!fp) {
		message();
		printf("New drawing: '%s'", dfile); }

	for(;;) {
		if((i=mouse_status()) & MOUSE_LEFT) {	
			message();
			printf("A)rc B)ox C)ircle D)up E)rase F)unc L)ine M)ove R)edraw S)etup T)ext U)ndo");
			select = get_key(); }
		else if(j = test_key())
			select = j;
		else if(!(i & MOUSE_RIGHT))				
			continue;
		switch(toupper(select)) {
			case 'A' :		
				draw_arc();
				break;
			case 'B' :		
				draw_box();
				break;
			case 'C' :		
				draw_circle();
				break;
			case 'D' :		
				copy();
				break;
			case 'E' :		
				erase();
				break;
			case 'F' :		
				function();
				break;
			case 'L' :		
				draw_line();
				break;
			case 'M' :		
				move();
				break;
			case 'R' :		
				redraw();
				break;
			case 'T' :		
				draw_text();
				break;
			case 'U' :		
				undo();
				break;
			case 'S' :		
				message();
				printf("B)ase-markers C)ursor-base G)rid S)nap T)ext-scale");
				switch(toupper(get_key())) {
					case 'B' :		
						tick = tick ? 0 : 1;
						redraw();
						continue;
					case 'C' :		
						i = xbase;
						j = ybase;
						xbase = ybase = 0;
						if(!wait_for_left("Select cursor base point")) {
							xbase = mousex;
							ybase = mousey;
							goto exit; }
						xbase = i;
						ybase = j;
						continue;
					case 'G' :		
						grid = get_value("Grid spacing (0 to remove)?");
						redraw();
						continue;
					case 'S' :		
						snap = get_value("Snap spacing (0 to remove)?");
						goto exit;
					case 'T' :		
						tscale = get_value("Text scale (100 = 1:1)?");
					exit:
						message();
						continue; }
			default:
				message(); 
				printf("Unknown command!"); } }
}


function()
{
	FILE *fp;
	
	message();
	printf("F)ont I)nsert L)oad Q)uit S)ave");
	switch(toupper(get_key())) {
		case 'I' :		
			insert();
			return;
		case 'F' :		
			if(fp = get_file("font", ffile, ".FNT", "rbv")) {
				fread(font, sizeof(font), 1,fp);
				fclose(fp);
				redraw(); }
			return;
		case 'L' :		
			if(fp = get_file("drawing", dfile, ".DWG", "rbv")) {
				fread(&grid, PBYTES,1, fp);
				zero_drawing(fread(drawing, sizeof(drawing),1, fp));
				fclose(fp);
				redraw(); }
			return;
		case 'S' :		
			if(fp = get_file("drawing", dfile, ".DWG", "wbv")) {
				fwrite(&grid, PBYTES, 1,fp);
				if(dtop)
					fwrite(drawing, dtop,1, fp);
				fclose(fp);
				message(); }
			return;
		case 'Q' :		
			video_mode(vmode);
			exit(0); }

		message();
		printf("Unknown command!");
}


insert()
{
	unsigned i, dsave, base, size, minx, miny, x, y;
	char buffer[PBYTES];
	FILE *fp;

	if(!(fp = get_file("drawing", ifile, ".DWG", "rbv")))
		return;

	dsave = dtop;
	drawing[dtop++] = GROUP;
	draw(0);	
	draw(0);	
	draw(0);	

	fread(buffer, PBYTES,1, fp);
	size = fread(&drawing[base = dpos = dtop], sizeof(drawing) - dtop,1, fp);
	fclose(fp);

	minx = miny = 32767;
	while(drawing[dpos]) {
		i = dpos++;
		minx = min(x = dvalue(), minx);
		miny = min(y = dvalue(), miny);
		dpos = i;
		skip_object(); }
	
	dpos = base;
	while(x = drawing[i = dpos]) {
		dtop = ++dpos;
		draw(dvalue() - minx);
		draw(dvalue() - miny);
		
		if(x == ACOPY) {
			drawing[i] = RCOPY;
			draw((dvalue()+base) - i); }
		dpos = i;
		skip_object(); }

	dtop = dsave + 1;
	draw(0);
	draw(0);
	draw(size);

	message();
	printf("INSERT: Selection insertion point");
	pixel = FLIP_PIXEL;
newbox:
	dpos = dsave;
	draw_object(x = mousex, y=mousey);
	while(!((i = mouse_status()) & MOUSE_LEFT)) {
		if(i & MOUSE_RIGHT) {
			dpos = dsave;
			draw_object(x, y);
			zero_drawing(dsave);
			message();
			return; }
		if((x != mousex) || (y != mousey)) {
			dpos = dsave;
			draw_object(x, y);
			goto newbox; } }

	dpos = dsave;
	pixel = SET_PIXEL;
	dtop = dsave + 1;
	draw(x);
	draw(y);
	dtop = base + size;
	redraw();
}


draw_line()
{
	int x, y, x1, y1, s;
	if(wait_for_left("LINE: From point?"))
		return;
	gotoxy(30, 0); printf(" To point?");
	x = mousex;
	y = mousey;
again:
	pixel = FLIP_PIXEL;
newline:
	line(x, y, x1 = mousex, y1 = mousey);
	while(!((s = mouse_status()) & MOUSE_LEFT)) {
		if(s & MOUSE_RIGHT) {
			line(x, y, x1, y1);
			pixel = SET_PIXEL;
			message();
			return; }
		if((mousex != x1) || (mousey != y1)) {
			line(x, y, x1, y1);
			goto newline; } }
	pixel = SET_PIXEL;
	drawing[dpos = dtop++] = LINE;
	draw(x);
	draw(y);
	draw(x1 - x);
	draw(y1 - y);
	draw_object(0, 0);
	x = x1;
	y = y1;
	goto again;
}


draw_box()
{
	int x, y, x1, y1, s;
	if(wait_for_left("BOX: First corner?"))
		return;
	gotoxy(30, 0); printf(" Second corner?");
	x = mousex;
	y = mousey;
	pixel = FLIP_PIXEL;
newbox:
	box(x, y, x1 = mousex, y1 = mousey);
	while(!((s = mouse_status()) & MOUSE_LEFT)) {
		if(s & MOUSE_RIGHT) {
			box(x, y, x1, y1);
			message();
			return; }
		if((mousex != x1) || (mousey != y1)) {
			box(x, y, x1, y1);
			goto newbox; } }
	pixel = SET_PIXEL;
	message();
	drawing[dpos = dtop++] = BOX;
	draw(x);
	draw(y);
	draw(x1 - x);
	draw(y1 - y);
	draw_object(0, 0);
}

draw_circle()
{
	int x, y, r, r1, a, b, s;
	if(wait_for_left("CIRCLE: Select center point"))
		return;
	x = mousex;
	y = mousey;
	pixel = FLIP_PIXEL;
	r = 1;
newcircle:
	circle(x, y, r);
	while(!((s = mouse_status()) & MOUSE_LEFT)) {
		if(s & MOUSE_RIGHT) {
			circle(x, y, r);
			message();
			return; }
		a = abs(mousex - x);
		b = abs(mousey - y);
		if((r1 = sqrt((a*a) + (b*b))) != r) {
			gotoxy(40, 0); printf("Radius=%-3u", r1);
			circle(x, y, r);
			r = r1;
			goto newcircle; } }
	pixel = SET_PIXEL;
	message();
	drawing[dpos = dtop++] = CIRCLE;
	draw(x);
	draw(y);
	draw(r);
	draw_object(0, 0);
}


draw_arc()
{
	int x, y, r, v1, v2, x1, y1, s;

	if(wait_for_left("ARC: Select center point"))
		return;
	x = mousex;
	y = mousey;
	if(wait_for_left("ARC: Select starting point"))
		return;
	x1 = abs(mousex - x);
	y1 = abs(mousey - y);
	r = sqrt((x1*x1) + (y1*y1));
	v1 = find_vector(x, y, r, mousex, mousey);
	message();
	printf("ARC: Select ending point");
	pixel = FLIP_PIXEL;
newarc:
	v2 = find_vector(x, y, r, x1=mousex, y1=mousey);
	arc(x, y, r, v1, v2);
	while(!((s = mouse_status()) & MOUSE_LEFT)) {
		if(s & MOUSE_RIGHT) {
			arc(x, y, r, v1, v2);
			message();
			return; }
		if((mousex != x1) || (mousey != y1)) {
			arc(x, y, r, v1, v2);
			goto newarc; } }
	pixel = SET_PIXEL;
	message();
	drawing[dpos = dtop++] = ARC;
	draw(x);
	draw(y);
	draw(r);
	draw(v1);
	draw(v2);
	draw_object(0, 0);
}


draw_text()
{
	unsigned i, x, y;
	char buffer[80], *ptr;

	message();
	printf("String?");
	fgets(ptr = buffer, sizeof(buffer)-1, stdin);
	message();
	if(*buffer) {
		printf("TEXT: Select position");
		pixel = FLIP_PIXEL;
	newtext:
		text(buffer, x = mousex, y = mousey, tscale);
		while(!((i = mouse_status()) & MOUSE_LEFT)) {
			if(i & MOUSE_RIGHT) {					
				text(buffer, x, y, tscale);
				pixel = SET_PIXEL;
				message();
				return; }
			if((x != mousex) || (y != mousey)) {	
				text(buffer, x, y, tscale);
				goto newtext; } }

		pixel = SET_PIXEL;
		message();
		drawing[dpos = dtop++] = TEXT;
		draw(mousex);
		draw(mousey);
		draw(tscale);
		do
			drawing[dtop++] = *ptr;
		while(*ptr++);
		draw_object(0, 0); }
}


draw_cursor()
{
	char s;

	s = pixel;
	pixel = FLIP_PIXEL;
	line(max(0, mousex-3), mousey, min(HORIZONTAL-1,mousex+3), mousey);
	line(mousex, max(0,mousey-3), mousex, min(VERTICAL-1,mousey+3));
	pixel = s;
}


redraw()
{
	int x, y;

	video_mode(0x11);
	dpos = cursor_flag = 0;
	pixel = SET_PIXEL;
	while(!draw_object(0, 0));
	if(grid) {
		for(y = 0; y < VERTICAL; y += grid)
			for(x=0; x < HORIZONTAL; x += grid)
				set_pixel(x, y); }
}


undo()
{
	unsigned dlast;
	dpos = dlast = 0;
	while(drawing[dpos]) {
		dlast = dpos;
		if(skip_object())
			return; }
	dpos = dlast;
	pixel = CLEAR_PIXEL;
	draw_object(0, 0);
	pixel = SET_PIXEL;
	zero_drawing(dlast);
	message();
}


erase()
{
	unsigned e, d, i, x, y;

	if(select_object("ERASE")) {
		d = dtop;
		e = dpos;		
		while(i = drawing[dpos]) {
			if(i == ACOPY) {
				i = dpos++;
				x = dvalue();
				y = dvalue();
				if(dvalue() == e) {	
					dtop = e + 1;
					draw(x);
					draw(y);
					e = i; }
				dpos = i; }
			skip_object(); }

		dpos = e;
		skip_object();
		x = dpos - e;
		while(i = drawing[dpos]) {
			if(i == ACOPY) {
				i = dpos;
				dpos += 5;
				if((y = dvalue()) > e) {
					dtop = dpos - 2;
					draw(y - x); }
				dpos = i; }
			skip_object(); }

	/* Remove the selected entry */
		dpos = e;
		skip_object();
		while(dpos < d)
			drawing[e++] = drawing[dpos++];
		zero_drawing(e); }
}

move()
{
	unsigned psave, i, x, y, sx, sy;
	if(select_object("MOVE")) {
		psave = dpos;
		++dpos;
		sx = dvalue();
		sy = dvalue();
		message();
		printf("MOVE: Select new position");
		pixel = FLIP_PIXEL;
newmove:
		dpos = psave;
		draw_object((x = mousex) - sx, (y = mousey) - sy);
		while(!((i = mouse_status()) & MOUSE_LEFT)) {
			if(i & MOUSE_RIGHT) {					
				dpos = psave;
				draw_object(x - sx, y - sy);
				goto skipmove; }
			if((x != mousex) || (y != mousey)) {	
				dpos = psave;
				draw_object(x - sx, y - sy);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -