#include #include #include "tierra.h" #define NLines 40 extern Display *dpy; extern Window win, bar_win; extern Pixmap pmap; extern GC gc; extern unsigned long fg, bg; extern unsigned short width, height; extern Organism *QueueHead; extern unsigned char World[WorldSize]; #define NColors 14 static char *cnames[] = { "grey","red","green","blue","magenta", "orange","violet","yellowGreen","pink","slateBlue","kahki", "white","yellow","cyan", NULL}; static unsigned long pixel[NColors]; #define NOpColors (NColors-3) #define ColEmpty pixel[NColors-3] #define ColReserved pixel[NColors-2] #define ColHalt pixel[NColors-1] draw_init() { int i; XColor scol, ecol; Colormap cmap = DefaultColormap(dpy,DefaultScreen(dpy)); for (i = 0; i < NColors && cnames[i]; i ++) { XAllocNamedColor(dpy,cmap,cnames[i],&scol,&ecol); pixel[i] = scol.pixel; } } static unsigned long get_color(code) unsigned char code; { switch (code) { case InstEmpty: return ColEmpty; case InstReserved: return ColReserved; case InstHalt: return ColHalt; } return pixel[code % NOpColors]; } draw_one(k) unsigned short k; { int x, y, lh = width * height / WorldSize; unsigned char code = World[k]; x = k % width; y = k / width * lh; if (code == (InstLabel|LabelStart) || code == (InstLabel|LabelEnd)) { XSetForeground(dpy, gc, get_color(code)); XDrawLine(dpy,pmap,gc,x,y,x,y+2); XDrawLine(dpy,win,gc,x,y,x,y+2); } else { XSetForeground(dpy, gc, bg); XDrawPoint(dpy,pmap,gc,x,y); XDrawPoint(dpy,win,gc,x,y); XDrawPoint(dpy,pmap,gc,x,y+2); XDrawPoint(dpy,win,gc,x,y+2); XSetForeground(dpy, gc, get_color(code)); XDrawPoint(dpy,pmap,gc,x,y+1); XDrawPoint(dpy,win,gc,x,y+1); } } draw_area(start,length,code) unsigned short start, length; unsigned char code; { int x, y, w, x2, lh = width * height / WorldSize; x = start % width; y = start / width * lh; while (length > 0) { w = (x + length < width)? length : width - x; x2 = x + w - 1; XSetForeground(dpy, gc, bg); XDrawLine(dpy,pmap,gc,x,y,x2,y); XDrawLine(dpy,win,gc,x,y,x2,y); y += 2; XDrawLine(dpy,pmap,gc,x,y,x2,y); XDrawLine(dpy,win,gc,x,y,x2,y); XSetForeground(dpy, gc, get_color(code)); y --; XDrawLine(dpy,pmap,gc,x,y,x2,y); XDrawLine(dpy,win,gc,x,y,x2,y); length -= w; x = 0; y += lh - 1; } } draw_specie() { Organism *p; unsigned short start, length; int x, y, w, x2, lh = width * height / WorldSize; for (p = QueueHead; p; p = p->next) { start = p->start; length = p->length - 1; x = start % width; y = start / width * lh + 2; XSetForeground(dpy, gc, pixel[p->specie % NColors]); while (length > 0) { w = (x + length < width)? length : width - x; x2 = x + w - 1; XDrawLine(dpy,pmap,gc,x,y,x2,y); XDrawLine(dpy,win,gc,x,y,x2,y); length -= w; x = 0; y += lh; } } } xy_to_index(x, y) int x, y; { int lh = width * height / WorldSize; return (y / lh) * width + x; } draw_step(step) unsigned long step; { char str[32]; sprintf(str,"%08X",step); XSetBackground(dpy,gc,bg); XSetForeground(dpy,gc,fg); XDrawImageString(dpy,bar_win,gc,width-80,20,str,8); }