[refactor]: c++ implementation #13
@@ -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<float>* m_ring_buffer;
|
||||
AudioStream m_synth_stream;
|
||||
int m_sound_played_count;
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -21,4 +21,5 @@ public:
|
||||
~Synth();
|
||||
void ProduceNoteSound(Note input);
|
||||
const std::vector<float> & GetOutSignal() { return m_out_signal; }
|
||||
const std::vector<Oscillator*>& GetOscillators() { return m_oscillators; }
|
||||
};
|
||||
16
inc/SynthGuiState.h
Normal file
16
inc/SynthGuiState.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "OscillatorType.h"
|
||||
#include "raygui.h"
|
||||
#include <vector>
|
||||
|
||||
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<OscillatorGuiState> oscillators;
|
||||
};
|
||||
@@ -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<Oscillator*> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user