Browse Source

Fixes for NanoVG images

gh-pages
falkTX 11 years ago
parent
commit
2f1fb1b2de
2 changed files with 27 additions and 72 deletions
  1. +8
    -19
      dgl/NanoVG.hpp
  2. +19
    -53
      dgl/src/NanoVG.cpp

+ 8
- 19
dgl/NanoVG.hpp View File

@@ -37,11 +37,6 @@ START_NAMESPACE_DGL
class NanoImage class NanoImage
{ {
public: public:
/**
Constructor for null image.
*/
NanoImage() noexcept;

/** /**
Destructor. Destructor.
*/ */
@@ -62,25 +57,19 @@ public:
*/ */
void updateImage(const uchar* data); void updateImage(const uchar* data);


/**
Operator =.
Takes the image data from @a img, invalidating it.
*/
NanoImage operator=(NanoImage img) noexcept;

protected: protected:
/** /**
Constructors are protected. Constructors are protected.
NanoImages must be created within a NanoVG or NanoWidget class. 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: private:
NVGcontext* fContext; NVGcontext* fContext;
int fImageId; int fImageId;
friend class NanoVG; 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. 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. 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. 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 */ * Paints */
@@ -573,12 +562,12 @@ public:
Paint radialGradient(float cx, float cy, float inr, float outr, const Color& icol, const Color& ocol); 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, (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. 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(). 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 */ * Scissoring */


+ 19
- 53
dgl/src/NanoVG.cpp View File

@@ -49,7 +49,7 @@ NanoVG::Color::Color(const NVGcolor& c) noexcept


NanoVG::Color::operator NVGcolor() const noexcept NanoVG::Color::operator NVGcolor() const noexcept
{ {
NVGcolor nc = { r, g, b, a };
NVGcolor nc = {{{ r, g, b, a }}};
return nc; return nc;
} }


@@ -84,33 +84,9 @@ NanoVG::Paint::operator NVGpaint() const noexcept
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// NanoImage // 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() NanoImage::~NanoImage()
{ {
@@ -139,20 +115,6 @@ void NanoImage::updateImage(const uchar* data)
nvgUpdateImage(fContext, fImageId, 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 // NanoVG


@@ -401,22 +363,25 @@ float NanoVG::radToDeg(float rad)
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// Images // 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); 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);
} }


// ----------------------------------------------------------------------- // -----------------------------------------------------------------------


Loading…
Cancel
Save