DPF

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

commit 51233543614e25b47361a66bac917a75ee90cafe
parent 4281406e68aeef25c5e8a9dff5542f0e0eee337a
Author: falkTX <falktx@falktx.com>
Date:   Sun, 16 May 2021 21:11:59 +0100

Fix getGraphicsContext, fix nanovg linkage, cleanup

Signed-off-by: falkTX <falktx@falktx.com>

Diffstat:
Mdgl/Cairo.hpp | 2++
Mdgl/Geometry.hpp | 58++++++++++++++++++++++++++++++++++++++++++++--------------
Mdgl/NanoVG.hpp | 12+++++++-----
Mdgl/StandaloneWindow.hpp | 4++++
Mdgl/Widget.hpp | 2+-
Mdgl/Window.hpp | 8++++++++
Mdgl/src/Cairo.cpp | 96++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mdgl/src/Geometry.cpp | 120+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mdgl/src/NanoVG.cpp | 60++++++------------------------------------------------------
Mdgl/src/OpenGL.cpp | 103++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mdgl/src/Widget.cpp | 7+++++++
Mdgl/src/Window.cpp | 7+++++++
Mtests/Demo.cpp | 22++--------------------
Mtests/Makefile | 6+++---
Mtests/Window.cpp | 2+-
Mtests/widgets/ExampleColorWidget.hpp | 71++++++++++++++++++++++++++++++++++++++++++-----------------------------
Mtests/widgets/ExampleTextWidget.hpp | 49+++++++++++++++++++++++++++++++------------------
17 files changed, 352 insertions(+), 277 deletions(-)

