From 8106e4ae70809e1a1e6c69c1b81494547dbb1a27 Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Wed, 23 Sep 2020 20:09:12 -0400 Subject: [PATCH] Sprite shifting support --- src/droplets/droplet.cpp | 7 ++++--- src/droplets/droplet.h | 1 + src/droplets/vco_droplet.cpp | 15 +++++++++------ src/graphics/sprite.cpp | 36 +++++++++++++++++++++++++++++++++--- src/graphics/sprite.h | 9 +++++++++ 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/droplets/droplet.cpp b/src/droplets/droplet.cpp index 3bfbda4..831136f 100644 --- a/src/droplets/droplet.cpp +++ b/src/droplets/droplet.cpp @@ -57,9 +57,6 @@ void Droplet::AnimationInc() { count = 0; } count++; - //animation_count = animation_count % animation_rate; - WriteCenteredString(*patch, (screen_min + screen_max) / 2, 40, - Font_6x8, std::to_string(animation_count)); } void Droplet::SetAnimationRate(int rate) { @@ -69,3 +66,7 @@ void Droplet::SetAnimationRate(int rate) { int Droplet::GetAnimationCount() { return animation_count; } + +bool Droplet::NeedUpdate() { + return count % animation_rate == 0; +} diff --git a/src/droplets/droplet.h b/src/droplets/droplet.h index 20fd503..9d730e7 100644 --- a/src/droplets/droplet.h +++ b/src/droplets/droplet.h @@ -43,6 +43,7 @@ public: void AnimationInc(); void SetAnimationRate(int); int GetAnimationCount(); + bool NeedUpdate(); }; #endif // CASCADE_DROPLETS_DROPLET_H_ diff --git a/src/droplets/vco_droplet.cpp b/src/droplets/vco_droplet.cpp index 1226a5f..7b95a5d 100644 --- a/src/droplets/vco_droplet.cpp +++ b/src/droplets/vco_droplet.cpp @@ -16,6 +16,12 @@ VCODroplet::VCODroplet(DaisyPatch* m_patch, num_waves, Parameter::LINEAR); ampctrl.Init(Patch()->controls[Patch()->CTRL_4], 0.0, 0.5f, Parameter::LINEAR); + + wave->SetBlank(); + for (int i = 0; i < sine_width; i++) { + int pixel = (int) round(std::sin(2*pi*((double)(i%sine_width)/sine_width)) * (GetTitleHeight()/2) + GetTitleHeight()/2); + wave->SetPixel(i, pixel, true); + } } VCODroplet::~VCODroplet() { @@ -49,15 +55,12 @@ void VCODroplet::Process(float** in, float** out, size_t size) { } void VCODroplet::Draw() { - wave->SetBlank(); - for (int i = 0; i < sine_width; i++) { - int pixel = (int) round(std::sin(2*pi*((double)(i + GetAnimationCount()%sine_width)/sine_width)) * (GetTitleHeight()/2) + GetTitleHeight()/2); - wave->SetPixel(i, pixel, true); - } - WriteString(*Patch(), 0, 54, Font_6x8, WaveToString(wavectrl.Process())); wave->DrawTile(*Patch(), GetScreenMin(), 0, GetScreenMax(), GetTitleHeight()); + if(NeedUpdate()) { + wave->AdjustXShift(1); + } DrawName("VCO"); AnimationInc(); } diff --git a/src/graphics/sprite.cpp b/src/graphics/sprite.cpp index 8f3b6ef..b31ffa3 100644 --- a/src/graphics/sprite.cpp +++ b/src/graphics/sprite.cpp @@ -36,7 +36,9 @@ int Sprite::GetWidth() { void Sprite::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, sprite[w][h]); + patch.display.DrawPixel(x+w, y+h, + sprite[GetShiftArrayX(w)] + [GetShiftArrayY(h)]); } } } @@ -52,8 +54,8 @@ void Sprite::DrawTile(DaisyPatch patch, int y_max = std::max(y1, y2); for (int w = x_min; w < x_max; w++) { for (int h = y_min; h < y_max; h++) { - patch.display.DrawPixel(w, h, sprite[(w-x_min) % width] - [(h-y_min) % height]); + patch.display.DrawPixel(w, h, sprite[GetShiftArrayX((w-x_min) % width)] + [GetShiftArrayY((h-y_min+y_shift) % height)]); } } } @@ -69,3 +71,31 @@ void Sprite::SetBlank() { } } } + +void Sprite::SetXShift(int x) { + x_shift = x; +} + +void Sprite::SetYShift(int y) { + y_shift = y; +} + +void Sprite::AdjustXShift(int x) { + x_shift -= x; +} + +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); +} + +int Sprite::GetShiftArrayY(int pos) { + return GetShiftArray(pos, y_shift, height); +} diff --git a/src/graphics/sprite.h b/src/graphics/sprite.h index bd1699f..38523f3 100644 --- a/src/graphics/sprite.h +++ b/src/graphics/sprite.h @@ -11,6 +11,11 @@ class Sprite { private: int width, height; bool** sprite; + int x_shift = 0; + int y_shift = 0; + int GetShiftArray(int, int, int); + int GetShiftArrayX(int); + int GetShiftArrayY(int); public: Sprite(int, int); ~Sprite(); @@ -21,6 +26,10 @@ class Sprite { void Draw(DaisyPatch, int, int); void DrawTile(DaisyPatch, int, int, int, int); void SetBlank(); + void SetXShift(int); + void SetYShift(int); + void AdjustXShift(int); + void AdjustYShift(int); }; #endif // CASCADE_GRAPHICS_SPRITE_H_