+/*****************************************************************************
+** This is part of the CTSim program
+** Copyright (C) 1983-2000 Kevin Rosenberg
+**
+** $Id: sgpdriver.cpp,v 1.1 2000/06/19 19:20:11 kevin Exp $
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License (version 2) as
+** published by the Free Software Foundation.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+******************************************************************************/
+
+/* Device Driver for IBM PC Kevin Rosenberg March 84 */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include "ctsupport.h"
+#include "sgp.h"
+
+
+static int init_prt(void);
+static void term_prt(void);
+
+/*===========================================================*/
+/* fundamental device drivers */
+/*===========================================================*/
+
+#define XCHARSIZ (1.0 / 80.0) /* size of standard characters */
+#define YCHARSIZ (1.0 / 25.0) /* on screen in NDC */
+
+#define CRTASPT (0.75) /* for IBM ECD */
+
+/*-------------------*/
+/* print buffer data */
+/*-------------------*/
+#define PRTINIT 1
+#define PRTLIN 2
+#define PRTTERM 3
+
+#define PRTSIZE 200
+#define PRTMODE 1 /* Epson 120 dot/in graphic mode */
+#define PRTASPT (120.0 / 72.0) /* dots/in vectical / horizontal */
+
+static DEVICE crt, prt;
+CHARSPEC cspec;
+static GRFSTATE state;
+
+
+
+/* NAME
+ * gp_init Initialize graphics package
+ *
+ * SYNOPSIS
+ * gp_init()
+ */
+
+
+int
+_sgp2_init_dev (SGP_ID cwin)
+{
+#if 0
+ initdevice (CRTDEV, GM_ENHANCED, 0, 0);
+ initdevice (PRTDEV, PRTMODE, (int) (PRTSIZE * PRTASPT), PRTSIZE);
+ opendevice (CRTDEV);
+ setlinestyle (LS_SOLID);
+ setcolor (1);
+ _sgp2_set_text (XCHARSIZ, YCHARSIZ, 0.0, 0);
+ setbackg (0);
+ settextclr (1, 0);
+ gp_set_aspect (CRTDEV, CRTASPT);
+ gp_set_aspect (PRTDEV, 1.0);
+#endif
+
+ state.xndc = 10.; /* to guarntee a move */
+ _sgp2_stylus (cwin, 0.0,0.0,0); /* move to starting corner */
+
+ return (TRUE);
+}
+
+
+/* NAME
+ * gp_initdev Initializes graphic device
+ *
+ * SYNOPSIS
+ * initdevice (dev, mode, xsize, ysize)
+ * int dev Device handle
+ * int mode Mode for device (device specific)
+ * int xsize Size of x buffer for PRTDEV
+ * int ysize Size of y buffer for PRTDEV
+ */
+
+int
+initdevice (int dev, int mode, int xsize, int ysize)
+{
+ if (dev & CRTDEV)
+ {
+ // crt.dotfunc = crt_wdot;
+ // crt.linefunc = crt_line;
+ crt.xmin = 0;
+ crt.ymin = 0;
+ switch (crt.mode)
+ {
+#if 0
+ case 4:
+ case 5:
+ crt.xmax = 319;
+ crt.xsize = 320;
+ crt.ymax = 199;
+ crt.ysize = 200;
+ crt.colormax = 3;
+ break;
+ case GM_640x200:
+ crt.xmax = 639;
+ crt.xsize = 640;
+ crt.ymax = 199;
+ crt.ysize = 200;
+ crt.colormax = 1;
+ break;
+ case GM_ENHANCED:
+ case GM_MONOGRF:
+ crt.xmax = 639;
+ crt.xsize = 640;
+ crt.ymax = 349;
+ crt.ysize = 350;
+ crt.colormax = 15;
+ break;
+ case GM_640x480x16:
+ crt.xmax = 639;
+ crt.xsize = 640;
+ crt.ymax = 479;
+ crt.ysize = 480;
+ crt.colormax = 15;
+ break;
+ case GM_640x480x256:
+ crt.xmax = 639;
+ crt.xsize = 640;
+ crt.ymax = 479;
+ crt.ysize = 480;
+ crt.colormax = 255;
+ break;
+ case GM_1024x768x16:
+ crt.xmax = 1023;
+ crt.xsize = 1024;
+ crt.ymax = 767;
+ crt.ysize = 768;
+ crt.colormax = 15;
+ break;
+ case GM_1024x768x256:
+ crt.xmax = 1023;
+ crt.xsize = 1024;
+ crt.ymax = 767;
+ crt.ysize = 768;
+ crt.colormax = 255;
+ break;
+#endif
+ default:
+ return (FALSE);
+ }
+ // crt_set_mode (crt.mode, TRUE); /* Initialize device */
+ }
+
+ if (dev & PRTDEV) {
+ if (prt.open == TRUE)
+ closedevice (PRTDEV);
+ prt.buf = NULL;
+ if (mode < 0)
+ prt.mode = PRTMODE;
+ if (mode > 6)
+ prt.mode = PRTMODE;
+ else
+ prt.mode = mode;
+
+ if (xsize > 1 && ysize > 1) {
+ prt.xsize = xsize;
+ prt.ysize = ysize;
+ prt.xmin = 0;
+ prt.ymin = 0;
+ prt.xmax = prt.xsize - 1;
+ prt.ymax = prt.ysize - 1;
+ }
+
+ prt.colormax = 1;
+ // prt.dotfunc = prtdot;
+ // prt.linefunc = prtline;
+
+ init_prt (); /* Initialize the device */
+ }
+
+ return (TRUE);
+}
+
+
+/* NAME
+ * gp_opendev Open device for output
+ *
+ * SYNOPSIS
+ * gp_opendev (dev)
+ * int dev Device handle
+ * devices are number in powers of two
+ */
+
+int
+opendevice (int dev)
+{
+ if (dev & CRTDEV)
+ crt.open = TRUE;
+
+ if (dev & PRTDEV)
+ prt.open = TRUE;
+
+ return(TRUE);
+}
+
+
+/* NAME
+ * gp_closedev Close device for output
+ *
+ * SYNOPSIS
+ * gp_closedev (dev)
+ * int dev Device handle
+ *
+ * DESCRIPTION
+ * Temporarily suspends output from going to a device
+ */
+
+void
+closedevice (int dev)
+{
+ if (dev & CRTDEV) /* close crt */
+ crt.open = FALSE;
+
+ if (dev & PRTDEV)
+ prt.open = FALSE;
+}
+
+
+void
+termdevice (int dev)
+{
+ if (dev & PRTDEV)
+ term_prt ();
+}
+
+static int init_prt(void)
+{
+ return(0);
+}
+
+
+static void term_prt(void)
+{
+}
+
+/*===========================================================*/
+/* stylus draws a line to the absolute point x,y in NDC's */
+/* t=0 for move, t=1 for draw */
+/*===========================================================*/
+void
+_sgp2_stylus (SGP_ID cwin, double x, double y, int beam)
+{
+ int xp, yp;
+
+ if ((state.xndc == x) && (state.yndc == y) && (beam == 0))
+ return; /* no need to move */
+
+ state.xndc = x; /* save current beam location */
+ state.yndc = y;
+
+ if (cwin != NULL) {
+ xp = (int) (x * cwin->pw_xsize + 0.5);
+ yp = (int) (y * cwin->pw_ysize + 0.5);
+#if HAVE_G2_H
+ if (beam != 0)
+ g2_line (cwin->g2_id, (double) cwin->phys_curx, (double) cwin->phys_cury, (double) xp, (double) yp);
+#endif
+ cwin->phys_curx = xp;
+ cwin->phys_cury = yp;
+ }
+}
+
+void
+pntndc (SGP_ID cwin, double x, double y)
+{
+ _sgp2_stylus (cwin, x, y, 0); /* move to point */
+ // if (crt.open == TRUE)
+ // cpix_set (crt.icurx, crt.icury, crt.color);
+}
+
+void
+markndc (SGP_ID cwin, double x, double y)
+{
+ _sgp2_stylus (cwin, x, y, 0); /* move to point */
+
+ if (crt.open == TRUE)
+ wrtsymbol (state.marktype, crt.icurx, crt.icury, &crt);
+
+ if (prt.open == TRUE)
+ wrtsymbol (state.marktype, prt.icurx, prt.icury, &prt);
+}
+
+GRFSTATE *
+inqstate (void)
+{
+ return (&state);
+}
+
+void
+gp_set_aspect (int dev, double asp)
+{
+ if (asp > 0.0) {
+ if (dev & CRTDEV)
+ crt.asp = asp;
+ if (dev & PRTDEV)
+ prt.asp = asp;
+ }
+}
+
+void
+setlinestyle (int style)
+{
+ if (style == state.linestyle)
+ return;
+
+ state.linestyle = style;
+
+ crt.style = style;
+ prt.style = style;
+
+ // crt_line_style (crt.style);
+}
+
+void
+setlinewidth (int wid)
+{
+ state.linewidth = wid;
+}
+
+DEVICE *
+inqdev (int dev)
+{
+ if (dev & CRTDEV)
+ return (&crt);
+ else if (dev & PRTDEV)
+ return (&prt);
+ else
+ return (NULL);
+}
+
+/*===========================================================*/
+/* set text size */
+/*===========================================================*/
+void
+_sgp2_set_text ( SGP_ID cwin,
+ double width,
+ double height, /* size of character in NDC */
+ double textangle, /* text angle */
+ int font /* text font */
+ )
+{
+ double temp;
+
+#if HAVE_G2_H
+ g2_set_font_size(cwin->g2_id, (height * cwin->pw_ysize));
+#endif
+ height = clamp (height, 0.0, 1.0);
+ width = clamp (width, 0.0, 1.0);
+ /* textangle = textangle - (2 * PI * (int) (textangle / (2 * PI)));
+ */
+ cspec.width = width;
+ cspec.height = height;
+ cspec.textangle = textangle;
+ cspec.font = font;
+
+ if (textangle > - HALFPI / 2 && textangle < HALFPI / 2) {
+ cspec.updir = YPLUS;
+ cspec.textdir = XPLUS;
+ } else if (textangle > HALFPI / 2 && textangle < 3 * HALFPI / 2) {
+ cspec.updir = XMINUS;
+ cspec.textdir = YPLUS;
+ } else if (textangle > 3 * HALFPI / 2 && textangle < 5 * HALFPI / 2) {
+ cspec.updir = YMINUS;
+ cspec.textdir = XMINUS;
+ } else if (textangle > 5 * HALFPI / 2 && textangle < 7 * HALFPI / 2) {
+ cspec.updir = XPLUS;
+ cspec.textdir = YMINUS;
+ } else {
+ cspec.updir = YPLUS;
+ cspec.textdir = XPLUS;
+ }
+
+ if (cspec.updir == XMINUS || cspec.updir == XPLUS) {
+ temp = height;
+ height = width;
+ width = temp;
+ }
+
+ crt.icwidth = (int) (width * crt.xsize + 0.5);
+ crt.icheight = (int) (height * crt.ysize + 0.5);
+ crt.icwidth = clamp (crt.icwidth, 8, crt.xsize);
+ crt.icheight = clamp (crt.icheight, 8, crt.ysize);
+
+ prt.icwidth = (int) (width * prt.xsize + 0.5);
+ prt.icheight = (int) (height * prt.ysize + 0.5);
+ prt.icwidth = clamp (prt.icwidth, 8, prt.xsize);
+ prt.icheight = clamp (prt.icheight, 8, prt.ysize);
+}
+
+void
+settextclr (int fore, int back)
+{
+ cspec.fore = fore;
+ cspec.back = back;
+ crt.cfore = fore;
+ crt.cback = back;
+ prt.cfore = clamp (fore, 0, prt.colormax);
+ prt.cback = back;
+}
+
+void
+setcolor (int fore)
+{
+ int back;
+
+ state.foregnd = fore;
+ back = state.backgnd;
+ crtcolor (crt.mode, &fore, &back); /* set colors on crt */
+ crt.color = fore;
+ prt.color = clamp (fore, 0, prt.colormax);
+}
+
+void
+setbackg (int back)
+{
+ int fore;
+
+ state.backgnd = back;
+ fore = state.foregnd;
+ crtcolor (crt.mode, &fore, &back);
+}
+
+/*
+ * INITMARKER (<SYMBOL>)
+ *
+ * Sets the current marker symbol (cross, square, diamond..)
+ */
+int
+initmarker (int marker, int color)
+{
+ if (marker > NMARKERS || marker < 0)
+ return(-1);
+ else {
+ state.marktype = marker;
+ state.markcolor = color;
+ }
+ return(0);
+} /* end initmarker */
+
+/*
+ * SETCHARDIR ( <DIRECTION> )
+ *
+ * indicates in which direction a string should be printed, e.g.:
+ * direction = YPLUS -> print on the horizontal from left to right
+ * direction = YMINUS -> print upside-down characters from right to left
+ * direction = XMINUS -> print on the vertical from down to up
+ * direction = XPLUS -> print on the vertical from up to down
+ */
+int
+settextdir (
+ int direction /* direction flag */
+ )
+{
+
+ if (direction != XPLUS &&
+ direction != XMINUS &&
+ direction != YPLUS &&
+ direction != YMINUS ) {
+ printf("Error in character direction: %d\n", direction);
+ return(-1);
+ }
+ cspec.textdir = direction;
+ charsize (cspec.width, cspec.height);
+
+ return(0);
+} /* end setchardir */
+
+
+void
+_sgp2_dev_text (SGP_ID cwin, char *message)
+{
+#if HAVE_G2_H
+ g2_string (cwin->g2_id, cwin->phys_curx, cwin->phys_cury, message);
+#endif
+}
+
+/*===========================================================*/
+/* terminate graphics to current device */
+/* close any files, and output any buffers */
+/*===========================================================*/
+void
+termgrf2 (void)
+{
+ if (prt.open == TRUE) {
+ flushdevice (PRTDEV);
+ closedevice (PRTDEV);
+ termdevice (PRTDEV);
+ }
+
+ if (crt.open == TRUE) {
+ closedevice (CRTDEV);
+ }
+}
+
+
+void
+flushdevice (int dev)
+{
+ // if (dev & PRTDEV)
+ // prtline (PRTTERM, 0, 0, 0, 0, 0, 0); /* print contents of printer buffer */
+}