From a76a6e7f5e982217ec5aa087cac7bbda5e437968 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 10 Feb 2017 00:30:00 -0500 Subject: [PATCH] revised Lights for Component Library, removed constructor argument from ModuleWidget --- Makefile | 2 +- include/components.hpp | 56 ++++++++++++++++++++++++++++++++++++ include/engine.hpp | 1 + include/math.hpp | 9 +++--- include/rack.hpp | 8 ++++++ include/scene.hpp | 5 ++-- include/util.hpp | 1 - src/core/AudioInterface.cpp | 7 +++-- src/core/MidiInterface.cpp | 4 ++- src/gui.cpp | 4 +-- src/main.cpp | 5 +++- src/widgets/Light.cpp | 17 ++++++----- src/widgets/ModuleWidget.cpp | 19 ++++++------ 13 files changed, 105 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 795e7390..980ce553 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ LDFLAGS += \ -L$(HOME)/pkg/portaudio-r1891-build/lib/x64/ReleaseMinDependency -lportaudio_x64 \ -Wl,--export-all-symbols,--out-implib,libRack.a -mwindows TARGET = Rack.exe -OBJECTS = Rack.res +# OBJECTS = Rack.res %.res: %.rc windres $^ -O coff -o $@ diff --git a/include/components.hpp b/include/components.hpp index 0949ba79..5c1a0173 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -4,6 +4,16 @@ namespace rack { +#define SCHEME_BLACK nvgRGB(0x00, 0x00, 0x00) +#define SCHEME_WHITE nvgRGB(0xff, 0xff, 0xff) +#define SCHEME_RED nvgRGB(0xed, 0x2c, 0x24) +#define SCHEME_ORANGE nvgRGB(0xf2, 0xb1, 0x20) +#define SCHEME_YELLOW nvgRGB(0xf9, 0xdf, 0x1c) +#define SCHEME_GREEN nvgRGB(0x90, 0xc7, 0x3e) +#define SCHEME_CYAN nvgRGB(0x22, 0xe6, 0xef) +#define SCHEME_BLUE nvgRGB(0x29, 0xb2, 0xef) +#define SCHEME_PURPLE nvgRGB(0xd5, 0x2b, 0xed) + //////////////////// // Knobs //////////////////// @@ -113,6 +123,52 @@ struct CL1362 : BASE { typedef CL1362 InputPortCL1362; typedef CL1362 OutputPortCL1362; +//////////////////// +// Lights +//////////////////// + +struct ValueLight : Light { + float *value; +}; + +struct RedValueLight : ValueLight { + void step() { + float v = sqrtBipolar(getf(value)); + color = nvgLerpRGBA(SCHEME_BLACK, SCHEME_RED, v); + } +}; + +struct GreenRedPolarityLight : ValueLight { + void step() { + float v = sqrtBipolar(getf(value)); + if (v >= 0.0) + color = nvgLerpRGBA(SCHEME_BLACK, SCHEME_GREEN, v); + else + color = nvgLerpRGBA(SCHEME_BLACK, SCHEME_RED, -v); + } +}; + +template +struct LargeLight : BASE { + LargeLight() { + this->box.size = Vec(20, 20); + } +}; + +template +struct MediumLight : BASE { + MediumLight() { + this->box.size = Vec(12, 12); + } +}; + +template +struct SmallLight : BASE { + SmallLight() { + this->box.size = Vec(8, 8); + } +}; + //////////////////// // Misc //////////////////// diff --git a/include/engine.hpp b/include/engine.hpp index bcc7f2c7..f726dbd0 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include "util.hpp" namespace rack { diff --git a/include/math.hpp b/include/math.hpp index 0af62659..61a86a07 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -45,14 +45,11 @@ inline float quadraticBipolar(float x) { } inline float cubic(float x) { - // optimal with --fast-math return x*x*x; } inline float quarticBipolar(float x) { - float x2 = x*x; - float x4 = x2*x2; - return x >= 0.0 ? x4 : -x4; + return x >= 0.0 ? x*x*x*x : -x*x*x*x; } inline float quintic(float x) { @@ -60,6 +57,10 @@ inline float quintic(float x) { return x*x*x*x*x; } +inline float sqrtBipolar(float x) { + return x >= 0.0 ? sqrtf(x) : -sqrtf(-x); +} + inline float sincf(float x) { if (x == 0.0) return 1.0; diff --git a/include/rack.hpp b/include/rack.hpp index 2b659d50..3984e667 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -68,5 +68,13 @@ Screw *createScrew(Vec pos) { return screw; } +template +ValueLight *createValueLight(Vec pos, float *value) { + ValueLight *light = new TLight(); + light->box.pos = pos; + light->value = value; + return light; +} + } // namespace rack diff --git a/include/scene.hpp b/include/scene.hpp index 0b51540b..03531272 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -24,16 +24,15 @@ struct Scene; struct Model; struct ModuleWidget : OpaqueWidget { Model *model = NULL; - // Eventually this should be replaced with a `moduleId` which will be used for inter-process communication between the gui world and the audio world. + /** Owns the module pointer */ Module *module = NULL; - // int moduleId; std::vector inputs; std::vector outputs; std::vector params; - ModuleWidget(Module *module); ~ModuleWidget(); + void setModule(Module *module); // Convenience functions for adding special widgets (calls addChild()) void addInput(InputPort *input); void addOutput(OutputPort *output); diff --git a/include/util.hpp b/include/util.hpp index d63a765a..20872765 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -1,7 +1,6 @@ #pragma once // Include most of the C standard library for convenience -// (C++ programmers will hate me) #include #include #include diff --git a/src/core/AudioInterface.cpp b/src/core/AudioInterface.cpp index 80083cfa..ff96b236 100644 --- a/src/core/AudioInterface.cpp +++ b/src/core/AudioInterface.cpp @@ -256,7 +256,8 @@ void AudioInterface::closeDevice() { if (stream) { PaError err; streamRunning = false; - err = Pa_StopStream(stream); + err = Pa_AbortStream(stream); + // err = Pa_StopStream(stream); if (err) { fprintf(stderr, "Failed to stop audio stream: %s\n", Pa_GetErrorText(err)); } @@ -390,7 +391,9 @@ struct BlockSizeChoice : ChoiceButton { }; -AudioInterfaceWidget::AudioInterfaceWidget() : ModuleWidget(new AudioInterface()) { +AudioInterfaceWidget::AudioInterfaceWidget() { + AudioInterface *module = new AudioInterface(); + setModule(module); box.size = Vec(15*8, 380); { diff --git a/src/core/MidiInterface.cpp b/src/core/MidiInterface.cpp index 2dffaf8a..5816616d 100644 --- a/src/core/MidiInterface.cpp +++ b/src/core/MidiInterface.cpp @@ -221,7 +221,9 @@ struct MidiChoice : ChoiceButton { }; -MidiInterfaceWidget::MidiInterfaceWidget() : ModuleWidget(new MidiInterface()) { +MidiInterfaceWidget::MidiInterfaceWidget() { + MidiInterface *module = new MidiInterface(); + setModule(module); box.size = Vec(15*8, 380); { diff --git a/src/gui.cpp b/src/gui.cpp index 40cdc81f..c2d72515 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -180,12 +180,12 @@ void guiInit() { err = glfwInit(); assert(err); -#ifndef WINDOWS +// #ifndef WINDOWS glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); -#endif +// #endif window = glfwCreateWindow(1000, 750, gApplicationName.c_str(), NULL, NULL); assert(window); glfwMakeContextCurrent(window); diff --git a/src/main.cpp b/src/main.cpp index 2612d491..4fb59fef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,10 @@ #include #endif -#include "rack.hpp" +#include "engine.hpp" +#include "gui.hpp" +#include "scene.hpp" +#include "plugin.hpp" using namespace rack; diff --git a/src/widgets/Light.cpp b/src/widgets/Light.cpp index fe0bf619..878bce6f 100644 --- a/src/widgets/Light.cpp +++ b/src/widgets/Light.cpp @@ -6,10 +6,10 @@ namespace rack { void Light::draw(NVGcontext *vg) { NVGcolor colorOutline = nvgLerpRGBA(color, nvgRGBf(0.0, 0.0, 0.0), 0.5); - Vec r = box.size.div(2.0); + float radius = box.size.x / 2.0; nvgBeginPath(vg); - nvgEllipse(vg, r.x, r.y, r.x - 1.0, r.y - 1.0); + nvgCircle(vg, radius, radius, radius); nvgFillColor(vg, color); nvgFill(vg); @@ -17,18 +17,17 @@ void Light::draw(NVGcontext *vg) { nvgStrokeColor(vg, colorOutline); nvgStroke(vg); - // float radius = box.size.x / 2.0; - // NVGcolor icol, ocol; + // nvgGlobalCompositeOperation(vg, NVG_LIGHTER); // NVGpaint paint; - // icol = color; - // icol.a = clampf(color.a / 10.0, 0.0, 1.0); - // ocol = color; + // NVGcolor icol = color; + // icol.a = 0.2; + // NVGcolor ocol = color; // ocol.a = 0.0; // float oradius = radius + 20.0; - // paint = nvgRadialGradient(vg, c.x, c.y, radius, oradius, icol, ocol); + // paint = nvgRadialGradient(vg, radius, radius, radius, oradius, icol, ocol); // nvgFillPaint(vg, paint); // nvgBeginPath(vg); - // nvgRect(vg, c.x - oradius, c.y - oradius, 2*oradius, 2*oradius); + // nvgRect(vg, radius - oradius, radius - oradius, 2*oradius, 2*oradius); // nvgFill(vg); } diff --git a/src/widgets/ModuleWidget.cpp b/src/widgets/ModuleWidget.cpp index 18b4c697..9d8d7ce2 100644 --- a/src/widgets/ModuleWidget.cpp +++ b/src/widgets/ModuleWidget.cpp @@ -5,20 +5,21 @@ namespace rack { -ModuleWidget::ModuleWidget(Module *module) { - this->module = module; - if (module) { - engineAddModule(module); - } -} - ModuleWidget::~ModuleWidget() { // Make sure WireWidget destructors are called *before* removing `module` from the rack. disconnectPorts(); + setModule(NULL); +} + +void ModuleWidget::setModule(Module *module) { + if (this->module) { + engineRemoveModule(this->module); + delete this->module; + } if (module) { - engineRemoveModule(module); - delete module; + engineAddModule(module); } + this->module = module; } void ModuleWidget::addInput(InputPort *input) {