OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / graphic / psychlops_g_canvas.h
1 /*
2  *  psychlops_g_canvas.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2006/01/04 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9 #ifndef HEADER_PSYCHLOPS_GRAPHIC_DISPLAY
10 #define HEADER_PSYCHLOPS_GRAPHIC_DISPLAY
11
12 #include <sstream>
13 #include <vector>
14 #include <list>
15
16 #include "psychlops_g_color.h"
17 #include "psychlops_g_fundamental.h"
18 #include "psychlops_g_shape.h"
19 #include "psychlops_g_font.h"
20
21 #include "../devices/psychlops_io_clock.h"
22 //#include "../ApplicationInterfaces/psychlops_code_snippets.h"
23
24 #include "../devices/psychlops_io_display.h"\r
25 #include "../../extension/standard/widgets/psychlops_widgets_event.h"
26
27 namespace Psychlops {
28
29         class APICanvasProperties;
30         class APIImageProperties;
31         class ImageCache_;
32         class DebugTweakTool;
33         class Canvas : virtual public DrawableWithCache
34         {
35                 friend class APICanvasProperties;
36                 friend class APIImageProperties;
37                 friend class Image;
38                 friend class DebugTweakTool;
39
40         public:
41                 static void setDefaultCanvas(Canvas *cnvs);
42         protected:
43                 static void resetDefaultCanvas(Canvas *cnvs);
44
45         protected:
46                 Color bgcolor_;
47
48                 //      Physical Settings
49                 double DisplayWidthCentimeter_;
50                 double DisplayHeightCentimeter_;
51                 double DisplayDistanceCentimeter_;
52                 //      Display Resolution
53                 int DisplayHorizontalPixelNumber_;
54                 int DisplayVerticalPixelNumber_;
55
56         public:
57                 void setPhysicalSettings(double width, double height, double distance, int horizontalpixs, int varticalpixs);
58                 int horizontalArcdegToPixel(double angle);
59                 int varticalArcdegToPixel(double angle);
60                 int getDisplayHorizontalPixelNumber();
61                 int getDisplayVerticalPixelNumber();
62
63                 void popMatrices();
64                 void pushPixToPixProjection();
65                 void RenderModeSmooth2D();
66                 void RenderModeSmooth3D(double left, double right, double top, double bottom, double near, double far);
67
68         public:
69                 long DISPLAYED_FRAMES, FAILED_FRAMES, TOTAL_REFRESH, LAST_FAILED_FRAMES, FAILED_FLIPS;
70                 enum Error { MEMORY_SHORTAGE, RESET, SINGLETON };
71                 enum CanvasMode { fullscreen, window };
72                 enum TargetSurface { ALL=1, BACK=2, FRONT=4 };\r
73         protected:\r
74                 bool not_time_critical_;\r
75         public:\r
76                 void makeTimeCritical(bool b = true);\r
77                 bool isTimeCritical() const;\r
78
79
80         protected:
81                 bool has_instance_;
82                 int Width, Height, colordepth;
83                 double refreshrate;
84
85                 Clock time_lastFrame, time_lastFrame_before_flip_, time_init_;
86                 int rest_wating_vsyncs_;
87
88                 double frameAccuracy;
89                 bool checkingFPS, checkingFPS_last, displayingFPS;
90                 double frameInterval, frameIntervalAccurate;
91                 long displayedFrames, failedFrames, lastFailedFrames_;
92 //              GLenum default_drawing_mode_;
93                 Rectangle clear_area_;
94
95                 Rectangle progressbarrect;
96                 Color progressbarbordercolor, progressbarfillcolor, progressbaremptycolor, progressbarerrorcolor;
97
98         // Pseudo Properties
99         public:
100                 template <typename X> class READ_ONLY_ {
101                         friend class Canvas;
102                         private:
103                         X val_;
104                         void set(X val) { val_=val; }
105                         public:
106                         READ_ONLY_() {}
107                         operator X() const { return val_; }
108                 };
109                 READ_ONLY_<double> RefreshRate;
110                 class READ_ONLY_POINT_ {
111                         friend class Canvas;
112                         public:
113                         READ_ONLY_<double> x, y;
114                         READ_ONLY_POINT_();
115                         operator Point() const;
116                 };
117                 READ_ONLY_POINT_ center;
118
119         protected:
120                 APICanvasProperties *api;
121                 void (*APIsetColor)(const Color &col);
122                 void (*APIsetColorClear)(const Color &col);
123 /*
124                 static GLuint font_LCD[128];
125                 static GLuint fillOvalPrimitives[4], drawOvalPrimitives[4];
126 */              static int font_LCD_height;
127                 static void initPrimitives();
128                 static void setfontLCD();
129                 static void setfontMinimum();
130                 static void setFillOvalPrimitives();
131                 void loadFontMinimum();
132
133
134                 ////////        Construct and Destruct  ////////
135                 virtual void initProperties();
136                 void initCounters();
137                 virtual void initAPIprop();
138                 virtual void initAPIattributes();
139                 virtual void popAPIattributes();\r
140
141         private:
142                 Canvas(const Canvas&);
143         public:
144                 Canvas();
145                 Canvas(CanvasMode mode, const Display disp = Display::primary);
146                 Canvas(int d_width, int d_height, CanvasMode mode, const Display disp = Display::primary);
147                 Canvas(Rectangle rect, CanvasMode mode = Canvas::window);
148                 Canvas(int d_width, int d_height, int d_colordepth, double d_refreshrate, const Display disp = Display::primary);
149                 ~Canvas(void);
150                 void set(CanvasMode mode, const Display disp = Display::primary);
151                 void set(int d_width, int d_height, CanvasMode mode, const Display disp = Display::primary);
152                 void set(Rectangle rect, CanvasMode mode = Canvas::window);
153                 void set(int d_width, int d_height, int d_colordepth, double d_refreshrate, const Display disp = Display::primary);
154                 void release();
155
156
157
158                 ////////        Critical Operations ////////
159
160                 void flip(const int frame_duration_by_vsyncs = 1);
161                 void flipAndWait(const int frame_duration_by_vsyncs);
162                 void flipAfter(const int vsyncs);
163                 private:
164                 void flip(float N);
165                 void flip(double N);
166                 public:
167                         //      obsolete
168                         void displayFrame(void);
169                         void displayFrameWithWait(int N_vsyncs_after_latest_flip);
170                         private:
171                         void displayFrameWithWait(double N);
172                 public:
173                 Canvas& clear(const Color &col);
174                 Canvas& clear(TargetSurface target = BACK);
175                 Canvas& clear(const Color &col, TargetSurface target);
176                 void setClearColor(Color col);
177                 void setGammaValue(const double gamma_r, const double gamma_g, const double gamma_b);
178                 void setGammaTable(const std::vector<double> &table_r, const std::vector<double> &table_g, const std::vector<double> &table_b);
179 //              void setGammaTable(const Matrix &table_all);
180
181
182
183                 ////////        Drawing Graphical Elements   ////////
184                 protected:
185                 inline void setStrokeState(const Stroke& strk);
186                 public:
187                 virtual Drawable& pix(const double x, const double y, const Color &col);
188                 virtual Canvas& pix(const Point &po, const Color &col);
189                 void pix(int dotsCnt, double x[], double y[], const Color col[]);
190                 void pix(int dotsCnt, double x[], double y[], const Color &col);
191                 void pix(int dotsCnt, const Point po[], const Color col[]);
192                 void line(double x1, double y1, double x2, double y2, const Color &col);
193                 void line(const Point &po1, const Point &po2, const Color &col);
194                 void line(double x1, double y1, double x2, double y2, const Stroke &strk);
195                 void line(const Point &po1, const Point &po2, const Stroke &strk);
196                 virtual Canvas& line(const Line &drawee, const Color &col);
197                 virtual Canvas& line(const Line &drawee, const Stroke &strk);
198                 virtual Canvas& rect(const Rectangle &drawee, const Color &col);
199                 Canvas& rect(const int nRects, const Rectangle *rec, const Color &col);
200                 Canvas& rect(const int nRects, const Rectangle *rec, const Color *col);
201                 Canvas& rect(const unsigned int nRectsBegin, const unsigned int nRectsEnd, const Rectangle *rec, const Color *col);
202                 virtual Canvas& rect(const Rectangle &drawee, const Stroke &strk);
203                 Canvas& rect(const int nRects, const Rectangle *rec, const Stroke &strk);
204                 Canvas& rect(const int nRects, const Rectangle *rec, const Stroke *strk);
205                 Canvas& rect(const unsigned int nRectsBegin, const unsigned int nRectsEnd, const Rectangle *rec, const Stroke *strk);
206                 Canvas& oval(const Ellipse &drawee, const Color &col);
207                 Canvas& oval(const Ellipse &drawee, const Stroke &strk);
208                 Canvas& oval(const Rectangle &rect, const Color &col);
209                 Canvas& oval(const Rectangle &rect, const Stroke &strk);
210                 virtual Canvas& ellipse(const Ellipse &drawee);\r
211                 virtual Canvas& ellipse(const Ellipse &drawee, const Color &col);
212                 virtual Canvas& ellipse(const Ellipse &drawee, const Stroke &strk);
213                 Canvas& ellipse(const Rectangle &rect, const Color &col);
214                 Canvas& ellipse(const Rectangle &rect, const Stroke &strk);
215                 void polygon(Point *vertices, unsigned int nVertices, const Color &col);
216                 void polygon(Point *vertices, unsigned int nVertices, const Stroke &strk);
217                 virtual Canvas& polygon(const Polygon &drawee);
218                 virtual Canvas& polygon(const Polygon &drawee, const Color &col);
219                 virtual Canvas& polygon(const Polygon &drawee, const Stroke &strk);
220                 virtual Canvas& polyline(const PolyLine &drawee);
221                 virtual Canvas& polyline(const PolyLine &drawee, const Color &col);
222                 virtual Canvas& polyline(const PolyLine &drawee, const Stroke &strk);
223                 virtual Canvas& figures(const Group &drawee);
224
225                 Canvas& drawPixel(const double x, const double y, const Color &col);
226                 Canvas& drawPixel(const Point &po, const Color &col);
227                 void drawPixel(int dotsCnt, double x[], double y[], const Color col[]);
228                 void drawPixel(int dotsCnt, double x[], double y[], const Color &col);
229                 void drawPixel(int dotsCnt, const Point po[], const Color col[]);
230                 void drawLine(double x1, double y1, double x2, double y2, const Color &col);
231                 void drawLine(const Point &po1, const Point &po2, const Color &col);
232                 Canvas& fillRect(const Rectangle &rect, const Color &col);
233                 Canvas& fillRect(const int nRects, const Rectangle *rec, const Color &col);
234                 Canvas& fillRect(const int nRects, const Rectangle *rec, const Color *col);
235                 Canvas& fillRect(const unsigned int nRectsBegin, const unsigned int nRectsEnd, const Rectangle *rec, const Color *col);
236                 Canvas& fillOval(const Rectangle &rect, const Color &col);
237                 void drawPolygon(Point *vertices, unsigned int nVertices, const Color &col);
238
239
240
241                 void copy(const Rectangle &source, const Rectangle &target, bool doesdeletesource=false, const Color &delcol=Color::black);
242                 void to(Image &img, const Rectangle &srcrect) const;
243 //              void to(Image &img, const Rectangle &srcrect, const Rectangle &tgtrect) const;
244                 Canvas& image(const Image &img);
245                 Canvas& image(Image &img, const Rectangle &target_area, const Rectangle &source_rect);
246                 Canvas& image(const Image &img, const double left, const double top);
247                         Canvas& image(const Image &img, double alpha);
248                         void drawImage(const Image &img);
249                         void drawImage(const Image &img, const double left, const double top);
250                         void drawImage(Image &img, const Rectangle &target_area);
251                         //void drawImage(const Image &img, Point lt, Point rt, Point rb, Point lb);
252                 virtual void cacheImage(Image &img);
253                 virtual void uncacheImage(Image &img);
254         protected:
255                 virtual void cacheImageBody(Image &img);
256                 virtual void uncacheImageBody(Image &img);
257         public:
258
259
260                 ////////        Utilities   ////////
261
262                 //      FPS Checker
263                 void displayFPS();
264                 void initFPS();
265                 void watchFPS();
266                 void ignoreFPS();
267                 void showFPS();
268                 void hideFPS();
269                 int lastFailedFrames();
270                         //      obsolete
271                         void enableFPSChecker();        //      obsolete
272                         void disableFPSChecker();       //      obsolete
273                         void enableDisplayingFPS();     //      obsolete
274                         void disableDisplayingFPS();    //      obsolete
275
276                 void progressbar(Rectangle &inrect, Color fillcol=Color::blue, Color empcol=Color(0.25,0.25,0.25));
277                 void progressbar(double l, double t, double r, double b, Color fillcol=Color::blue, Color empcol=Color(0.25,0.25,0.25));
278                 void progressbar(double percentage);
279                 template <typename X> void progressbar(X now, X max) { progressbar((double)now/(double)max); }
280
281 //              void var(int number, double x, double y, Color col=Color::white, bool fillForward=false);
282                 void var_(std::string str, double x, double y, const Color &col=Color::white, bool fillForward = false);
283                 template <typename X> void var(X val, double x, double y, const Color &col=Color::white, bool fillForward = false) {
284                         std::stringstream var_buffer_("");
285                         var_buffer_ << val;
286                         std::string str = var_buffer_.str();
287                         var_(str, x, y, col, fillForward);
288                 }
289 //              void msg(const char* string, const double x, const double y, const Color col=Color::white);
290 //              void msg(const std::string &string, const double x, const double y, const Color col=Color::white);
291                 int msg(const char* string, const double x, const double y, const Color &col=Color::white, const int horiz_align=Letters::TEXT_ALIGN_LEFT, const int vertical_align=Letters::BASELINE, double max_width=-1.0);
292                 int msg(const std::string &string, const double x, const double y, const Color &col=Color::white, const int horiz_align=Letters::TEXT_ALIGN_LEFT, const int vertical_align=Letters::BASELINE, const double max_width=-1.0);
293                 template <typename X> void checkVar(const X val, const double x, const double y, const Color &col=Color::white, const bool fillForward=false) {
294                         var(val, x, y, col, fillForward);
295                 }
296 //              void showMessage(const char* string, const double x, const double y, Color col=Color::white);
297 //              void showMessage(const std::string &string, const double x, const double y, Color col=Color::white);
298                 int showMessage(const char* string, const double x, const double y, const Color &col=Color::white, const int align=Letters::TEXT_ALIGN_LEFT, const int vertical_align=Letters::BASELINE, const double max_width=-1.0);
299                 int showMessage(const std::string &string, const double x, const double y, const Color &col=Color::white, const int align=Letters::TEXT_ALIGN_LEFT, const int vertical_align=Letters::BASELINE, const double max_width=-1.0);
300                         //      obsolete
301                         template <typename X> void varcheck(const X val, const double x, const double y, const Color &col=Color::white, const bool fillForward=false) { //      obsolete
302                                 var(val, x, y, col, fillForward);
303                         }
304                         void message(const char* string, const double x, const double y, const Color &col=Color::white);        //      obsolete
305
306                 Canvas& letters(Letters &letters, const Color &col=Color::white);
307                 int msg(Letters &letters, const double x, const double y, const Color &col=Color::white, const int horiz_align=Letters::TEXT_ALIGN_LEFT, const int vertical_align=Letters::BASELINE, const double max_width=-1.0);
308                 int msg(const std::wstring &string, const double x, const double y, const Color &col=Color::white, const int horiz_align=Letters::TEXT_ALIGN_LEFT, const int vertical_align=Letters::BASELINE, const double max_width=-1.0);
309                 void cacheLetters(Letters &letters);
310                 void uncacheLetters(Letters &letters);
311
312                 protected:
313                 std::list<Letters*> letter_cache;
314                 typedef std::map<Image*, ImageCache_*> ImageCache;
315                 mutable ImageCache img_caches;
316                 typedef std::map<Letters*, Letters::Cache*> LettersCache;
317                 mutable LettersCache let_caches;
318
319                 public:
320                 std::vector<Figure *> internal_billboard;
321                 public:
322                 std::vector<Figure *> billboard;\r
323 \r
324                 public:\r
325                 Events::RootContainer eventroot;\r
326                 virtual void getBubble(const char* ev);
327 \r
328 \r
329                 void shift(int x, int y);
330
331
332                 ////////        Properties      ////////
333
334                 virtual int getWidth() const;
335                 virtual int getHeight() const;
336                 int getColorDepth() const;
337                 double getRefreshRate() const;
338                 double getHcenter() const;
339                 double getVcenter() const;
340                 virtual const Point getCenter() const;
341                 Point mouse() const;
342                 Point mouse(const Point &pp) const;
343
344
345
346
347                 ////////////////        Special Utility ////////////////
348                 struct Geometry {
349                         double dist_cm, horiz_cm, vert_cm, horiz_pix, vert_pix, horiz_pix_per_cm, vert_pix_per_cm;
350                         Geometry();
351                         Geometry(double dist, double horiz, double vert, Canvas &cnvs);
352                         const double pixels(double visual_angle);
353                 };
354
355         };
356 #ifdef __GNUC__
357 extern template void Canvas::var<int>(int val, double x, double y, const Color &col, bool fillForward);
358 extern template void Canvas::var<double>(double val, double x, double y, const Color &col, bool fillForward);
359 #endif
360
361
362
363 }       /*      <- namespace Psycholops         */
364
365
366 #endif