From c5a6d6f7dc5119851cbc14dbfaa4d248985685d2 Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 27 Apr 2015 22:57:47 +0200 Subject: [PATCH] Finish updating, code builds again --- dgl/Color.hpp | 2 -- dgl/NanoVG.hpp | 37 +++++++++++++++++++++------- dgl/src/NanoVG.cpp | 61 +++++++++++++++++++++++++++++++++------------- 3 files changed, 72 insertions(+), 28 deletions(-) diff --git a/dgl/Color.hpp b/dgl/Color.hpp index 182e9214..037fe0a7 100644 --- a/dgl/Color.hpp +++ b/dgl/Color.hpp @@ -25,8 +25,6 @@ START_NAMESPACE_DGL // ----------------------------------------------------------------------- -// TODO: create color from "#333" and "#112233" like strings - /** A color made from red, green, blue and alpha floating-point values in [0..1] range. */ diff --git a/dgl/NanoVG.hpp b/dgl/NanoVG.hpp index 495aba1a..b0eaf0bf 100644 --- a/dgl/NanoVG.hpp +++ b/dgl/NanoVG.hpp @@ -168,6 +168,33 @@ private: class NanoVG { public: + enum CreateFlags { + /** + Flag indicating if geometry based anti-aliasing is used (may not be needed when using MSAA). + */ + CREATE_ANTIALIAS = 1 << 0, + + /** + Flag indicating if strokes should be drawn using stencil buffer. The rendering will be a little + slower, but path overlaps (i.e. self-intersecting or sharp turns) will be drawn just once. + */ + CREATE_STENCIL_STROKES = 1 << 1, + + /** + Flag indicating that additional debug checks are done. + */ + CREATE_DEBUG = 1 << 2, + }; + + enum ImageFlags { + IMAGE_GENERATE_MIPMAPS = 1 << 0, // Generate mipmaps during creation of the image. + IMAGE_REPEAT_X = 1 << 1, // Repeat image in X direction. + IMAGE_REPEAT_Y = 1 << 2, // Repeat image in Y direction. + IMAGE_FLIP_Y = 1 << 3, // Flips (inverses) image in Y direction when rendered. + IMAGE_PREMULTIPLIED = 1 << 4, // Image data has premultiplied alpha. + IMAGE_NODELETE = 1 << 16,// Do not delete GL texture handle. + }; + enum Align { // Horizontal align ALIGN_LEFT = 1 << 0, // Align horizontally to left (default). @@ -180,14 +207,6 @@ public: ALIGN_BASELINE = 1 << 6 // Align vertically to baseline (default). }; - enum ImageFlags { - IMAGE_GENERATE_MIPMAPS = 1 << 0, // Generate mipmaps during creation of the image. - IMAGE_REPEAT_X = 1 << 1, // Repeat image in X direction. - IMAGE_REPEAT_Y = 1 << 2, // Repeat image in Y direction. - IMAGE_FLIP_Y = 1 << 3, // Flips (inverses) image in Y direction when rendered. - IMAGE_PREMULTIPLIED = 1 << 4, // Image data has premultiplied alpha. - }; - enum LineCap { BUTT, ROUND, @@ -243,7 +262,7 @@ public: /** Constructor. */ - NanoVG(int textAtlasWidth = 512, int textAtlasHeight = 512); + NanoVG(int flags = CREATE_ANTIALIAS); /** Destructor. diff --git a/dgl/src/NanoVG.cpp b/dgl/src/NanoVG.cpp index 2c6e0af5..2bccd8cf 100644 --- a/dgl/src/NanoVG.cpp +++ b/dgl/src/NanoVG.cpp @@ -43,7 +43,7 @@ // ----------------------------------------------------------------------- // Include NanoVG OpenGL implementation -#define STB_IMAGE_STATIC 1 +//#define STB_IMAGE_STATIC 1 #define NANOVG_GL2_IMPLEMENTATION 1 #include "nanovg/nanovg_gl.h" @@ -80,14 +80,14 @@ START_NAMESPACE_DGL // Paint NanoVG::Paint::Paint() noexcept - : radius(0.0f), feather(0.0f), innerColor(), outerColor(), imageId(0), repeat(REPEAT_NONE) + : radius(0.0f), feather(0.0f), innerColor(), outerColor(), imageId(0) { std::memset(xform, 0, sizeof(float)*6); std::memset(extent, 0, sizeof(float)*2); } NanoVG::Paint::Paint(const NVGpaint& p) noexcept - : radius(p.radius), feather(p.feather), innerColor(p.innerColor), outerColor(p.outerColor), imageId(p.image), repeat(static_cast(p.repeat)) + : radius(p.radius), feather(p.feather), innerColor(p.innerColor), outerColor(p.outerColor), imageId(p.image) { std::memcpy(xform, p.xform, sizeof(float)*6); std::memcpy(extent, p.extent, sizeof(float)*2); @@ -101,7 +101,6 @@ NanoVG::Paint::operator NVGpaint() const noexcept p.innerColor = innerColor; p.outerColor = outerColor; p.image = imageId; - p.repeat = repeat; std::memcpy(p.xform, xform, sizeof(float)*6); std::memcpy(p.extent, extent, sizeof(float)*2); return p; @@ -159,8 +158,8 @@ void NanoImage::_updateSize() // ----------------------------------------------------------------------- // NanoVG -NanoVG::NanoVG(int textAtlasWidth, int textAtlasHeight) - : fContext(nvgCreateGL(textAtlasWidth, textAtlasHeight, NVG_ANTIALIAS)), +NanoVG::NanoVG(int flags) + : fContext(nvgCreateGL(flags)), fInFrame(false), leakDetector_NanoVG() { @@ -177,14 +176,14 @@ NanoVG::~NanoVG() // ----------------------------------------------------------------------- -void NanoVG::beginFrame(const uint width, const uint height, const float scaleFactor, const Alpha alpha) +void NanoVG::beginFrame(const uint width, const uint height, const float scaleFactor) { if (fContext == nullptr) return; DISTRHO_SAFE_ASSERT_RETURN(scaleFactor > 0.0f,); DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,); fInFrame = true; - nvgBeginFrame(fContext, static_cast(width), static_cast(height), scaleFactor, static_cast(alpha)); + nvgBeginFrame(fContext, static_cast(width), static_cast(height), scaleFactor); } void NanoVG::beginFrame(Widget* const widget) @@ -196,7 +195,17 @@ void NanoVG::beginFrame(Widget* const widget) Window& window(widget->getParentWindow()); fInFrame = true; - nvgBeginFrame(fContext, static_cast(window.getWidth()), static_cast(window.getHeight()), 1.0f, NVG_PREMULTIPLIED_ALPHA); + nvgBeginFrame(fContext, static_cast(window.getWidth()), static_cast(window.getHeight()), 1.0f); +} + +void NanoVG::cancelFrame() +{ + DISTRHO_SAFE_ASSERT_RETURN(fInFrame,); + + if (fContext != nullptr) + nvgCancelFrame(fContext); + + fInFrame = false; } void NanoVG::endFrame() @@ -329,6 +338,12 @@ void NanoVG::lineJoin(NanoVG::LineCap join) nvgLineJoin(fContext, join); } +void NanoVG::globalAlpha(float alpha) +{ + if (fContext != nullptr) + nvgGlobalAlpha(fContext, alpha); +} + // ----------------------------------------------------------------------- // Transforms @@ -452,35 +467,35 @@ float NanoVG::radToDeg(float rad) // ----------------------------------------------------------------------- // Images -NanoImage* NanoVG::createImage(const char* filename) +NanoImage* NanoVG::createImage(const char* filename, int imageFlags) { if (fContext == nullptr) return nullptr; DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr); - if (const int imageId = nvgCreateImage(fContext, filename)) + if (const int imageId = nvgCreateImage(fContext, filename, imageFlags)) return new NanoImage(fContext, imageId); return nullptr; } -NanoImage* NanoVG::createImageMem(uchar* data, int ndata) +NanoImage* NanoVG::createImageMem(uchar* data, int ndata, int imageFlags) { if (fContext == nullptr) return nullptr; DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, nullptr); DISTRHO_SAFE_ASSERT_RETURN(ndata > 0, nullptr); - if (const int imageId = nvgCreateImageMem(fContext, data, ndata)) + if (const int imageId = nvgCreateImageMem(fContext, imageFlags, data, ndata)) return new NanoImage(fContext, imageId); return nullptr; } -NanoImage* NanoVG::createImageRGBA(uint w, uint h, const uchar* data) +NanoImage* NanoVG::createImageRGBA(uint w, uint h, const uchar* data, int imageFlags) { if (fContext == nullptr) return nullptr; DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, nullptr); - if (const int imageId = nvgCreateImageRGBA(fContext, static_cast(w), static_cast(h), data)) + if (const int imageId = nvgCreateImageRGBA(fContext, static_cast(w), static_cast(h), imageFlags, data)) return new NanoImage(fContext, imageId); return nullptr; @@ -507,12 +522,12 @@ NanoVG::Paint NanoVG::radialGradient(float cx, float cy, float inr, float outr, return nvgRadialGradient(fContext, cx, cy, inr, outr, icol, ocol); } -NanoVG::Paint NanoVG::imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage* image, NanoVG::PatternRepeat repeat) +NanoVG::Paint NanoVG::imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage* image, float alpha) { if (fContext == nullptr) return Paint(); DISTRHO_SAFE_ASSERT_RETURN(image != nullptr, Paint()); - return nvgImagePattern(fContext, ox, oy, ex, ey, angle, image->fImageId, repeat); + return nvgImagePattern(fContext, ox, oy, ex, ey, angle, image->fImageId, alpha); } // ----------------------------------------------------------------------- @@ -524,6 +539,12 @@ void NanoVG::scissor(float x, float y, float w, float h) nvgScissor(fContext, x, y, w, h); } +void NanoVG::intersectScissor(float x, float y, float w, float h) +{ + if (fContext != nullptr) + nvgIntersectScissor(fContext, x, y, w, h); +} + void NanoVG::resetScissor() { if (fContext != nullptr) @@ -557,6 +578,12 @@ void NanoVG::bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float nvgBezierTo(fContext, c1x, c1y, c2x, c2y, x, y); } +void NanoVG::quadTo(float cx, float cy, float x, float y) +{ + if (fContext != nullptr) + nvgQuadTo(fContext, cx, cy, x, y); +} + void NanoVG::arcTo(float x1, float y1, float x2, float y2, float radius) { if (fContext != nullptr)