3 * Psychlops Standard Library (Universal)
5 * Last Modified 2006/01/04 by Kenchi HOSOKAWA
6 * (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
9 #ifndef HEADER_PSYCHLOPS_GRAPHIC_DISPLAY
10 #define HEADER_PSYCHLOPS_GRAPHIC_DISPLAY
16 #include "psychlops_g_color.h"
17 #include "psychlops_g_fundamental.h"
18 #include "psychlops_g_shape.h"
19 #include "psychlops_g_font.h"
21 #include "../devices/psychlops_io_clock.h"
22 //#include "../ApplicationInterfaces/psychlops_code_snippets.h"
24 #include "../devices/psychlops_io_display.h"
\r
25 #include "../../extension/standard/widgets/psychlops_widgets_event.h"
29 class APICanvasProperties;
30 class APIImageProperties;
33 class Canvas : virtual public DrawableWithCache
35 friend class APICanvasProperties;
36 friend class APIImageProperties;
38 friend class DebugTweakTool;
41 static void setDefaultCanvas(Canvas *cnvs);
43 static void resetDefaultCanvas(Canvas *cnvs);
49 double DisplayWidthCentimeter_;
50 double DisplayHeightCentimeter_;
51 double DisplayDistanceCentimeter_;
53 int DisplayHorizontalPixelNumber_;
54 int DisplayVerticalPixelNumber_;
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();
64 void pushPixToPixProjection();
65 void RenderModeSmooth2D();
66 void RenderModeSmooth3D(double left, double right, double top, double bottom, double near, double far);
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
74 bool not_time_critical_;
\r
76 void makeTimeCritical(bool b = true);
\r
77 bool isTimeCritical() const;
\r
82 int Width, Height, colordepth;
85 Clock time_lastFrame, time_lastFrame_before_flip_, time_init_;
86 int rest_wating_vsyncs_;
89 bool checkingFPS, checkingFPS_last, displayingFPS;
90 double frameInterval, frameIntervalAccurate;
91 long displayedFrames, failedFrames, lastFailedFrames_;
92 // GLenum default_drawing_mode_;
93 Rectangle clear_area_;
95 Rectangle progressbarrect;
96 Color progressbarbordercolor, progressbarfillcolor, progressbaremptycolor, progressbarerrorcolor;
100 template <typename X> class READ_ONLY_ {
104 void set(X val) { val_=val; }
107 operator X() const { return val_; }
109 READ_ONLY_<double> RefreshRate;
110 class READ_ONLY_POINT_ {
113 READ_ONLY_<double> x, y;
115 operator Point() const;
117 READ_ONLY_POINT_ center;
120 APICanvasProperties *api;
121 void (*APIsetColor)(const Color &col);
122 void (*APIsetColorClear)(const Color &col);
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();
134 //////// Construct and Destruct ////////
135 virtual void initProperties();
137 virtual void initAPIprop();
138 virtual void initAPIattributes();
139 virtual void popAPIattributes();
\r
142 Canvas(const 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);
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);
158 //////// Critical Operations ////////
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);
168 void displayFrame(void);
169 void displayFrameWithWait(int N_vsyncs_after_latest_flip);
171 void displayFrameWithWait(double N);
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);
183 //////// Drawing Graphical Elements ////////
185 inline void setStrokeState(const Stroke& strk);
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);
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);
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);
255 virtual void cacheImageBody(Image &img);
256 virtual void uncacheImageBody(Image &img);
260 //////// Utilities ////////
269 int lastFailedFrames();
271 void enableFPSChecker(); // obsolete
272 void disableFPSChecker(); // obsolete
273 void enableDisplayingFPS(); // obsolete
274 void disableDisplayingFPS(); // obsolete
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); }
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_("");
286 std::string str = var_buffer_.str();
287 var_(str, x, y, col, fillForward);
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);
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);
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);
304 void message(const char* string, const double x, const double y, const Color &col=Color::white); // obsolete
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);
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;
320 std::vector<Figure *> internal_billboard;
322 std::vector<Figure *> billboard;
\r
325 Events::RootContainer eventroot;
\r
326 virtual void getBubble(const char* ev);
329 void shift(int x, int y);
332 //////// Properties ////////
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;
342 Point mouse(const Point &pp) const;
347 //////////////// Special Utility ////////////////
349 double dist_cm, horiz_cm, vert_cm, horiz_pix, vert_pix, horiz_pix_per_cm, vert_pix_per_cm;
351 Geometry(double dist, double horiz, double vert, Canvas &cnvs);
352 const double pixels(double visual_angle);
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);
363 } /* <- namespace Psycholops */