diff --git a/Makefile b/Makefile index e5e9dfb4..ac81318b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -ARCH ?= lin -FLAGS += -g -Wall -O3 -march=core2 -ffast-math \ +include Makefile-arch.inc + +FLAGS += \ -I./ext -I./include -CXXFLAGS += -std=c++11 -fno-exceptions SOURCES = $(wildcard src/*.cpp src/*/*.cpp) \ ext/nanovg/src/nanovg.c @@ -11,7 +11,9 @@ ifeq ($(ARCH), lin) SOURCES += ext/noc/noc_file_dialog.c CFLAGS += -DNOC_FILE_DIALOG_GTK $(shell pkg-config --cflags gtk+-2.0) LDFLAGS += -rdynamic \ - -lpthread -lGL -lGLEW -lglfw -ldl -ljansson -lportaudio -lportmidi -lsamplerate -lcurl -lzip \ + -lpthread -lGL -ldl \ + -lportaudio -lportmidi \ + -Ldep/lib -lGLEW -lglfw -ljansson -lsamplerate -lcurl -lzip \ $(shell pkg-config --libs gtk+-2.0) TARGET = Rack endif @@ -40,9 +42,6 @@ LDFLAGS += \ -Wl,--export-all-symbols,--out-implib,libRack.a -mwindows TARGET = Rack.exe # OBJECTS = Rack.res - -%.res: %.rc - windres $^ -O coff -o $@ endif @@ -51,4 +50,8 @@ all: $(TARGET) clean: rm -rf $(TARGET) build -include Makefile.inc \ No newline at end of file +# For Windows resources +%.res: %.rc + windres $^ -O coff -o $@ + +include Makefile.inc diff --git a/Makefile-plugin.inc b/Makefile-plugin.inc index b453fb25..de945db5 100644 --- a/Makefile-plugin.inc +++ b/Makefile-plugin.inc @@ -1,10 +1,9 @@ -ARCH ?= lin +# All paths here assume the PWD is plugin/something +include ../../Makefile-arch.inc -FLAGS += -fPIC -g -Wall -O3 -march=core2 -ffast-math \ +FLAGS += -fPIC \ -I../../include -CXXFLAGS += -std=c++11 -fno-exceptions -LDFLAGS += ifeq ($(ARCH), lin) @@ -23,11 +22,9 @@ TARGET = plugin.dll endif - all: $(TARGET) clean: rm -rfv build $(TARGET) - include ../../Makefile.inc diff --git a/Makefile.inc b/Makefile.inc index 66aa19ce..b20c24b0 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -1,4 +1,9 @@ +# Generate dependency files build/*.d FLAGS += -MMD +# Optimization +FLAGS += -O3 -march=core2 -ffast-math +FLAGS += -g -Wall +CXXFLAGS += -std=c++11 ifeq ($(ARCH), lin) @@ -22,9 +27,6 @@ FLAGS += -DARCH_WIN -D_USE_MATH_DEFINES endif - -CFLAGS := $(FLAGS) $(CFLAGS) -CXXFLAGS := $(FLAGS) $(CXXFLAGS) OBJECTS += $(patsubst %, build/%.o, $(SOURCES)) DEPS = $(patsubst %, build/%.d, $(SOURCES)) @@ -40,16 +42,16 @@ $(TARGET): $(OBJECTS) build/%.c.o: %.c @mkdir -p $(@D) - $(CC) $(CFLAGS) -c -o $@ $< + $(CC) $(FLAGS) $(CFLAGS) -c -o $@ $< build/%.cpp.o: %.cpp @mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -c -o $@ $< + $(CXX) $(FLAGS) $(CXXFLAGS) -c -o $@ $< build/%.cc.o: %.cc @mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -c -o $@ $< + $(CXX) $(FLAGS) $(CXXFLAGS) -c -o $@ $< build/%.m.o: %.m @mkdir -p $(@D) - $(CC) $(CFLAGS) -c -o $@ $< + $(CC) $(FLAGS) $(CFLAGS) -c -o $@ $< diff --git a/include/app.hpp b/include/app.hpp index 2d6ab15d..4e875105 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -39,9 +39,15 @@ struct ModuleWidget : OpaqueWidget { json_t *toJson(); void fromJson(json_t *root); - void disconnectPorts(); - void resetParams(); - void randomizeParams(); + + /** Disconnects cables from all ports */ + void disconnect(); + /** Resets the state of the module */ + void initialize(); + /** Randomizes the state of the module + This method just randomizes parameters. Override and call this function if your module contains other state information that you wish to randomize. + */ + void randomize(); void draw(NVGcontext *vg); diff --git a/include/engine.hpp b/include/engine.hpp index 793719e3..a58572c4 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include #include "util.hpp" +#include namespace rack { @@ -21,8 +21,13 @@ struct Module { /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate */ virtual void step() {} - virtual json_t *toJsonData() { return NULL; } - virtual void fromJsonData(json_t *root) {} + + /** Override these to store extra internal data in the "data" property */ + virtual json_t *toJson() { return NULL; } + virtual void fromJson(json_t *root) {} + + virtual void initialize() {} + virtual void randomize() {} }; struct Wire { diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 93d7bfaa..08af4a9d 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -7,7 +7,7 @@ namespace rack { ModuleWidget::~ModuleWidget() { // Make sure WireWidget destructors are called *before* removing `module` from the rack. - disconnectPorts(); + disconnect(); setModule(NULL); } @@ -57,15 +57,19 @@ json_t *ModuleWidget::toJson() { } json_object_set_new(rootJ, "params", paramsJ); // data - json_t *dataJ = module ? module->toJsonData() : NULL; - if (dataJ) { - json_object_set_new(rootJ, "data", dataJ); + if (module) { + json_t *dataJ = module->toJson(); + if (dataJ) { + json_object_set_new(rootJ, "data", dataJ); + } } return rootJ; } void ModuleWidget::fromJson(json_t *rootJ) { + initialize(); + // pos json_t *pos = json_object_get(rootJ, "pos"); double x, y; @@ -85,11 +89,11 @@ void ModuleWidget::fromJson(json_t *rootJ) { // data json_t *dataJ = json_object_get(rootJ, "data"); if (dataJ && module) { - module->fromJsonData(dataJ); + module->fromJson(dataJ); } } -void ModuleWidget::disconnectPorts() { +void ModuleWidget::disconnect() { for (Port *input : inputs) { input->disconnect(); } @@ -98,16 +102,22 @@ void ModuleWidget::disconnectPorts() { } } -void ModuleWidget::resetParams() { +void ModuleWidget::initialize() { for (ParamWidget *param : params) { param->setValue(param->defaultValue); } + if (module) { + module->initialize(); + } } -void ModuleWidget::randomizeParams() { +void ModuleWidget::randomize() { for (ParamWidget *param : params) { param->setValue(rescalef(randomf(), 0.0, 1.0, param->minValue, param->maxValue)); } + if (module) { + module->randomize(); + } } void ModuleWidget::draw(NVGcontext *vg) { @@ -117,7 +127,7 @@ void ModuleWidget::draw(NVGcontext *vg) { bndBevel(vg, 0.0, 0.0, box.size.x, box.size.y); // CPU usage text - if (dynamic_cast(gScene)->toolbar->cpuUsageButton->value != 0.0) { + if (dynamic_cast(gScene)->toolbar->cpuUsageButton->value > 0.0) { float cpuTime = module ? module->cpuTime : 0.0; std::string text = stringf("%.1f%%", cpuTime * 100.0); @@ -152,32 +162,33 @@ void ModuleWidget::onDragMove(Vec mouseRel) { void ModuleWidget::onDragEnd() { } -struct DisconnectPortsMenuItem : MenuItem { +struct DisconnectMenuItem : MenuItem { ModuleWidget *moduleWidget; void onAction() { - moduleWidget->disconnectPorts(); + moduleWidget->disconnect(); } }; -struct ResetParamsMenuItem : MenuItem { +struct InitializeMenuItem : MenuItem { ModuleWidget *moduleWidget; void onAction() { - moduleWidget->resetParams(); + moduleWidget->initialize(); } }; -struct RandomizeParamsMenuItem : MenuItem { +struct RandomizeMenuItem : MenuItem { ModuleWidget *moduleWidget; void onAction() { - moduleWidget->randomizeParams(); + moduleWidget->randomize(); } }; -struct CloneModuleMenuItem : MenuItem { +struct CloneMenuItem : MenuItem { ModuleWidget *moduleWidget; void onAction() { // Create new module from model ModuleWidget *clonedModuleWidget = moduleWidget->model->createModuleWidget(); + // JSON serialization is the most straightforward way to do this json_t *moduleJ = moduleWidget->toJson(); clonedModuleWidget->fromJson(moduleJ); json_decref(moduleJ); @@ -187,7 +198,7 @@ struct CloneModuleMenuItem : MenuItem { } }; -struct DeleteModuleMenuItem : MenuItem { +struct DeleteMenuItem : MenuItem { ModuleWidget *moduleWidget; void onAction() { gRackWidget->moduleContainer->removeChild(moduleWidget); @@ -203,27 +214,27 @@ void ModuleWidget::onMouseDown(int button) { menuLabel->text = model->plugin->name + ": " + model->name; menu->pushChild(menuLabel); - ResetParamsMenuItem *resetItem = new ResetParamsMenuItem(); + InitializeMenuItem *resetItem = new InitializeMenuItem(); resetItem->text = "Initialize"; resetItem->moduleWidget = this; menu->pushChild(resetItem); - RandomizeParamsMenuItem *randomizeParams = new RandomizeParamsMenuItem(); - randomizeParams->text = "Randomize"; - randomizeParams->moduleWidget = this; - menu->pushChild(randomizeParams); + RandomizeMenuItem *randomizeItem = new RandomizeMenuItem(); + randomizeItem->text = "Randomize"; + randomizeItem->moduleWidget = this; + menu->pushChild(randomizeItem); - DisconnectPortsMenuItem *disconnectItem = new DisconnectPortsMenuItem(); + DisconnectMenuItem *disconnectItem = new DisconnectMenuItem(); disconnectItem->text = "Disconnect cables"; disconnectItem->moduleWidget = this; menu->pushChild(disconnectItem); - CloneModuleMenuItem *cloneItem = new CloneModuleMenuItem(); + CloneMenuItem *cloneItem = new CloneMenuItem(); cloneItem->text = "Clone"; cloneItem->moduleWidget = this; menu->pushChild(cloneItem); - DeleteModuleMenuItem *deleteItem = new DeleteModuleMenuItem(); + DeleteMenuItem *deleteItem = new DeleteMenuItem(); deleteItem->text = "Delete"; deleteItem->moduleWidget = this; menu->pushChild(deleteItem);