Updated NanoVG and Blendish.

This commit is contained in:
Branimir Karadžić 2014-11-16 18:59:17 -08:00
parent 24df14f9f3
commit ff44d73480
11 changed files with 6201 additions and 5684 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
// stb_truetype.h - v0.6c - public domain
// authored from 2009-2012 by Sean Barrett / RAD Game Tools
// stb_truetype.h - v0.8 - public domain
// authored from 2009-2013 by Sean Barrett / RAD Game Tools
//
// This library processes TrueType files:
// parse files
@ -27,9 +27,15 @@
// stoiko (Haemimont Games)
// Brian Hook
// Walter van Niftrik
// David Gow
// David Given
// Ivan-Assen Ivanov
// Anthony Pesch
//
// VERSION HISTORY
//
// 0.8 (2014-05-25) fix a few more warnings
// 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back
// 0.6c (2012-07-24) improve documentation
// 0.6b (2012-07-20) fix a few more warnings
// 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels,
@ -222,7 +228,7 @@ void my_stbtt_print(float x, float y, char *text)
while (*text) {
if (*text >= 32 && *text < 128) {
stbtt_aligned_quad q;
stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl,0=old d3d
stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9
glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);
glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);
glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);
@ -291,7 +297,7 @@ int main(int arg, char **argv)
{
stbtt_fontinfo font;
int i,j,ascent,baseline,ch=0;
float scale, xpos=0;
float scale, xpos=2; // leave a little padding in case the character extends left
char *text = "Heljo World!";
fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb"));
@ -363,10 +369,15 @@ int main(int arg, char **argv)
#define STBTT_iceil(x) ((int) ceil(x))
#endif
#ifndef STBTT_sqrt
#include <math.h>
#define STBTT_sqrt(x) sqrt(x)
#endif
// #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h
#ifndef STBTT_malloc
#include <malloc.h>
#define STBTT_malloc(x,u) malloc(x)
#include <stdlib.h>
#define STBTT_malloc(x,u) ((void)(u),malloc(x))
#define STBTT_free(x,u) free(x)
#endif
@ -531,7 +542,6 @@ extern void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint
extern int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);
// an additional amount to add to the 'advance' value between ch1 and ch2
// @TODO; for now always returns 0!
extern int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);
// Gets the bounding box of the visible part of the glyph, in unscaled coordinates
@ -573,6 +583,13 @@ extern int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codep
extern int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);
// returns # of vertices and fills *vertices with the pointer to them
// these are expressed in "unscaled" coordinates
//
// The shape is a series of countours. Each one starts with
// a STBTT_moveto, then consists of a series of mixed
// STBTT_lineto and STBTT_curveto segments. A lineto
// draws a line from previous endpoint to its x,y; a curveto
// draws a quadratic bezier from previous endpoint to
// its x,y, using cx,cy as the bezier control point.
extern void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);
// frees the data allocated above
@ -916,8 +933,6 @@ int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)
search -= 2;
while (entrySelector) {
searchRange >>= 1;
start = ttUSHORT(data + search + 2 + segcount*2 + 2);
end = ttUSHORT(data + search + 2);
start = ttUSHORT(data + search + searchRange*2 + segcount*2 + 2);
end = ttUSHORT(data + search + searchRange*2);
if (unicode_codepoint > end)
@ -1222,8 +1237,8 @@ int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_verte
}
// Find transformation scales.
m = (float) sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
n = (float) sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
// Get indexed glyph.
comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
@ -1246,8 +1261,8 @@ int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_verte
if (comp_verts) STBTT_free(comp_verts, info->userdata);
return 0;
}
if (num_vertices > 0) memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex));
memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex));
if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex));
STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex));
if (vertices) STBTT_free(vertices, info->userdata);
vertices = tmp;
STBTT_free(comp_verts, info->userdata);
@ -1759,7 +1774,7 @@ unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float sc
scale_y = scale_x;
}
stbtt_GetGlyphBitmapBox(info, glyph, scale_x, scale_y, &ix0,&iy0,&ix1,&iy1);
stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);
// now we get the size
gbm.w = (ix1 - ix0);
@ -1991,14 +2006,16 @@ static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name,
// is this a Unicode encoding?
if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
stbtt_int32 slen = ttUSHORT(fc+loc+8), off = ttUSHORT(fc+loc+10);
stbtt_int32 slen = ttUSHORT(fc+loc+8);
stbtt_int32 off = ttUSHORT(fc+loc+10);
// check if there's a prefix match
stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
if (matchlen >= 0) {
// check for target_id+1 immediately following, with same encoding & language
if (i+1 < count && ttUSHORT(fc+loc+12+6) == next_id && ttUSHORT(fc+loc+12) == platform && ttUSHORT(fc+loc+12+2) == encoding && ttUSHORT(fc+loc+12+4) == language) {
stbtt_int32 slen = ttUSHORT(fc+loc+12+8), off = ttUSHORT(fc+loc+12+10);
slen = ttUSHORT(fc+loc+12+8);
off = ttUSHORT(fc+loc+12+10);
if (slen == 0) {
if (matchlen == nlen)
return 1;

View file

@ -25,12 +25,6 @@ THE SOFTWARE.
#ifndef BLENDISH_H
#define BLENDISH_H
#if BX_COMPILER_MSVC
# pragma warning(push)
# pragma warning(disable: 4305) // warning C4305: 'initializing' : truncation from 'double' to 'float'
# pragma warning(disable: 4244) // warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data
#endif // BX_COMPILER_MSVC
#ifndef NANOVG_H
#error "nanovg.h must be included first."
#endif
@ -808,6 +802,13 @@ BND_EXPORT void bndRadioButton(NVGcontext *ctx,
float x, float y, float w, float h, int flags, BNDwidgetState state,
int iconid, const char *label);
// Calculate the corresponding text position for given coordinates px/py
// in a text field.
// See bndTextField for more info.
BND_EXPORT int bndTextFieldTextPosition(NVGcontext *ctx, float x, float y, float w, float h,
int iconid, const char *text, int px, int py);
// Draw a text field with its lower left origin at (x,y) and size of (w,h),
// where flags is one or multiple flags from BNDcornerFlags and state denotes
// the widgets current UI state.
@ -916,6 +917,11 @@ BND_EXPORT void bndNodePort(NVGcontext *ctx, float x, float y, BNDwidgetState st
BND_EXPORT void bndNodeWire(NVGcontext *ctx, float x0, float y0, float x1, float y1,
BNDwidgetState state0, BNDwidgetState state1);
// Draw a node wire originating at (x0,y0) and floating to (x1,y1), with
// a colored gradient based on the two colors color0 and color1
BND_EXPORT void bndColoredNodeWire(NVGcontext *ctx, float x0, float y0, float x1, float y1,
NVGcolor color0, NVGcolor color1);
// Draw a node background with its upper left origin at (x,y) and size of (w,h)
// where titleColor provides the base color for the title bar
BND_EXPORT void bndNodeBackground(NVGcontext *ctx, float x, float y, float w, float h,
@ -1046,6 +1052,12 @@ BND_EXPORT void bndNodeIconLabel(NVGcontext *ctx, float x, float y, float w, flo
int iconid, NVGcolor color, NVGcolor shadowColor, int align,
float fontsize, const char *label);
// Calculate the corresponding text position for given coordinates px/py
// in an iconLabel.
// See bndIconLabelCaret for more info.
BND_EXPORT int bndIconLabelTextPosition(NVGcontext *ctx, float x, float y, float w, float h,
int iconid, float fontsize, const char *label, int px, int py);
// Draw an optional icon specified by <iconid>, an optional label and
// a caret with given fontsize and color within a widget box.
// if iconid is >= 0, an icon will be drawn and the labels remaining space
@ -1095,6 +1107,8 @@ BND_EXPORT NVGcolor bndNodeWireColor(const BNDnodeTheme *theme, BNDwidgetState s
#ifdef _MSC_VER
#pragma warning (disable: 4996) // Switch off security warnings
#pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
#pragma warning (disable: 4244)
#pragma warning (disable: 4305)
#ifdef __cplusplus
#define BND_INLINE inline
#else
@ -1207,6 +1221,9 @@ inline double bnd_fmax ( double a, double b )
// max glyphs for position testing
#define BND_MAX_GLYPHS 1024
// max rows for position testing
#define BND_MAX_ROWS 32
// text distance from bottom
#define BND_TEXT_PAD_DOWN 7
@ -1444,6 +1461,12 @@ void bndRadioButton(NVGcontext *ctx,
BND_LABEL_FONT_SIZE, label, NULL);
}
int bndTextFieldTextPosition(NVGcontext *ctx, float x, float y, float w, float h,
int iconid, const char *text, int px, int py) {
return bndIconLabelTextPosition(ctx, x, y, w, h,
iconid, BND_LABEL_FONT_SIZE, text, px, py);
}
void bndTextField(NVGcontext *ctx,
float x, float y, float w, float h, int flags, BNDwidgetState state,
int iconid, const char *text, int cbegin, int cend) {
@ -1678,9 +1701,10 @@ void bndNodePort(NVGcontext *ctx, float x, float y, BNDwidgetState state,
nvgFill(ctx);
}
void bndNodeWire(NVGcontext *ctx, float x0, float y0, float x1, float y1,
BNDwidgetState state0, BNDwidgetState state1) {
float delta = fabsf(x1 - x0)*(float)bnd_theme.nodeTheme.noodleCurving/10.0f;
void bndColoredNodeWire(NVGcontext *ctx, float x0, float y0, float x1, float y1,
NVGcolor color0, NVGcolor color1) {
float length = bnd_fmaxf(fabsf(x1 - x0),fabsf(y1 - y0));
float delta = length*(float)bnd_theme.nodeTheme.noodleCurving/10.0f;
nvgBeginPath(ctx);
nvgMoveTo(ctx, x0, y0);
@ -1688,17 +1712,26 @@ void bndNodeWire(NVGcontext *ctx, float x0, float y0, float x1, float y1,
x0 + delta, y0,
x1 - delta, y1,
x1, y1);
nvgStrokeColor(ctx, bnd_theme.nodeTheme.wiresColor);
NVGcolor colorw = bnd_theme.nodeTheme.wiresColor;
colorw.a = (color0.a<color1.a)?color0.a:color1.a;
nvgStrokeColor(ctx, colorw);
nvgStrokeWidth(ctx, BND_NODE_WIRE_OUTLINE_WIDTH);
nvgStroke(ctx);
nvgStrokePaint(ctx, nvgLinearGradient(ctx,
x0, y0, x1, y1,
bndNodeWireColor(&bnd_theme.nodeTheme, state0),
bndNodeWireColor(&bnd_theme.nodeTheme, state1)));
color0,
color1));
nvgStrokeWidth(ctx,BND_NODE_WIRE_WIDTH);
nvgStroke(ctx);
}
void bndNodeWire(NVGcontext *ctx, float x0, float y0, float x1, float y1,
BNDwidgetState state0, BNDwidgetState state1) {
bndColoredNodeWire(ctx, x0, y0, x1, y1,
bndNodeWireColor(&bnd_theme.nodeTheme, state0),
bndNodeWireColor(&bnd_theme.nodeTheme, state1));
}
void bndNodeBackground(NVGcontext *ctx, float x, float y, float w, float h,
BNDwidgetState state, int iconid, const char *label, NVGcolor titleColor) {
bndInnerBox(ctx,x,y,w,BND_NODE_TITLE_HEIGHT+2,
@ -1735,9 +1768,11 @@ void bndNodeBackground(NVGcontext *ctx, float x, float y, float w, float h,
bndOutlineBox(ctx,x,y,w,h+1,
BND_NODE_RADIUS,BND_NODE_RADIUS,BND_NODE_RADIUS,BND_NODE_RADIUS,
bndTransparent(borderColor));
/*
bndNodeArrowDown(ctx,
x + BND_NODE_MARGIN_SIDE, y + BND_NODE_TITLE_HEIGHT-4,
BND_NODE_ARROW_SIZE, arrowColor);
*/
bndDropShadow(ctx,x,y,w,h,BND_NODE_RADIUS,
BND_SHADOW_FEATHER,BND_SHADOW_ALPHA);
}
@ -2118,7 +2153,7 @@ void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
+ nvgTextBounds(ctx, 1, 1, value, NULL, NULL);
x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
}
y += h-BND_TEXT_PAD_DOWN;
y += BND_WIDGET_HEIGHT-BND_TEXT_PAD_DOWN;
nvgText(ctx, x, y, label, NULL);
x += label_width;
nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL);
@ -2128,7 +2163,7 @@ void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
nvgTextAlign(ctx,
(align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE):
(NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE));
nvgTextBox(ctx,x+pleft,y+h-BND_TEXT_PAD_DOWN,
nvgTextBox(ctx,x+pleft,y+BND_WIDGET_HEIGHT-BND_TEXT_PAD_DOWN,
w-BND_PAD_RIGHT-pleft,label, NULL);
}
} else if (iconid >= 0) {
@ -2138,7 +2173,7 @@ void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h,
void bndNodeIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
int iconid, NVGcolor color, NVGcolor shadowColor,
int /*align*/, float fontsize, const char *label) {
int align, float fontsize, const char *label) {
if (label && (bnd_font >= 0)) {
nvgFontFaceId(ctx, bnd_font);
nvgFontSize(ctx, fontsize);
@ -2158,10 +2193,70 @@ void bndNodeIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
}
}
void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float /*h*/,
int bndIconLabelTextPosition(NVGcontext *ctx, float x, float y, float w, float h,
int iconid, float fontsize, const char *label, int px, int py) {
float bounds[4];
float pleft = BND_TEXT_RADIUS;
if (!label) return -1;
if (iconid >= 0)
pleft += BND_ICON_SHEET_RES;
if (bnd_font < 0) return -1;
x += pleft;
y += BND_WIDGET_HEIGHT - BND_TEXT_PAD_DOWN;
nvgFontFaceId(ctx, bnd_font);
nvgFontSize(ctx, fontsize);
nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_BASELINE);
w -= BND_TEXT_RADIUS + pleft;
float asc, desc, lh;
static NVGtextRow rows[BND_MAX_ROWS];
int nrows = nvgTextBreakLines(
ctx, label, NULL, w, rows, BND_MAX_ROWS);
if (nrows == 0) return 0;
nvgTextBoxBounds(ctx, x, y, w, label, NULL, bounds);
nvgTextMetrics(ctx, &asc, &desc, &lh);
// calculate vertical position
int row = bnd_clamp((int)((float)(py - bounds[1]) / lh), 0, nrows - 1);
// search horizontal position
static NVGglyphPosition glyphs[BND_MAX_GLYPHS];
int nglyphs = nvgTextGlyphPositions(
ctx, x, y, rows[row].start, rows[row].end + 1, glyphs, BND_MAX_GLYPHS);
int col, p = 0;
for (col = 0; col < nglyphs && glyphs[col].x < px; ++col)
p = glyphs[col].str - label;
// see if we should move one character further
if (col > 0 && col < nglyphs && glyphs[col].x - px < px - glyphs[col - 1].x)
p = glyphs[col].str - label;
return p;
}
static void bndCaretPosition(NVGcontext *ctx, float x, float y,
float desc, float lineHeight, const char *caret, NVGtextRow *rows,int nrows,
int *cr, float *cx, float *cy) {
static NVGglyphPosition glyphs[BND_MAX_GLYPHS];
int r,nglyphs;
for (r=0; r < nrows && rows[r].end < caret; ++r);
*cr = r;
*cx = x;
*cy = y-lineHeight-desc + r*lineHeight;
if (nrows == 0) return;
*cx = rows[r].minx;
nglyphs = nvgTextGlyphPositions(
ctx, x, y, rows[r].start, rows[r].end+1, glyphs, BND_MAX_GLYPHS);
for (int i=0; i < nglyphs; ++i) {
*cx=glyphs[i].x;
if (glyphs[i].str == caret) break;
}
}
void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h,
int iconid, NVGcolor color, float fontsize, const char *label,
NVGcolor caretcolor, int cbegin, int cend) {
float bounds[4];
float pleft = BND_TEXT_RADIUS;
if (!label) return;
if (iconid >= 0) {
@ -2181,70 +2276,37 @@ void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float /*h*/,
w -= BND_TEXT_RADIUS+pleft;
if (cend >= cbegin) {
#if 1
float c0,c1;
const char *cb;const char *ce;
static NVGglyphPosition glyphs[BND_MAX_GLYPHS];
int nglyphs = nvgTextGlyphPositions(
ctx, x, y, label, label+cend+1, glyphs, BND_MAX_GLYPHS);
c0=glyphs[0].x;
c1=glyphs[nglyphs-1].x;
cb = label+cbegin; ce = label+cend;
// TODO: this is slow
for (int i=0; i < nglyphs; ++i) {
if (glyphs[i].str == cb)
c0 = glyphs[i].x;
if (glyphs[i].str == ce)
c1 = glyphs[i].x;
}
int c0r,c1r;
float c0x,c0y,c1x,c1y;
float desc,lh;
static NVGtextRow rows[BND_MAX_ROWS];
int nrows = nvgTextBreakLines(
ctx, label, label+cend+1, w, rows, BND_MAX_ROWS);
nvgTextMetrics(ctx, NULL, &desc, &lh);
bndCaretPosition(ctx, x, y, desc, lh, label+cbegin,
rows, nrows, &c0r, &c0x, &c0y);
bndCaretPosition(ctx, x, y, desc, lh, label+cend,
rows, nrows, &c1r, &c1x, &c1y);
nvgTextBounds(ctx,x,y,label,NULL, bounds);
nvgBeginPath(ctx);
if (cbegin == cend) {
nvgFillColor(ctx, nvgRGBf(0.337,0.502,0.761));
nvgRect(ctx, c0-1, bounds[1], 2, bounds[3]-bounds[1]);
nvgRect(ctx, c0x-1, c0y, 2, lh+1);
} else {
nvgFillColor(ctx, caretcolor);
nvgRect(ctx, c0-1, bounds[1], c1-c0+1, bounds[3]-bounds[1]);
}
nvgFill(ctx);
#else
float c0,c1;
const char *cb;
const char *ce;
const char *line;
int numlines;
cb = label+cbegin; ce = label+cend;
line = label;
NVGtextRow rows[2];
numlines = nvgTextBreakLines(ctx, line, NULL, w, rows, 2);
/*
int nglyphs = nvgTextGlyphPositions(
ctx, x, y, label, label+cend+1, glyphs, BND_MAX_GLYPHS);
c0=glyphs[0].x;
c1=glyphs[nglyphs-1].x;
// TODO: this is slow
for (int i=0; i < nglyphs; ++i) {
if (glyphs[i].str == cb)
c0 = glyphs[i].x;
if (glyphs[i].str == ce)
c1 = glyphs[i].x;
}
nvgTextBounds(ctx,x,y,label,NULL, bounds);
nvgBeginPath(ctx);
if (cbegin == cend) {
nvgFillColor(ctx, nvgRGBf(0.337,0.502,0.761));
nvgRect(ctx, c0-1, bounds[1], 2, bounds[3]-bounds[1]);
if (c0r == c1r) {
nvgRect(ctx, c0x-1, c0y, c1x-c0x+1, lh+1);
} else {
nvgFillColor(ctx, caretcolor);
nvgRect(ctx, c0-1, bounds[1], c1-c0+1, bounds[3]-bounds[1]);
int blk=c1r-c0r-1;
nvgRect(ctx, c0x-1, c0y, x+w-c0x+1, lh+1);
nvgRect(ctx, x, c1y, c1x-x+1, lh+1);
if (blk)
nvgRect(ctx, x, c0y+lh, w, blk*lh+1);
}
}
nvgFill(ctx);
*/
#endif
}
nvgBeginPath(ctx);
@ -2333,8 +2395,4 @@ NVGcolor bndNodeWireColor(const BNDnodeTheme *theme, BNDwidgetState state) {
#undef BND_INLINE
#endif
#if BX_COMPILER_MSVC
# pragma warning(pop)
#endif // BX_COMPILER_MSVC
#endif // BLENDISH_IMPLEMENTATION

View file

@ -32,8 +32,11 @@
#include "entry/entry.h"
#include "nanovg/nanovg.h"
BX_PRAGMA_DIAGNOSTIC_PUSH();
BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wunused-parameter");
#define BLENDISH_IMPLEMENTATION
#include "blendish.h"
BX_PRAGMA_DIAGNOSTIC_POP();
#define ICON_SEARCH 0x1F50D
#define ICON_CIRCLED_CROSS 0x2716
@ -941,7 +944,7 @@ int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
{
char file[128];
bx::snprintf(file, 128, "images/image%d.jpg", ii+1);
data->images[ii] = nvgCreateImage(vg, file);
data->images[ii] = nvgCreateImage(vg, file, 0);
if (data->images[ii] == bgfx::invalidHandle)
{
printf("Could not load %s.\n", file);
@ -1218,14 +1221,14 @@ int _main_(int /*_argc*/, char** /*_argv*/)
, 0
);
NVGcontext* nvg = nvgCreate(512, 512, 1, 0);
NVGcontext* nvg = nvgCreate(1, 0);
bgfx::setViewSeq(0, true);
DemoData data;
loadDemoData(nvg, &data);
bndSetFont(nvgCreateFont(nvg, "droidsans", "font/droidsans.ttf"));
bndSetIconImage(nvgCreateImage(nvg, "images/blender_icons16.png"));
bndSetFont(nvgCreateFont(nvg, "droidsans", "font/droidsans.ttf") );
bndSetIconImage(nvgCreateImage(nvg, "images/blender_icons16.png", 0) );
int64_t timeOffset = bx::getHPCounter();
@ -1248,7 +1251,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/20-nanovg");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: NanoVG is small antialiased vector graphics rendering library.");
nvgBeginFrame(nvg, width, height, 1.0f, NVG_STRAIGHT_ALPHA);
nvgBeginFrame(nvg, width, height, 1.0f);
renderDemo(nvg, float(mouseState.m_mx), float(mouseState.m_my), float(width), float(height), time, 0, &data);

View file

@ -12,8 +12,9 @@
#include "dbg.h"
#include "cmd.h"
#include <string>
#include <tinystl/allocator.h>
#include <tinystl/string.h>
#include <tinystl/unordered_map.h>
namespace stl = tinystl;
@ -62,14 +63,14 @@ struct CmdContext
case -1:
{
std::string tmp(_cmd, next-_cmd - (*next == '\0' ? 0 : 1) );
stl::string tmp(_cmd, next-_cmd - (*next == '\0' ? 0 : 1) );
DBG("Command '%s' doesn't exist.", tmp.c_str() );
}
break;
default:
{
std::string tmp(_cmd, next-_cmd - (*next == '\0' ? 0 : 1) );
stl::string tmp(_cmd, next-_cmd - (*next == '\0' ? 0 : 1) );
DBG("Failed '%s' err: %d.", tmp.c_str(), err);
}
break;

View file

@ -5,18 +5,19 @@
#define USE_EDTAA3 0
#include "bx/platform.h"
#include <bx/macros.h>
#if BX_COMPILER_MSVC
#define generic GenericFromFreeType // WinRT language extensions see "generic" as a keyword... this is stupid
#define interface InterfaceFromFreeType
#pragma warning(push)
#pragma warning(disable : 4245) // conversion from 'int' to 'FT_UInt', signed/unsigned mismatch
# define generic GenericFromFreeType // WinRT language extensions see "generic" as a keyword... this is stupid
#endif // BX_COMPILER_MSVC
BX_PRAGMA_DIAGNOSTIC_PUSH();
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4245) // error C4245: '=' : conversion from 'int' to 'FT_UInt', signed/unsigned mismatch
#pragma push_macro("interface")
#undef interface
#include <freetype/freetype.h>
#pragma warning(pop)
#else
#include <freetype/freetype.h>
#endif
#pragma pop_macro("interface")
BX_PRAGMA_DIAGNOSTIC_POP();
#include "../common.h"

View file

@ -77,7 +77,7 @@ static void imguiFree(void* _ptr, void* /*_userptr*/)
#define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
#define STBTT_free(_x, _y) imguiFree(_x, _y)
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype/stb_truetype.h>
#include <stb/stb_truetype.h>
namespace
{
@ -441,7 +441,7 @@ struct Imgui
ImguiFontHandle create(const void* _data, float _fontSize)
{
m_nvg = nvgCreate(512, 512, 1, m_view);
m_nvg = nvgCreate(1, m_view);
nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
nvgFontSize(m_nvg, _fontSize);
nvgFontFace(m_nvg, "default");
@ -768,7 +768,7 @@ struct Imgui
void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, uint8_t _view)
{
nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
nvgBeginFrame(m_nvg, _width, _height, 1.0f);
m_view = _view;
m_viewWidth = _width;

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -25,7 +25,12 @@ extern "C" {
#define NVG_PI 3.14159265358979323846264338327f
struct NVGcontext;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4201) // nonstandard extension used : nameless struct/union
#endif
typedef struct NVGcontext NVGcontext;
struct NVGcolor {
union {
@ -35,17 +40,18 @@ struct NVGcolor {
};
};
};
typedef struct NVGcolor NVGcolor;
struct NVGpaint {
float xform[6];
float extent[2];
float radius;
float feather;
struct NVGcolor innerColor;
struct NVGcolor outerColor;
NVGcolor innerColor;
NVGcolor outerColor;
int image;
int repeat;
};
typedef struct NVGpaint NVGpaint;
enum NVGwinding {
NVG_CCW = 1, // Winding for solid shapes
@ -65,11 +71,6 @@ enum NVGlineCap {
NVG_MITER,
};
enum NVGpatternRepeat {
NVG_REPEATX = 0x01, // Repeat image pattern in X direction
NVG_REPEATY = 0x02, // Repeat image pattern in Y direction
};
enum NVGalign {
// Horizontal align
NVG_ALIGN_LEFT = 1<<0, // Default, align text horizontally to left.
@ -82,15 +83,12 @@ enum NVGalign {
NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline.
};
enum NVGalpha {
NVG_STRAIGHT_ALPHA,
NVG_PREMULTIPLIED_ALPHA,
};
struct NVGglyphPosition {
const char* str; // Position of the glyph in the input string.
float x; // The x- coordinate of the position of the glyph .
float x; // The x-coordinate of the logical glyph position.
float minx, maxx; // The bounds of the glyph shape.
};
typedef struct NVGglyphPosition NVGglyphPosition;
struct NVGtextRow {
const char* start; // Pointer to the input text where the row starts.
@ -99,7 +97,15 @@ struct NVGtextRow {
float width; // Logical width of the row.
float minx, maxx; // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
};
typedef struct NVGtextRow NVGtextRow;
enum NVGimageFlags {
NVG_IMAGE_GENERATE_MIPMAPS = 1<<0, // Generate mipmaps during creation of the image.
NVG_IMAGE_REPEATX = 1<<1, // Repeat image in X direction.
NVG_IMAGE_REPEATY = 1<<2, // Repeat image in Y direction.
NVG_IMAGE_FLIPY = 1<<3, // Flips (inverses) image in Y direction when rendered.
NVG_IMAGE_PREMULTIPLIED = 1<<4, // Image data has premultiplied alpha.
};
// Begin drawing a new frame
// Calls to nanovg drawing API should be wrapped in nvgBeginFrame() & nvgEndFrame()
@ -109,14 +115,13 @@ struct NVGtextRow {
// For example, GLFW returns two dimension for an opened window: window size and
// frame buffer size. In that case you would set windowWidth/Height to the window size
// devicePixelRatio to: frameBufferWidth / windowWidth.
// AlphaBlend controls if drawing the shapes to the render target should be done using straight or
// premultiplied alpha. If rendering directly to framebuffer you probably want to use NVG_STRAIGHT_ALPHA,
// if rendering to texture which should contain transparent regions NVG_PREMULTIPLIED_ALPHA is the
// right choice.
void nvgBeginFrame(struct NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio, int alphaBlend);
void nvgBeginFrame(NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio);
// Cancels drawing the current frame.
void nvgCancelFrame(NVGcontext* ctx);
// Ends drawing flushing remaining render state.
void nvgEndFrame(struct NVGcontext* ctx);
void nvgEndFrame(NVGcontext* ctx);
//
// Color utils
@ -124,38 +129,35 @@ void nvgEndFrame(struct NVGcontext* ctx);
// Colors in NanoVG are stored as unsigned ints in ABGR format.
// Returns a color value from red, green, blue values. Alpha will be set to 255 (1.0f).
struct NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b);
NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b);
// Returns a color value from red, green, blue values. Alpha will be set to 1.0f.
struct NVGcolor nvgRGBf(float r, float g, float b);
NVGcolor nvgRGBf(float r, float g, float b);
// Returns a color value from red, green, blue and alpha values.
struct NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
//
struct NVGcolor nvgRGBAu(unsigned int abgr);
NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
// Returns a color value from red, green, blue and alpha values.
struct NVGcolor nvgRGBAf(float r, float g, float b, float a);
NVGcolor nvgRGBAf(float r, float g, float b, float a);
// Linearly interpoaltes from color c0 to c1, and returns resulting color value.
struct NVGcolor nvgLerpRGBA(struct NVGcolor c0, struct NVGcolor c1, float u);
NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u);
// Sets transparency of a color value.
struct NVGcolor nvgTransRGBA(struct NVGcolor c0, unsigned char a);
NVGcolor nvgTransRGBA(NVGcolor c0, unsigned char a);
// Sets transparency of a color value.
struct NVGcolor nvgTransRGBAf(struct NVGcolor c0, float a);
NVGcolor nvgTransRGBAf(NVGcolor c0, float a);
// Returns color value specified by hue, saturation and lightness.
// HSL values are all in range [0..1], alpha will be set to 255.
struct NVGcolor nvgHSL(float h, float s, float l);
NVGcolor nvgHSL(float h, float s, float l);
// Returns color value specified by hue, saturation and lightness and alpha.
// HSL values are all in range [0..1], alpha in range [0..255]
struct NVGcolor nvgHSLA(float h, float s, float l, unsigned char a);
NVGcolor nvgHSLA(float h, float s, float l, unsigned char a);
//
// State Handling
@ -166,13 +168,13 @@ struct NVGcolor nvgHSLA(float h, float s, float l, unsigned char a);
// Pushes and saves the current render state into a state stack.
// A matching nvgRestore() must be used to restore the state.
void nvgSave(struct NVGcontext* ctx);
void nvgSave(NVGcontext* ctx);
// Pops and restores current render state.
void nvgRestore(struct NVGcontext* ctx);
void nvgRestore(NVGcontext* ctx);
// Resets current render state to default values. Does not affect the render state stack.
void nvgReset(struct NVGcontext* ctx);
void nvgReset(NVGcontext* ctx);
//
// Render styles
@ -184,31 +186,35 @@ void nvgReset(struct NVGcontext* ctx);
// Current render style can be saved and restored using nvgSave() and nvgRestore().
// Sets current stroke style to a solid color.
void nvgStrokeColor(struct NVGcontext* ctx, struct NVGcolor color);
void nvgStrokeColor(NVGcontext* ctx, NVGcolor color);
// Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
void nvgStrokePaint(struct NVGcontext* ctx, struct NVGpaint paint);
void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint);
// Sets current fill cstyle to a solid color.
void nvgFillColor(struct NVGcontext* ctx, struct NVGcolor color);
void nvgFillColor(NVGcontext* ctx, NVGcolor color);
// Sets current fill style to a paint, which can be a one of the gradients or a pattern.
void nvgFillPaint(struct NVGcontext* ctx, struct NVGpaint paint);
void nvgFillPaint(NVGcontext* ctx, NVGpaint paint);
// Sets the miter limit of the stroke style.
// Miter limit controls when a sharp corner is beveled.
void nvgMiterLimit(struct NVGcontext* ctx, float limit);
void nvgMiterLimit(NVGcontext* ctx, float limit);
// Sets the stroke witdth of the stroke style.
void nvgStrokeWidth(struct NVGcontext* ctx, float size);
void nvgStrokeWidth(NVGcontext* ctx, float size);
// Sets how the end of the line (cap) is drawn,
// Can be one of: NVG_BUTT (default), NVG_ROUND, NVG_SQUARE.
void nvgLineCap(struct NVGcontext* ctx, int cap);
void nvgLineCap(NVGcontext* ctx, int cap);
// Sets how sharp path corners are drawn.
// Can be one of NVG_MITER (default), NVG_ROUND, NVG_BEVEL.
void nvgLineJoin(struct NVGcontext* ctx, int join);
void nvgLineJoin(NVGcontext* ctx, int join);
// Sets the transparency applied to all rendered shapes.
// Alreade transparent paths will get proportionally more transparent as well.
void nvgGlobalAlpha(NVGcontext* ctx, float alpha);
//
// Transforms
@ -228,50 +234,103 @@ void nvgLineJoin(struct NVGcontext* ctx, int join);
// Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore().
// Resets current transform to a identity matrix.
void nvgResetTransform(struct NVGcontext* ctx);
void nvgResetTransform(NVGcontext* ctx);
// Premultiplies current coordinate system by specified matrix.
// The parameters are interpreted as matrix as follows:
// [a c e]
// [b d f]
// [0 0 1]
void nvgTransform(struct NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f);
// Translates current coordinate system.
void nvgTranslate(struct NVGcontext* ctx, float x, float y);
void nvgTranslate(NVGcontext* ctx, float x, float y);
// Rotates current coordinate system.
void nvgRotate(struct NVGcontext* ctx, float angle);
// Rotates current coordinate system. Angle is specifid in radians.
void nvgRotate(NVGcontext* ctx, float angle);
// Skews the current coordinate system along X axis. Angle is specifid in radians.
void nvgSkewX(NVGcontext* ctx, float angle);
// Skews the current coordinate system along Y axis. Angle is specifid in radians.
void nvgSkewY(NVGcontext* ctx, float angle);
// Scales the current coordinat system.
void nvgScale(struct NVGcontext* ctx, float x, float y);
void nvgScale(NVGcontext* ctx, float x, float y);
// Stores the top part (a-f) of the current transformation matrix in to the specified buffer.
// [a c e]
// [b d f]
// [0 0 1]
// There should be space for 6 floats in the return buffer for the values a-f.
void nvgCurrentTransform(NVGcontext* ctx, float* xform);
// The following functions can be used to make calculations on 2x3 transformation matrices.
// A 2x3 matrix is representated as float[6].
// Sets the transform to identity matrix.
void nvgTransformIdentity(float* dst);
// Sets the transform to translation matrix matrix.
void nvgTransformTranslate(float* dst, float tx, float ty);
// Sets the transform to scale matrix.
void nvgTransformScale(float* dst, float sx, float sy);
// Sets the transform to rotate matrix. Angle is specifid in radians.
void nvgTransformRotate(float* dst, float a);
// Sets the transform to skew-x matrix. Angle is specifid in radians.
void nvgTransformSkewX(float* dst, float a);
// Sets the transform to skew-y matrix. Angle is specifid in radians.
void nvgTransformSkewY(float* dst, float a);
// Sets the transform to the result of multiplication of two transforms, of A = A*B.
void nvgTransformMultiply(float* dst, const float* src);
// Sets the transform to the result of multiplication of two transforms, of A = B*A.
void nvgTransformPremultiply(float* dst, const float* src);
// Sets the destination to inverse of specified transform.
// Returns 1 if the inverse could be calculated, else 0.
int nvgTransformInverse(float* dst, const float* src);
// Transform a point by given transform.
void nvgTransformPoint(float* dstx, float* dsty, const float* xform, float srcx, float srcy);
// Converts degress to radians and vice versa.
float nvgDegToRad(float deg);
float nvgRadToDeg(float rad);
//
// Images
//
// NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
// In addition you can upload your own image. The image loading is provided by stb_image.
// The parameter imageFlags is combination of flags defined in NVGimageFlags.
// Creates image by loading it from the disk from specified file name.
// Returns handle to the image.
int nvgCreateImage(struct NVGcontext* ctx, const char* filename);
int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags);
// Creates image by loading it from the specified chunk of memory.
// Returns handle to the image.
int nvgCreateImageMem(struct NVGcontext* ctx, unsigned char* data, int ndata);
int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata);
// Creates image from specified image data.
// Returns handle to the image.
int nvgCreateImageRGBA(struct NVGcontext* ctx, int w, int h, const unsigned char* data);
int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data);
// Updates image data specified by image handle.
void nvgUpdateImage(struct NVGcontext* ctx, int image, const unsigned char* data);
void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data);
// Returns the domensions of a created image.
void nvgImageSize(struct NVGcontext* ctx, int image, int* w, int* h);
void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h);
// Deletes created image.
void nvgDeleteImage(struct NVGcontext* ctx, int image);
void nvgDeleteImage(NVGcontext* ctx, int image);
//
// Paints
@ -282,29 +341,28 @@ void nvgDeleteImage(struct NVGcontext* ctx, int image);
// Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
// of the linear gradient, icol specifies the start color and ocol the end color.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
struct NVGpaint nvgLinearGradient(struct NVGcontext* ctx, float sx, float sy, float ex, float ey,
struct NVGcolor icol, struct NVGcolor ocol);
NVGpaint nvgLinearGradient(NVGcontext* ctx, float sx, float sy, float ex, float ey,
NVGcolor icol, NVGcolor ocol);
// Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
// drop shadows or hilights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
// (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
// the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
struct NVGpaint nvgBoxGradient(struct NVGcontext* ctx, float x, float y, float w, float h,
float r, float f, struct NVGcolor icol, struct NVGcolor ocol);
NVGpaint nvgBoxGradient(NVGcontext* ctx, float x, float y, float w, float h,
float r, float f, NVGcolor icol, NVGcolor ocol);
// Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
// the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
struct NVGpaint nvgRadialGradient(struct NVGcontext* ctx, float cx, float cy, float inr, float outr,
struct NVGcolor icol, struct NVGcolor ocol);
NVGpaint nvgRadialGradient(NVGcontext* ctx, float cx, float cy, float inr, float outr,
NVGcolor icol, NVGcolor ocol);
// Creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
// (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render,
// and repeat is combination of NVG_REPEATX and NVG_REPEATY which tells if the image should be repeated across x or y.
// (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
struct NVGpaint nvgImagePattern(struct NVGcontext* ctx, float ox, float oy, float ex, float ey,
float angle, int image, int repeat);
NVGpaint nvgImagePattern(NVGcontext* ctx, float ox, float oy, float ex, float ey,
float angle, int image, float alpha);
//
// Scissoring
@ -312,12 +370,20 @@ struct NVGpaint nvgImagePattern(struct NVGcontext* ctx, float ox, float oy, floa
// Scissoring allows you to clip the rendering into a rectangle. This is useful for varius
// user interface cases like rendering a text edit or a timeline.
// Sets the current
// Sets the current scissor rectangle.
// The scissor rectangle is transformed by the current transform.
void nvgScissor(struct NVGcontext* ctx, float x, float y, float w, float h);
void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h);
// Intersects current scissor rectangle with the specified rectangle.
// The scissor rectangle is transformed by the current transform.
// Note: in case the rotation of previous scissor rect differs from
// the current one, the intersection will be done between the specified
// rectangle and the previous scissor rectangle transformed in the current
// transform space. The resulting shape is always rectangle.
void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h);
// Reset and disables scissoring.
void nvgResetScissor(struct NVGcontext* ctx);
void nvgResetScissor(NVGcontext* ctx);
//
// Paths
@ -337,46 +403,51 @@ void nvgResetScissor(struct NVGcontext* ctx);
// The curve segments and sub-paths are transformed by the current transform.
// Clears the current path and sub-paths.
void nvgBeginPath(struct NVGcontext* ctx);
void nvgBeginPath(NVGcontext* ctx);
// Starts new sub-path with specified point as first point.
void nvgMoveTo(struct NVGcontext* ctx, float x, float y);
void nvgMoveTo(NVGcontext* ctx, float x, float y);
// Adds line segment from the last point in the path to the specified point.
void nvgLineTo(struct NVGcontext* ctx, float x, float y);
void nvgLineTo(NVGcontext* ctx, float x, float y);
// Adds bezier segment from last point in the path via two control points to the specified point.
void nvgBezierTo(struct NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
// Adds cubic bezier segment from last point in the path via two control points to the specified point.
void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y);
// Adds quadratic bezier segment from last point in the path via a control point to the specified point.
void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y);
// Adds an arc segment at the corner defined by the last path point, and two specified points.
void nvgArcTo(struct NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius);
// Closes current sub-path with a line segment.
void nvgClosePath(struct NVGcontext* ctx);
void nvgClosePath(NVGcontext* ctx);
// Sets the current sub-path winding, see NVGwinding and NVGsolidity.
void nvgPathWinding(struct NVGcontext* ctx, int dir);
void nvgPathWinding(NVGcontext* ctx, int dir);
// Creates new arc shaped sub-path.
void nvgArc(struct NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
// Creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r,
// and the arc is drawn from angle a0 to a1, and swept in direction dir (NVG_CCW, or NVG_CW).
// Angles are specified in radians.
void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir);
// Creates new rectangle shaped sub-path.
void nvgRect(struct NVGcontext* ctx, float x, float y, float w, float h);
void nvgRect(NVGcontext* ctx, float x, float y, float w, float h);
// Creates new rounded rectangle shaped sub-path.
void nvgRoundedRect(struct NVGcontext* ctx, float x, float y, float w, float h, float r);
void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r);
// Creates new ellipse shaped sub-path.
void nvgEllipse(struct NVGcontext* ctx, float cx, float cy, float rx, float ry);
void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry);
// Creates new circle shaped sub-path.
void nvgCircle(struct NVGcontext* ctx, float cx, float cy, float r);
void nvgCircle(NVGcontext* ctx, float cx, float cy, float r);
// Fills the current path with current fill style.
void nvgFill(struct NVGcontext* ctx);
void nvgFill(NVGcontext* ctx);
// Fills the current path with current stroke style.
void nvgStroke(struct NVGcontext* ctx);
void nvgStroke(NVGcontext* ctx);
//
@ -414,67 +485,67 @@ void nvgStroke(struct NVGcontext* ctx);
// Creates font by loading it from the disk from specified file name.
// Returns handle to the font.
int nvgCreateFont(struct NVGcontext* ctx, const char* name, const char* filename);
int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename);
// Creates image by loading it from the specified memory chunk.
// Returns handle to the font.
int nvgCreateFontMem(struct NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData);
// Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
int nvgFindFont(struct NVGcontext* ctx, const char* name);
int nvgFindFont(NVGcontext* ctx, const char* name);
// Sets the font size of current text style.
void nvgFontSize(struct NVGcontext* ctx, float size);
void nvgFontSize(NVGcontext* ctx, float size);
// Sets the blur of current text style.
void nvgFontBlur(struct NVGcontext* ctx, float blur);
void nvgFontBlur(NVGcontext* ctx, float blur);
// Sets the letter spacing of current text style.
void nvgTextLetterSpacing(struct NVGcontext* ctx, float spacing);
void nvgTextLetterSpacing(NVGcontext* ctx, float spacing);
// Sets the proportional line height of current text style. The line height is specified as multiple of font size.
void nvgTextLineHeight(struct NVGcontext* ctx, float lineHeight);
void nvgTextLineHeight(NVGcontext* ctx, float lineHeight);
// Sets the text align of current text style, see NVGaling for options.
void nvgTextAlign(struct NVGcontext* ctx, int align);
void nvgTextAlign(NVGcontext* ctx, int align);
// Sets the font face based on specified id of current text style.
void nvgFontFaceId(struct NVGcontext* ctx, int font);
void nvgFontFaceId(NVGcontext* ctx, int font);
// Sets the font face based on specified name of current text style.
void nvgFontFace(struct NVGcontext* ctx, const char* font);
void nvgFontFace(NVGcontext* ctx, const char* font);
// Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
float nvgText(struct NVGcontext* ctx, float x, float y, const char* string, const char* end);
float nvgText(NVGcontext* ctx, float x, float y, const char* string, const char* end);
// Draws multi-line text string at specified location wrapped at the specified width. If end is specified only the sub-string up to the end is drawn.
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
void nvgTextBox(struct NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end);
void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end);
// Measures the specified text string. Parameter bounds should be a pointer to float[4],
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
// Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
// Measured values are returned in local coordinate space.
float nvgTextBounds(struct NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds);
float nvgTextBounds(NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds);
// Measures the specified multi-text string. Parameter bounds should be a pointer to float[4],
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
// Measured values are returned in local coordinate space.
void nvgTextBoxBounds(struct NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds);
void nvgTextBoxBounds(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds);
// Calculates the glyph x positions of the specified text. If end is specified only the sub-string will be used.
// Measured values are returned in local coordinate space.
int nvgTextGlyphPositions(struct NVGcontext* ctx, float x, float y, const char* string, const char* end, struct NVGglyphPosition* positions, int maxPositions);
int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string, const char* end, NVGglyphPosition* positions, int maxPositions);
// Returns the vertical metrics based on the current text style.
// Measured values are returned in local coordinate space.
void nvgTextMetrics(struct NVGcontext* ctx, float* ascender, float* descender, float* lineh);
void nvgTextMetrics(NVGcontext* ctx, float* ascender, float* descender, float* lineh);
// Breaks the specified text into lines. If end is specified only the sub-string will be used.
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
int nvgTextBreakLines(struct NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, struct NVGtextRow* rows, int maxRows);
int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows);
//
// Internal Render API
@ -484,60 +555,66 @@ enum NVGtexture {
NVG_TEXTURE_RGBA = 0x02,
};
struct NVGscissor
{
struct NVGscissor {
float xform[6];
float extent[2];
};
typedef struct NVGscissor NVGscissor;
struct NVGvertex {
float x,y,u,v;
};
typedef struct NVGvertex NVGvertex;
struct NVGpath {
int first;
int count;
unsigned char closed;
int nbevel;
struct NVGvertex* fill;
NVGvertex* fill;
int nfill;
struct NVGvertex* stroke;
NVGvertex* stroke;
int nstroke;
int winding;
int convex;
};
typedef struct NVGpath NVGpath;
struct NVGparams {
void* userPtr;
int atlasWidth, atlasHeight;
int edgeAntiAlias;
int (*renderCreate)(void* uptr);
int (*renderCreateTexture)(void* uptr, int type, int w, int h, const unsigned char* data);
int (*renderCreateTexture)(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data);
int (*renderDeleteTexture)(void* uptr, int image);
int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data);
int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h);
void (*renderViewport)(void* uptr, int width, int height, int alphaBlend);
void (*renderFlush)(void* uptr, int alphaBlend);
void (*renderFill)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float fringe, const float* bounds, const struct NVGpath* paths, int npaths);
void (*renderStroke)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, float fringe, float strokeWidth, const struct NVGpath* paths, int npaths);
void (*renderTriangles)(void* uptr, struct NVGpaint* paint, struct NVGscissor* scissor, const struct NVGvertex* verts, int nverts);
void (*renderViewport)(void* uptr, int width, int height);
void (*renderCancel)(void* uptr);
void (*renderFlush)(void* uptr);
void (*renderFill)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths, int npaths);
void (*renderStroke)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths, int npaths);
void (*renderTriangles)(void* uptr, NVGpaint* paint, NVGscissor* scissor, const NVGvertex* verts, int nverts);
void (*renderDelete)(void* uptr);
};
typedef struct NVGparams NVGparams;
// Contructor and destructor, called by the render back-end.
struct NVGcontext* nvgCreateInternal(struct NVGparams* params);
void nvgDeleteInternal(struct NVGcontext* ctx);
// Debug function to dump cached path data.
void nvgDebugDumpPathCache(struct NVGcontext* ctx);
//
struct NVGcontext* nvgCreate(int atlasw, int atlash, int edgeaa, unsigned char viewid);
//
NVGcontext* nvgCreate(int edgeaa, unsigned char viewid);
void nvgDelete(struct NVGcontext* ctx);
#define NVG_NOTUSED(v) for (;;) { (void)(true ? (void)0 : ( (void)(v) ) ); break; }
// Contructor and destructor, called by the render back-end.
NVGcontext* nvgCreateInternal(NVGparams* params);
void nvgDeleteInternal(NVGcontext* ctx);
NVGparams* nvgInternalParams(NVGcontext* ctx);
// Debug function to dump cached path data.
void nvgDebugDumpPathCache(NVGcontext* ctx);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#define NVG_NOTUSED(v) for (;;) { (void)(1 ? (void)0 : ( (void)(v) ) ); break; }
#ifdef __cplusplus
}

