liballofw
graphics.h
Go to the documentation of this file.
1 #ifndef ALLOFW_GRAPHICS_H
2 #define ALLOFW_GRAPHICS_H
3 
4 #include "common.h"
5 #include "stream.h"
6 #include "math/math.h"
7 
8 namespace allofw {
9 
10  // Graphics API based on Skia's API.
11 
12  class Path2D;
13  class Paint2D;
14  class Surface2D;
15  class GraphicalBackend;
16 
17  enum class LineCap : short {
18  BUTT = 0,
19  ROUND = 1,
20  SQUARE = 2
21  };
22 
23  enum class LineJoin : short {
24  BEVEL = 0,
25  MITER = 1,
26  ROUND = 2
27  };
28 
29  enum class FontStyle : short {
30  NORMAL = 0,
31  BOLD = 1,
32  ITALIC = 2,
33  BOLDITALIC = 3
34  };
35 
36  enum class TextAlign : short {
37  LEFT = 0,
38  CENTER = 1,
39  RIGHT = 2
40  };
41 
42  enum class PaintMode : short {
43  STROKE = 0,
44  FILL = 1,
45  STROKEFILL = 2
46  };
47 
48  enum class TransferMode : short {
49  SRC_OVER = 0, DST_OVER = 1,
50  SRC = 2, DST = 3,
51  SRC_IN = 4, DST_IN = 5,
52  SRC_OUT = 6, DST_OUT = 7,
53  SRC_ATOP = 8, DST_ATOP = 9,
54  XOR = 10, PLUS = 11, CLEAR = 12
55  };
56 
57  // Abstract class for 2D graphical contexts.
59  public:
60 
61  // The initial origin is defined at the center of the canvas.
62  // X: left to right, Y: bottom to top.
63 
64  struct State {
66  };
67 
68  // Begin a path, delete the path after use.
69  virtual Path2D* path() = 0;
70  virtual Paint2D* paint() = 0;
71 
72  virtual void destroyPath(Path2D* path) = 0;
73  virtual void destroyPaint(Paint2D* paint) = 0;
74 
75  // Draw a path.
76  virtual void drawPath(Path2D* path, Paint2D* paint) = 0;
77  // Draw text.
78  virtual void drawText(const char* text, double x, double y, Paint2D* paint) = 0;
79  // Draw line.
80  virtual void drawLine(double x1, double y1, double x2, double y2, Paint2D* paint) = 0;
81  // Draw line.
82  virtual void drawCircle(double x, double y, double radius, Paint2D* paint) = 0;
83 
84  virtual void drawRectangle(const Rectangle2d& rect, Paint2D* paint) = 0;
85 
86  // Simple drawSurface
87  virtual void drawSurface(Surface2D* surface, double x, double y, Paint2D* paint) = 0;
88  virtual void drawSurface(Surface2D* surface, const Rectangle2d& src, const Rectangle2d& dest, Paint2D* paint) = 0;
89 
90  virtual void rotate(double radius) = 0;
91  virtual void translate(double tx, double ty) = 0;
92  virtual void scale(double tx, double ty) = 0;
93  virtual void concatTransform(const Matrix3d& matrix) = 0;
94  virtual void setTransform(const Matrix3d& matrix) = 0;
95  virtual Matrix3d getTransform() const = 0;
96 
97  // Fill the entire canvas with color.
98  virtual void clear(const Color& color) = 0;
99 
100  // Reset the graphical state.
101  virtual void reset() = 0;
102 
103  // Process pending operations.
104  virtual void flush() = 0;
105 
106  // Save/load the current graphical state.
107  virtual State getState() const = 0;
108  virtual void setState(const State& state) = 0;
109 
110  // Push and pop the graphical state.
111  virtual void save() = 0;
112  virtual void restore() = 0;
113 
114  protected:
115  virtual ~GraphicalContext2D();
116  };
117 
119  };
120 
121  // This can be used for both 2D and 3D contexts,
122  // for 2D drawing, only x, y components will be considered.
123  class Path2D {
124  public:
125 
126  // Like most graphical contexts, we can construct a path with commands:
127  // Move the pen position to p.
128  virtual void moveTo(double x, double y) = 0;
129  // Add a line from the pen position to p, and move the pen to p.
130  virtual void lineTo(double x, double y) = 0;
131  // Add a bezier curve, with four points: the pen, c1, c2, p, and move the pen to p.
132  virtual void bezierCurveTo(double c1x, double c1y, double c2x, double c2y, double x, double y) = 0;
133  // Draw circle centered at center, with normal, and radius.
134  virtual void circle(double x, double y, double radius) = 0;
135 
136  // Draw 2D arc centered at center, radius, and angle1 to angle2.
137  virtual void arc(double x, double y, double radius, double angle1, double angle2) = 0;
138 
139  // Close the current contour.
140  virtual void close() = 0;
141 
142  protected:
143  virtual ~Path2D();
144  };
145 
146  class Paint2D {
147  public:
148 
149  virtual void setMode(const PaintMode& mode) = 0;
150 
151  // Set stroke/fill styles.
152  virtual void setColor(const Color& color) = 0;
153  virtual void setStrokeWidth(double value) = 0;
154  virtual void setLineCap(LineCap value) = 0;
155  virtual void setLineJoin(LineJoin value) = 0;
156 
157  // Font styles.
158  virtual void setTextSize(double value) = 0;
159  virtual void setTextAlign(TextAlign align) = 0;
160  virtual void setTypeface(const char* name, FontStyle style = FontStyle::NORMAL) = 0;
161 
162  // Measure text width.
163  virtual double measureText(const char* text) = 0;
164 
165  // 5x4 matrix
166  virtual void setColorMatrix(double matrix[20]) = 0;
167  virtual void setColorMatrixScaleAlpha(double a) = 0;
168  virtual void setColorMatrixScale(double r, double g, double b, double a) = 0;
169  virtual void setTransferMode(TransferMode mode) = 0;
170 
171  virtual Paint2D* clone() = 0;
172 
173  protected:
174  virtual ~Paint2D();
175  };
176 
177  // 2D surface.
178  class Surface2D {
179  public:
180 
181  // Width, height, stride.
182  virtual int width() const = 0;
183  virtual int height() const = 0;
184  virtual const void* pixels() const = 0;
185 
186  // Create a OpenGL texture for the surface.
187  // Important: We are using premultiplied alpha in the textures.
188  // Please use glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) for the texture be correctly blended.
189  virtual void bindTexture(unsigned int unit) = 0;
190  virtual void uploadTexture() = 0;
191  virtual void unbindTexture(unsigned int unit) = 0;
192 
193  virtual void save(ByteStream* stream) = 0;
194 
195  protected:
196  virtual ~Surface2D();
197  };
198 
200  public:
201 
202  virtual int width() const = 0;
203  virtual int height() const = 0;
204 
205  virtual bool nextFrame() = 0;
206 
207  virtual void seek(double time) = 0;
208  virtual double fps() = 0;
209  virtual double duration() = 0;
210 
211  // Write to the givin pixel buffer.
212  virtual void setPixelBuffer(void* buffer) = 0;
213 
214  virtual const void* pixels() const = 0;
215 
216  protected:
217  virtual ~VideoSurface2D();
218  };
219 
221  public:
222 
223  // Create new 2D surface. We use RGBA8888, Premultiplied alpha format.
224  virtual Surface2D* createSurface2D(int width, int height) = 0;
225  // "Use" the pixels, not copy them.
226  virtual Surface2D* createSurface2DWithPixels(int width, int height, void* pixels) = 0;
227  // Render to PDF.
228  virtual Surface2D* createPDFSurface2D(int width, int height) = 0;
229 
230  // Load image from stream or buffer.
231  virtual Surface2D* createSurface2DFromImage(ByteStream* stream) = 0;
232  virtual Surface2D* createSurface2DFromImage(const void* data, size_t length) = 0;
233 
234  // Load video from stream or file.
235  virtual VideoSurface2D* createVideoSurface2DFromStream(ByteStream* stream) = 0;
236  virtual VideoSurface2D* createVideoSurface2DFromFile(const char* path) = 0;
237 
238  // Create 2D graphical context for a surface.
239  virtual GraphicalContext2D* createGraphicalContext2D(Surface2D* surface) = 0;
240 
241  virtual void destroySurface2D(Surface2D* surface) = 0;
242  virtual void destroyVideoSurface2D(VideoSurface2D* video) = 0;
243  virtual void destroyGraphicalContext2D(GraphicalContext2D* context) = 0;
244 
245  // Create different backends.
246  static GraphicalBackend* CreateSkia();
247  // Cairo binding is not unavailable yet.
248  // static GraphicalBackend* CreateCairo();
249  static void Destroy(GraphicalBackend* backend);
250 
251  protected:
252  virtual ~GraphicalBackend();
253  };
254 }
255 
256 #endif
Definition: stream.h:8
Matrix3d transform
Definition: graphics.h:65
Definition: graphics.h:220
Color struct.
Definition: color.h:9
Definition: allofw.h:12
PaintMode
Definition: graphics.h:42
TransferMode
Definition: graphics.h:48
TextAlign
Definition: graphics.h:36
Definition: graphics.h:118
Definition: graphics.h:58
LineCap
Definition: graphics.h:17
Definition: geometry.h:16
Definition: graphics.h:178
Definition: graphics.h:199
Definition: graphics.h:64
Definition: graphics.h:123
FontStyle
Definition: graphics.h:29
LineJoin
Definition: graphics.h:23
Definition: graphics.h:146