create generic buffer template

This commit is contained in:
j3d1 2018-11-05 21:49:40 +01:00
parent bfef6fdc1e
commit f3037a06ce
7 changed files with 70 additions and 70 deletions

View file

@ -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

50
inc/gfx/buffer.h Normal file
View file

@ -0,0 +1,50 @@
//
// Created by jedi on 11/2/18.
//
#ifndef MGL_DMXMENU_BUFFER_1_H
#define MGL_DMXMENU_BUFFER_1_H
#include <stdlib.h>
#include <stdint.h>
enum bufferBlocks{
_1bit = 1, _2bit = 2, _4bit = 4, _8bit = 8, _16bit = 16, _24bit = 24, _32bit = 32
};
template <bufferBlocks N>
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<<N)-1) << (i*N%block_);
}
void set(int i, int v) {
if (i < size_) {
ptr_[i*N/block_] &= ~(((1<<N)-1) << (i*N%block_));
ptr_[i*N/block_] |= (((1<<N)-1)&v) << (i*N%block_);
}
}
~buffer() {
free(ptr_);
}
};
template <>
int buffer<_32bit>::get(int i);
template <>
void buffer<_32bit>::set(int i, int v);
#endif //MGL_DMXMENU_BUFFER_1_H

View file

@ -1,40 +0,0 @@
//
// Created by jedi on 11/2/18.
//
#ifndef MGL_DMXMENU_BUFFER_1_H
#define MGL_DMXMENU_BUFFER_1_H
#include <stdlib.h>
#include <stdint.h>
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

View file

@ -9,29 +9,7 @@
#include <stdlib.h>
#include <stdint.h>
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

View file

@ -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 &);

17
src/gfx/buffer.cpp Normal file
View file

@ -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;
}

View file

@ -1,5 +0,0 @@
//
// Created by jedi on 11/2/18.
//
#include "gfx/buffer_1.h"