From 65a26b8e3159daec30d5cf7c935451a30adef821 Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Sun, 9 Jan 2022 22:13:14 -0500 Subject: [PATCH] Basic graph rendering system --- lib/libDaisy | 2 +- src/droplets/ad_droplet.cpp | 13 ++++++++ src/droplets/ad_droplet.h | 6 ++-- src/graphics/graph.cpp | 66 +++++++++++++++++++++++++++++++++++++ src/graphics/graph.h | 29 ++++++++++++++++ src/graphics/sprite.cpp | 6 ---- src/graphics/sprite.h | 21 +++--------- src/util.cpp | 5 +++ src/util.h | 13 ++++++++ 9 files changed, 136 insertions(+), 25 deletions(-) create mode 100644 src/graphics/graph.cpp create mode 100644 src/graphics/graph.h diff --git a/lib/libDaisy b/lib/libDaisy index ceb8c02..ffd93e5 160000 --- a/lib/libDaisy +++ b/lib/libDaisy @@ -1 +1 @@ -Subproject commit ceb8c02fb06b279c333e28cda731dd7f35bb0684 +Subproject commit ffd93e5a95755b5e990d64058f171cd2f849f088 diff --git a/src/droplets/ad_droplet.cpp b/src/droplets/ad_droplet.cpp index fe1b100..45588a8 100644 --- a/src/droplets/ad_droplet.cpp +++ b/src/droplets/ad_droplet.cpp @@ -105,6 +105,7 @@ ADDroplet::ADDroplet(DaisyPatch* m_patch, Droplet(m_patch, m_state) { sample_rate = m_sample_rate; + SetAnimationRate(20); ad[0].Init(Patch(), sample_rate, &m_state); @@ -113,6 +114,9 @@ ADDroplet::ADDroplet(DaisyPatch* m_patch, sample_rate, &m_state); } + + title_graph = new Graph(GetScreenMax()-GetScreenMin(), + GetTitleHeight()); } ADDroplet::~ADDroplet() {} @@ -185,8 +189,17 @@ void ADDroplet::Draw() { DrawSolidRect(Patch(), GetScreenMin(), 10, GetScreenMin()+1, 29, true); } } + + if(NeedUpdate()) { + title_graph->Update(); + } + title_graph->SetPixelPercentage(ad[0].GetSignal()); + + //title_graph->SetPixelPercentage(0.99f); + title_graph->Draw(Patch(), 0, 0); DrawName("AD"); + AnimationInc(); } void ADDroplet::UpdateStateCallback() { diff --git a/src/droplets/ad_droplet.h b/src/droplets/ad_droplet.h index 6167e75..e085f87 100644 --- a/src/droplets/ad_droplet.h +++ b/src/droplets/ad_droplet.h @@ -8,6 +8,7 @@ #include "droplet.h" #include "../util.h" +#include "../graphics/graph.h" using namespace daisy; using namespace daisysp; @@ -47,6 +48,7 @@ class ADDroplet: public Droplet { private: AD ad[2]; float sample_rate; + Graph* title_graph; public: /* @@ -56,8 +58,8 @@ public: * @param m_state droplet position */ ADDroplet(DaisyPatch* m_patch, - DropletState m_state, - float sample_rate); + DropletState m_state, + float sample_rate); /* * Destructor for vco droplet. diff --git a/src/graphics/graph.cpp b/src/graphics/graph.cpp new file mode 100644 index 0000000..187f247 --- /dev/null +++ b/src/graphics/graph.cpp @@ -0,0 +1,66 @@ +#include "graph.h" + +Graph::Graph(int m_width, int m_height) { + width = m_width; + height = m_height; + graph = new bool*[width]; + for (int w = 0; w < width; w++) { + graph[w] = new bool[height]; + for (int h = 0; h < height; h++) { + graph[w][h] = false; + } + } + active = 0; +} + +Graph::~Graph() { + for (int w = 0; w < width; w++) { + delete[] graph[w]; + } + delete[] graph; +} + +int Graph::GetNextActive() { + if (active == width - 1) { + return 0; + } else { + return active+1; + } +} + +void Graph::Update() { + active = GetNextActive(); + ClearColumn(); +} + +void Graph::ClearColumn() { + for (int h = 0; h < height; h++) { + graph[active][h] = false; + } +} + +void Graph::SetPixel(int pos) { + SetPixel(pos, true); +} + +void Graph::SetPixel(int pos, bool on) { + graph[active][pos] = on; +} + +void Graph::SetPixelPercentage(double percentage) { + SetPixelPercentage(percentage, true); +} + +void Graph::SetPixelPercentage(double percentage, bool on) { + SetPixel((int)(percentage*height), on); +} + +void Graph::Draw(DaisyPatch* patch, int x, int y) { + for (int w = 0; w < width; w++) { + for (int h = 0; h < height; h++) { + patch->display.DrawPixel(x+w, y+h, + graph[GetShiftArray(w, active, width)] + [GetShiftArray(height-h, height-1, height)]); + } + } +} diff --git a/src/graphics/graph.h b/src/graphics/graph.h new file mode 100644 index 0000000..58f1454 --- /dev/null +++ b/src/graphics/graph.h @@ -0,0 +1,29 @@ +#pragma once + +#ifndef DROPLETS_GRAPHICS_GRAPH_H +#define DROPLETS_GRAPHICS_GRAPH_H + +#include "daisy_patch.h" +#include "../util.h" + +using namespace daisy; + +class Graph { + private: + int width, height, active; + bool** graph; + int GetNextActive(); + public: + Graph(int m_width, + int m_height); + ~Graph(); + void Update(); + void ClearColumn(); + void SetPixel(int pos); + void SetPixel(int pos, bool on); + void SetPixelPercentage(double percentage); + void SetPixelPercentage(double percentage, bool on); + void Draw(DaisyPatch* patch, int x, int y); +}; + +#endif // DROPLETS_GRAPHICS_GRAPH_H diff --git a/src/graphics/sprite.cpp b/src/graphics/sprite.cpp index 542c6fd..b2baa33 100644 --- a/src/graphics/sprite.cpp +++ b/src/graphics/sprite.cpp @@ -116,12 +116,6 @@ void Sprite::AdjustYShift(int y) { y_shift += y; } -int Sprite::GetShiftArray(int pos, - int shift, - int array_size) { - return (array_size + ((pos + shift) % array_size)) % array_size; -} - int Sprite::GetShiftArrayX(int pos) { return GetShiftArray(pos, x_shift, width); } diff --git a/src/graphics/sprite.h b/src/graphics/sprite.h index 5076319..546485a 100644 --- a/src/graphics/sprite.h +++ b/src/graphics/sprite.h @@ -1,9 +1,10 @@ #pragma once -#ifndef CASCADE_GRAPHICS_SPRITE_H_ -#define CASCADE_GRAPHICS_SPRITE_H_ +#ifndef DROPLETS_GRAPHICS_SPRITE_H_ +#define DROPLETS_GRAPHICS_SPRITE_H_ #include "daisy_patch.h" +#include "../util.h" using namespace daisy; @@ -13,19 +14,7 @@ class Sprite { bool** sprite; int x_shift = 0; int y_shift = 0; - - /* - * Converts a graphic pixel location to a new one based on - * a given shift. - * - * @param pos pixel position - * @param shift shift amount - * @param array_size max length of dimension - * return new pixel position - */ - int GetShiftArray(int pos, - int shift, - int array_size); + /* * Converts a x axis position based upon the sprites shift. * @@ -163,4 +152,4 @@ class Sprite { void AdjustYShift(int y); }; -#endif // CASCADE_GRAPHICS_SPRITE_H_ +#endif // DROPLETS_GRAPHICS_SPRITE_H_ diff --git a/src/util.cpp b/src/util.cpp index 95dc4fc..866f00f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -179,3 +179,8 @@ void DrawFourDividedRectangles(DaisyPatch* patch, rect_three_max); } +int GetShiftArray(int pos, + int shift, + int array_size) { + return (array_size + ((pos + shift) % array_size)) % array_size; +} diff --git a/src/util.h b/src/util.h index 2f9a034..c0994c4 100644 --- a/src/util.h +++ b/src/util.h @@ -197,4 +197,17 @@ void DrawFourDividedRectangles(DaisyPatch* patch, int rect_four_min, int rect_four_max); + /* + * Converts a graphic pixel location to a new one based on + * a given shift. + * + * @param pos pixel position + * @param shift shift amount + * @param array_size max length of dimension + * return new pixel position + */ + int GetShiftArray(int pos, + int shift, + int array_size); + #endif // CASCADE_UTIL_H_