From c43cc0ae809b49090e00def7fda0eef95f3a42f3 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 8 Jan 2022 18:18:23 -0500 Subject: [PATCH] GUI: add option to add more systems --- src/engine/engine.cpp | 29 ++++++++++++++++++++++++++++- src/engine/engine.h | 4 ++-- src/gui/gui.cpp | 24 +++++++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 9c0372644..0eb886fdf 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -485,7 +485,12 @@ bool DivEngine::loadDMF(unsigned char* file, size_t len) { if (ds.version>0x0a) { String hz=reader.readString(3); if (ds.customTempo) { - ds.hz=std::stoi(hz); + try { + ds.hz=std::stoi(hz); + } catch (std::exception& e) { + logW("invalid custom Hz!\n"); + ds.hz=60; + } } } // TODO @@ -1516,6 +1521,28 @@ void DivEngine::changeSystem(int index, DivSystem which) { isBusy.unlock(); } +bool DivEngine::addSystem(DivSystem which) { + if (song.systemLen>32) { + lastError="cannot add more than 32 systems"; + return false; + } + if (chans+getChannelCount(which)>DIV_MAX_CHANS) { + lastError="max number of total channels is 128"; + return false; + } + quitDispatch(); + isBusy.lock(); + song.system[song.systemLen++]=which; + recalcChans(); + isBusy.unlock(); + initDispatch(); + isBusy.lock(); + renderSamples(); + reset(); + isBusy.unlock(); + return true; +} + String DivEngine::getLastError() { return lastError; } diff --git a/src/engine/engine.h b/src/engine/engine.h index 7540d4127..bedf92d10 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -402,10 +402,10 @@ class DivEngine { void changeSystem(int index, DivSystem which); // add system - void addSystem(DivSystem which); + bool addSystem(DivSystem which); // remove system - void removeSystem(int index); + bool removeSystem(int index); // get last error String getLastError(); diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index dbb016c7a..295200efd 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -2555,6 +2555,14 @@ void FurnaceGUI::processDrags(int dragX, int dragY) { } } +#define sysAddOption(x) \ + if (ImGui::MenuItem(e->getSystemName(x))) { \ + if (!e->addSystem(x)) { \ + showError("cannot add system! ("+e->getLastError()+")"); \ + } \ + updateWindowTitle(); \ + } + #define sysChangeOption(x) \ if (ImGui::MenuItem(e->getSystemName(x),NULL,e->song.system[0]==x)) { \ e->changeSystem(0,x); \ @@ -2664,7 +2672,21 @@ bool FurnaceGUI::loop() { openFileDialog(GUI_FILE_SAVE); } ImGui::Separator(); - if (ImGui::BeginMenu("change platform...")) { + if (ImGui::BeginMenu("add platform...")) { + sysAddOption(DIV_SYSTEM_GENESIS); + sysAddOption(DIV_SYSTEM_GENESIS_EXT); + sysAddOption(DIV_SYSTEM_SMS); + sysAddOption(DIV_SYSTEM_GB); + sysAddOption(DIV_SYSTEM_PCE); + sysAddOption(DIV_SYSTEM_NES); + sysAddOption(DIV_SYSTEM_C64_8580); + sysAddOption(DIV_SYSTEM_C64_6581); + sysAddOption(DIV_SYSTEM_ARCADE); + sysAddOption(DIV_SYSTEM_YM2610); + sysAddOption(DIV_SYSTEM_YM2610_EXT); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("change first platform...")) { sysChangeOption(DIV_SYSTEM_GENESIS); sysChangeOption(DIV_SYSTEM_GENESIS_EXT); sysChangeOption(DIV_SYSTEM_SMS);