r185: *** empty log message ***
[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.15 2000/08/27 20:32:55 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 #endif
40
41 #if HAVE_G2_H
42 extern "C" {
43 #include "g2.h"
44 #include "g2_X11.h"
45 }
46 #endif
47
48 #include <string>
49
50
51 class SGPDriver {
52 private:
53   int m_iPhysicalXSize;
54   int m_iPhysicalYSize;
55   string m_sWindowTitle;
56   int m_idDriver;
57
58 #ifdef HAVE_WXWINDOWS
59   wxDC* m_pDC;
60 #endif
61   int m_idG2;
62
63 public:
64   enum {
65     SGPDRIVER_WXWINDOWS = 1,
66     SGPDRIVER_G2 = 2,
67     SGPDRIVER_OPENGL = 4,
68   };
69
70 #ifdef HAVE_WXWINDOWS
71   SGPDriver (wxDC* pDC, int xsize = 640, int ysize = 480);
72 #endif
73
74   SGPDriver (const char* szWinTitle = "", int xsize = 640, int ysize = 480);
75
76   ~SGPDriver ();
77
78   int getPhysicalXSize () const
79     { return m_iPhysicalXSize; }
80
81   int getPhysicalYSize () const
82     { return m_iPhysicalYSize; }
83
84   const string& getWindowTitle () const
85     { return m_sWindowTitle; }
86
87   bool isWX () const
88     { return (m_idDriver & SGPDRIVER_WXWINDOWS); }
89
90   bool isG2 () const
91     { return (m_idDriver & SGPDRIVER_G2); }
92
93   int idG2 () const
94     { return m_idG2; }
95
96 #ifdef HAVE_WXWINDOWS
97   wxDC* idWX () const
98     { return m_pDC; }
99 #endif
100 };
101
102
103 class RGBColor;
104 class SGP {
105 private:
106   int m_iPhysicalXSize;   // Physical Window size 
107   int m_iPhysicalYSize;
108   const SGPDriver m_driver;
109
110   double xw_min;    // Window extents 
111   double yw_min;
112   double xw_max;
113   double yw_max;
114   double xv_min;    // Viewport extents 
115   double yv_min;
116   double xv_max;
117   double yv_max;
118   double viewNDC[4];   // Viewport array for clip_rect() 
119
120   int m_iCurrentPhysicalX;
121   int m_iCurrentPhysicalY;
122   double m_dCurrentWorldX;
123   double m_dCurrentWorldY;
124   double m_dTextAngle;
125   bool m_bRecalcTransform;
126
127   // Master coordinates are coordinates before CTM transformation
128   // World coordinates are coordinates defined by setWindow()
129   // Normalized device coordinates range from 0. to 1. in both axes
130   TransformationMatrix2D wc_to_ndc;     // World coord to NDC matrix 
131   TransformationMatrix2D mc_to_ndc;     // Master to NDC 
132   TransformationMatrix2D ndc_to_mc;     // NDC to Master
133   TransformationMatrix2D m_ctm;         // Current transfromation matrix 
134   
135   void calc_transform ();
136
137   static RGBColor s_aRGBColor[];
138   static int s_iRGBColorCount;
139
140 #if HAVE_WXWINDOWS
141   wxPen m_pen;
142 #endif
143
144 public:
145   enum {                  // linestyles 
146       LS_NOLINE = 0,
147       LS_SOLID = 0xffff,
148       LS_DASH1 = 0xff00,
149       LS_DASH2 = 0xf0f0,
150       LS_DASH3 = 0xcccc,
151       LS_DASH4 = 0xff3e,
152       LS_DOTTED = 0xaaaa,
153   };
154
155   enum {            // Codes for marker symbols
156       MARK_POINT = 0,   // small dot 
157       MARK_SQUARE = 1,  // empty square 
158       MARK_FSQUARE = 2, // filled square 
159       MARK_DIAMOND = 3, // empty diamond 
160       MARK_FDIAMOND = 4,        // filled diamond 
161       MARK_CROSS =  5,  // cross 
162       MARK_XCROSS = 6,  // x 
163       MARK_CIRCLE = 7,    // open circle 
164       MARK_FCIRCLE = 8, // filled circle 
165       MARK_BSQUARE = 9, // big open square 
166       MARK_BDIAMOND = 10,       // big open diamond 
167   };
168   static const int MARK_COUNT = 11;
169   static const unsigned char MARKER_BITMAP[MARK_COUNT][5];
170
171   SGP (const SGPDriver& driver);
172
173   void drawCircle (const double r);
174   void drawArc (const double r, double start, double stop);
175   void drawRect (double xmin, double ymin, double xmax, double ymax);
176   void lineAbs(double x, double y);
177   void moveAbs(double x, double y);
178   void lineRel(double x, double y);
179   void moveRel(double x, double y);
180   void drawText(const char *szMessage);
181   void drawText(const string& rsMessage);
182   void polylineAbs(double x[], double y[], int n);
183   void markerAbs (double x, double y);
184   void markerRel(double x, double y);
185   void pointAbs(double x, double y);
186   void pointRel(double x, double y);
187
188   void eraseWindow ();
189   void setWindow (double xmin, double ymin, double xmax, double ymax);
190   void setViewport (double xmin, double ymin, double xmax, double ymax);
191   void frameViewport();
192
193   void setColor (int icol);
194   void setLineStyle (int style);
195   void setTextSize (double height);
196   void setTextAngle (double angle);
197   void setTextColor (int iFGcolor, int iBGcolor);
198   void setPenWidth (int width);
199   void setMarker (int idMarker, int color);
200   void setRasterOp (int ro);
201
202   void getTextExtent (const char *szText, double* x, double* y);
203   double getCharHeight ();
204
205   void ctmClear ();
206   void ctmSet (const TransformationMatrix2D& m);
207   void preTranslate (double x, double y);
208   void postTranslate (double x, double y);
209   void preScale (double sx, double sy);
210   void postScale (double sx, double sy);
211   void preRotate (double theta);
212   void postRotate (double theta);
213   void preShear (double shrx, double shry);
214   void postShear (double shrx, double shry);
215   void transformNDCtoMC (double* x, double* y);
216   void transformMCtoNDC (double* x, double* y);
217   void transformMCtoNDC (double xIn, double yIn, double* xOut, double* yOut);
218
219   void stylusNDC (double x, double y, bool beam);
220   void pointNDC (double x, double y);
221   void markerNDC (double x, double y);
222 };
223
224
225 enum {
226     C_BLACK     = 0,     // color codes 
227     C_BLUE      = 1,
228     C_GREEN     = 2,
229     C_CYAN      = 3,
230     C_RED       = 4,
231     C_MAGENTA   = 5,
232     C_BROWN     = 6,
233     C_GRAY      = 7,
234     C_LTGRAY    = 8,
235     C_LTBLUE    = 9,
236     C_LTGREEN   = 10,
237     C_LTCYAN    = 11,
238     C_LTRED     = 12,
239     C_LTMAGENTA = 13,
240     C_YELLOW    = 14,
241     C_WHITE     = 15,
242 };
243
244 enum RasterOp {
245     RO_AND = 1,
246     RO_AND_INVERT,
247     RO_AND_REVERSE,
248     RO_CLEAR,
249     RO_COPY,
250     RO_EQUIV,
251     RO_INVERT,
252     RO_NAND,
253     RO_NOR,
254     RO_NO_OP,
255     RO_OR,
256     RO_OR_INVERT,
257     RO_OR_REVERSE,
258     RO_SET,
259     RO_SRC_INVERT,
260     RO_XOR,
261 };
262
263
264 class RGBColor {
265  private:
266   short int r;
267   short int g;
268   short int b;
269
270  public:
271   RGBColor (int r, int g, int b)
272     : r(r), g(g), b(b)
273     {}
274
275   int getRed () const
276     { return r; }
277
278   int getGreen () const
279     { return g; }
280
281   int getBlue () const
282     { return b; }
283
284 };
285
286 #endif