View file

@ -45,11 +45,17 @@ namespace
NSVG_SHADER_IMG
};
// These are additional flags on top of NVGimageFlags.
enum NVGimageFlagsGL {
NVG_IMAGE_NODELETE = 1<<16, // Do not delete GL texture handle.
};
struct GLNVGtexture
{
bgfx::TextureHandle id;
int width, height;
int type;
int flags;
};
enum GLNVGcallType
@ -198,20 +204,21 @@ namespace
static int glnvg__deleteTexture(struct GLNVGcontext* gl, int id)
{
int i;
for (i = 0; i < gl->ntextures; i++)
for (int ii = 0; ii < gl->ntextures; ii++)
{
if (gl->textures[i].id.idx == id)
if (gl->textures[ii].id.idx == id)
{
if (bgfx::isValid(gl->textures[i].id) )
if (bgfx::isValid(gl->textures[ii].id)
&& (gl->textures[ii].flags & NVG_IMAGE_NODELETE) == 0)
{
bgfx::destroyTexture(gl->textures[i].id);
bgfx::destroyTexture(gl->textures[ii].id);
}
memset(&gl->textures[i], 0, sizeof(gl->textures[i]));
gl->textures[i].id.idx = bgfx::invalidHandle;
memset(&gl->textures[ii], 0, sizeof(gl->textures[ii]));
gl->textures[ii].id.idx = bgfx::invalidHandle;
return 1;
}
}
return 0;
}
@ -277,7 +284,7 @@ namespace
return 1;
}
static int nvgRenderCreateTexture(void* _userPtr, int _type, int _width, int _height, const unsigned char* _rgba)
static int nvgRenderCreateTexture(void* _userPtr, int _type, int _width, int _height, int _flags, const unsigned char* _rgba)
{
struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
struct GLNVGtexture* tex = glnvg__allocTexture(gl);
@ -290,6 +297,7 @@ namespace
tex->width = _width;
tex->height = _height;
tex->type = _type;
tex->flags = _flags;
uint32_t bytesPerPixel = NVG_TEXTURE_RGBA == tex->type ? 4 : 1;
uint32_t pitch = tex->width * bytesPerPixel;
@ -505,10 +513,9 @@ namespace
gl->th = handle;
}
static void nvgRenderViewport(void* _userPtr, int width, int height, int alphaBlend)
static void nvgRenderViewport(void* _userPtr, int width, int height)
{
struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
NVG_NOTUSED(alphaBlend);
gl->view[0] = (float)width;
gl->view[1] = (float)height;
bgfx::setViewRect(gl->viewid, 0, 0, width, height);
@ -672,7 +679,7 @@ namespace
}
}
static void nvgRenderFlush(void* _userPtr, int alphaBlend)
static void nvgRenderFlush(void* _userPtr)
{
struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr;
@ -687,14 +694,14 @@ namespace
| BGFX_STATE_ALPHA_WRITE
;
if (alphaBlend == NVG_PREMULTIPLIED_ALPHA)
{
gl->state |= BGFX_STATE_BLEND_FUNC_SEPARATE(
BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA
, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_INV_SRC_ALPHA
);
}
else
// if (alphaBlend == NVG_PREMULTIPLIED_ALPHA)
// {
// gl->state |= BGFX_STATE_BLEND_FUNC_SEPARATE(
// BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA
// , BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_INV_SRC_ALPHA
// );
// }
// else
{
gl->state |= BGFX_STATE_BLEND_FUNC(
BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA
@ -977,7 +984,8 @@ namespace
for (uint32_t ii = 0, num = gl->ntextures; ii < num; ++ii)
{
if (bgfx::isValid(gl->textures[ii].id) )
if (bgfx::isValid(gl->textures[ii].id)
&& (gl->textures[ii].flags & NVG_IMAGE_NODELETE) == 0)
{
bgfx::destroyTexture(gl->textures[ii].id);
}
@ -990,7 +998,7 @@ namespace
} // namespace
struct NVGcontext* nvgCreate(int atlasw, int atlash, int edgeaa, unsigned char viewid)
NVGcontext* nvgCreate(int edgeaa, unsigned char viewid)
{
struct NVGparams params;
struct NVGcontext* ctx = NULL;
@ -1011,8 +1019,6 @@ struct NVGcontext* nvgCreate(int atlasw, int atlash, int edgeaa, unsigned char v
params.renderTriangles = nvgRenderTriangles;
params.renderDelete = nvgRenderDelete;
params.userPtr = gl;
params.atlasWidth = atlasw;
params.atlasHeight = atlash;
params.edgeAntiAlias = edgeaa;
gl->edgeAntiAlias = edgeaa;