174d83b00486b602156e6f60c2b5213103571450
[ctsim.git] / include / sgp.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         sgp.h
5 **      Purpose:      Header file for Simple Graphics Package
6 **      Author:       Kevin Rosenberg
7 **      Date Started: 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: sgp.h,v 1.21 2000/12/06 15:17:51 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef __H_SGP
29 #define __H_SGP
30
31 #if HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "transformmatrix.h"
36
37 #ifdef HAVE_WXWINDOWS
38 #include <wx/wx.h>
39 #include <wx/font.h>
40 #endif
41
42 #if HAVE_G2_H
43 extern "C" {
44 #include "g2.h"
45 #include "g2_X11.h"
46 }
47 #endif
48
49 #include <string>
50 using namespace std;
51
52 class SGPDriver {
53 private:
54   int m_iPhysicalXSize;
55   int m_iPhysicalYSize;
56   string m_sWindowTitle;
57   int m_idDriver;
58
59 #ifdef HAVE_WXWINDOWS
60   wxDC* m_pDC;
61 #endif
62   int m_idG2;
63
64 public:
65   enum {
66     SGPDRIVER_WXWINDOWS = 1,
67     SGPDRIVER_G2 = 2,
68     SGPDRIVER_OPENGL = 4,
69   };
70
71 #ifdef HAVE_WXWINDOWS
72   SGPDriver (wxDC* pDC, int xsize = 640, int ysize = 480);
73 #endif
74
75   SGPDriver (const char* szWinTitle = "", int xsize = 640, int ysize = 480);
76   
77   ~SGPDriver ();
78
79   int getPhysicalXSize () const
80     { return m_iPhysicalXSize; }
81
82   int getPhysicalYSize () const
83     { return m_iPhysicalYSize; }
84
85   const string& getWindowTitle () const
86     { return m_sWindowTitle; }
87
88   bool isWX () const
89   { return (m_idDriver & SGPDRIVER_WXWINDOWS ? true : false); }
90
91   bool isG2 () const
92   { return (m_idDriver & SGPDRIVER_G2 ? true : false); }
93
94   int idG2 () const
95     { return m_idG2; }
96
97 #ifdef HAVE_WXWINDOWS
98   wxDC* idWX () const
99     { return m_pDC; }
100
101   void setDC (wxDC* dc)
102       { m_pDC = dc; }
103 #endif
104 };
105
106
107 class RGBColor;
108 class SGP {
109 private:
110   int m_iPhysicalXSize;   // Physical Window size 
111   int m_iPhysicalYSize;
112   SGPDriver m_driver;
113
114   double xw_min;    // Window extents 
115   double yw_min;
116   double xw_max;
117   double yw_max;
118   double xv_min;    // Viewport extents 
119   double yv_min;
120   double xv_max;
121   double yv_max;
122   double viewNDC[4];   // Viewport array for clip_rect() 
123
124   int m_iCurrentPhysicalX;
125   int m_iCurrentPhysicalY;
126   double m_dCurrentWorldX;
127   double m_dCurrentWorldY;
128   double m_dTextAngle;
129   bool m_bRecalcTransform;
130   double m_dPointsPerPixel;  // points (72pt/in) per screen pixel;
131
132   // Master coordinates are coordinates before CTM transformation
133   // World coordinates are coordinates defined by setWindow()
134   // Normalized device coordinates range from 0. to 1. in both axes
135   TransformationMatrix2D wc_to_ndc;     // World coord to NDC matrix 
136   TransformationMatrix2D mc_to_ndc;     // Master to NDC 
137   TransformationMatrix2D ndc_to_mc;     // NDC to Master
138   TransformationMatrix2D m_ctm;         // Current transfromation matrix 
139   
140   void calc_transform ();
141
142   static RGBColor s_aRGBColor[];
143   static int s_iRGBColorCount;
144
145 #if HAVE_WXWINDOWS
146   wxPen m_pen;
147   wxFont m_font;
148 #endif
149
150 public:
151   enum {                  // linestyles 
152       LS_NOLINE = 0,
153       LS_SOLID = 0xffff,
154       LS_DASH1 = 0xff00,
155       LS_DASH2 = 0xf0f0,
156       LS_DASH3 = 0xcccc,
157       LS_DASH4 = 0xff3e,
158       LS_DOTTED = 0xaaaa,
159   };
160
161   enum {            // Codes for marker symbols
162       MARK_POINT = 0,   // small dot 
163       MARK_SQUARE = 1,  // empty square 
164       MARK_FSQUARE = 2, // filled square 
165       MARK_DIAMOND = 3, // empty diamond 
166       MARK_FDIAMOND = 4,        // filled diamond 
167       MARK_CROSS =  5,  // cross 
168       MARK_XCROSS = 6,  // x 
169       MARK_CIRCLE = 7,    // open circle 
170       MARK_FCIRCLE = 8, // filled circle 
171       MARK_BSQUARE = 9, // big open square 
172       MARK_BDIAMOND = 10,       // big open diamond 
173   };
174   enum  { MARK_COUNT = 11, };
175   static const unsigned char MARKER_BITMAP[MARK_COUNT][5];
176
177   SGP (const SGPDriver& driver);
178
179   void drawCircle (const double r);
180   void drawArc (const double r, double start, double stop);
181   void drawRect (double xmin, double ymin, double xmax, double ymax);
182   void lineAbs(double x, double y);
183   void moveAbs(double x, double y);
184   void lineRel(double x, double y);
185   void moveRel(double x, double y);
186   void drawText(const char *szMessage);
187   void drawText(const string& rsMessage);
188   void polylineAbs(double x[], double y[], int n);
189   void markerAbs (double x, double y);
190   void markerRel(double x, double y);
191   void pointAbs(double x, double y);
192   void pointRel(double x, double y);
193
194   void eraseWindow ();
195   void setWindow (double xmin, double ymin, double xmax, double ymax);
196   void setViewport (double xmin, double ymin, double xmax, double ymax);
197   void frameViewport();
198
199   void setColor (int icol);
200   void setLineStyle (int style);
201   void setTextSize (double height);
202   void setTextNDCSize (double height);
203   void setTextPointSize (double height);
204   void setTextAngle (double angle);
205   void setTextColor (int iFGcolor, int iBGcolor);
206   void setPenWidth (int width);
207   void setMarker (int idMarker, int color);
208   void setRasterOp (int ro);
209
210   void getWindow (double& xmin, double& ymin, double& xmax, double& ymax);
211   void getViewport (double& xmin, double& ymin, double& xmax, double& ymax);
212   void getTextExtent (const char *szText, double* x, double* y);
213   double getCharHeight ();
214   double getCharWidth ();
215   SGPDriver& getDriver() {return m_driver;}
216   const SGPDriver& getDriver() const {return m_driver;}
217
218   void ctmClear ();
219   void ctmSet (const TransformationMatrix2D& m);
220   void preTranslate (double x, double y);
221   void postTranslate (double x, double y);
222   void preScale (double sx, double sy);
223   void postScale (double sx, double sy);
224   void preRotate (double theta);
225   void postRotate (double theta);
226   void preShear (double shrx, double shry);
227   void postShear (double shrx, double shry);
228   void transformNDCtoMC (double* x, double* y);
229   void transformMCtoNDC (double* x, double* y);
230   void transformMCtoNDC (double xIn, double yIn, double* xOut, double* yOut);
231
232   void stylusNDC (double x, double y, bool beam);
233   void pointNDC (double x, double y);
234   void markerNDC (double x, double y);
235
236 #if HAVE_WXWINDOWS
237   void setDC (wxDC* pDC);
238 #endif
239 };
240
241
242 enum {
243     C_BLACK     = 0,     // color codes 
244     C_BLUE      = 1,
245     C_GREEN     = 2,
246     C_CYAN      = 3,
247     C_RED       = 4,
248     C_MAGENTA   = 5,
249     C_BROWN     = 6,
250     C_GRAY      = 7,
251     C_LTGRAY    = 8,
252     C_LTBLUE    = 9,
253     C_LTGREEN   = 10,
254     C_LTCYAN    = 11,
255     C_LTRED     = 12,
256     C_LTMAGENTA = 13,
257     C_YELLOW    = 14,
258     C_WHITE     = 15,
259 };
260
261 enum RasterOp {
262     RO_AND = 1,
263     RO_AND_INVERT,
264     RO_AND_REVERSE,
265     RO_CLEAR,
266     RO_COPY,
267     RO_EQUIV,
268     RO_INVERT,
269     RO_NAND,
270     RO_NOR,
271     RO_NO_OP,
272     RO_OR,
273     RO_OR_INVERT,
274     RO_OR_REVERSE,
275     RO_SET,
276     RO_SRC_INVERT,
277     RO_XOR,
278 };
279
280
281 class RGBColor {
282  private:
283   short int r;
284   short int g;
285   short int b;
286
287  public:
288   RGBColor (int r, int g, int b)
289     : r(r), g(g), b(b)
290     {}
291
292   int getRed () const
293     { return r; }
294
295   int getGreen () const
296     { return g; }
297
298   int getBlue () const
299     { return b; }
300
301 };
302
303 #endif