/* DOSGRAPH.C * Renato Ferreira Borges * Setembro 94 */ #include #include #include #include #include "dg.h" static struct { int x0, x1, y0, y1; } clip; static int maxx, maxy; DgMode mode = dgCOPY; static void limit( int*p, int menor, int maior ) { if (*pmaior) *p=maior; } static void normaliza(int *x0, int *y0, int *x1, int *y1) { if (*x1 < *x0) { int x = *x0; *x0 = *x1; *x1 = x; } if (*y1 < *y0) { int y = *y0; *y0 = *y1; *y1 = y; } limit(x0, 0, maxx); limit(x1, 0, maxx); limit(y0, 0, maxy); limit(y1, 0, maxy); } static void showMouse(void) { union REGS regs; regs.x.ax = 1; int86(0x33, ®s, ®s); } static void hideMouse(void) { union REGS regs; regs.x.ax = 2; int86(0x33, ®s, ®s); } static void mouseInit(void) { union REGS regs; regs.x.ax = 0; int86(0x33, ®s, ®s); showMouse(); } int far dgOpen(void) { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, ""); errorcode = graphresult(); if (errorcode != grOk) { printf("Erro: %s\n", grapherrormsg(errorcode)); return 1; } maxx = getmaxx(); maxy = getmaxy(); clip.x0 = 0; clip.y0 = 0; clip.x1 = maxx; clip.y1 = maxy; mode = dgCOPY; mouseInit(); return 0; } void far dgClose(void) { closegraph(); } int far dgWidth(void) { return maxx; } int far dgHeight(void) { return maxy; } void far dgLine(int x0, int y0, int x1, int y1) { setviewport(clip.x0, clip.y0, clip.x1, clip.y1, 1); hideMouse(); line(x0-clip.x0, y0-clip.y0, x1-clip.x0, y1-clip.y0); showMouse(); setviewport(0, 0, maxx, maxy, 1); } void far dgRectangle(int x0, int y0, int x1, int y1) { setviewport(clip.x0, clip.y0, clip.x1, clip.y1, 1); hideMouse(); moveto(x0-clip.x0, y0-clip.y0); lineto(x0-clip.x0, y1-clip.y0); lineto(x1-clip.x0, y1-clip.y0); lineto(x1-clip.x0, y0-clip.y0); lineto(x0-clip.x0, y0-clip.y0); showMouse(); setviewport(0, 0, maxx, maxy, 1); } void far dgFill( int x0, int y0, int x1, int y1) { int p[8]; normaliza(&x0, &y0, &x1, &y1); setviewport(clip.x0, clip.y0, clip.x1, clip.y1, 1); hideMouse(); if (mode == dgXOR) { int i=0; for (i=y0; i<=y1; i++) line(x0,i,x1,i); } else { p[0] = x0 - clip.x0; p[1] = y0 - clip.y0; p[2] = x1 - clip.x0; p[3] = y0 - clip.y0; p[4] = x1 - clip.x0; p[5] = y1 - clip.y0; p[6] = x0 - clip.x0; p[7] = y1 - clip.y0; fillpoly(4, p); } showMouse(); setviewport(0, 0, maxx, maxy, 1); } void far dgSetColor(DgColor cor) { setcolor((int)cor); setfillstyle(1, (int)cor); } void far dgSetMode(DgMode modo) { mode = modo; setwritemode((int)modo); } void far dgClear(int x0, int y0, int x1, int y1) { setviewport(x0, y0, x1, y1, 1); hideMouse(); clearviewport(); showMouse(); setviewport(0, 0, maxx, maxy, 1); } void far dgMove(int x0, int y0, int x1, int y1, int newx, int newy) { unsigned size; void *image; normaliza(&x0, &y0, &x1, &y1); if (x0==newx && y0==newy) return; size = imagesize(x0,y0,x1,y1); if (size==0xffff) return; image = malloc(size); getimage(x0,y0,x1,y1,image); putimage(newx, newy, image, COPY_PUT); free(image); } void far dgClip(int x0, int y0, int x1, int y1) { normaliza(&x0, &y0, &x1, &y1); clip.x0 = x0; clip.x1 = x1; clip.y0 = y0; clip.y1 = y1; } DgEventType far dgGetEvent(DgEvent far* ev) { union REGS regs; static int x=0, y=0, b1=0, b2=0; while (1) { regs.x.ax = 3; int86(0x33, ®s, ®s); if (b1!=(regs.x.bx&1)) { b1 = regs.x.bx&1; ev->x = x; ev->y = y; if (b1==0) { ev->tipo = dgUNCLICK1; return dgUNCLICK1; } else { ev->tipo = dgCLICK1; return dgCLICK1; } } if (b2!=(regs.x.bx&2)) { b2 = regs.x.bx&2; ev->x = x; ev->y = y; if (b2==0) { ev->tipo = dgUNCLICK2; return dgUNCLICK2; } else { ev->tipo = dgCLICK2; return dgCLICK2; } } if ((x!=regs.x.cx) || (y!=regs.x.dx)) { x = regs.x.cx; y = regs.x.dx; ev->x = x; ev->y = y; ev->tipo = dgMOVE; return dgMOVE; } } } int far points[200]; int used; void far dgBeginPoly() { used = 0; } void far dgPolyPoint(int x, int y) { points[used++]=x; points[used++]=y; } void far dgEndPoly(DgPolyType type) { hideMouse(); switch (type) { case dgOPENED: drawpoly(used/2, points); break; case dgCLOSED: points[used++] = points[0]; points[used++] = points[1]; drawpoly(used/2, points); break; case dgFILL: fillpoly(used/2, points); break; } showMouse(); }