commit 2f1fb1b2de8d4eb0fd7d5f8dd54ba492cf2d4da5
parent 8a1c6cdb728b95223501943012bdca23f1051f1a
Author: falkTX <falktx@gmail.com>
Date: Fri, 30 May 2014 01:13:06 +0100
Fixes for NanoVG images
Diffstat:
2 files changed, 27 insertions(+), 72 deletions(-)
diff --git a/dgl/NanoVG.hpp b/dgl/NanoVG.hpp
@@ -38,11 +38,6 @@ class NanoImage
{
public:
/**
- Constructor for null image.
- */
- NanoImage() noexcept;
-
- /**
Destructor.
*/
~NanoImage();
@@ -62,25 +57,19 @@ public:
*/
void updateImage(const uchar* data);
- /**
- Operator =.
- Takes the image data from @a img, invalidating it.
- */
- NanoImage operator=(NanoImage img) noexcept;
-
protected:
/**
Constructors are protected.
NanoImages must be created within a NanoVG or NanoWidget class.
*/
- NanoImage(const char* filename);
- NanoImage(uchar* data, int ndata);
- NanoImage(int w, int h, const uchar* data);
+ NanoImage(NVGcontext* context, int imageId) noexcept;
private:
NVGcontext* fContext;
int fImageId;
friend class NanoVG;
+
+ DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoImage)
};
// -----------------------------------------------------------------------
@@ -534,17 +523,17 @@ public:
/**
Creates image by loading it from the disk from specified file name.
*/
- NanoImage createImage(const char* filename);
+ NanoImage* createImage(const char* filename);
/**
Creates image by loading it from the specified chunk of memory.
*/
- NanoImage createImageMem(uchar* data, int ndata);
+ NanoImage* createImageMem(uchar* data, int ndata);
/**
Creates image from specified image data.
*/
- NanoImage createImageRGBA(int w, int h, const uchar* data);
+ NanoImage* createImageRGBA(int w, int h, const uchar* data);
/* --------------------------------------------------------------------
* Paints */
@@ -573,12 +562,12 @@ public:
Paint radialGradient(float cx, float cy, float inr, float outr, const Color& icol, const Color& ocol);
/**
- Creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
+ Creates and returns an image pattern. 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 tells if the image should be repeated across x or y.
The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
*/
- Paint imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage& image, PatternRepeat repeat);
+ Paint imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage* image, PatternRepeat repeat);
/* --------------------------------------------------------------------
* Scissoring */
diff --git a/dgl/src/NanoVG.cpp b/dgl/src/NanoVG.cpp
@@ -49,7 +49,7 @@ NanoVG::Color::Color(const NVGcolor& c) noexcept
NanoVG::Color::operator NVGcolor() const noexcept
{
- NVGcolor nc = { r, g, b, a };
+ NVGcolor nc = {{{ r, g, b, a }}};
return nc;
}
@@ -84,33 +84,9 @@ NanoVG::Paint::operator NVGpaint() const noexcept
// -----------------------------------------------------------------------
// NanoImage
-static NVGcontext* sLastContext = nullptr;
-
-NanoImage::NanoImage() noexcept
- : fContext(nullptr),
- fImageId(0) {}
-
-#if 0
-NanoImage::NanoImage(NanoImage& img) noexcept
- : fContext(img.fContext),
- fImageId(img.fImageId)
-{
- img.fContext = nullptr;
- img.fImageId = 0;
-}
-#endif
-
-NanoImage::NanoImage(const char* filename)
- : fContext(sLastContext),
- fImageId((fContext != nullptr) ? nvgCreateImage(fContext, filename) : 0) {}
-
-NanoImage::NanoImage(uchar* data, int ndata)
- : fContext(sLastContext),
- fImageId((fContext != nullptr) ? nvgCreateImageMem(fContext, data, ndata) : 0) {}
-
-NanoImage::NanoImage(int w, int h, const uchar* data)
- : fContext(sLastContext),
- fImageId((fContext != nullptr) ? nvgCreateImageRGBA(fContext, w, h, data) : 0) {}
+NanoImage::NanoImage(NVGcontext* context, int imageId) noexcept
+ : fContext(context),
+ fImageId(imageId) {}
NanoImage::~NanoImage()
{
@@ -139,20 +115,6 @@ void NanoImage::updateImage(const uchar* data)
nvgUpdateImage(fContext, fImageId, data);
}
-NanoImage NanoImage::operator=(NanoImage img) noexcept
-{
- if (fContext != nullptr && fImageId != 0)
- nvgDeleteImage(fContext, fImageId);
-
- fContext = img.fContext;
- fImageId = img.fImageId;
-
- img.fContext = nullptr;
- img.fImageId = 0;
-
- return *this;
-}
-
// -----------------------------------------------------------------------
// NanoVG
@@ -401,22 +363,25 @@ float NanoVG::radToDeg(float rad)
// -----------------------------------------------------------------------
// Images
-NanoImage NanoVG::createImage(const char* filename)
+NanoImage* NanoVG::createImage(const char* filename)
{
- sLastContext = fContext;
- return NanoImage(filename);
+ if (const int imageId = nvgCreateImage(fContext, filename))
+ return new NanoImage(fContext, imageId);
+ return nullptr;
}
-NanoImage NanoVG::createImageMem(uchar* data, int ndata)
+NanoImage* NanoVG::createImageMem(uchar* data, int ndata)
{
- sLastContext = fContext;
- return NanoImage(data, ndata);
+ if (const int imageId = nvgCreateImageMem(fContext, data, ndata))
+ return new NanoImage(fContext, imageId);
+ return nullptr;
}
-NanoImage NanoVG::createImageRGBA(int w, int h, const uchar* data)
+NanoImage* NanoVG::createImageRGBA(int w, int h, const uchar* data)
{
- sLastContext = fContext;
- return NanoImage(w, h, data);
+ if (const int imageId = nvgCreateImageRGBA(fContext, w, h, data))
+ return new NanoImage(fContext, imageId);
+ return nullptr;
}
// -----------------------------------------------------------------------
@@ -437,9 +402,10 @@ 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, NanoVG::PatternRepeat repeat)
{
- return nvgImagePattern(fContext, ox, oy, ex, ey, angle, image.fImageId, repeat);
+ DISTRHO_SAFE_ASSERT_RETURN(image != nullptr, Paint());
+ return nvgImagePattern(fContext, ox, oy, ex, ey, angle, image->fImageId, repeat);
}
// -----------------------------------------------------------------------