|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T g
Length: 6833 (0x1ab1)
Types: TextFile
Names: »graphics.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
└─⟦this⟧ »EUUGD18/X/Xmandel/graphics.c«
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include "graphics.h"
#define NMAX 256
#define SCFAC 5
#define FLOOR2(x) ((x)/2)
#define CEIL2(x) ((x) - (x)/2)
unsigned long pixellist[NMAX];
char *malloc();
int ntx,nrx,nty,nry;
int nmaxt,nmint,numpix;
int nomin = 1;
int nomax = NMAX-1;
char *display = NULL;
double xin = 0;
double yin = 0;
double scale = 1;
int rsiz = 12;
double dx,dy;
double pscale;
int restartflag;
int (*func)();
char *name = "XGraphics";
Rectangle root;
int rscreen;
Display *dpy;
Colormap cid;
Window rwindow,nwindow;
Pixmap pixmap;
Pixmap npixmap;
GC gc;
XGCValues gcv;
graphics(display0,xin0,yin0,scale0,res,f)
char *display0;
double xin0,yin0,scale0;
int res,(*f)();
{
display = display0;
xin = xin0;
yin = yin0;
scale = scale0;
func = f;
rsiz = res;
root.x = root.y = 0;
root.w = root.h = rsiz;
rectinit(&root);
init();
doplot();
finish();
}
init()
{
int sigfin();
int i,width,height;
XWindowAttributes war;
XSetWindowAttributes setwa;
XColor sdr;
signal(SIGINT, sigfin);
signal(SIGQUIT, sigfin);
signal(SIGTERM, sigfin);
if ((dpy = XOpenDisplay(display)) == 0) {
fprintf(stderr, "Graphics: Can't open display \"%s\"\n",
XDisplayName(display));
exit(1);
}
rwindow = XDefaultRootWindow(dpy);
pointerinit();
rscreen = XDefaultScreen(dpy);
XGetWindowAttributes(dpy,rwindow,&war);
width = war.width;
height = war.height;
nrx = ceil(((double) width)/rsiz);
ntx = nrx*rsiz;
nry = ceil(((double) height)/rsiz);
nty = nry*rsiz;
nwindow = XCreateWindow(dpy, rwindow, 0, 0,
(unsigned int) nrx, (unsigned int) nry, 0, 0,
CopyFromParent, CopyFromParent, 0L, &setwa);
/* Give the window a name */
XChangeProperty(dpy, nwindow, XA_WM_NAME, XA_STRING, 8,
PropModeReplace, name, strlen(name));
XMapWindow(dpy,nwindow);
XMoveWindow(dpy,nwindow,0,0);
cid = XDefaultColormap(dpy,rscreen);
numpix = 1<<war.depth;
pixellist[0] = 1;
for (i=1;i<NMAX;i++){
pixellist[i] = (i + 1) % numpix;
}
pixmap = XCreatePixmap(dpy,rwindow,ntx,nty,war.depth);
if (pixmap==0){
fprintf(stderr, "Graphics: Unable to create Pixmap");
exit(1);
}
npixmap = XCreatePixmap(dpy,nwindow,nrx,nry,war.depth);
if (npixmap==0){
fprintf(stderr, "Graphics: Unable to create Pixmap");
exit(1);
}
gc = XCreateGC(dpy,pixmap,0L,&gcv);
}
initpix()
{
int i;
pixellist[0] = 1;
nomax = nmaxt;
nomin = nmint;
for (i=1;i<NMAX;i++){
pixellist[i] = (2 + ((i - nomin)*(numpix-2)) /
(nomax-nomin)) % numpix;
}
}
pointerinit()
{
long event_mask;
int mkpm;
XModifierKeymap *modmap;
KeyCode *mkdptr;
modmap = XGetModifierMapping(dpy);
mkpm = modmap->max_keypermod;
mkdptr = modmap->modifiermap;
mkdptr[4*mkpm] = (char) 26; /* This is the keycode for the Alt key,
and Mod2 has position 4 */
XSetModifierMapping(dpy,modmap);
event_mask = ButtonPressMask;
XGrabButton(dpy,AnyButton,Mod2Mask,rwindow,True,event_mask,
GrabModeAsync,GrabModeAsync,None,0);
}
int rescale(n)
int n;
{
int i;
i = abs(n)%NMAX;
if (i>nmaxt) nmaxt = i;
if ((i>0)&&(i<nmint)) nmint = i;
return i;
}
plotpt(i,j,w,h,n)
int i,j,w,h,n;
{
XSetForeground(dpy,gc,pixellist[n]);
XFillRectangle(dpy,pixmap,gc,i,j,w,h);
if ((i%rsiz==0)&&(j%rsiz==0))
XDrawPoint(dpy,npixmap,gc,i/rsiz,j/rsiz);
}
plotall()
{
XSetWindowBackgroundPixmap(dpy,rwindow,pixmap);
XSetWindowBackgroundPixmap(dpy,nwindow,npixmap);
XClearWindow(dpy,rwindow);
XClearWindow(dpy,nwindow);
XFlush(dpy);
checkevent();
}
checkevent()
{
long event_mask;
XEvent evr;
XButtonEvent xbevr;
char *args = NULL;
Bool checkb2();
event_mask = ButtonPressMask;
if (XCheckIfEvent(dpy,&evr,checkb2,args)== True){
restartflag = 1;
xbevr = evr.xbutton;
xin += (-(nrx+1)*rsiz + 2*xbevr.x)/(pscale*rsiz);
yin += (-(nry+1)*rsiz + 2*xbevr.y)/(pscale*rsiz);
}
while(XCheckMaskEvent(dpy,event_mask,&evr)==True){
restartflag = 1;
xbevr = evr.xbutton;
if (xbevr.button == Button1) scale /= SCFAC;
if (xbevr.button == Button3) scale *= SCFAC;
}
printf("xin,yin: %g, %g, scale: %g\n",xin,yin,scale);
}
Bool checkb2(display,event,args)
Display *display;
XEvent *event;
char *args;
{
XButtonEvent xbevr;
xbevr = event->xbutton;
if ((xbevr.button == Button2)&&
(event->type==ButtonPress)) return True;
else return False;
}
Rectangle *newrect()
{
Rectangle *t;
t = (Rectangle *) malloc(sizeof(Rectangle));
return t;
}
rectinit(rptr)
Rectangle *rptr;
{
Rectangle *nptr;
int w,h;
w = rptr->w;
h = rptr->h;
if ((h==1)&&(w==1)) rptr->ul = NULL;
else {
nptr = rptr->ul = newrect();
nptr->x = rptr->x;
nptr->y = rptr->y;
nptr->w = CEIL2(w);
nptr->h = CEIL2(h);
rectinit(nptr);
}
if (w==1) rptr->ur = NULL;
else {
nptr = rptr->ur = newrect();
nptr->x = rptr->x + CEIL2(w);
nptr->y = rptr->y;
nptr->w = FLOOR2(w);
nptr->h = CEIL2(h);
rectinit(nptr);
}
if (h == 1) rptr->ll = NULL;
else {
nptr = rptr->ll = newrect();
nptr->x = rptr->x;
nptr->y = rptr->y + CEIL2(h);
nptr->w = CEIL2(w);
nptr->h = FLOOR2(h);
rectinit(nptr);
}
if ((h == 1)||(w == 1)) rptr->lr = NULL;
else {
nptr = rptr->lr = newrect();
nptr->x = rptr->x + CEIL2(w);
nptr->y = rptr->y + CEIL2(h);
nptr->w = FLOOR2(w);
nptr->h = FLOOR2(h);
rectinit(nptr);
}
}
doplot()
{
int dographics();
int l,maxl;
maxl = 0;
l = 1;
while (l<rsiz){
l *=2;
maxl++;
}
restart:
restartflag = 0;
pscale = nrx*scale;
dx = 2/(pscale);
dy = 2/(pscale);
nmaxt = 0;
nmint = NMAX;
if (dographics(&root,0)==0) goto restart;
initpix();
for (l=0;l<=maxl;l++){
if (dographics(&root,l)==0) goto restart;
}
}
int dographics(rptr,level)
Rectangle *rptr;
int level;
{
int i,j,ii,jj,n;
double xstart,ystart,x,y;
if (rptr==NULL) return 1;
if (level==0){
ii = rptr->x;
jj = rptr->y;
xstart = xin - nrx/pscale + (-rsiz+2*ii)/(pscale*rsiz);
ystart = yin - nry/pscale + (-rsiz + 2*jj)/(pscale*rsiz);
for (x = xstart,i=0;i<nrx;i++,x += dx)
for (y = ystart, j=0;j<nry;j++,y += dy){
n = rescale((*func)(x,y));
plotpt(rsiz*i+ii,rsiz*j+jj,rptr->w,rptr->h,n);
}
plotall();
if (restartflag==1) return(0);
return(1);
}
if (dographics2(rptr,level)==0) return 0;
return 1;
}
int dographics2(rptr,level)
Rectangle *rptr;
int level;
{
if (rptr==NULL) return 1;
if (dographics2(rptr->ul,level-1)==0) return 0;
if (dographics(rptr->ur,level-1)==0) return 0;
if (dographics(rptr->ll,level-1)==0) return 0;
if (dographics(rptr->lr,level-1)==0) return 0;
return 1;
}
finish()
{
XFreeGC(dpy,gc);
XFreePixmap(dpy,pixmap);
XFreePixmap(dpy,npixmap);
XDestroyWindow(dpy,nwindow);
}
sigfin(sig,code,scp)
int sig,code;
struct sigcontext *scp;
{
finish();
exit(sig);
}