diff --git a/libraries/picosystem.hpp b/libraries/picosystem.hpp index edbc1cd..c0cbf05 100644 --- a/libraries/picosystem.hpp +++ b/libraries/picosystem.hpp @@ -34,10 +34,15 @@ namespace picosystem { struct buffer_t { int32_t w, h; color_t *data; + bool alloc; color_t *p(int32_t x, int32_t y) { return data + (x + y * w); } + + ~buffer_t() { + if (alloc) delete data; + } }; struct voice_t { diff --git a/libraries/utility.cpp b/libraries/utility.cpp index 7a9848a..aa17092 100644 --- a/libraries/utility.cpp +++ b/libraries/utility.cpp @@ -60,7 +60,16 @@ namespace picosystem { buffer_t *b = new buffer_t(); b->w = w; b->h = h; - b->data = data ? (color_t *)data : new color_t[w * h]; + if (data) + { + b->data = (color_t *)data; + b->alloc = false; + } + else + { + b->data = new color_t[w * h]{}; + b->alloc = true; + } return b; } @@ -111,4 +120,4 @@ namespace picosystem { return _fsin_lut[uint8_t((v * _2PI_LUTS) + 64)]; } -} \ No newline at end of file +} diff --git a/micropython/examples/picosystem/text.py b/micropython/examples/picosystem/text.py index 8f09c0b..24a1623 100644 --- a/micropython/examples/picosystem/text.py +++ b/micropython/examples/picosystem/text.py @@ -10,10 +10,10 @@ def update(tick): global view - if(pressed(RIGHT)): + if pressed(RIGHT): view += 1 - if(pressed(LEFT)): + if pressed(LEFT): view -= 1 view %= view_count