/*
 * $Id: djsvga.trm,v 1.5 1995/12/20 21:47:42 drd Exp $
 */

/* GNUPLOT - djsvga.trm */
/*
 * Copyright (C) 1992
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and 
 * that both that copyright notice and this permission notice appear 
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *
 * This software  is provided "as is" without express or implied warranty.
 *
 * This file is included by ../term.c.
 *
 * This terminal driver supports:
 *  svga
 *
 * AUTHORS
 *  Russell Lang
 *
 * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
 *
 */

/*
 * adapted to the new terminal layout by Stefan Bodewig (Dec. 1995)
 */

#ifndef GOT_DRIVER_H
#include "driver.h"
#endif

#ifdef TERM_REGISTER
register_term(svga)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void DJSVGA_init __P((void));
TERM_PUBLIC void DJSVGA_graphics __P((void));
TERM_PUBLIC void DJSVGA_text __P((void));
TERM_PUBLIC void DJSVGA_reset __P((void));
TERM_PUBLIC void DJSVGA_linetype __P((int linetype));
TERM_PUBLIC void DJSVGA_move __P((unsigned int x, unsigned int y));
TERM_PUBLIC void DJSVGA_vector __P((unsigned int x, unsigned int y));
TERM_PUBLIC void DJSVGA_put_text __P((unsigned int x, unsigned int y, char *str));
#define DJSVGA_XMAX 640
#define DJSVGA_YMAX 480
#define DJSVGA_VCHAR 16
#define DJSVGA_HCHAR 8
#define DJSVGA_VTIC 4
#define DJSVGA_HTIC 4
#endif

#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY

/* SVGA driver using DJGPP */
#include <graphics.h>
#include <pc.h>


int dj_startx, dj_starty;
int dj_xlast, dj_ylast;
int dj_color;


#define DJSVGA_XLAST (DJSVGA_XMAX - 1)
#define DJSVGA_YLAST (DJSVGA_YMAX - 1)


#define DJNUMCOLOR 15
static int svga256color[DJNUMCOLOR] = {7,8,2,3,4,5,9,14,12,15,13,10,11,1,6};
static int dj_colors[DJNUMCOLOR];


TERM_PUBLIC void DJSVGA_init()
{
int i, on, r, g, b;
	/* Allocate colors */
	for (i=0; i<DJNUMCOLOR; i++) {
		on = svga256color[i] & 8 ? 255 : 170;
		r  = svga256color[i] & 4 ? on : 0;
		g  = svga256color[i] & 2 ? on : 0;
		b  = svga256color[i] & 1 ? on : 0;
		if (svga256color[i] == 8) r=g=b=85;
		dj_colors[i] = GrAllocColor(r,g,b);
	}
	/* Get the screen size: */
	GrSetMode(GR_default_graphics,0,0); /* */
	dj_xlast = GrMaxX();
    	term->xmax = dj_xlast + 1;
	dj_ylast = GrMaxY();
    	term->ymax = dj_ylast + 1;
	GrSetMode(GR_default_text,0,0); /* */
}

TERM_PUBLIC void DJSVGA_graphics()
{
	GrSetMode(GR_default_graphics,0,0);
}

TERM_PUBLIC void DJSVGA_text()
{
	(void)getkey();
	GrSetMode(GR_default_text,0,0);
}

TERM_PUBLIC void DJSVGA_reset()
{
int i;
	/* Free colors */
	for (i=0; i<DJNUMCOLOR; i++) {
		GrFreeColor(dj_colors[i]);
	}
}

TERM_PUBLIC void DJSVGA_linetype(linetype)
int linetype;
{
	if (linetype >= 13)
		linetype %= 13;
	dj_color = dj_colors[linetype+2];
}

TERM_PUBLIC void DJSVGA_move(x,y)
unsigned int x,y;
{
	dj_startx = x;
	dj_starty = y;
}


TERM_PUBLIC void DJSVGA_vector(x,y)
unsigned int x,y;
{
	GrLine(dj_startx,dj_ylast-dj_starty,x,dj_ylast-y,dj_color);
	dj_startx = x;
	dj_starty = y;
}


TERM_PUBLIC void DJSVGA_put_text(x,y,str)
unsigned int x, y;
char *str;
{
	GrTextXY(x,dj_ylast-y-DJSVGA_VCHAR/2,str,dj_color,GrNOCOLOR);
}

#endif /* TERM_BODY */

#ifdef TERM_TABLE 

TERM_TABLE_START(svga_driver)
    "svga", "IBM PC/Clone with Super VGA graphics board",
	   DJSVGA_XMAX, DJSVGA_YMAX, DJSVGA_VCHAR, DJSVGA_HCHAR,
	   DJSVGA_VTIC, DJSVGA_HTIC, options_null, DJSVGA_init, DJSVGA_reset,
	   DJSVGA_text, null_scale, DJSVGA_graphics, DJSVGA_move, DJSVGA_vector,
	   DJSVGA_linetype, DJSVGA_put_text, null_text_angle, 
	   null_justify_text, do_point, do_arrow, set_font_null
TERM_TABLE_END(svga_driver)

#undef LAST_TERM
#define LAST_TERM svga_driver

#endif /* TERM_TABLE */
#endif /* TERM_PROTO_ONLY */

/*
 * NAME: svga
 *
 * OPTIONS: none
 *
 * SUPPORTS: PC with SVGA graphics board
 *
 * Further Info: Only used if compiled with DJGPP.
 *
 */