diff options
author | falkTX <falktx@gmail.com> | 2013-12-25 07:33:55 +0000 |
---|---|---|
committer | falkTX <falktx@gmail.com> | 2013-12-25 07:33:55 +0000 |
commit | 8aad566744c5f26663510ddeaf2dea4b2db63a60 (patch) | |
tree | 2ddc545b0287b1d7f753fdff62ede2ffcb38f3ff /examples | |
parent | e076d574a95ed438be7fe0ff19611c9052387c26 (diff) |
CairoWidget fixes; add cairo example (needs a little more work)
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 7 | ||||
-rw-r--r-- | examples/cairo.cpp | 213 | ||||
-rw-r--r-- | examples/images.cpp | 2 |
3 files changed, 219 insertions, 3 deletions
diff --git a/examples/Makefile b/examples/Makefile index f5375a90..f87c026e 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -14,9 +14,9 @@ LINK_FLAGS += -L.. -ldgl $(DGL_LIBS) # -------------------------------------------------------------- ifeq ($(WIN32),true) -TARGETS = app.exe color images.exe nekobi-ui.exe +TARGETS = app.exe cairo.exe color images.exe nekobi-ui.exe else -TARGETS = app color images nekobi-ui +TARGETS = app cairo color images nekobi-ui endif # -------------------------------------------------------------- @@ -35,6 +35,9 @@ all: ../libdgl.a $(TARGETS) app: app.cpp ../dgl/* $(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@ +cairo: cairo.cpp ../dgl/* + $(CXX) $< $(BUILD_CXX_FLAGS) $(shell pkg-config --cflags --libs cairo) $(LINK_FLAGS) -o $@ + color: color.cpp ../dgl/* $(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@ diff --git a/examples/cairo.cpp b/examples/cairo.cpp new file mode 100644 index 00000000..7e89e98b --- /dev/null +++ b/examples/cairo.cpp @@ -0,0 +1,213 @@ +/* + * DISTRHO Plugin Toolkit (DPT) + * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com> + * + * Permission to use, copy, modify, and/or distribute this software for any purpose with + * or without fee is hereby granted, provided that the above copyright notice and this + * permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD + * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +// ------------------------------------------------------ +// DGL Stuff + +#include "App.hpp" +#include "CairoWidget.hpp" +#include "Window.hpp" + +#include <cstdio> + +// ------------------------------------------------------ +// use namespace + +using namespace DGL; + +// ------------------------------------------------------ +// Background widget (cairo will be painted on top) + +class BackgroundWidget : public Widget +{ +public: + BackgroundWidget(Window& parent) + : Widget(parent) + { + } + +private: + void onDisplay() override + { + int x = 0; + int y = 0; + int width = getWidth(); + int height = getHeight(); + + // paint bg color (in full size) + glColor3b(20, 80, 20); + + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); + glVertex2i(x, y); + + glTexCoord2f(1.0f, 0.0f); + glVertex2i(x+width, y); + + glTexCoord2f(1.0f, 1.0f); + glVertex2i(x+width, y+height); + + glTexCoord2f(0.0f, 1.0f); + glVertex2i(x, y+height); + glEnd(); + } + + void onReshape(int width, int height) override + { + // make this widget same size as window + setSize(width, height); + Widget::onReshape(width, height); + } +}; + +// ------------------------------------------------------ +// Custom Cairo Widget + +class CustomCairoWidget : public App::IdleCallback, + CairoWidget +{ +public: + CustomCairoWidget(Window& parent) + : CairoWidget(parent), + value(0.0f), + pressed(false) + { + setSize(100, 100); + } + +private: + void idleCallback() override + { + value += 0.001f; + + if (value > 1.0f) + value = 0; + + repaint(); + } + + void cairoDisplay(cairo_t* const context) override + { + const int w = getWidth(); + const int h = getHeight(); + + float radius = 40.0f; + + // * 0.9 for line width to remain inside redraw area + if (w > h) + radius = (h / 2.0f)*0.9f; + else + radius = (w / 2.0f)*0.9f; + + cairo_save(context); + + cairo_rectangle(context, 0, 0, w, h ); + cairo_set_source_rgba(context, 1.1, 0.1, 0.1, 0 ); + cairo_fill(context); + + cairo_set_line_join(context, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap(context, CAIRO_LINE_CAP_ROUND); + + cairo_set_line_width(context, 5-0.2); + cairo_move_to(context, w/2, h/2); + cairo_line_to(context, w/2, h/2); + cairo_set_source_rgba(context, 0.1, 0.1, 0.1, 0 ); + cairo_stroke(context); + + cairo_arc(context, w/2, h/2, radius, 2.46, 0.75 ); + cairo_set_source_rgb(context, 0.1, 0.1, 0.1 ); + cairo_stroke(context); + + float angle = 2.46 + ( 4.54 * value ); + cairo_set_line_width(context, 5); + cairo_arc(context, w/2, h/2, radius, 2.46, angle ); + cairo_line_to(context, w/2, h/2); + cairo_set_source_rgba(context, 1.0, 0.48, 0, 0.8); + cairo_stroke(context); + + cairo_restore(context); + } + + bool onMouse(int button, bool press, int x, int y) + { + if (button == 1) + { + pressed = press; + + if (press) + { + setX(x-100/2); + setY(y-100/2); + } + + return true; + } + + return false; + } + + bool onMotion(int x, int y) + { + if (pressed) + { + setX(x-100/2); + setY(y-100/2); + return true; + } + + return false; + } + + float value; + bool pressed; +}; + +// ------------------------------------------------------ +// Custom window, with bg + cairo + image + +class CustomWindow : public Window +{ +public: + CustomWindow(App& app) + : Window(app), + bg(*this), + cairo(*this) + { + app.addIdleCallback(&cairo); + } + +private: + BackgroundWidget bg; + CustomCairoWidget cairo; +}; + +// ------------------------------------------------------ +// main entry point + +int main() +{ + App app; + CustomWindow win(app); + + win.setSize(300, 300); + win.setTitle("Cairo"); + win.show(); + app.exec(); + + return 0; +} + +// ------------------------------------------------------ diff --git a/examples/images.cpp b/examples/images.cpp index 2ba70695..97e0abb7 100644 --- a/examples/images.cpp +++ b/examples/images.cpp @@ -35,7 +35,7 @@ using namespace DGL; // our widget class ExampleImagesWidget : public App::IdleCallback, - Widget + Widget { public: static const int kImg1y = 0; |