Browse Source

Update to latest nanovg and dpf

pull/1/head
falkTX 10 years ago
parent
commit
a78e4a3574
8 changed files with 234 additions and 90 deletions
  1. +1
    -1
      dpf
  2. +1
    -1
      examples/demo-multi.cpp
  3. +1
    -1
      examples/nanovg2.cpp
  4. +172
    -50
      examples/nanovg_res/demo.c
  5. +9
    -8
      examples/nanovg_res/demo.h
  6. +26
    -8
      examples/nanovg_res/perf.c
  7. +20
    -17
      examples/nanovg_res/perf.h
  8. +4
    -4
      examples/widgets/NanoPerfWidget.hpp

+ 1
- 1
dpf

@@ -1 +1 @@
Subproject commit 28822a20701a75444b755463daeb82e640e1ceea
Subproject commit d1ecc17a687cdade92954e548249a7f747aec392

+ 1
- 1
examples/demo-multi.cpp View File

@@ -66,7 +66,7 @@ public:
{ {
setResizable(false); setResizable(false);
setSize(fWidget.getSize()); setSize(fWidget.getSize());
setTitle(d_string("transient #") + d_string(++gWindowCount));
setTitle(String("transient #") + String(++gWindowCount));
show(); show();
} }




+ 1
- 1
examples/nanovg2.cpp View File

