DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit ba4b903cbf5c5424716236333914666725d96b6f
parent f55f3c85ae9c543f3c767559fdedd075f0840340
Author: falkTX <falktx@falktx.com>
Date:   Mon, 17 May 2021 02:05:55 +0100

Rename core Geometry vars, start proper graphics context use

Diffstat:
Mdgl/Cairo.hpp | 2+-
Mdgl/Color.hpp | 5+++++
Mdgl/Geometry.hpp | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Mdgl/src/Cairo.cpp | 147++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mdgl/src/Geometry.cpp | 244+++++++++++++++++++++++++++++++++++--------------------------------------------
Mdgl/src/OpenGL.cpp | 136++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mtests/Demo.cpp | 36++++++++++++++----------------------
Mtests/widgets/ExampleColorWidget.hpp | 10+++++-----
Mtests/widgets/ExampleRectanglesWidget.hpp | 21+++++++++------------
Mtests/widgets/ExampleShapesWidget.hpp | 57+++++++++++++++++++++++----------------------------------
10 files changed, 475 insertions(+), 268 deletions(-)

diff --git a/dgl/Cairo.hpp b/dgl/Cairo.hpp @@ -130,7 +130,7 @@ protected: private: /** Widget display function. - Implemented internally to wrap begin/endFrame() automatically. + Implemented internally to pass context into the drawing function. */ void onDisplay() override { diff --git a/dgl/Color.hpp b/dgl/Color.hpp @@ -96,6 +96,11 @@ struct Color { void fixBounds() noexcept; /** + Set this color for use in the next drawing operation for the provided context. + */ + void setFor(const GraphicsContext& context, bool includeAlpha = false); + + /** @internal Needed for NanoVG compatibility. */ diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp @@ -114,7 +114,7 @@ public: bool operator!=(const Point<T>& pos) const noexcept; private: - T fX, fY; + T x, y; template<typename> friend class Line; template<typename> friend class Circle; template<typename> friend class Triangle; @@ -348,13 +348,6 @@ public: */ void moveBy(const Point<T>& pos) noexcept; -#ifndef DPF_TEST_POINT_CPP - /** - Draw this line using the current OpenGL state. - */ - void draw(); -#endif - /** Return true if line is null (start and end pos are equal). */ @@ -365,12 +358,28 @@ public: */ bool isNotNull() const noexcept; +#ifndef DPF_TEST_POINT_CPP + /** + Draw this line using the provided graphics context, optionally specifying line width. + */ + void draw(const GraphicsContext& context, T width = 1); +#endif + Line<T>& operator=(const Line<T>& line) noexcept; bool operator==(const Line<T>& line) const noexcept; bool operator!=(const Line<T>& line) const noexcept; +#ifndef DPF_TEST_POINT_CPP + /** + Draw this line using the current OpenGL state. + DEPRECATED please use draw(const GraphicsContext&) instead. + */ + // TODO mark deprecated + void draw(); +#endif + private: - Point<T> fPosStart, fPosEnd; + Point<T> posStart, posEnd; }; // ----------------------------------------------------------------------- @@ -464,22 +473,36 @@ public: */ void setNumSegments(const uint num); + /** + Draw this circle using the provided graphics context. + */ + void draw(const GraphicsContext& context); + + /** + Draw lines (outline of this circle) using the provided graphics context, optionally specifying line width. + */ + void drawOutline(const GraphicsContext& context, T lineWidth = 1); + + Circle<T>& operator=(const Circle<T>& cir) noexcept; + bool operator==(const Circle<T>& cir) const noexcept; + bool operator!=(const Circle<T>& cir) const noexcept; + #ifndef DPF_TEST_POINT_CPP /** Draw this circle using the current OpenGL state. + DEPRECATED please use draw(const GraphicsContext&) instead. */ + // TODO mark deprecated void draw(); /** Draw lines (outline of this circle) using the current OpenGL state. + DEPRECATED please use draw(const GraphicsContext&) instead. */ + // TODO mark deprecated void drawOutline(); #endif - Circle<T>& operator=(const Circle<T>& cir) noexcept; - bool operator==(const Circle<T>& cir) const noexcept; - bool operator!=(const Circle<T>& cir) const noexcept; - private: Point<T> fPos; float fSize; @@ -487,9 +510,6 @@ private: // cached values float fTheta, fCos, fSin; - - /** @internal */ - void _draw(const bool outline); }; // ----------------------------------------------------------------------- @@ -546,29 +566,38 @@ public: */ bool isInvalid() const noexcept; + /** + Draw this triangle using the provided graphics context. + */ + void draw(const GraphicsContext& context); + + /** + Draw lines (outline of this triangle) using the provided graphics context, optionally specifying line width. + */ + void drawOutline(const GraphicsContext& context, T lineWidth = 1); + + Triangle<T>& operator=(const Triangle<T>& tri) noexcept; + bool operator==(const Triangle<T>& tri) const noexcept; + bool operator!=(const Triangle<T>& tri) const noexcept; + #ifndef DPF_TEST_POINT_CPP /** Draw this triangle using the current OpenGL state. + DEPRECATED please use draw(const GraphicsContext&) instead. */ + // TODO mark deprecated void draw(); /** Draw lines (outline of this triangle) using the current OpenGL state. + DEPRECATED please use draw(const GraphicsContext&) instead. */ + // TODO mark deprecated void drawOutline(); #endif - Triangle<T>& operator=(const Triangle<T>& tri) noexcept; - bool operator==(const Triangle<T>& tri) const noexcept; - bool operator!=(const Triangle<T>& tri) const noexcept; - private: - Point<T> fPos1, fPos2, fPos3; - -#ifndef DPF_TEST_POINT_CPP - /** @internal */ - void _draw(const bool outline); -#endif + Point<T> pos1, pos2, pos3; }; // ----------------------------------------------------------------------- @@ -761,9 +790,9 @@ public: void draw(const GraphicsContext& context); /** - Draw lines (outline of this rectangle) using the provided graphics context. + Draw lines (outline of this rectangle) using the provided graphics context, optionally specifying line width. */ - void drawOutline(const GraphicsContext& context); + void drawOutline(const GraphicsContext& context, T lineWidth = 1); Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept; Rectangle<T>& operator*=(double m) noexcept; diff --git a/dgl/src/Cairo.cpp b/dgl/src/Cairo.cpp @@ -15,6 +15,7 @@ */ #include "../Cairo.hpp" +#include "../Color.hpp" #include "SubWidgetPrivateData.hpp" #include "TopLevelWidgetPrivateData.hpp" @@ -31,9 +32,36 @@ static void notImplemented(const char* const name) } // ----------------------------------------------------------------------- +// Color + +void Color::setFor(const GraphicsContext& context, const bool includeAlpha) +{ + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + if (includeAlpha) + cairo_set_source_rgba(handle, red, green, blue, alpha); + else + cairo_set_source_rgb(handle, red, green, blue); +} + +// ----------------------------------------------------------------------- // Line template<typename T> +void Line<T>::draw(const GraphicsContext& context, const T width) +{ + DISTRHO_SAFE_ASSERT_RETURN(posStart != posEnd,); + DISTRHO_SAFE_ASSERT_RETURN(width != 0,); + + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + cairo_set_line_width(handle, width); + cairo_move_to(handle, posStart.getX(), posStart.getY()); + cairo_line_to(handle, posEnd.getX(), posEnd.getY()); + cairo_stroke(handle); +} + +template<typename T> void Line<T>::draw() { notImplemented("Line::draw"); @@ -50,11 +78,67 @@ template class Line<ushort>; // Circle template<typename T> -void Circle<T>::_draw(const bool outline) +static void drawCircle(cairo_t* const handle, + const Point<T>& pos, + const uint numSegments, + const float size, + const float sin, + const float cos, + const bool outline) +{ + DISTRHO_SAFE_ASSERT_RETURN(numSegments >= 3 && size > 0.0f,); + + const T origx = pos.getX(); + const T origy = pos.getY(); + double t, x = size, y = 0.0; + + /* + glBegin(outline ? GL_LINE_LOOP : GL_POLYGON); + + for (uint i=0; i<numSegments; ++i) + { + glVertex2d(x + origx, y + origy); + + t = x; + x = cos * x - sin * y; + y = sin * t + cos * y; + } + + glEnd(); + */ +} + +template<typename T> +void Circle<T>::draw(const GraphicsContext& context) +{ + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + drawCircle<T>(handle, fPos, fNumSegments, fSize, fSin, fCos, false); +} + +template<typename T> +void Circle<T>::drawOutline(const GraphicsContext& context, const T lineWidth) +{ + DISTRHO_SAFE_ASSERT_RETURN(lineWidth != 0,); + + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + cairo_set_line_width(handle, lineWidth); + drawCircle<T>(handle, fPos, fNumSegments, fSize, fSin, fCos, true); +} + +template<typename T> +void Circle<T>::draw() { notImplemented("Circle::draw"); } +template<typename T> +void Circle<T>::drawOutline() +{ + notImplemented("Circle::drawOutline"); +} + template class Circle<double>; template class Circle<float>; template class Circle<int>; @@ -66,11 +150,48 @@ template class Circle<ushort>; // Triangle template<typename T> -void Triangle<T>::_draw(const bool outline) +static void drawTriangle(cairo_t* const handle, + const Point<T>& pos1, + const Point<T>& pos2, + const Point<T>& pos3, + const bool outline) +{ + DISTRHO_SAFE_ASSERT_RETURN(pos1 != pos2 && pos1 != pos3,); + + // TODO +} + +template<typename T> +void Triangle<T>::draw(const GraphicsContext& context) +{ + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + drawTriangle<T>(handle, pos1, pos2, pos3, false); +} + +template<typename T> +void Triangle<T>::drawOutline(const GraphicsContext& context, const T lineWidth) +{ + DISTRHO_SAFE_ASSERT_RETURN(lineWidth != 0,); + + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + cairo_set_line_width(handle, lineWidth); + drawTriangle<T>(handle, pos1, pos2, pos3, true); +} + +template<typename T> +void Triangle<T>::draw() { notImplemented("Triangle::draw"); } +template<typename T> +void Triangle<T>::drawOutline() +{ + notImplemented("Triangle::drawOutline"); +} + template class Triangle<double>; template class Triangle<float>; template class Triangle<int>; @@ -83,23 +204,31 @@ template class Triangle<ushort>; // Rectangle template<typename T> -static void drawRectangle(const Rectangle<T>& rect, const bool outline) +static void drawRectangle(cairo_t* const handle, const Rectangle<T>& rect, const bool outline) { - DISTRHO_SAFE_ASSERT_RETURN(rect.isValid(),); - // TODO } template<typename T> -void Rectangle<T>::draw(const GraphicsContext&) +void Rectangle<T>::draw(const GraphicsContext& context) { - drawRectangle(*this, false); + DISTRHO_SAFE_ASSERT_RETURN(isValid(),); + + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + drawRectangle(handle, *this, false); } template<typename T> -void Rectangle<T>::drawOutline(const GraphicsContext&) +void Rectangle<T>::drawOutline(const GraphicsContext& context, const T lineWidth) { - drawRectangle(*this, true); + DISTRHO_SAFE_ASSERT_RETURN(isValid(),); + DISTRHO_SAFE_ASSERT_RETURN(lineWidth != 0,); + + cairo_t* const handle = ((const CairoGraphicsContext&)context).handle; + + cairo_set_line_width(handle, lineWidth); + drawRectangle(handle, *this, true); } template<typename T> diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp @@ -27,129 +27,129 @@ static const float M_2PIf = 3.14159265358979323846f*2.0f; template<typename T> Point<T>::Point() noexcept - : fX(0), - fY(0) {} + : x(0), + y(0) {} template<typename T> -Point<T>::Point(const T& x, const T& y) noexcept - : fX(x), - fY(y) {} +Point<T>::Point(const T& x2, const T& y2) noexcept + : x(x2), + y(y2) {} template<typename T> Point<T>::Point(const Point<T>& pos) noexcept - : fX(pos.fX), - fY(pos.fY) {} + : x(pos.x), + y(pos.y) {} template<typename T> const T& Point<T>::getX() const noexcept { - return fX; + return x; } template<typename T> const T& Point<T>::getY() const noexcept { - return fY; + return y; } template<typename T> -void Point<T>::setX(const T& x) noexcept +void Point<T>::setX(const T& x2) noexcept { - fX = x; + x = x2; } template<typename T> -void Point<T>::setY(const T& y) noexcept +void Point<T>::setY(const T& y2) noexcept { - fY = y; + y = y2; } template<typename T> -void Point<T>::setPos(const T& x, const T& y) noexcept +void Point<T>::setPos(const T& x2, const T& y2) noexcept { - fX = x; - fY = y; + x = x2; + y = y2; } template<typename T> void Point<T>::setPos(const Point<T>& pos) noexcept { - fX = pos.fX; - fY = pos.fY; + x = pos.x; + y = pos.y; } template<typename T> -void Point<T>::moveBy(const T& x, const T& y) noexcept +void Point<T>::moveBy(const T& x2, const T& y2) noexcept { - fX = static_cast<T>(fX+x); - fY = static_cast<T>(fY+y); + x = static_cast<T>(x+x2); + y = static_cast<T>(y+y2); } template<typename T> void Point<T>::moveBy(const Point<T>& pos) noexcept { - fX = static_cast<T>(fX+pos.fX); - fY = static_cast<T>(fY+pos.fY); + x = static_cast<T>(x+pos.x); + y = static_cast<T>(y+pos.y); } template<typename T> bool Point<T>::isZero() const noexcept { - return fX == 0 && fY == 0; + return x == 0 && y == 0; } template<typename T> bool Point<T>::isNotZero() const noexcept { - return fX != 0 || fY != 0; + return x != 0 || y != 0; } template<typename T> Point<T> Point<T>::operator+(const Point<T>& pos) noexcept { - return Point<T>(fX+pos.fX, fY+pos.fY); + return Point<T>(x+pos.x, y+pos.y); } template<typename T> Point<T> Point<T>::operator-(const Point<T>& pos) noexcept { - return Point<T>(fX-pos.fX, fY-pos.fY); + return Point<T>(x-pos.x, y-pos.y); } template<typename T> Point<T>& Point<T>::operator=(const Point<T>& pos) noexcept { - fX = pos.fX; - fY = pos.fY; + x = pos.x; + y = pos.y; return *this; } template<typename T> Point<T>& Point<T>::operator+=(const Point<T>& pos) noexcept { - fX = static_cast<T>(fX+pos.fX); - fY = static_cast<T>(fY+pos.fY); + x = static_cast<T>(x+pos.x); + y = static_cast<T>(y+pos.y); return *this; } template<typename T> Point<T>& Point<T>::operator-=(const Point<T>& pos) noexcept { - fX = static_cast<T>(fX-pos.fX); - fY = static_cast<T>(fY-pos.fY); + x = static_cast<T>(x-pos.x); + y = static_cast<T>(y-pos.y); return *this; } template<typename T> bool Point<T>::operator==(const Point<T>& pos) const noexcept { - return (fX == pos.fX && fY == pos.fY); + return (x == pos.x && y == pos.y); } template<typename T> bool Point<T>::operator!=(const Point<T>& pos) const noexcept { - return (fX != pos.fX || fY != pos.fY); + return (x != pos.x || y != pos.y); } // ----------------------------------------------------------------------- @@ -321,162 +321,162 @@ bool Size<T>::operator!=(const Size<T>& size) const noexcept template<typename T> Line<T>::Line() noexcept - : fPosStart(0, 0), - fPosEnd(0, 0) {} + : posStart(0, 0), + posEnd(0, 0) {} template<typename T> Line<T>::Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept - : fPosStart(startX, startY), - fPosEnd(endX, endY) {} + : posStart(startX, startY), + posEnd(endX, endY) {} template<typename T> Line<T>::Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept - : fPosStart(startX, startY), - fPosEnd(endPos) {} + : posStart(startX, startY), + posEnd(endPos) {} template<typename T> Line<T>::Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept - : fPosStart(startPos), - fPosEnd(endX, endY) {} + : posStart(startPos), + posEnd(endX, endY) {} template<typename T> Line<T>::Line(const Point<T>& startPos, const Point<T>& endPos) noexcept - : fPosStart(startPos), - fPosEnd(endPos) {} + : posStart(startPos), + posEnd(endPos) {} template<typename T> Line<T>::Line(const Line<T>& line) noexcept - : fPosStart(line.fPosStart), - fPosEnd(line.fPosEnd) {} + : posStart(line.posStart), + posEnd(line.posEnd) {} template<typename T> const T& Line<T>::getStartX() const noexcept { - return fPosStart.fX; + return posStart.x; } template<typename T> const T& Line<T>::getStartY() const noexcept { - return fPosStart.fY; + return posStart.y; } template<typename T> const T& Line<T>::getEndX() const noexcept { - return fPosEnd.fX; + return posEnd.x; } template<typename T> const T& Line<T>::getEndY() const noexcept { - return fPosEnd.fY; + return posEnd.y; } template<typename T> const Point<T>& Line<T>::getStartPos() const noexcept { - return fPosStart; + return posStart; } template<typename T> const Point<T>& Line<T>::getEndPos() const noexcept { - return fPosEnd; + return posEnd; } template<typename T> void Line<T>::setStartX(const T& x) noexcept { - fPosStart.fX = x; + posStart.x = x; } template<typename T> void Line<T>::setStartY(const T& y) noexcept { - fPosStart.fY = y; + posStart.y = y; } template<typename T> void Line<T>::setStartPos(const T& x, const T& y) noexcept { - fPosStart = Point<T>(x, y); + posStart = Point<T>(x, y); } template<typename T> void Line<T>::setStartPos(const Point<T>& pos) noexcept { - fPosStart = pos; + posStart = pos; } template<typename T> void Line<T>::setEndX(const T& x) noexcept { - fPosEnd.fX = x; + posEnd.x = x; } template<typename T> void Line<T>::setEndY(const T& y) noexcept { - fPosEnd.fY = y; + posEnd.y = y; } template<typename T> void Line<T>::setEndPos(const T& x, const T& y) noexcept { - fPosEnd = Point<T>(x, y); + posEnd = Point<T>(x, y); } template<typename T> void Line<T>::setEndPos(const Point<T>& pos) noexcept { - fPosEnd = pos; + posEnd = pos; } template<typename T> void Line<T>::moveBy(const T& x, const T& y) noexcept { - fPosStart.moveBy(x, y); - fPosEnd.moveBy(x, y); + posStart.moveBy(x, y); + posEnd.moveBy(x, y); } template<typename T> void Line<T>::moveBy(const Point<T>& pos) noexcept { - fPosStart.moveBy(pos); - fPosEnd.moveBy(pos); + posStart.moveBy(pos); + posEnd.moveBy(pos); } template<typename T> bool Line<T>::isNull() const noexcept { - return fPosStart == fPosEnd; + return posStart == posEnd; } template<typename T> bool Line<T>::isNotNull() const noexcept { - return fPosStart != fPosEnd; + return posStart != posEnd; } template<typename T> Line<T>& Line<T>::operator=(const Line<T>& line) noexcept { - fPosStart = line.fPosStart; - fPosEnd = line.fPosEnd; + posStart = line.posStart; + posEnd = line.posEnd; return *this; } template<typename T> bool Line<T>::operator==(const Line<T>& line) const noexcept { - return (fPosStart == line.fPosStart && fPosEnd == line.fPosEnd); + return (posStart == line.posStart && posEnd == line.posEnd); } template<typename T> bool Line<T>::operator!=(const Line<T>& line) const noexcept { - return (fPosStart != line.fPosStart || fPosEnd != line.fPosEnd); + return (posStart != line.posStart || posEnd != line.posEnd); } // ----------------------------------------------------------------------- @@ -530,13 +530,13 @@ Circle<T>::Circle(const Circle<T>& cir) noexcept template<typename T> const T& Circle<T>::getX() const noexcept { - return fPos.fX; + return fPos.x; } template<typename T> const T& Circle<T>::getY() const noexcept { - return fPos.fY; + return fPos.y; } template<typename T> @@ -548,20 +548,20 @@ const Point<T>& Circle<T>::getPos() const noexcept template<typename T> void Circle<T>::setX(const T& x) noexcept { - fPos.fX = x; + fPos.x = x; } template<typename T> void Circle<T>::setY(const T& y) noexcept { - fPos.fY = y; + fPos.y = y; } template<typename T> void Circle<T>::setPos(const T& x, const T& y) noexcept { - fPos.fX = x; - fPos.fY = y; + fPos.x = x; + fPos.y = y; } template<typename T> @@ -605,20 +605,6 @@ void Circle<T>::setNumSegments(const uint num) fSin = std::sin(fTheta); } -#ifndef DPF_TEST_POINT_CPP -template<typename T> -void Circle<T>::draw() -{ - _draw(false); -} - -template<typename T> -void Circle<T>::drawOutline() -{ - _draw(true); -} -#endif - template<typename T> Circle<T>& Circle<T>::operator=(const Circle<T>& cir) noexcept { @@ -648,85 +634,71 @@ bool Circle<T>::operator!=(const Circle<T>& cir) const noexcept template<typename T> Triangle<T>::Triangle() noexcept - : fPos1(0, 0), - fPos2(0, 0), - fPos3(0, 0) {} + : pos1(0, 0), + pos2(0, 0), + pos3(0, 0) {} template<typename T> Triangle<T>::Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept - : fPos1(x1, y1), - fPos2(x2, y2), - fPos3(x3, y3) {} + : pos1(x1, y1), + pos2(x2, y2), + pos3(x3, y3) {} template<typename T> Triangle<T>::Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept - : fPos1(pos1), - fPos2(pos2), - fPos3(pos3) {} + : pos1(pos1), + pos2(pos2), + pos3(pos3) {} template<typename T> Triangle<T>::Triangle(const Triangle<T>& tri) noexcept - : fPos1(tri.fPos1), - fPos2(tri.fPos2), - fPos3(tri.fPos3) {} + : pos1(tri.pos1), + pos2(tri.pos2), + pos3(tri.pos3) {} template<typename T> bool Triangle<T>::isNull() const noexcept { - return fPos1 == fPos2 && fPos1 == fPos3; + return pos1 == pos2 && pos1 == pos3; } template<typename T> bool Triangle<T>::isNotNull() const noexcept { - return fPos1 != fPos2 || fPos1 != fPos3; + return pos1 != pos2 || pos1 != pos3; } template<typename T> bool Triangle<T>::isValid() const noexcept { - return fPos1 != fPos2 && fPos1 != fPos3; + return pos1 != pos2 && pos1 != pos3; } template<typename T> bool Triangle<T>::isInvalid() const noexcept { - return fPos1 == fPos2 || fPos1 == fPos3; -} - -#ifndef DPF_TEST_POINT_CPP -template<typename T> -void Triangle<T>::draw() -{ - _draw(false); -} - -template<typename T> -void Triangle<T>::drawOutline() -{ - _draw(true); + return pos1 == pos2 || pos1 == pos3; } -#endif template<typename T> Triangle<T>& Triangle<T>::operator=(const Triangle<T>& tri) noexcept { - fPos1 = tri.fPos1; - fPos2 = tri.fPos2; - fPos3 = tri.fPos3; + pos1 = tri.pos1; + pos2 = tri.pos2; + pos3 = tri.pos3; return *this; } template<typename T> bool Triangle<T>::operator==(const Triangle<T>& tri) const noexcept { - return (fPos1 == tri.fPos1 && fPos2 == tri.fPos2 && fPos3 == tri.fPos3); + return (pos1 == tri.pos1 && pos2 == tri.pos2 && pos3 == tri.pos3); } template<typename T> bool Triangle<T>::operator!=(const Triangle<T>& tri) const noexcept { - return (fPos1 != tri.fPos1 || fPos2 != tri.fPos2 || fPos3 != tri.fPos3); + return (pos1 != tri.pos1 || pos2 != tri.pos2 || pos3 != tri.pos3); } // ----------------------------------------------------------------------- @@ -765,13 +737,13 @@ Rectangle<T>::Rectangle(const Rectangle<T>& rect) noexcept template<typename T> const T& Rectangle<T>::getX() const noexcept { - return pos.fX; + return pos.x; } template<typename T> const T& Rectangle<T>::getY() const noexcept { - return pos.fY; + return pos.y; } template<typename T> @@ -801,20 +773,20 @@ const Size<T>& Rectangle<T>::getSize() const noexcept template<typename T> void Rectangle<T>::setX(const T& x) noexcept { - pos.fX = x; + pos.x = x; } template<typename T> void Rectangle<T>::setY(const T& y) noexcept { - pos.fY = y; + pos.y = y; } template<typename T> void Rectangle<T>::setPos(const T& x, const T& y) noexcept { - pos.fX = x; - pos.fY = y; + pos.x = x; + pos.y = y; } template<typename T> @@ -889,25 +861,25 @@ void Rectangle<T>::setRectangle(const Rectangle<T>& rect) noexcept template<typename T> bool Rectangle<T>::contains(const T& x, const T& y) const noexcept { - return (x >= pos.fX && y >= pos.fY && x <= pos.fX+size.fWidth && y <= pos.fY+size.fHeight); + return (x >= pos.x && y >= pos.y && x <= pos.x+size.fWidth && y <= pos.y+size.fHeight); } template<typename T> bool Rectangle<T>::contains(const Point<T>& pos) const noexcept { - return contains(pos.fX, pos.fY); + return contains(pos.x, pos.y); } template<typename T> bool Rectangle<T>::containsX(const T& x) const noexcept { - return (x >= pos.fX && x <= pos.fX + size.fWidth); + return (x >= pos.x && x <= pos.x + size.fWidth); } template<typename T> bool Rectangle<T>::containsY(const T& y) const noexcept { - return (y >= pos.fY && y <= pos.fY + size.fHeight); + return (y >= pos.y && y <= pos.y + size.fHeight); } template<typename T> diff --git a/dgl/src/OpenGL.cpp b/dgl/src/OpenGL.cpp @@ -15,6 +15,7 @@ */ #include "../OpenGL.hpp" +#include "../Color.hpp" #include "../ImageWidgets.hpp" #include "SubWidgetPrivateData.hpp" @@ -25,23 +26,49 @@ START_NAMESPACE_DGL // ----------------------------------------------------------------------- +// Color + +void Color::setFor(const GraphicsContext&, const bool includeAlpha) +{ + if (includeAlpha) + glColor4f(red, green, blue, alpha); + else + glColor3f(red, green, blue); +} + +// ----------------------------------------------------------------------- // Line template<typename T> -void Line<T>::draw() +static void drawLine(const Point<T>& posStart, const Point<T>& posEnd) { - DISTRHO_SAFE_ASSERT_RETURN(fPosStart != fPosEnd,); + DISTRHO_SAFE_ASSERT_RETURN(posStart != posEnd,); glBegin(GL_LINES); { - glVertex2d(fPosStart.fX, fPosStart.fY); - glVertex2d(fPosEnd.fX, fPosEnd.fY); + glVertex2d(posStart.getX(), posStart.getY()); + glVertex2d(posEnd.getX(), posEnd.getY()); } glEnd(); } +template<typename T> +void Line<T>::draw(const GraphicsContext&, const T width) +{ + DISTRHO_SAFE_ASSERT_RETURN(width != 0,); + + glLineWidth(width); + drawLine<T>(posStart, posEnd); +} + +template<typename T> +void Line<T>::draw() +{ + drawLine<T>(posStart, posEnd); +} + template class Line<double>; template class Line<float>; template class Line<int>; @@ -53,26 +80,60 @@ template class Line<ushort>; // Circle template<typename T> -void Circle<T>::_draw(const bool outline) +static void drawCircle(const Point<T>& pos, + const uint numSegments, + const float size, + const float sin, + const float cos, + const bool outline) { - DISTRHO_SAFE_ASSERT_RETURN(fNumSegments >= 3 && fSize > 0.0f,); + DISTRHO_SAFE_ASSERT_RETURN(numSegments >= 3 && size > 0.0f,); - double t, x = fSize, y = 0.0; + const T origx = pos.getX(); + const T origy = pos.getY(); + double t, x = size, y = 0.0; glBegin(outline ? GL_LINE_LOOP : GL_POLYGON); - for (uint i=0; i<fNumSegments; ++i) + for (uint i=0; i<numSegments; ++i) { - glVertex2d(x + fPos.fX, y + fPos.fY); + glVertex2d(x + origx, y + origy); t = x; - x = fCos * x - fSin * y; - y = fSin * t + fCos * y; + x = cos * x - sin * y; + y = sin * t + cos * y; } glEnd(); } +template<typename T> +void Circle<T>::draw(const GraphicsContext&) +{ + drawCircle<T>(fPos, fNumSegments, fSize, fSin, fCos, false); +} + +template<typename T> +void Circle<T>::drawOutline(const GraphicsContext&, const T lineWidth) +{ + DISTRHO_SAFE_ASSERT_RETURN(lineWidth != 0,); + + glLineWidth(lineWidth); + drawCircle<T>(fPos, fNumSegments, fSize, fSin, fCos, true); +} + +template<typename T> +void Circle<T>::draw() +{ + drawCircle<T>(fPos, fNumSegments, fSize, fSin, fCos, false); +} + +template<typename T> +void Circle<T>::drawOutline() +{ + drawCircle<T>(fPos, fNumSegments, fSize, fSin, fCos, true); +} + template class Circle<double>; template class Circle<float>; template class Circle<int>; @@ -84,21 +145,51 @@ template class Circle<ushort>; // Triangle template<typename T> -void Triangle<T>::_draw(const bool outline) +static void drawTriangle(const Point<T>& pos1, + const Point<T>& pos2, + const Point<T>& pos3, + const bool outline) { - DISTRHO_SAFE_ASSERT_RETURN(fPos1 != fPos2 && fPos1 != fPos3,); + DISTRHO_SAFE_ASSERT_RETURN(pos1 != pos2 && pos1 != pos3,); glBegin(outline ? GL_LINE_LOOP : GL_TRIANGLES); { - glVertex2d(fPos1.fX, fPos1.fY); - glVertex2d(fPos2.fX, fPos2.fY); - glVertex2d(fPos3.fX, fPos3.fY); + glVertex2d(pos1.getX(), pos1.getY()); + glVertex2d(pos2.getX(), pos2.getY()); + glVertex2d(pos3.getX(), pos3.getY()); } glEnd(); } +template<typename T> +void Triangle<T>::draw(const GraphicsContext&) +{ + drawTriangle<T>(pos1, pos2, pos3, false); +} + +template<typename T> +void Triangle<T>::drawOutline(const GraphicsContext&, const T lineWidth) +{ + DISTRHO_SAFE_ASSERT_RETURN(lineWidth != 0,); + + glLineWidth(lineWidth); + drawTriangle<T>(pos1, pos2, pos3, true); +} + +template<typename T> +void Triangle<T>::draw() +{ + drawTriangle<T>(pos1, pos2, pos3, false); +} + +template<typename T> +void Triangle<T>::drawOutline() +{ + drawTriangle<T>(pos1, pos2, pos3, true); +} + template class Triangle<double>; template class Triangle<float>; template class Triangle<int>; @@ -141,25 +232,28 @@ static void drawRectangle(const Rectangle<T>& rect, const bool outline) template<typename T> void Rectangle<T>::draw(const GraphicsContext&) { - drawRectangle(*this, false); + drawRectangle<T>(*this, false); } template<typename T> -void Rectangle<T>::drawOutline(const GraphicsContext&) +void Rectangle<T>::drawOutline(const GraphicsContext&, const T lineWidth) { - drawRectangle(*this, true); + DISTRHO_SAFE_ASSERT_RETURN(lineWidth != 0,); + + glLineWidth(lineWidth); + drawRectangle<T>(*this, true); } template<typename T> void Rectangle<T>::draw() { - drawRectangle(*this, true); + drawRectangle<T>(*this, false); } template<typename T> void Rectangle<T>::drawOutline() { - drawRectangle(*this, true); + drawRectangle<T>(*this, true); } template class Rectangle<double>; diff --git a/tests/Demo.cpp b/tests/Demo.cpp @@ -14,10 +14,6 @@ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -// #ifndef DGL_OPENGL -// #error OpenGL build required for Demo -// #endif - #include "tests.hpp" #include "widgets/ExampleColorWidget.hpp" @@ -90,36 +86,33 @@ protected: const GraphicsContext& context(getGraphicsContext()); const int iconSize = bgIcon.getWidth(); -#if 0 /* TODO make generic */ - glColor3f(0.027f, 0.027f, 0.027f); - Rectangle<uint>(0, 0, getSize()).draw(); + Color(0.027f, 0.027f, 0.027f).setFor(context); + Rectangle<uint>(0, 0, getSize()).draw(context); bgIcon.setY(curPage*iconSize + curPage*3); - glColor3f(0.129f, 0.129f, 0.129f); - bgIcon.draw(); + Color(0.129f, 0.129f, 0.129f).setFor(context); + bgIcon.draw(context); - glColor3f(0.184f, 0.184f, 0.184f); - bgIcon.drawOutline(); + Color(0.184f, 0.184f, 0.184f).setFor(context); + bgIcon.drawOutline(context); if (curHover != curPage && curHover != -1) { Rectangle<int> rHover(1, curHover*iconSize + curHover*3, iconSize-2, iconSize-2); - glColor3f(0.071f, 0.071f, 0.071f); - rHover.draw(); + Color(0.071f, 0.071f, 0.071f).setFor(context); + rHover.draw(context); - glColor3f(0.102f, 0.102f, 0.102f); - rHover.drawOutline(); + Color(0.102f, 0.102f, 0.102f).setFor(context); + rHover.drawOutline(context); } - glLineWidth(2.0f); - glColor3f(0.184f, 0.184f, 0.184f); - lineSep.draw(); + Color(0.184f, 0.184f, 0.184f).setFor(context); + lineSep.draw(context); // reset color - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); -#endif + Color(1.0f, 1.0f, 1.0f, 1.0f).setFor(context, true); const int pad = iconSize/2 - DemoArtwork::ico1Width/2; @@ -274,6 +267,7 @@ public: #endif wLeft.setAbsolutePos(2, 2); + setResizable(true); setSize(600, 500); setTitle("DGL Demo"); @@ -375,8 +369,6 @@ int main(int argc, char* argv[]) if (argc > 1) { - // TODO images, text - /**/ if (std::strcmp(argv[1], "color") == 0) createAndShowExampleWidgetStandaloneWindow<ExampleColorStandaloneWindow>(app); else if (std::strcmp(argv[1], "images") == 0) diff --git a/tests/widgets/ExampleColorWidget.hpp b/tests/widgets/ExampleColorWidget.hpp @@ -20,6 +20,7 @@ // ------------------------------------------------------ // DGL Stuff +#include "../../dgl/Color.hpp" #include "../../dgl/StandaloneWindow.hpp" #include "../../dgl/SubWidget.hpp" @@ -108,15 +109,13 @@ protected: { const GraphicsContext& context(BaseWidget::getGraphicsContext()); -#if 0 /* TODO make generic */ // paint bg color (in full size) - glColor3b(r, g, b); + Color(r, g, b).setFor(context); bgFull.draw(context); // paint inverted color (in 2/3 size) - glColor3b(100-r, 100-g, 100-b); - bgSmall.draw(); -#endif + Color(100-r, 100-g, 100-b).setFor(context); + bgSmall.draw(context); } void onResize(const ResizeEvent& ev) override @@ -132,6 +131,7 @@ protected: } }; +// SubWidget template<> inline ExampleColorWidget<SubWidget>::ExampleColorWidget(Widget* const parent) : SubWidget(parent), diff --git a/tests/widgets/ExampleRectanglesWidget.hpp b/tests/widgets/ExampleRectanglesWidget.hpp @@ -20,6 +20,7 @@ // ------------------------------------------------------ // DGL Stuff +#include "../../dgl/Color.hpp" #include "../../dgl/SubWidget.hpp" #include "../../dgl/TopLevelWidget.hpp" @@ -65,6 +66,8 @@ public: protected: void onDisplay() override { + const GraphicsContext& context(BaseWidget::getGraphicsContext()); + const uint width = BaseWidget::getWidth(); const uint height = BaseWidget::getHeight(); @@ -81,36 +84,30 @@ protected: // 1st r.setY(3); -#if 0 /* TODO make generic */ if (clicked[0+i]) - glColor3f(0.8f, 0.5f, 0.3f); + Color(0.8f, 0.5f, 0.3f).setFor(context); else - glColor3f(0.3f, 0.5f, 0.8f); -#endif + Color(0.3f, 0.5f, 0.8f).setFor(context); r.draw(); // 2nd r.setY(3 + height/3); -#if 0 /* TODO make generic */ if (clicked[3+i]) - glColor3f(0.8f, 0.5f, 0.3f); + Color(0.8f, 0.5f, 0.3f).setFor(context); else - glColor3f(0.3f, 0.5f, 0.8f); -#endif + Color(0.3f, 0.5f, 0.8f).setFor(context); r.draw(); // 3rd r.setY(3 + height*2/3); -#if 0 /* TODO make generic */ if (clicked[6+i]) - glColor3f(0.8f, 0.5f, 0.3f); + Color(0.8f, 0.5f, 0.3f).setFor(context); else - glColor3f(0.3f, 0.5f, 0.8f); -#endif + Color(0.3f, 0.5f, 0.8f).setFor(context); r.draw(); } diff --git a/tests/widgets/ExampleShapesWidget.hpp b/tests/widgets/ExampleShapesWidget.hpp @@ -20,6 +20,7 @@ // ------------------------------------------------------ // DGL Stuff +#include "../../dgl/Color.hpp" #include "../../dgl/SubWidget.hpp" #include "../../dgl/TopLevelWidget.hpp" @@ -60,40 +61,28 @@ public: protected: void onDisplay() override { -#if 0 - glEnable(GL_MULTISAMPLE); - glEnable(GL_LINE_SMOOTH); - glEnable(GL_SRC_ALPHA); - glEnable(GL_ONE_MINUS_SRC_ALPHA); - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); - glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); -#endif - -#if 0 /* TODO make generic */ - glLineWidth(1.0f); - glColor3f(0.302f, 0.337f, 0.361f); - bg.draw(); - - glColor3f(0.235f, 0.271f, 0.294f); - rect.draw(); - - glColor3f(0.176f, 0.212f, 0.235f); - rect.drawOutline(); - - glColor3f(0.302f*2, 0.337f*2, 0.361f*2); - tri.draw(); - - glLineWidth(3.0f); - glColor3f(0.302f/2.0f, 0.337f/2.0f, 0.361f/2.0f); - tri.drawOutline(); - - glColor3f(0.235f, 0.271f, 0.294f); - cir.draw(); - - glLineWidth(2.0f); - glColor3f(0.176f/4, 0.212f/4, 0.235f/4); - cir.drawOutline(); -#endif + const GraphicsContext& context(BaseWidget::getGraphicsContext()); + + Color(0.302f, 0.337f, 0.361f).setFor(context);; + bg.draw(context); + + Color(0.235f, 0.271f, 0.294f).setFor(context); + rect.draw(context); + + Color(0.176f, 0.212f, 0.235f).setFor(context); + rect.drawOutline(context, 1); + + Color(0.302f*2, 0.337f*2, 0.361f*2).setFor(context); + tri.draw(context); + + Color(0.302f/2.0f, 0.337f/2.0f, 0.361f/2.0f).setFor(context); + tri.drawOutline(context, 3); + + Color(0.235f, 0.271f, 0.294f).setFor(context); + cir.draw(context); + + Color(0.176f/4, 0.212f/4, 0.235f/4).setFor(context); + cir.drawOutline(context, 2); } void onResize(const ResizeEvent& ev) override