Repository navigation
Expand file tree
/
Copy pathvger.h
More file actions
257 lines (185 loc) · 8.84 KB
/
Copy pathvger.h
File metadata and controls
257 lines (185 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright © 2021 Audulus LLC.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#ifndef vger_h
#define vger_h
#ifndef __METAL_VERSION__
#include <simd/simd.h>
#endif
/// Text alignment.
enum vgerAlign {
// Horizontal align
VGER_ALIGN_LEFT = 1<<0, // Default, align text horizontally to left.
VGER_ALIGN_CENTER = 1<<1, // Align text horizontally to center.
VGER_ALIGN_RIGHT = 1<<2, // Align text horizontally to right.
// Vertical align
VGER_ALIGN_TOP = 1<<3, // Align text vertically to top.
VGER_ALIGN_MIDDLE = 1<<4, // Align text vertically to middle.
VGER_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom.
VGER_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline.
};
/// Type safety for paint indices.
typedef struct { uint32_t index; } vgerPaintIndex;
/// Type safety for image indices.
typedef struct { uint32_t index; } vgerImageIndex;
#ifndef __METAL_VERSION__
#ifdef __OBJC__
#import <Metal/Metal.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct vger *vgerContext;
#pragma mark - Context
enum vgerCreateFlags {
// Flag indicating if double buffering scheme is used.
VGER_DOUBLE_BUFFER = 1 << 0,
// Flag indicating if triple buffering scheme is used.
VGER_TRIPLE_BUFFER = 1 << 1,
};
#ifdef __OBJC__
/// Create a new state object.
vgerContext vgerNew(uint32_t flags, MTLPixelFormat pixelFormat);
#endif
/// Deallocate state object.
void vgerDelete(vgerContext);
/// Begin rendering a frame.
void vgerBegin(vgerContext, float windowWidth, float windowHeight, float devicePxRatio);
#pragma mark - Textures
/// Load an image from a file.
vgerImageIndex vgerCreateImage(vgerContext, const char* filename);
/// Load an image from data in memory.
vgerImageIndex vgerCreateImageMem(vgerContext, const uint8_t* data, size_t size);
/// Create a texture to sample from.
vgerImageIndex vgerAddTexture(vgerContext, const uint8_t* data, int width, int height);
#ifdef __OBJC__
/// Add a MTLTexture. This references the texture, so you can render to it, etc.
vgerImageIndex vgerAddMTLTexture(vgerContext, id<MTLTexture>);
#endif
/// Remove a texture.
void vgerDeleteTexture(vgerContext, vgerImageIndex texID);
/// Remove all textures.
void vgerDeleteTextures(vgerContext);
/// Get the size of a texture.
vector_int2 vgerTextureSize(vgerContext, vgerImageIndex texID);
#pragma mark - Primitives
void vgerFillCircle(vgerContext, vector_float2 center, float radius, vgerPaintIndex paint);
void vgerStrokeArc(vgerContext, vector_float2 center, float radius, float width, float rotation, float aperture, vgerPaintIndex paint);
void vgerFillRect(vgerContext, vector_float2 min, vector_float2 max, float radius, vgerPaintIndex paint);
void vgerStrokeRect(vgerContext, vector_float2 min, vector_float2 max, float radius, float width, vgerPaintIndex paint);
typedef struct { vector_float2 a, b, c; } vgerBezierSegment;
void vgerStrokeBezier(vgerContext, vgerBezierSegment, float width, vgerPaintIndex paint);
void vgerStrokeSegment(vgerContext, vector_float2 a, vector_float2 b, float width, vgerPaintIndex paint);
void vgerStrokeWire(vgerContext, vector_float2 a, vector_float2 b, float width, vgerPaintIndex paint);
/// Returns the number of primitives sent for rendering.
///
/// This can be useful if you want to impose a limit.
size_t vgerPrimCount(vgerContext);
#pragma mark - Text
/// Render text.
void vgerText(vgerContext, const char* str, vector_float4 color, int align);
/// Return bounds for text in local coordinates.
void vgerTextBounds(vgerContext, const char* str, vector_float2* min, vector_float2* max, int align);
/// Renders multi-line text.
void vgerTextBox(vgerContext, const char* str, float breakRowWidth, vector_float4 color, int align);
/// Returns bounds of multi-line text.
void vgerTextBoxBounds(vgerContext, const char* str, float breakRowWidth, vector_float2* min, vector_float2* max, int align);
#pragma mark - Paths
/// Move the pen to a point.
void vgerMoveTo(vgerContext, vector_float2 pt);
/// Line.
void vgerLineTo(vgerContext, vector_float2 b);
/// Quadratic bezier.
void vgerQuadTo(vgerContext, vector_float2 b, vector_float2 c);
/// Crude approximation of cubic bezier with two quadratics.
void vgerCubicApproxTo(vgerContext vg, vector_float2 b, vector_float2 c, vector_float2 d);
/// Fills the current path (and clears the path).
///
/// Returns false if there were too many primitives to render the fill.
bool vgerFill(vgerContext, vgerPaintIndex paint);
/// Clear path information.
///
/// This is useful for ensuring scripts don't mess up other rendering.
void vgerCancelPath(vgerContext vg);
#pragma mark - Transforms
/// Translates current coordinate system.
void vgerTranslate(vgerContext, vector_float2 t);
/// Scales current coordinate system.
void vgerScale(vgerContext, vector_float2 s);
/// Rotates current coordinate system (radians).
void vgerRotate(vgerContext, float theta);
/// Transforms a point according to the current transformation.
vector_float2 vgerTransform(vgerContext, vector_float2 p);
/// Returns current transformation matrix.
simd_float3x2 vgerCurrentTransform(vgerContext);
/// Pushes and saves the current transform onto a transform stack. A matching vgerRestore must
/// be used to restore the state.
void vgerSave(vgerContext);
/// Pops and restores the current transform.
void vgerRestore(vgerContext);
/// Returns the depth of the transform stack.
size_t vgerStackDepth(vgerContext);
#pragma mark - Layers
/// Sets the number of layers (currently the max is 4). Default is 1.
void vgerSetLayerCount(vgerContext, int layers);
/// Sets the current layer index.
void vgerSetLayer(vgerContext, int layer);
#pragma mark - Encoding
#ifdef __OBJC__
/// Encode drawing commands to a metal command buffer.
void vgerEncode(vgerContext, id<MTLCommandBuffer> buf, MTLRenderPassDescriptor* pass);
/// Encode drawing commands from one layer to a metal command buffer.
void vgerEncodeLayer(vgerContext, id<MTLCommandBuffer> buf, MTLRenderPassDescriptor* pass, int layer);
/// Encode drawing commands to a metal command buffer for the glow pass.
void vgerEncodeGlowPass(vgerContext, id<MTLCommandBuffer> buf, MTLRenderPassDescriptor* pass);
/// For debugging.
id<MTLTexture> vgerGetGlyphAtlas(vgerContext);
/// For debugging.
id<MTLTexture> vgerGetCoarseDebugTexture(vgerContext);
#endif
#pragma mark - Paints
/// Create a paint for a constant color. Returns paint index. Paints are cleared each frame.
vgerPaintIndex vgerColorPaint(vgerContext vg, vector_float4 color);
/// Create a paint for a linear gradient. Returns paint index. Paints are cleared each frame.
vgerPaintIndex vgerLinearGradient(vgerContext vg,
vector_float2 start,
vector_float2 end,
vector_float4 innerColor,
vector_float4 outerColor,
float glow);
/// Create a paint for a linear gradient. Returns paint index. Paints are cleared each frame.
vgerPaintIndex vgerRadialGradient(vgerContext vg,
vector_float2 center,
float innerRadius,
float outerRadius,
vector_float4 innerColor,
vector_float4 outerColor,
float glow);
/// Create a paint using a texture image. Returns paint index. Paints are cleared each frame.
vgerPaintIndex vgerImagePattern(vgerContext vg,
vector_float2 origin,
vector_float2 size,
float angle,
bool flipY,
vgerImageIndex image,
float alpha);
/// Create a paint to render a grid. Reduces the number of primitives required for a big grid.
vgerPaintIndex vgerGrid(vgerContext, vector_float2 offset, vector_float2 size, float width, vector_float4 color);
#ifdef __cplusplus
}
#endif
#endif // __METAL_VERSION__
#endif /* vger_h */