@@ -55,7 +55,7 @@ public:
{ {
char file[128]; char file[128];
std::snprintf(file, 128, "./nanovg_res/images/image%d.jpg", i+1); std::snprintf(file, 128, "./nanovg_res/images/image%d.jpg", i+1);
fImages[i] = createImage(file);
fImages[i] = createImage(file, 0);


if (fImages[i] == nullptr) if (fImages[i] == nullptr)
{ {


+ 172
- 50
examples/nanovg_res/demo.c View File

@@ -20,8 +20,13 @@
#define ICON_LOGIN 0xE740 #define ICON_LOGIN 0xE740
#define ICON_TRASH 0xE729 #define ICON_TRASH 0xE729


//static float minf(float a, float b) { return a < b ? a : b; }
static float maxf(float a, float b) { return a > b ? a : b; }
//static float absf(float a) { return a >= 0.0f ? a : -a; }
static float clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }

// Returns 1 if col.rgba is 0.0f,0.0f,0.0f,0.0f, 0 otherwise // Returns 1 if col.rgba is 0.0f,0.0f,0.0f,0.0f, 0 otherwise
int isBlack( struct NVGcolor col )
int isBlack(NVGcolor col)
{ {
if( col.r == 0.0f && col.g == 0.0f && col.b == 0.0f && col.a == 0.0f ) if( col.r == 0.0f && col.g == 0.0f && col.b == 0.0f && col.a == 0.0f )
{ {
@@ -52,11 +57,11 @@ static char* cpToUTF8(int cp, char* str)
} }




void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, float w, float h)
void drawWindow(NVGcontext* vg, const char* title, float x, float y, float w, float h)
{ {
float cornerRadius = 3.0f; float cornerRadius = 3.0f;
struct NVGpaint shadowPaint;
struct NVGpaint headerPaint;
NVGpaint shadowPaint;
NVGpaint headerPaint;


nvgSave(vg); nvgSave(vg);
// nvgClearState(vg); // nvgClearState(vg);
@@ -104,9 +109,9 @@ void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, floa
nvgRestore(vg); nvgRestore(vg);
} }


void drawSearchBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
void drawSearchBox(NVGcontext* vg, const char* text, float x, float y, float w, float h)
{ {
struct NVGpaint bg;
NVGpaint bg;
char icon[8]; char icon[8];
float cornerRadius = h/2-1; float cornerRadius = h/2-1;


@@ -142,9 +147,9 @@ void drawSearchBox(struct NVGcontext* vg, const char* text, float x, float y, fl
nvgText(vg, x+w-h*0.55f, y+h*0.55f, cpToUTF8(ICON_CIRCLED_CROSS,icon), NULL); nvgText(vg, x+w-h*0.55f, y+h*0.55f, cpToUTF8(ICON_CIRCLED_CROSS,icon), NULL);
} }


void drawDropDown(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
void drawDropDown(NVGcontext* vg, const char* text, float x, float y, float w, float h)
{ {
struct NVGpaint bg;
NVGpaint bg;
char icon[8]; char icon[8];
float cornerRadius = 4.0f; float cornerRadius = 4.0f;


@@ -172,7 +177,7 @@ void drawDropDown(struct NVGcontext* vg, const char* text, float x, float y, flo
nvgText(vg, x+w-h*0.5f, y+h*0.5f, cpToUTF8(ICON_CHEVRON_RIGHT,icon), NULL); nvgText(vg, x+w-h*0.5f, y+h*0.5f, cpToUTF8(ICON_CHEVRON_RIGHT,icon), NULL);
} }


void drawLabel(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
void drawLabel(NVGcontext* vg, const char* text, float x, float y, float w, float h)
{ {
NVG_NOTUSED(w); NVG_NOTUSED(w);


@@ -184,9 +189,9 @@ void drawLabel(struct NVGcontext* vg, const char* text, float x, float y, float
nvgText(vg, x,y+h*0.5f,text, NULL); nvgText(vg, x,y+h*0.5f,text, NULL);
} }


void drawEditBoxBase(struct NVGcontext* vg, float x, float y, float w, float h)
void drawEditBoxBase(NVGcontext* vg, float x, float y, float w, float h)
{ {
struct NVGpaint bg;
NVGpaint bg;
// Edit // Edit
bg = nvgBoxGradient(vg, x+1,y+1+1.5f, w-2,h-2, 3,4, nvgRGBA(255,255,255,32), nvgRGBA(32,32,32,32)); bg = nvgBoxGradient(vg, x+1,y+1+1.5f, w-2,h-2, 3,4, nvgRGBA(255,255,255,32), nvgRGBA(32,32,32,32));
nvgBeginPath(vg); nvgBeginPath(vg);
@@ -200,7 +205,7 @@ void drawEditBoxBase(struct NVGcontext* vg, float x, float y, float w, float h)
nvgStroke(vg); nvgStroke(vg);
} }


void drawEditBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
void drawEditBox(NVGcontext* vg, const char* text, float x, float y, float w, float h)
{ {


drawEditBoxBase(vg, x,y, w,h); drawEditBoxBase(vg, x,y, w,h);
@@ -212,7 +217,7 @@ void drawEditBox(struct NVGcontext* vg, const char* text, float x, float y, floa
nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL); nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
} }


void drawEditBoxNum(struct NVGcontext* vg,
void drawEditBoxNum(NVGcontext* vg,
const char* text, const char* units, float x, float y, float w, float h) const char* text, const char* units, float x, float y, float w, float h)
{ {
float uw; float uw;
@@ -234,9 +239,9 @@ void drawEditBoxNum(struct NVGcontext* vg,
nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text, NULL); nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text, NULL);
} }


void drawCheckBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
void drawCheckBox(NVGcontext* vg, const char* text, float x, float y, float w, float h)
{ {
struct NVGpaint bg;
NVGpaint bg;
char icon[8]; char icon[8];
NVG_NOTUSED(w); NVG_NOTUSED(w);


@@ -260,9 +265,9 @@ void drawCheckBox(struct NVGcontext* vg, const char* text, float x, float y, flo
nvgText(vg, x+9+2, y+h*0.5f, cpToUTF8(ICON_CHECK,icon), NULL); nvgText(vg, x+9+2, y+h*0.5f, cpToUTF8(ICON_CHECK,icon), NULL);
} }


void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, struct NVGcolor col)
void drawButton(NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, NVGcolor col)
{ {
struct NVGpaint bg;
NVGpaint bg;
char icon[8]; char icon[8];
float cornerRadius = 4.0f; float cornerRadius = 4.0f;
float tw = 0, iw = 0; float tw = 0, iw = 0;
@@ -309,9 +314,9 @@ void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, f
nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text, NULL); nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text, NULL);
} }


void drawSlider(struct NVGcontext* vg, float pos, float x, float y, float w, float h)
void drawSlider(NVGcontext* vg, float pos, float x, float y, float w, float h)
{ {
struct NVGpaint bg, knob;
NVGpaint bg, knob;
float cy = y+(int)(h*0.5f); float cy = y+(int)(h*0.5f);
float kr = (int)(h*0.25f); float kr = (int)(h*0.25f);


@@ -351,9 +356,9 @@ void drawSlider(struct NVGcontext* vg, float pos, float x, float y, float w, flo
nvgRestore(vg); nvgRestore(vg);
} }


void drawEyes(struct NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
void drawEyes(NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
{ {
struct NVGpaint gloss, bg;
NVGpaint gloss, bg;
float ex = w *0.23f; float ex = w *0.23f;
float ey = h * 0.5f; float ey = h * 0.5f;
float lx = x + ex; float lx = x + ex;
@@ -417,9 +422,9 @@ void drawEyes(struct NVGcontext* vg, float x, float y, float w, float h, float m
nvgFill(vg); nvgFill(vg);
} }


void drawGraph(struct NVGcontext* vg, float x, float y, float w, float h, float t)
void drawGraph(NVGcontext* vg, float x, float y, float w, float h, float t)
{ {
struct NVGpaint bg;
NVGpaint bg;
float samples[6]; float samples[6];
float sx[6], sy[6]; float sx[6], sy[6];
float dx = w/5.0f; float dx = w/5.0f;
@@ -488,10 +493,36 @@ void drawGraph(struct NVGcontext* vg, float x, float y, float w, float h, float
nvgStrokeWidth(vg, 1.0f); nvgStrokeWidth(vg, 1.0f);
} }


void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
void drawSpinner(NVGcontext* vg, float cx, float cy, float r, float t)
{
float a0 = 0.0f + t*6;
float a1 = NVG_PI + t*6;
float r0 = r;
float r1 = r * 0.75f;
float ax,ay, bx,by;
NVGpaint paint;

nvgSave(vg);

nvgBeginPath(vg);
nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
nvgClosePath(vg);
ax = cx + cosf(a0) * (r0+r1)*0.5f;
ay = cy + sinf(a0) * (r0+r1)*0.5f;
bx = cx + cosf(a1) * (r0+r1)*0.5f;
by = cy + sinf(a1) * (r0+r1)*0.5f;
paint = nvgLinearGradient(vg, ax,ay, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,128));
nvgFillPaint(vg, paint);
nvgFill(vg);

nvgRestore(vg);
}

void drawThumbnails(NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
{ {
float cornerRadius = 3.0f; float cornerRadius = 3.0f;
struct NVGpaint shadowPaint, imgPaint, fadePaint;
NVGpaint shadowPaint, imgPaint, fadePaint;
float ix,iy,iw,ih; float ix,iy,iw,ih;
float thumb = 60.0f; float thumb = 60.0f;
float arry = 30.5f; float arry = 30.5f;
@@ -499,7 +530,8 @@ void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, c
float stackh = (nimages/2) * (thumb+10) + 10; float stackh = (nimages/2) * (thumb+10) + 10;
int i; int i;
float u = (1+cosf(t*0.5f))*0.5f; float u = (1+cosf(t*0.5f))*0.5f;
float scrollh;
float u2 = (1-cosf(t*0.2f))*0.5f;
float scrollh, dv;


nvgSave(vg); nvgSave(vg);
// nvgClearState(vg); // nvgClearState(vg);
@@ -526,8 +558,10 @@ void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, c
nvgScissor(vg, x,y,w,h); nvgScissor(vg, x,y,w,h);
nvgTranslate(vg, 0, -(stackh - h)*u); nvgTranslate(vg, 0, -(stackh - h)*u);


dv = 1.0f / (float)(nimages-1);

for (i = 0; i < nimages; i++) { for (i = 0; i < nimages; i++) {
float tx, ty;
float tx, ty, v, a;
tx = x+10; tx = x+10;
ty = y+10; ty = y+10;
tx += (i%2) * (thumb+10); tx += (i%2) * (thumb+10);
@@ -544,7 +578,14 @@ void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, c
ix = -(iw-thumb)*0.5f; ix = -(iw-thumb)*0.5f;
iy = 0; iy = 0;
} }
imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], 0);

v = i * dv;
a = clampf((u2-v) / dv, 0, 1);

if (a < 1.0f)
drawSpinner(vg, tx+thumb/2,ty+thumb/2, thumb*0.25f, t);

imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], a);
nvgBeginPath(vg); nvgBeginPath(vg);
nvgRoundedRect(vg, tx,ty, thumb,thumb, 5); nvgRoundedRect(vg, tx,ty, thumb,thumb, 5);
nvgFillPaint(vg, imgPaint); nvgFillPaint(vg, imgPaint);
@@ -598,12 +639,12 @@ void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, c
nvgRestore(vg); nvgRestore(vg);
} }


void drawColorwheel(struct NVGcontext* vg, float x, float y, float w, float h, float t)
void drawColorwheel(NVGcontext* vg, float x, float y, float w, float h, float t)
{ {
int i; int i;
float r0, r1, ax,ay, bx,by, cx,cy, aeps, r; float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
float hue = sinf(t * 0.12f); float hue = sinf(t * 0.12f);
struct NVGpaint paint;
NVGpaint paint;


nvgSave(vg); nvgSave(vg);


@@ -703,7 +744,7 @@ void drawColorwheel(struct NVGcontext* vg, float x, float y, float w, float h, f
nvgRestore(vg); nvgRestore(vg);
} }


void drawLines(struct NVGcontext* vg, float x, float y, float w, float h, float t)
void drawLines(NVGcontext* vg, float x, float y, float w, float h, float t)
{ {
int i, j; int i, j;
float pad = 5.0f, s = w/9.0f - pad*2; float pad = 5.0f, s = w/9.0f - pad*2;
@@ -757,7 +798,7 @@ void drawLines(struct NVGcontext* vg, float x, float y, float w, float h, float
nvgRestore(vg); nvgRestore(vg);
} }


int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
int loadDemoData(NVGcontext* vg, DemoData* data)
{ {
int i; int i;


@@ -766,25 +807,25 @@ int loadDemoData(struct NVGcontext* vg, struct DemoData* data)


for (i = 0; i < 12; i++) { for (i = 0; i < 12; i++) {
char file[128]; char file[128];
snprintf(file, 128, "./nanovg_res/images/image%d.jpg", i+1);
data->images[i] = nvgCreateImage(vg, file);
snprintf(file, 128, "../example/images/image%d.jpg", i+1);
data->images[i] = nvgCreateImage(vg, file, 0);
if (data->images[i] == 0) { if (data->images[i] == 0) {
printf("Could not load %s.\n", file); printf("Could not load %s.\n", file);
return -1; return -1;
} }
} }


data->fontIcons = nvgCreateFont(vg, "icons", "./nanovg_res/entypo.ttf");
data->fontIcons = nvgCreateFont(vg, "icons", "../example/entypo.ttf");
if (data->fontIcons == -1) { if (data->fontIcons == -1) {
printf("Could not add font icons.\n"); printf("Could not add font icons.\n");
return -1; return -1;
} }
data->fontNormal = nvgCreateFont(vg, "sans", "./nanovg_res/Roboto-Regular.ttf");
data->fontNormal = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf");
if (data->fontNormal == -1) { if (data->fontNormal == -1) {
printf("Could not add font italic.\n"); printf("Could not add font italic.\n");
return -1; return -1;
} }
data->fontBold = nvgCreateFont(vg, "sans-bold", "./nanovg_res/Roboto-Bold.ttf");
data->fontBold = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf");
if (data->fontBold == -1) { if (data->fontBold == -1) {
printf("Could not add font bold.\n"); printf("Could not add font bold.\n");
return -1; return -1;
@@ -793,7 +834,7 @@ int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
return 0; return 0;
} }


void freeDemoData(struct NVGcontext* vg, struct DemoData* data)
void freeDemoData(NVGcontext* vg, DemoData* data)
{ {
int i; int i;


@@ -804,10 +845,10 @@ void freeDemoData(struct NVGcontext* vg, struct DemoData* data)
nvgDeleteImage(vg, data->images[i]); nvgDeleteImage(vg, data->images[i]);
} }


void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
void drawParagraph(NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
{ {
struct NVGtextRow rows[3];
struct NVGglyphPosition glyphs[100];
NVGtextRow rows[3];
NVGglyphPosition glyphs[100];
const char* text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party."; const char* text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.";
const char* start; const char* start;
const char* end; const char* end;
@@ -815,6 +856,7 @@ void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float h
float lineh; float lineh;
float caretx, px; float caretx, px;
float bounds[4]; float bounds[4];
float a;
float gx,gy; float gx,gy;
int gutter = 0; int gutter = 0;
NVG_NOTUSED(height); NVG_NOTUSED(height);
@@ -833,11 +875,11 @@ void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float h
end = text + strlen(text); end = text + strlen(text);
while ((nrows = nvgTextBreakLines(vg, start, end, width, rows, 3))) { while ((nrows = nvgTextBreakLines(vg, start, end, width, rows, 3))) {
for (i = 0; i < nrows; i++) { for (i = 0; i < nrows; i++) {
struct NVGtextRow* row = &rows[i];
NVGtextRow* row = &rows[i];
int hit = mx > x && mx < (x+width) && my >= y && my < (y+lineh); int hit = mx > x && mx < (x+width) && my >= y && my < (y+lineh);


nvgBeginPath(vg); nvgBeginPath(vg);
nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:8));
nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:16));
nvgRect(vg, x, y, row->width, lineh); nvgRect(vg, x, y, row->width, lineh);
nvgFill(vg); nvgFill(vg);


@@ -896,6 +938,14 @@ void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float h
nvgTextLineHeight(vg, 1.2f); nvgTextLineHeight(vg, 1.2f);


nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds); nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds);

// Fade the tooltip out when close to it.
gx = fabsf((mx - (bounds[0]+bounds[2])*0.5f) / (bounds[0] - bounds[2]));
gy = fabsf((my - (bounds[1]+bounds[3])*0.5f) / (bounds[1] - bounds[3]));
a = maxf(gx, gy) - 0.5f;
a = clampf(a, 0, 1);
nvgGlobalAlpha(vg, a);

nvgBeginPath(vg); nvgBeginPath(vg);
nvgFillColor(vg, nvgRGBA(220,220,220,255)); nvgFillColor(vg, nvgRGBA(220,220,220,255));
nvgRoundedRect(vg, bounds[0]-2,bounds[1]-2, (int)(bounds[2]-bounds[0])+4, (int)(bounds[3]-bounds[1])+4, 3); nvgRoundedRect(vg, bounds[0]-2,bounds[1]-2, (int)(bounds[2]-bounds[0])+4, (int)(bounds[3]-bounds[1])+4, 3);
@@ -911,7 +961,7 @@ void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float h
nvgRestore(vg); nvgRestore(vg);
} }


void drawWidths(struct NVGcontext* vg, float x, float y, float width)
void drawWidths(NVGcontext* vg, float x, float y, float width)
{ {
int i; int i;


@@ -932,8 +982,75 @@ void drawWidths(struct NVGcontext* vg, float x, float y, float width)
nvgRestore(vg); nvgRestore(vg);
} }


void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float height,
float t, int blowup, struct DemoData* data)
void drawCaps(NVGcontext* vg, float x, float y, float width)
{
int i;
int caps[3] = {NVG_BUTT, NVG_ROUND, NVG_SQUARE};
float lineWidth = 8.0f;

nvgSave(vg);

nvgBeginPath(vg);
nvgRect(vg, x-lineWidth/2, y, width+lineWidth, 40);
nvgFillColor(vg, nvgRGBA(255,255,255,32));
nvgFill(vg);

nvgBeginPath(vg);
nvgRect(vg, x, y, width, 40);
nvgFillColor(vg, nvgRGBA(255,255,255,32));
nvgFill(vg);

nvgStrokeWidth(vg, lineWidth);
for (i = 0; i < 3; i++) {
nvgLineCap(vg, caps[i]);
nvgStrokeColor(vg, nvgRGBA(0,0,0,255));
nvgBeginPath(vg);
nvgMoveTo(vg, x, y + i*10 + 5);
nvgLineTo(vg, x+width, y + i*10 + 5);
nvgStroke(vg);
}

nvgRestore(vg);
}

void drawScissor(NVGcontext* vg, float x, float y, float t)
{
nvgSave(vg);

// Draw first rect and set scissor to it's area.
nvgTranslate(vg, x, y);
nvgRotate(vg, nvgDegToRad(5));
nvgBeginPath(vg);
nvgRect(vg, -20,-20,60,40);
nvgFillColor(vg, nvgRGBA(255,0,0,255));
nvgFill(vg);
nvgScissor(vg, -20,-20,60,40);

// Draw second rectangle with offset and rotation.
nvgTranslate(vg, 40,0);
nvgRotate(vg, t);

// Draw the intended second rectangle without any scissoring.
nvgSave(vg);
nvgResetScissor(vg);
nvgBeginPath(vg);
nvgRect(vg, -20,-10,60,30);
nvgFillColor(vg, nvgRGBA(255,128,0,64));
nvgFill(vg);
nvgRestore(vg);

// Draw second rectangle with combined scissoring.
nvgIntersectScissor(vg, -20,-10,60,30);
nvgBeginPath(vg);
nvgRect(vg, -20,-10,60,30);
nvgFillColor(vg, nvgRGBA(255,128,0,255));
nvgFill(vg);

nvgRestore(vg);
}

void renderDemo(NVGcontext* vg, float mx, float my, float width, float height,
float t, int blowup, DemoData* data)
{ {
float x,y,popy; float x,y,popy;


@@ -943,11 +1060,16 @@ void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float he
drawColorwheel(vg, width - 300, height - 300, 250.0f, 250.0f, t); drawColorwheel(vg, width - 300, height - 300, 250.0f, 250.0f, t);


// Line joints // Line joints
drawLines(vg, 50, height-50, 600, 50, t);
drawLines(vg, 120, height-50, 600, 50, t);


// Line width;
// Line caps
drawWidths(vg, 10, 50, 30); drawWidths(vg, 10, 50, 30);


// Line caps
drawCaps(vg, 10, 300, 30);

drawScissor(vg, 50, height-80, t);

nvgSave(vg); nvgSave(vg);
if (blowup) { if (blowup) {
nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI); nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI);
@@ -1065,8 +1187,8 @@ static void flipHorizontal(unsigned char* image, int w, int h, int stride)
{ {
int i = 0, j = h-1, k; int i = 0, j = h-1, k;
while (i < j) { while (i < j) {
unsigned char* ri = &image[i * stride];
unsigned char* rj = &image[j * stride];
unsigned char* ri = &image[i * stride];
unsigned char* rj = &image[j * stride];
for (k = 0; k < w*4; k++) { for (k = 0; k < w*4; k++) {
unsigned char t = ri[k]; unsigned char t = ri[k];
ri[k] = rj[k]; ri[k] = rj[k];


+ 9
- 8
examples/nanovg_res/demo.h View File

@@ -1,20 +1,21 @@
#ifndef DEMO_H #ifndef DEMO_H
#define DEMO_H #define DEMO_H


#include "nanovg.h"

#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif


struct NVGcontext;

struct DemoData { struct DemoData {
int fontNormal, fontBold, fontIcons;
int images[12];
int fontNormal, fontBold, fontIcons;
int images[12];
}; };
typedef struct DemoData DemoData;


int loadDemoData(struct NVGcontext* vg, struct DemoData* data);
void freeDemoData(struct NVGcontext* vg, struct DemoData* data);
void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float height, float t, int blowup, struct DemoData* data);
int loadDemoData(NVGcontext* vg, DemoData* data);
void freeDemoData(NVGcontext* vg, DemoData* data);
void renderDemo(NVGcontext* vg, float mx, float my, float width, float height, float t, int blowup, DemoData* data);


void saveScreenShot(int w, int h, int premult, const char* name); void saveScreenShot(int w, int h, int premult, const char* name);


@@ -22,4 +23,4 @@ void saveScreenShot(int w, int h, int premult, const char* name);
} }
#endif #endif


#endif // DEMO_H
#endif // DEMO_H

+ 26
- 8
examples/nanovg_res/perf.c View File

@@ -17,7 +17,7 @@
//pfnGLGETQUERYOBJECTUI64V glGetQueryObjectui64v = 0; //pfnGLGETQUERYOBJECTUI64V glGetQueryObjectui64v = 0;
#endif #endif


void initGPUTimer(struct GPUtimer* timer)
void initGPUTimer(GPUtimer* timer)
{ {
memset(timer, 0, sizeof(*timer)); memset(timer, 0, sizeof(*timer));


@@ -35,7 +35,7 @@ void initGPUTimer(struct GPUtimer* timer)
}*/ }*/
} }


void startGPUTimer(struct GPUtimer* timer)
void startGPUTimer(GPUtimer* timer)
{ {
if (!timer->supported) if (!timer->supported)
return; return;
@@ -43,8 +43,10 @@ void startGPUTimer(struct GPUtimer* timer)
timer->cur++; timer->cur++;
} }


int stopGPUTimer(struct GPUtimer* timer, float* times, int maxTimes)
int stopGPUTimer(GPUtimer* timer, float* times, int maxTimes)
{ {
NVG_NOTUSED(times);
NVG_NOTUSED(maxTimes);
GLint available = 1; GLint available = 1;
int n = 0; int n = 0;
if (!timer->supported) if (!timer->supported)
@@ -68,21 +70,21 @@ int stopGPUTimer(struct GPUtimer* timer, float* times, int maxTimes)
} }




void initGraph(struct PerfGraph* fps, int style, const char* name)
void initGraph(PerfGraph* fps, int style, const char* name)
{ {
memset(fps, 0, sizeof(struct PerfGraph));
memset(fps, 0, sizeof(PerfGraph));
fps->style = style; fps->style = style;
strncpy(fps->name, name, sizeof(fps->name)); strncpy(fps->name, name, sizeof(fps->name));
fps->name[sizeof(fps->name)-1] = '\0'; fps->name[sizeof(fps->name)-1] = '\0';
} }


void updateGraph(struct PerfGraph* fps, float frameTime)
void updateGraph(PerfGraph* fps, float frameTime)
{ {
fps->head = (fps->head+1) % GRAPH_HISTORY_COUNT; fps->head = (fps->head+1) % GRAPH_HISTORY_COUNT;
fps->values[fps->head] = frameTime; fps->values[fps->head] = frameTime;
} }


float getGraphAverage(struct PerfGraph* fps)
float getGraphAverage(PerfGraph* fps)
{ {
int i; int i;
float avg = 0; float avg = 0;
@@ -92,7 +94,7 @@ float getGraphAverage(struct PerfGraph* fps)
return avg / (float)GRAPH_HISTORY_COUNT; return avg / (float)GRAPH_HISTORY_COUNT;
} }


void renderGraph(struct NVGcontext* vg, float x, float y, struct PerfGraph* fps)
void renderGraph(NVGcontext* vg, float x, float y, PerfGraph* fps)
{ {
int i; int i;
float avg, w, h; float avg, w, h;
@@ -119,6 +121,15 @@ void renderGraph(struct NVGcontext* vg, float x, float y, struct PerfGraph* fps)
vy = y + h - ((v / 80.0f) * h); vy = y + h - ((v / 80.0f) * h);
nvgLineTo(vg, vx, vy); nvgLineTo(vg, vx, vy);
} }
} else if (fps->style == GRAPH_RENDER_PERCENT) {
for (i = 0; i < GRAPH_HISTORY_COUNT; i++) {
float v = fps->values[(fps->head+i) % GRAPH_HISTORY_COUNT] * 1.0f;
float vx, vy;
if (v > 100.0f) v = 100.0f;
vx = x + ((float)i/(GRAPH_HISTORY_COUNT-1)) * w;
vy = y + h - ((v / 100.0f) * h);
nvgLineTo(vg, vx, vy);
}
} else { } else {
for (i = 0; i < GRAPH_HISTORY_COUNT; i++) { for (i = 0; i < GRAPH_HISTORY_COUNT; i++) {
float v = fps->values[(fps->head+i) % GRAPH_HISTORY_COUNT] * 1000.0f; float v = fps->values[(fps->head+i) % GRAPH_HISTORY_COUNT] * 1000.0f;
@@ -154,6 +165,13 @@ void renderGraph(struct NVGcontext* vg, float x, float y, struct PerfGraph* fps)
nvgFillColor(vg, nvgRGBA(240,240,240,160)); nvgFillColor(vg, nvgRGBA(240,240,240,160));
sprintf(str, "%.2f ms", avg * 1000.0f); sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+w-3,y+h-1, str, NULL); nvgText(vg, x+w-3,y+h-1, str, NULL);
}
else if (fps->style == GRAPH_RENDER_PERCENT) {
nvgFontSize(vg, 18.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.1f %%", avg * 1.0f);
nvgText(vg, x+w-3,y+1, str, NULL);
} else { } else {
nvgFontSize(vg, 18.0f); nvgFontSize(vg, 18.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP); nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_TOP);


+ 20
- 17
examples/nanovg_res/perf.h View File

@@ -1,43 +1,46 @@
#ifndef PERF_H #ifndef PERF_H
#define PERF_H #define PERF_H


#include "nanovg.h"

#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif


struct NVGcontext;

enum GraphrenderStyle { enum GraphrenderStyle {
GRAPH_RENDER_FPS, GRAPH_RENDER_FPS,
GRAPH_RENDER_MS, GRAPH_RENDER_MS,
GRAPH_RENDER_PERCENT,
}; };


#define GRAPH_HISTORY_COUNT 100 #define GRAPH_HISTORY_COUNT 100
struct PerfGraph { struct PerfGraph {
int style;
char name[32];
float values[GRAPH_HISTORY_COUNT];
int head;
int style;
char name[32];
float values[GRAPH_HISTORY_COUNT];
int head;
}; };
typedef struct PerfGraph PerfGraph;


void initGraph(struct PerfGraph* fps, int style, const char* name);
void updateGraph(struct PerfGraph* fps, float frameTime);
void renderGraph(struct NVGcontext* vg, float x, float y, struct PerfGraph* fps);
float getGraphAverage(struct PerfGraph* fps);
void initGraph(PerfGraph* fps, int style, const char* name);
void updateGraph(PerfGraph* fps, float frameTime);
void renderGraph(NVGcontext* vg, float x, float y, PerfGraph* fps);
float getGraphAverage(PerfGraph* fps);


#define GPU_QUERY_COUNT 5 #define GPU_QUERY_COUNT 5
struct GPUtimer { struct GPUtimer {
int supported;
int cur, ret;
unsigned int queries[GPU_QUERY_COUNT];
int supported;
int cur, ret;
unsigned int queries[GPU_QUERY_COUNT];
}; };
typedef struct GPUtimer GPUtimer;


void initGPUTimer(struct GPUtimer* timer);
void startGPUTimer(struct GPUtimer* timer);
int stopGPUTimer(struct GPUtimer* timer, float* times, int maxTimes);
void initGPUTimer(GPUtimer* timer);
void startGPUTimer(GPUtimer* timer);
int stopGPUTimer(GPUtimer* timer, float* times, int maxTimes);


#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif


#endif // PERF_H
#endif // PERF_H

+ 4
- 4
examples/widgets/NanoPerfWidget.hpp View File

@@ -21,12 +21,12 @@
// DGL Stuff // DGL Stuff


#include "NanoVG.hpp" #include "NanoVG.hpp"
#include "../../dpf/distrho/extra/d_string.hpp"
#include "../../dpf/distrho/extra/String.hpp"


// ------------------------------------------------------ // ------------------------------------------------------
// use namespace // use namespace


using DISTRHO::d_string;
using DISTRHO::String;
using DGL::IdleCallback; using DGL::IdleCallback;
using DGL::NanoVG; using DGL::NanoVG;
using DGL::NanoWidget; using DGL::NanoWidget;
@@ -222,8 +222,8 @@ private:
int fHead; int fHead;
float fValues[kHistoryCount]; float fValues[kHistoryCount];


const int fStyle;
const d_string fName;
const int fStyle;
const String fName;


double prevt; double prevt;




Loading…
Cancel
Save