/*
 * Grafico.c - Rutinas gr'aficas para acceso a la pantalla.
 * 'Ultima modificaci'on: 03-03-2002
 *
 * Antonio G'omez Muriana (correo@senco.net)
 * Miguel Montero G'amez
 *
 */

#include <X11/Xlib.h>
#include <assert.h>
#include "grafico.h"

#define NIL (0)

Display *dpy;
Window w;
GC gc;

void iniciarpanel(void);
void creapanel(void);

void ginicializa(void)
{
	creapanel();
	iniciarpanel();
}

void creapanel(void)
{
	int blackColor;

	dpy = XOpenDisplay(NIL);
	assert(dpy);
	blackColor = BlackPixel(dpy, DefaultScreen(dpy));
	w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 783, 260, 0,
								blackColor, blackColor);
	XSelectInput(dpy, w, StructureNotifyMask);
	XMapWindow(dpy, w);
	gc = XCreateGC(dpy, w, 0, NIL);
	
	for(;;){
		XEvent e;
		XNextEvent(dpy,&e);
		if(e.type==MapNotify)
			break;
	}
}

void iniciarpanel(void)
{
	int i,j;
	
	XSetForeground(dpy, gc, 3000*512);
	for(i=0; i<256; i+=8) {
		for(j=0; j<768; j+=8)
			XFillArc(dpy, w, gc, j+j/48, i+i/64, 7, 7, 0, 360*64);
	}
}

/*
 * Pintamos una bombilla en la pantalla
 */

#define CALCULA_X(id, n) ((id)%16)*49 + ((n)%6)*8
#define CALCULA_Y(id, n) ((id)/16)*65 + ((n)/6)*8
#define LUZ_ENCENDIDA 3000*1024
#define LUZ_APAGADA	  3000*512

void pintarpunto(int id, int n, unsigned char punto)
{
	int x,y;
	
	x = CALCULA_X(id, n);
	y = CALCULA_Y(id, n);
    XSetForeground(dpy, gc, (punto & 0x80) ? LUZ_ENCENDIDA : LUZ_APAGADA); 
	XFillArc(dpy, w, gc, x, y, 7, 7, 0, 360*64);
}
