| 1 | // This file is part of SmallBASIC |
| 2 | // |
| 3 | // Copyright(C) 2001-2019 Chris Warren-Smith. |
| 4 | // |
| 5 | // This program is distributed under the terms of the GPL v2.0 or later |
| 6 | // Download the GNU Public License (GPL) from www.gnu.org |
| 7 | // |
| 8 | |
| 9 | #ifndef DISPLAY_H |
| 10 | #define DISPLAY_H |
| 11 | |
| 12 | #include <FL/fl_draw.H> |
| 13 | #include <FL/Fl_Rect.H> |
| 14 | #include <FL/Fl_Image_Surface.H> |
| 15 | #include "lib/maapi.h" |
| 16 | |
| 17 | struct Font { |
| 18 | Font(Fl_Font font, Fl_Fontsize size) : _font(font), _size(size) {} |
| 19 | virtual ~Font() {} |
| 20 | void setCurrent() { fl_font(_font, _size); } |
| 21 | |
| 22 | private: |
| 23 | Fl_Font _font; |
| 24 | Fl_Fontsize _size; |
| 25 | }; |
| 26 | |
| 27 | struct Canvas { |
| 28 | Canvas(); |
| 29 | virtual ~Canvas(); |
| 30 | |
| 31 | bool create(int w, int h); |
| 32 | void drawArc(int xc, int yc, double r, double start, double end, double aspect); |
| 33 | void drawEllipse(int xc, int yc, int rx, int ry, bool fill); |
| 34 | void drawLine(int startX, int startY, int endX, int endY); |
| 35 | void drawPixel(int posX, int posY); |
| 36 | void drawRGB(const MAPoint2d *dstPoint, const void *src, const MARect *srcRect, int opacity, int bytesPerLine); |
| 37 | void drawRegion(Canvas *src, const MARect *srcRect, int dstx, int dsty); |
| 38 | void drawText(Font *font, int left, int top, const char *str, int len); |
| 39 | void fillRect(int x, int y, int w, int h, Fl_Color color); |
| 40 | Fl_Color getDrawColor() { return _drawColor; } |
| 41 | void getImageData(uint8_t *image, const MARect *srcRect, int bytesPerLine); |
| 42 | void setClip(int x, int y, int w, int h); |
| 43 | void setColor(Fl_Color color) { _drawColor = color; } |
| 44 | |
| 45 | int x() { return _clip ? _clip->x() : 0; } |
| 46 | int y() { return _clip ? _clip->y() : 0; } |
| 47 | int w() { return _clip ? _clip->w() : _w; } |
| 48 | int h() { return _clip ? _clip->h() : _h; } |
| 49 | |
| 50 | int _w; |
| 51 | int _h; |
| 52 | float _scale; |
| 53 | Fl_Offscreen _offscreen; |
| 54 | Fl_Rect *_clip; |
| 55 | Fl_Color _drawColor; |
| 56 | }; |
| 57 | |
| 58 | class GraphicsWidget : public Fl_Widget { |
| 59 | public: |
| 60 | GraphicsWidget(int x, int y, int w, int h); |
| 61 | virtual ~GraphicsWidget(); |
| 62 | |
| 63 | void deleteFont(Font *font); |
| 64 | void drawText(int left, int top, const char *str, int len); |
| 65 | int getHeight() { return _screen->_h; } |
| 66 | int getWidth() { return _screen->_w; } |
| 67 | Canvas *getDrawTarget() { return _drawTarget; } |
| 68 | Canvas *getScreen() { return _screen; } |
| 69 | MAExtent getTextSize(const char *str); |
| 70 | void layout(); |
| 71 | void resize(int x, int y, int w, int h); |
| 72 | void setColor(Fl_Color color); |
| 73 | MAHandle setDrawTarget(MAHandle maHandle); |
| 74 | void setFont(Font *font) { _font = font; } |
| 75 | |
| 76 | private: |
| 77 | void draw(); |
| 78 | |
| 79 | Canvas *_screen; |
| 80 | Canvas *_drawTarget; |
| 81 | Font *_font; |
| 82 | int _textOffset; |
| 83 | }; |
| 84 | |
| 85 | #endif |
| 86 | |