diff --git a/dgl/Cairo.hpp b/dgl/Cairo.hpp @@ -105,6 +105,8 @@ protected: DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CairoSubWidget); }; +// -------------------------------------------------------------------------------------------------------------------- + /** Cairo TopLevelWidget, handy class that takes graphics context during onDisplay and passes it in a new function. */ diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp @@ -195,7 +195,7 @@ public: /** Return true if size is not null (0x0). - A non-null size is still invalid if its width or height is negative. + A non-null size is still invalid if its width or height are negative. */ bool isNotNull() const noexcept; @@ -732,17 +732,38 @@ public: */ bool containsY(const T& y) const noexcept; -#ifndef DPF_TEST_POINT_CPP /** - Draw this rectangle using the current OpenGL state. + Return true if size is null (0x0). + An null size is also invalid. */ - void draw(); + bool isNull() const noexcept; /** - Draw lines (outline of this rectangle) using the current OpenGL state. + Return true if size is not null (0x0). + A non-null size is still invalid if its width or height are negative. */ - void drawOutline(); -#endif + bool isNotNull() const noexcept; + + /** + Return true if size is valid (width and height are higher than zero). + */ + bool isValid() const noexcept; + + /** + Return true if size is invalid (width or height are lower or equal to zero). + An invalid size might not be null under some circumstances. + */ + bool isInvalid() const noexcept; + + /** + Draw this rectangle using the provided graphics context. + */ + void draw(const GraphicsContext& context); + + /** + Draw lines (outline of this rectangle) using the provided graphics context. + */ + void drawOutline(const GraphicsContext& context); Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept; Rectangle<T>& operator*=(double m) noexcept; @@ -750,14 +771,23 @@ public: bool operator==(const Rectangle<T>& size) const noexcept; bool operator!=(const Rectangle<T>& size) const noexcept; -private: - Point<T> fPos; - Size<T> fSize; + /** + Draw this rectangle using the current OpenGL state. + DEPRECATED please use draw(const GraphicsContext&) instead. + */ + // TODO mark deprecated + void draw(); -#ifndef DPF_TEST_POINT_CPP - /** @internal */ - void _draw(const bool outline); -#endif + /** DEPRECATED + Draw lines (outline of this rectangle) using the current OpenGL state. + DEPRECATED please use drawOutline(const GraphicsContext&) instead. + */ + // TODO mark deprecated + void drawOutline(); + +private: + Point<T> pos; + Size<T> size; }; // ----------------------------------------------------------------------- diff --git a/dgl/NanoVG.hpp b/dgl/NanoVG.hpp @@ -903,7 +903,7 @@ public: /** Destructor. */ - virtual ~NanoWidget(); + virtual ~NanoWidget() {} protected: /** @@ -913,14 +913,16 @@ protected: virtual void onNanoDisplay() = 0; private: - struct PrivateData; - PrivateData* const nData; - /** Widget display function. Implemented internally to wrap begin/endFrame() automatically. */ - void onDisplay() override; + inline void onDisplay() override + { + NanoVG::beginFrame(BaseWidget::getWidth(), BaseWidget::getHeight()); + onNanoDisplay(); + NanoVG::endFrame(); + } // these should not be used void beginFrame(uint,uint) {} diff --git a/dgl/StandaloneWindow.hpp b/dgl/StandaloneWindow.hpp @@ -57,6 +57,10 @@ public: void setHeight(uint height) { Window::setHeight(height); } void setSize(uint width, uint height) { Window::setSize(width, height); } void setSize(const Size<uint>& size) { Window::setSize(size); } + bool addIdleCallback(IdleCallback* callback, uint timerFrequencyInMs = 0) + { return Window::addIdleCallback(callback, timerFrequencyInMs); } + bool removeIdleCallback(IdleCallback* callback) { return Window::removeIdleCallback(callback); } + const GraphicsContext& getGraphicsContext() const noexcept { return Window::getGraphicsContext(); } DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(StandaloneWindow) }; diff --git a/dgl/Widget.hpp b/dgl/Widget.hpp @@ -153,7 +153,7 @@ public: Window& getWindow() const noexcept; /** - Get the graphics context associated with this widget. + Get the graphics context associated with this widget's window. GraphicsContext is an empty struct and needs to be casted into a different type in order to be usable, for example GraphicsContext. @see CairoSubWidget, CairoTopLevelWidget diff --git a/dgl/Window.hpp b/dgl/Window.hpp @@ -215,6 +215,14 @@ public: Application& getApp() const noexcept; /** + Get the graphics context associated with this window. + GraphicsContext is an empty struct and needs to be casted into a different type in order to be usable, + for example GraphicsContext. + @see CairoSubWidget, CairoTopLevelWidget + */ + const GraphicsContext& getGraphicsContext() const noexcept; + + /** Get the "native" window handle. Returned value depends on the platform: - HaikuOS: This is a pointer to a `BView`. diff --git a/dgl/src/Cairo.cpp b/dgl/src/Cairo.cpp @@ -22,7 +22,7 @@ START_NAMESPACE_DGL // ----------------------------------------------------------------------- -static void notImplemented(const char *name) +static void notImplemented(const char* const name) { d_stderr2("cairo function not implemented: %s", name); } @@ -36,6 +36,13 @@ void Line<T>::draw() notImplemented("Line::draw"); } +template class Line<double>; +template class Line<float>; +template class Line<int>; +template class Line<uint>; +template class Line<short>; +template class Line<ushort>; + // ----------------------------------------------------------------------- // Circle @@ -45,6 +52,13 @@ void Circle<T>::_draw(const bool outline) notImplemented("Circle::draw"); } +template class Circle<double>; +template class Circle<float>; +template class Circle<int>; +template class Circle<uint>; +template class Circle<short>; +template class Circle<ushort>; + // ----------------------------------------------------------------------- // Triangle @@ -54,15 +68,56 @@ void Triangle<T>::_draw(const bool outline) notImplemented("Triangle::draw"); } +template class Triangle<double>; +template class Triangle<float>; +template class Triangle<int>; +template class Triangle<uint>; +template class Triangle<short>; +template class Triangle<ushort>; + + // ----------------------------------------------------------------------- // Rectangle template<typename T> -void Rectangle<T>::_draw(const bool outline) +static void drawRectangle(const Rectangle<T>& rect, const bool outline) +{ + DISTRHO_SAFE_ASSERT_RETURN(rect.isValid(),); + + // TODO +} + +template<typename T> +void Rectangle<T>::draw(const GraphicsContext&) +{ + drawRectangle(*this, false); +} + +template<typename T> +void Rectangle<T>::drawOutline(const GraphicsContext&) +{ + drawRectangle(*this, true); +} + +template<typename T> +void Rectangle<T>::draw() { notImplemented("Rectangle::draw"); } +template<typename T> +void Rectangle<T>::drawOutline() +{ + notImplemented("Rectangle::drawOutline"); +} + +template class Rectangle<double>; +template class Rectangle<float>; +template class Rectangle<int>; +template class Rectangle<uint>; +template class Rectangle<short>; +template class Rectangle<ushort>; + // ----------------------------------------------------------------------- // CairoImage @@ -94,6 +149,8 @@ void ImageBaseAboutWindow<CairoImage>::onDisplay() img.draw(getGraphicsContext()); } +template class ImageBaseAboutWindow<CairoImage>; + // ----------------------------------------------------------------------- void SubWidget::PrivateData::display(const uint width, const uint height, const double autoScaleFactor) @@ -122,45 +179,10 @@ void SubWidget::PrivateData::display(const uint width, const uint height, const const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept { GraphicsContext& context((GraphicsContext&)graphicsContext); -#ifdef DGL_CAIRO ((CairoGraphicsContext&)context).handle = (cairo_t*)puglGetContext(view); -#endif return context; } // ----------------------------------------------------------------------- -// Possible template data types - -template class Line<double>; -template class Line<float>; -template class Line<int>; -template class Line<uint>; -template class Line<short>; -template class Line<ushort>; - -template class Circle<double>; -template class Circle<float>; -template class Circle<int>; -template class Circle<uint>; -template class Circle<short>; -template class Circle<ushort>; - -template class Triangle<double>; -template class Triangle<float>; -template class Triangle<int>; -template class Triangle<uint>; -template class Triangle<short>; -template class Triangle<ushort>; - -template class Rectangle<double>; -template class Rectangle<float>; -template class Rectangle<int>; -template class Rectangle<uint>; -template class Rectangle<short>; -template class Rectangle<ushort>; - -template class ImageBaseAboutWindow<CairoImage>; - -// ----------------------------------------------------------------------- END_NAMESPACE_DGL diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp @@ -734,162 +734,162 @@ bool Triangle<T>::operator!=(const Triangle<T>& tri) const noexcept template<typename T> Rectangle<T>::Rectangle() noexcept - : fPos(0, 0), - fSize(0, 0) {} + : pos(0, 0), + size(0, 0) {} template<typename T> Rectangle<T>::Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept - : fPos(x, y), - fSize(width, height) {} + : pos(x, y), + size(width, height) {} template<typename T> Rectangle<T>::Rectangle(const T& x, const T& y, const Size<T>& size) noexcept - : fPos(x, y), - fSize(size) {} + : pos(x, y), + size(size) {} template<typename T> Rectangle<T>::Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept - : fPos(pos), - fSize(width, height) {} + : pos(pos), + size(width, height) {} template<typename T> Rectangle<T>::Rectangle(const Point<T>& pos, const Size<T>& size) noexcept - : fPos(pos), - fSize(size) {} + : pos(pos), + size(size) {} template<typename T> Rectangle<T>::Rectangle(const Rectangle<T>& rect) noexcept - : fPos(rect.fPos), - fSize(rect.fSize) {} + : pos(rect.pos), + size(rect.size) {} template<typename T> const T& Rectangle<T>::getX() const noexcept { - return fPos.fX; + return pos.fX; } template<typename T> const T& Rectangle<T>::getY() const noexcept { - return fPos.fY; + return pos.fY; } template<typename T> const T& Rectangle<T>::getWidth() const noexcept { - return fSize.fWidth; + return size.fWidth; } template<typename T> const T& Rectangle<T>::getHeight() const noexcept { - return fSize.fHeight; + return size.fHeight; } template<typename T> const Point<T>& Rectangle<T>::getPos() const noexcept { - return fPos; + return pos; } template<typename T> const Size<T>& Rectangle<T>::getSize() const noexcept { - return fSize; + return size; } template<typename T> void Rectangle<T>::setX(const T& x) noexcept { - fPos.fX = x; + pos.fX = x; } template<typename T> void Rectangle<T>::setY(const T& y) noexcept { - fPos.fY = y; + pos.fY = y; } template<typename T> void Rectangle<T>::setPos(const T& x, const T& y) noexcept { - fPos.fX = x; - fPos.fY = y; + pos.fX = x; + pos.fY = y; } template<typename T> -void Rectangle<T>::setPos(const Point<T>& pos) noexcept +void Rectangle<T>::setPos(const Point<T>& pos2) noexcept { - fPos = pos; + pos = pos2; } template<typename T> void Rectangle<T>::moveBy(const T& x, const T& y) noexcept { - fPos.moveBy(x, y); + pos.moveBy(x, y); } template<typename T> -void Rectangle<T>::moveBy(const Point<T>& pos) noexcept +void Rectangle<T>::moveBy(const Point<T>& pos2) noexcept { - fPos.moveBy(pos); + pos.moveBy(pos2); } template<typename T> void Rectangle<T>::setWidth(const T& width) noexcept { - fSize.fWidth = width; + size.fWidth = width; } template<typename T> void Rectangle<T>::setHeight(const T& height) noexcept { - fSize.fHeight = height; + size.fHeight = height; } template<typename T> void Rectangle<T>::setSize(const T& width, const T& height) noexcept { - fSize.fWidth = width; - fSize.fHeight = height; + size.fWidth = width; + size.fHeight = height; } template<typename T> -void Rectangle<T>::setSize(const Size<T>& size) noexcept +void Rectangle<T>::setSize(const Size<T>& size2) noexcept { - fSize = size; + size = size2; } template<typename T> void Rectangle<T>::growBy(double multiplier) noexcept { - fSize.growBy(multiplier); + size.growBy(multiplier); } template<typename T> void Rectangle<T>::shrinkBy(double divider) noexcept { - fSize.shrinkBy(divider); + size.shrinkBy(divider); } template<typename T> -void Rectangle<T>::setRectangle(const Point<T>& pos, const Size<T>& size) noexcept +void Rectangle<T>::setRectangle(const Point<T>& pos2, const Size<T>& size2) noexcept { - fPos = pos; - fSize = size; + pos = pos2; + size = size2; } template<typename T> void Rectangle<T>::setRectangle(const Rectangle<T>& rect) noexcept { - fPos = rect.fPos; - fSize = rect.fSize; + pos = rect.pos; + size = rect.size; } template<typename T> bool Rectangle<T>::contains(const T& x, const T& y) const noexcept { - return (x >= fPos.fX && y >= fPos.fY && x <= fPos.fX+fSize.fWidth && y <= fPos.fY+fSize.fHeight); + return (x >= pos.fX && y >= pos.fY && x <= pos.fX+size.fWidth && y <= pos.fY+size.fHeight); } template<typename T> @@ -901,61 +901,71 @@ bool Rectangle<T>::contains(const Point<T>& pos) const noexcept template<typename T> bool Rectangle<T>::containsX(const T& x) const noexcept { - return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth); + return (x >= pos.fX && x <= pos.fX + size.fWidth); } template<typename T> bool Rectangle<T>::containsY(const T& y) const noexcept { - return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight); + return (y >= pos.fY && y <= pos.fY + size.fHeight); } -#ifndef DPF_TEST_POINT_CPP template<typename T> -void Rectangle<T>::draw() +bool Rectangle<T>::isNull() const noexcept { - _draw(false); + return size.isNull(); } template<typename T> -void Rectangle<T>::drawOutline() +bool Rectangle<T>::isNotNull() const noexcept { - _draw(true); + return size.isNotNull(); +} + +template<typename T> +bool Rectangle<T>::isValid() const noexcept +{ + return size.isValid(); +} + +template<typename T> +bool Rectangle<T>::isInvalid() const noexcept +{ + return size.isInvalid(); } -#endif template<typename T> Rectangle<T>& Rectangle<T>::operator=(const Rectangle<T>& rect) noexcept { - fPos = rect.fPos; - fSize = rect.fSize; + pos = rect.pos; + size = rect.size; return *this; } template<typename T> Rectangle<T>& Rectangle<T>::operator*=(double m) noexcept { - fSize *= m; + size *= m; return *this; } template<typename T> Rectangle<T>& Rectangle<T>::operator/=(double d) noexcept { - fSize /= d; + size /= d; return *this; } template<typename T> bool Rectangle<T>::operator==(const Rectangle<T>& rect) const noexcept { - return (fPos == rect.fPos && fSize == rect.fSize); + return (pos == rect.pos && size == rect.size); } template<typename T> bool Rectangle<T>::operator!=(const Rectangle<T>& rect) const noexcept { - return (fPos != rect.fPos || fSize != rect.fSize); + return (pos != rect.pos || size != rect.size); } // ----------------------------------------------------------------------- diff --git a/dgl/src/NanoVG.cpp b/dgl/src/NanoVG.cpp @@ -943,36 +943,17 @@ bool NanoVG::loadSharedResources() #endif // ----------------------------------------------------------------------- - -template <class BaseWidget> -struct NanoWidget<BaseWidget>::PrivateData { - NanoWidget<BaseWidget>* const self; - - PrivateData(NanoWidget<BaseWidget>* const s) - : self(s) {} - - ~PrivateData() - { - } -}; - -// ----------------------------------------------------------------------- // SubWidget template <> NanoWidget<SubWidget>::NanoWidget(Widget* const parent, int flags) : SubWidget(parent), - NanoVG(flags), - nData(new PrivateData(this)) + NanoVG(flags) { pData->needsViewportScaling = true; } -template <> -NanoWidget<SubWidget>::~NanoWidget() -{ - delete nData; -} +template class NanoWidget<SubWidget>; // ----------------------------------------------------------------------- // TopLevelWidget @@ -980,16 +961,11 @@ NanoWidget<SubWidget>::~NanoWidget() template <> NanoWidget<TopLevelWidget>::NanoWidget(Window& windowToMapTo, int flags) : TopLevelWidget(windowToMapTo), - NanoVG(flags), - nData(new PrivateData(this)) + NanoVG(flags) { } -template <> -NanoWidget<TopLevelWidget>::~NanoWidget() -{ - delete nData; -} +template class NanoWidget<TopLevelWidget>; // ----------------------------------------------------------------------- // StandaloneWindow @@ -997,35 +973,11 @@ NanoWidget<TopLevelWidget>::~NanoWidget() template <> NanoWidget<StandaloneWindow>::NanoWidget(Application& app, int flags) : StandaloneWindow(app), - NanoVG(flags), - nData(new PrivateData(this)) + NanoVG(flags) { } -template <> -NanoWidget<StandaloneWindow>::~NanoWidget() -{ - delete nData; -} - -// ----------------------------------------------------------------------- - -template <class BaseWidget> -void NanoWidget<BaseWidget>::onDisplay() -{ - NanoVG::beginFrame(BaseWidget::getWidth(), BaseWidget::getHeight()); - onNanoDisplay(); - - /* - for (std::vector<NanoWidget*>::iterator it = nData->subWidgets.begin(); it != nData->subWidgets.end(); ++it) - { - NanoWidget* const widget(*it); - widget->onNanoDisplay(); - } - */ - - NanoVG::endFrame(); -} +template class NanoWidget<StandaloneWindow>; // ----------------------------------------------------------------------- diff --git a/dgl/src/OpenGL.cpp b/dgl/src/OpenGL.cpp @@ -42,6 +42,13 @@ void Line<T>::draw() glEnd(); } +template class Line<double>; +template class Line<float>; +template class Line<int>; +template class Line<uint>; +template class Line<short>; +template class Line<ushort>; + // ----------------------------------------------------------------------- // Circle @@ -66,6 +73,13 @@ void Circle<T>::_draw(const bool outline) glEnd(); } +template class Circle<double>; +template class Circle<float>; +template class Circle<int>; +template class Circle<uint>; +template class Circle<short>; +template class Circle<ushort>; + // ----------------------------------------------------------------------- // Triangle @@ -85,33 +99,76 @@ void Triangle<T>::_draw(const bool outline) glEnd(); } +template class Triangle<double>; +template class Triangle<float>; +template class Triangle<int>; +template class Triangle<uint>; +template class Triangle<short>; +template class Triangle<ushort>; + // ----------------------------------------------------------------------- // Rectangle template<typename T> -void Rectangle<T>::_draw(const bool outline) +static void drawRectangle(const Rectangle<T>& rect, const bool outline) { - DISTRHO_SAFE_ASSERT_RETURN(fSize.isValid(),); + DISTRHO_SAFE_ASSERT_RETURN(rect.isValid(),); glBegin(outline ? GL_LINE_LOOP : GL_QUADS); { + const T x = rect.getX(); + const T y = rect.getY(); + const T w = rect.getWidth(); + const T h = rect.getHeight(); + glTexCoord2f(0.0f, 0.0f); - glVertex2d(fPos.fX, fPos.fY); + glVertex2d(x, y); glTexCoord2f(1.0f, 0.0f); - glVertex2d(fPos.fX+fSize.fWidth, fPos.fY); + glVertex2d(x+w, y); glTexCoord2f(1.0f, 1.0f); - glVertex2d(fPos.fX+fSize.fWidth, fPos.fY+fSize.fHeight); + glVertex2d(x+w, y+h); glTexCoord2f(0.0f, 1.0f); - glVertex2d(fPos.fX, fPos.fY+fSize.fHeight); + glVertex2d(x, y+h); } glEnd(); } +template<typename T> +void Rectangle<T>::draw(const GraphicsContext&) +{ + drawRectangle(*this, false); +} + +template<typename T> +void Rectangle<T>::drawOutline(const GraphicsContext&) +{ + drawRectangle(*this, true); +} + +template<typename T> +void Rectangle<T>::draw() +{ + drawRectangle(*this, true); +} + +template<typename T> +void Rectangle<T>::drawOutline() +{ + drawRectangle(*this, true); +} + +template class Rectangle<double>; +template class Rectangle<float>; +template class Rectangle<int>; +template class Rectangle<uint>; +template class Rectangle<short>; +template class Rectangle<ushort>; + // ----------------------------------------------------------------------- static void setupOpenGLImage(const OpenGLImage& image, GLuint textureId) @@ -379,40 +436,6 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept } // ----------------------------------------------------------------------- -// Possible template data types - -#ifndef DPF_TEST_DEMO -// // FIXME -// template class Line<double>; -// template class Line<float>; -// template class Line<int>; -// template class Line<uint>; -// template class Line<short>; -// template class Line<ushort>; -// -// template class Circle<double>; -// template class Circle<float>; -// template class Circle<int>; -// template class Circle<uint>; -// template class Circle<short>; -// template class Circle<ushort>; -// -// template class Triangle<double>; -// template class Triangle<float>; -// template class Triangle<int>; -// template class Triangle<uint>; -// template class Triangle<short>; -// template class Triangle<ushort>; -// -template class Rectangle<double>; -template class Rectangle<float>; -template class Rectangle<int>; -template class Rectangle<uint>; -template class Rectangle<short>; -template class Rectangle<ushort>; -#endif - -// ----------------------------------------------------------------------- END_NAMESPACE_DGL diff --git a/dgl/src/Widget.cpp b/dgl/src/Widget.cpp @@ -16,6 +16,7 @@ #include "WidgetPrivateData.hpp" #include "../TopLevelWidget.hpp" +#include "../Window.hpp" START_NAMESPACE_DGL @@ -134,6 +135,12 @@ Window& Widget::getWindow() const noexcept return pData->topLevelWidget->getWindow(); } +const GraphicsContext& Widget::getGraphicsContext() const noexcept +{ + DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr); + return pData->topLevelWidget->getWindow().getGraphicsContext(); +} + TopLevelWidget* Widget::getTopLevelWidget() const noexcept { return pData->topLevelWidget; diff --git a/dgl/src/Window.cpp b/dgl/src/Window.cpp @@ -172,6 +172,13 @@ Application& Window::getApp() const noexcept return pData->app; } +#ifndef DPF_TEST_WINDOW_CPP +const GraphicsContext& Window::getGraphicsContext() const noexcept +{ + return pData->getGraphicsContext(); +} +#endif + uintptr_t Window::getNativeWindowHandle() const noexcept { return puglGetNativeWindow(pData->view); diff --git a/tests/Demo.cpp b/tests/Demo.cpp @@ -20,26 +20,8 @@ #include "tests.hpp" -#define DPF_TEST_DEMO -#include "dgl/OpenGL.hpp" -#include "dgl/src/pugl.cpp" -#include "dgl/src/Application.cpp" -#include "dgl/src/ApplicationPrivateData.cpp" -#include "dgl/src/Color.cpp" -#include "dgl/src/Geometry.cpp" -#include "dgl/src/ImageBase.cpp" -#include "dgl/src/NanoVG.cpp" -#include "dgl/src/OpenGL.cpp" -#include "dgl/src/Resources.cpp" -#include "dgl/src/SubWidget.cpp" -#include "dgl/src/SubWidgetPrivateData.cpp" -#include "dgl/src/TopLevelWidget.cpp" -#include "dgl/src/TopLevelWidgetPrivateData.cpp" -#include "dgl/src/Widget.cpp" -#include "dgl/src/WidgetPrivateData.cpp" -#include "dgl/src/Window.cpp" -#include "dgl/src/WindowPrivateData.cpp" -#include "dgl/StandaloneWindow.hpp" +// TODO backend agnostic +#include "../dgl/OpenGL.hpp" #include "widgets/ExampleColorWidget.hpp" #include "widgets/ExampleImagesWidget.hpp" diff --git a/tests/Makefile b/tests/Makefile @@ -25,7 +25,7 @@ ifeq ($(HAVE_CAIRO),true) WTESTS = Window.cairo endif ifeq ($(HAVE_OPENGL),true) -# TESTS += Demo +TESTS += Demo WTESTS = Window.opengl endif ifeq ($(HAVE_VULKAN),true) @@ -119,9 +119,9 @@ clean: @echo "Linking $*" $(SILENT)$(CXX) $< $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(CAIRO_LIBS) -o $@ -../build/tests/Demo: ../build/tests/Demo.cpp.o +../build/tests/Demo: ../build/tests/Demo.cpp.o ../build/libdgl-opengl.a @echo "Linking Demo" - $(SILENT)$(CXX) $< $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(OPENGL_LIBS) -o $@ + $(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(OPENGL_LIBS) -o $@ ../build/tests/Testing: ../build/tests/Testing.cpp.o @echo "Linking Testing" diff --git a/tests/Window.cpp b/tests/Window.cpp @@ -20,8 +20,8 @@ #include "tests.hpp" -#define DPF_TEST_WINDOW_CPP #define DPF_TEST_POINT_CPP +#define DPF_TEST_WINDOW_CPP #include "dgl/src/pugl.cpp" #include "dgl/src/Application.cpp" #include "dgl/src/ApplicationPrivateData.cpp" diff --git a/tests/widgets/ExampleColorWidget.hpp b/tests/widgets/ExampleColorWidget.hpp @@ -20,8 +20,8 @@ // ------------------------------------------------------ // DGL Stuff +#include "../../dgl/StandaloneWindow.hpp" #include "../../dgl/SubWidget.hpp" -#include "../../dgl/TopLevelWidget.hpp" START_NAMESPACE_DGL @@ -42,37 +42,13 @@ public: static constexpr const char* kExampleWidgetName = "Color"; // SubWidget - explicit ExampleColorWidget(Widget* const parent) - : BaseWidget(parent), - cur('r'), - reverse(false), - r(0), g(0), b(0) - { - BaseWidget::setSize(300, 300); - parent->getApp().addIdleCallback(this); - } + explicit ExampleColorWidget(Widget* const parent); // TopLevelWidget - explicit ExampleColorWidget(Window& windowToMapTo) - : BaseWidget(windowToMapTo), - cur('r'), - reverse(false), - r(0), g(0), b(0) - { - BaseWidget::setSize(300, 300); - windowToMapTo.getApp().addIdleCallback(this); - } + explicit ExampleColorWidget(Window& windowToMapTo); // StandaloneWindow - explicit ExampleColorWidget(Application& app) - : BaseWidget(app), - cur('r'), - reverse(false), - r(0), g(0), b(0) - { - BaseWidget::setSize(300, 300); - app.addIdleCallback(this); - } + explicit ExampleColorWidget(Application& app); protected: void idleCallback() noexcept override @@ -130,9 +106,11 @@ protected: void onDisplay() override { + const GraphicsContext& context(BaseWidget::getGraphicsContext()); + // paint bg color (in full size) glColor3b(r, g, b); - bgFull.draw(); + bgFull.draw(context); // paint inverted color (in 2/3 size) glColor3b(100-r, 100-g, 100-b); @@ -152,6 +130,41 @@ protected: } }; +template<> inline +ExampleColorWidget<SubWidget>::ExampleColorWidget(Widget* const parent) + : SubWidget(parent), + cur('r'), + reverse(false), + r(0), g(0), b(0) +{ + setSize(300, 300); + parent->getApp().addIdleCallback(this); +} + +// TopLevelWidget +template<> inline +ExampleColorWidget<TopLevelWidget>::ExampleColorWidget(Window& windowToMapTo) + : TopLevelWidget(windowToMapTo), + cur('r'), + reverse(false), + r(0), g(0), b(0) +{ + setSize(300, 300); + addIdleCallback(this); +} + +// StandaloneWindow +template<> inline +ExampleColorWidget<StandaloneWindow>::ExampleColorWidget(Application& app) + : StandaloneWindow(app), + cur('r'), + reverse(false), + r(0), g(0), b(0) +{ + setSize(300, 300); + addIdleCallback(this); +} + typedef ExampleColorWidget<SubWidget> ExampleColorSubWidget; typedef ExampleColorWidget<TopLevelWidget> ExampleColorTopLevelWidget; typedef ExampleColorWidget<StandaloneWindow> ExampleColorStandaloneWindow; diff --git a/tests/widgets/ExampleTextWidget.hpp b/tests/widgets/ExampleTextWidget.hpp @@ -34,28 +34,13 @@ public: static constexpr const char* kExampleWidgetName = "Text"; // SubWidget - explicit ExampleTextWidget(Widget* const parent) - : BaseWidget(parent) - { - NanoVG::loadSharedResources(); - BaseWidget::setSize(500, 300); - } + explicit ExampleTextWidget(Widget* const parent); // TopLevelWidget - explicit ExampleTextWidget(Window& windowToMapTo) - : BaseWidget(windowToMapTo) - { - NanoVG::loadSharedResources(); - BaseWidget::setSize(500, 300); - } + explicit ExampleTextWidget(Window& windowToMapTo); // StandaloneWindow - explicit ExampleTextWidget(Application& app) - : BaseWidget(app) - { - NanoVG::loadSharedResources(); - BaseWidget::setSize(500, 300); - } + explicit ExampleTextWidget(Application& app); protected: void onNanoDisplay() override @@ -77,6 +62,34 @@ protected: } }; +template<> inline +ExampleTextWidget<NanoSubWidget>::ExampleTextWidget(Widget* const parent) + : NanoSubWidget(parent) +{ + loadSharedResources(); + setSize(500, 300); +} + +template<> inline +ExampleTextWidget<NanoTopLevelWidget>::ExampleTextWidget(Window& windowToMapTo) + : NanoTopLevelWidget(windowToMapTo) +{ + loadSharedResources(); + setSize(500, 300); +} + +template<> inline +ExampleTextWidget<NanoStandaloneWindow>::ExampleTextWidget(Application& app) + : NanoStandaloneWindow(app) +{ + loadSharedResources(); + setSize(500, 300); +} + +template class ExampleTextWidget<NanoSubWidget>; +template class ExampleTextWidget<NanoTopLevelWidget>; +template class ExampleTextWidget<NanoStandaloneWindow>; + typedef ExampleTextWidget<NanoSubWidget> ExampleTextSubWidget; typedef ExampleTextWidget<NanoTopLevelWidget> ExampleTextTopLevelWidget; typedef ExampleTextWidget<NanoStandaloneWindow> ExampleTextStandaloneWindow;