diff --git a/OBJECTS b/OBJECTS index e1d1e63..9e4a69d 100644 --- a/OBJECTS +++ b/OBJECTS @@ -1,7 +1,7 @@ TARGET = main SRC = hal.cpp main.cpp example.cpp -SRC += gfx/screen.cpp gfx/canvas.cpp gfx/font.cpp gfx/layout.cpp +SRC += gfx/screen.cpp gfx/canvas.cpp gfx/font.cpp gfx/layout.cpp gfx/buffer.cpp SRC += fonts/basic_5x4.cpp diff --git a/inc/gfx/buffer.h b/inc/gfx/buffer.h new file mode 100644 index 0000000..364508f --- /dev/null +++ b/inc/gfx/buffer.h @@ -0,0 +1,50 @@ +// +// Created by jedi on 11/2/18. +// + +#ifndef MGL_DMXMENU_BUFFER_1_H +#define MGL_DMXMENU_BUFFER_1_H + + +#include +#include + +enum bufferBlocks{ + _1bit = 1, _2bit = 2, _4bit = 4, _8bit = 8, _16bit = 16, _24bit = 24, _32bit = 32 +}; + +template +class buffer { +private: + int *ptr_; + int size_; + constexpr static const int block_ = 8 * sizeof(int); +public: + buffer(int size) : size_(size) { + ptr_ = (int *) malloc(size * N / 8); + } + + int get(int i) { + if (i >= size_) return 0; + return ptr_[i*N/block_] & ((1< +int buffer<_32bit>::get(int i); + +template <> +void buffer<_32bit>::set(int i, int v); + +#endif //MGL_DMXMENU_BUFFER_1_H diff --git a/inc/gfx/buffer_1.h b/inc/gfx/buffer_1.h deleted file mode 100644 index 3ce889b..0000000 --- a/inc/gfx/buffer_1.h +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by jedi on 11/2/18. -// - -#ifndef MGL_DMXMENU_BUFFER_1_H -#define MGL_DMXMENU_BUFFER_1_H - - -#include -#include - -class buffer_1 { -private: - int *ptr_; - int size_; - constexpr static const int block_ = 8 * sizeof(int); -public: - buffer_1(int size) : size_(size) { - ptr_ = (int *) malloc(size * sizeof(int)); - } - - bool get(int i) { - if (i >= size_) return 0; - return ptr_[i/block_] & 1 << (i%block_); - } - - void set(int i, bool v) { - if (i < size_) { - ptr_[i/block_] &= ~(1 << (i%block_)); - ptr_[i/block_] |= v << (i%block_); - } - } - - ~buffer_1() { - free(ptr_); - } -}; - - -#endif //MGL_DMXMENU_BUFFER_1_H diff --git a/inc/gfx/buffer_32.h b/inc/gfx/buffer_32.h index 751c868..869f04f 100644 --- a/inc/gfx/buffer_32.h +++ b/inc/gfx/buffer_32.h @@ -9,29 +9,7 @@ #include #include -class buffer_32 { -private: - int size_; - void *ptr; -public: - buffer_32(int size) : size_(size) { - ptr = (uint8_t *) malloc(size * 4); - } - bool get(int i) { - if (i >= size_) return 0; - return ((int *) ptr)[i]; - } - - void set(int i, bool v) { - if (i < size_) - ((int *) ptr)[i] = v; - } - - ~buffer_32() { - free(ptr); - } -}; #endif //MGL_DMXMENU_BUFFER_8_H diff --git a/inc/gfx/canvas.h b/inc/gfx/canvas.h index f12a206..c65a4e6 100644 --- a/inc/gfx/canvas.h +++ b/inc/gfx/canvas.h @@ -8,7 +8,7 @@ #include "hal/hal.h" #include "gfx/font.h" #include "fonts/basic_5x4.h" -#include "gfx/buffer_1.h" +#include "gfx/buffer.h" #include "gfx/buffer_32.h" class screen; @@ -17,7 +17,7 @@ class canvas { private: const int width_; const int height_; - buffer_1 buffer_; + buffer<_1bit> buffer_; void draw_onto_(screen &); diff --git a/src/gfx/buffer.cpp b/src/gfx/buffer.cpp new file mode 100644 index 0000000..153b80f --- /dev/null +++ b/src/gfx/buffer.cpp @@ -0,0 +1,17 @@ +// +// Created by jedi on 11/2/18. +// + +#include "gfx/buffer.h" + +template <> +int buffer<_32bit>::get(int i){ + if (i >= size_) return 0; + return ptr_[i]; +} + +template <> +void buffer<_32bit>::set(int i, int v) { + if (i < size_) + ptr_[i] = v; +} \ No newline at end of file diff --git a/src/gfx/buffer_1.cpp b/src/gfx/buffer_1.cpp deleted file mode 100644 index 3e016ce..0000000 --- a/src/gfx/buffer_1.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by jedi on 11/2/18. -// - -#include "gfx/buffer_1.h"