#include #include #include "ant.h" #define BackGround "tan" #define AntColor "black" #define FoodColor "white" #define PheromoneColor "blue" #define NGrades 16 extern Display *dpy; extern int scr; extern GC gc; extern Cell world[NMESH][NMESH]; extern Ant ant[NAnts]; int ShowPheromone = 1; static unsigned int WinWidth, WinHeight; static int depth; static Window Root, win; static Pixmap pmap = 0; static unsigned long backGround = 0, antColor = 0, foodColor, pheromoneColor[NGrades]; static void color_init() { int i; Colormap cmap = DefaultColormap(dpy, scr); XColor bcol, pcol, scol, ecol; bcol.flags = pcol.flags = scol.flags = ecol.flags = DoRed | DoGreen | DoBlue; XAllocNamedColor(dpy, cmap, BackGround, &bcol, &ecol); backGround = pheromoneColor[0] = bcol.pixel; XAllocNamedColor(dpy, cmap, AntColor, &scol, &ecol); antColor = scol.pixel; XAllocNamedColor(dpy, cmap, FoodColor, &scol, &ecol); foodColor = scol.pixel; XAllocNamedColor(dpy, cmap, PheromoneColor, &pcol, &ecol); pheromoneColor[NGrades - 1] = pcol.pixel; for (i = 1; i < NGrades-1; i ++) { scol.red = (pcol.red*i + bcol.red*(NGrades-i)) / NGrades; scol.green = (pcol.green*i + bcol.green*(NGrades-i)) / NGrades; scol.blue = (pcol.blue*i + bcol.blue*(NGrades-i)) / NGrades; XAllocColor(dpy,cmap,&scol); pheromoneColor[i] = scol.pixel; } } redraw(e) XExposeEvent *e; { XCopyArea(dpy,pmap,win,gc,e->x,e->y,e->width,e->height,e->x,e->y); } show_world() { int i, j, k; int dx = WinWidth / NMESH, dy = WinHeight / NMESH; unsigned int pxl; XSetForeground(dpy,gc,backGround); XFillRectangle(dpy,pmap,gc,0,0,WinWidth,WinHeight); for (i = 0; i < NMESH; i ++) for (j = 0; j < NMESH; j ++) { if (world[i][j].Food) pxl = foodColor; else if (ShowPheromone) { k = world[i][j].phrmn * NGrades; if (k >= NGrades) k = NGrades - 1; pxl = pheromoneColor[k]; } else pxl = backGround; if (pxl != backGround) { XSetForeground(dpy,gc,pxl); XFillRectangle(dpy,pmap,gc,dx*j,dy*i,dx,dy); } } XSetForeground(dpy,gc,antColor); for (i = 0; i < NAnts; i ++) XFillRectangle(dpy,pmap,gc,dx*ant[i].x,dy*ant[i].y,dx,dy); XCopyArea(dpy,pmap,win,gc,0,0,WinWidth,WinHeight,0,0); } draw_init(w) Window w; { int x, y, bd; win = w; XGetGeometry(dpy,w,&Root,&x,&y,&WinWidth,&WinHeight,&bd,&depth); if (pmap) XFreePixmap(dpy,pmap); pmap = XCreatePixmap(dpy,w,WinWidth,WinHeight,depth); if (!pmap) { fprintf(stderr,"cannot create pixmap %dx%dx%d.\n", WinWidth,WinHeight,depth); exit(1); } if (backGround == antColor) color_init(); } domain_init(w) Window w; { if (w) draw_init(w); memory_init(); }