From a1fef25838592ecdb8f57b095ef5b6f1fe6bf84e Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Tue, 8 Aug 2023 17:05:08 +0400 Subject: [PATCH] wip: gui --- inc/Application.h | 3 +++ inc/Renderer.h | 8 +++++--- inc/Synth.h | 1 + inc/SynthGuiState.h | 16 ++++++++++++++++ src/Application.cpp | 24 ++++++++++++++---------- src/Renderer.cpp | 27 ++++++++++++++++++++++++--- 6 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 inc/SynthGuiState.h diff --git a/inc/Application.h b/inc/Application.h index cd52818..db9b9c9 100644 --- a/inc/Application.h +++ b/inc/Application.h @@ -4,10 +4,13 @@ #include "raylib.h" #include "RingBuffer.h" #include "Renderer.h" +#include "SynthGuiState.h" + class Application { private: Synth m_synth; + SynthGuiState m_synth_gui_state; RingBuffer* m_ring_buffer; AudioStream m_synth_stream; int m_sound_played_count; diff --git a/inc/Renderer.h b/inc/Renderer.h index 1afb6e3..19a887c 100644 --- a/inc/Renderer.h +++ b/inc/Renderer.h @@ -1,13 +1,15 @@ #pragma once - +#include "Synth.h" +#include "SynthGuiState.h" class Renderer { private: - /* data */ + void DrawUi(Synth & synth, const SynthGuiState & synthGui); + void DrawSignal(Synth & synth, const SynthGuiState & synthGui); public: Renderer(/* args */); ~Renderer(); - void Draw(); + void Draw(Synth& synth, const SynthGuiState & synthGui); }; diff --git a/inc/Synth.h b/inc/Synth.h index de508dd..c50d46e 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -21,4 +21,5 @@ public: ~Synth(); void ProduceNoteSound(Note input); const std::vector & GetOutSignal() { return m_out_signal; } + const std::vector& GetOscillators() { return m_oscillators; } }; \ No newline at end of file diff --git a/inc/SynthGuiState.h b/inc/SynthGuiState.h new file mode 100644 index 0000000..c873ec2 --- /dev/null +++ b/inc/SynthGuiState.h @@ -0,0 +1,16 @@ +#pragma once +#include "OscillatorType.h" +#include "raygui.h" +#include + +struct OscillatorGuiState { + float volume; + float freq;//todo: remove or change to pitch shift + OscillatorType waveshape; + bool is_dropdown_open; + Rectangle shape_dropdown_rect; +}; + +struct SynthGuiState { + std::vector oscillators; +}; \ No newline at end of file diff --git a/src/Application.cpp b/src/Application.cpp index 32ec379..3254d30 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -45,16 +45,20 @@ void Application::init_synth() }; //todo: move somewhere in initialization - // synth.ui_oscillators = malloc(sizeof(OscillatorUI) * synth.oscillators.count); - // for (size_t i = 0; i < synth.oscillators.count; i++) - // { - // OscillatorUI* ui = &synth.ui_oscillators[i]; - // assert(ui); + std::vector oscillators = m_synth.GetOscillators(); + m_synth_gui_state.oscillators.reserve(oscillators.size()); + for (size_t i = 0; i < oscillators.size(); i++) + { + Oscillator* osc = oscillators[i]; + assert(osc); - // ui->freq = synth.oscillators.array[i].freq; - // ui->waveshape = synth.oscillators.array[i].osc; - // ui->volume = synth.oscillators.array[i].volume; - // } + OscillatorGuiState ui = { + .freq = osc->GetFreq(), + .waveshape = osc->GetType(), + .volume = osc->GetVolume() + }; + m_synth_gui_state.oscillators.push_back(ui); + } } std::size_t Application::detect_note_pressed(Note* note) @@ -171,6 +175,6 @@ void Application::Run() play_buffered_audio(); update_on_note_input(); - m_renderer.Draw(); + m_renderer.Draw(m_synth, m_synth_gui_state); } } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 3c9dd41..6fcc6e5 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,5 +1,6 @@ #include "Renderer.h" #include "raylib.h" +#define RAYGUI_IMPLEMENTATION #include "raygui.h" #include "Settings.h" @@ -13,16 +14,36 @@ Renderer::~Renderer() { } -void Renderer::Draw() +void Renderer::Draw(Synth& synth, const SynthGuiState& synthGui) { BeginDrawing(); ClearBackground(RAYWHITE); //todo: implement renderer - //DrawUi(&synth); - //DrawSignal(&synth); + DrawUi(synth, synthGui); + DrawSignal(synth, synthGui); //DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); //DrawFPS(0,0); EndDrawing(); +} + +void Renderer::DrawSignal(Synth & synth, const SynthGuiState & synthGui) +{ + GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "", WINDOW_HEIGHT / 8, 2); + auto signal = synth.GetOutSignal(); + Vector2* signal_points = new Vector2[signal.size()]; + const float screen_vertical_midpoint = (WINDOW_HEIGHT/2); + for (int point_idx = 0; point_idx < signal.size(); point_idx++) + { + signal_points[point_idx].x = (float)point_idx + OSCILLATOR_PANEL_WIDTH; + signal_points[point_idx].y = screen_vertical_midpoint + (int)(signal[point_idx] * 300); + } + DrawLineStrip(signal_points, signal.size(), RED); + delete[] signal_points; +} + +void Renderer::DrawUi(Synth & synth, const SynthGuiState & synthGui) +{ + } \ No newline at end of file