render hollow circle, begin of rendering chars

This commit is contained in:
Emanuele Trabattoni
2021-06-12 17:32:45 +02:00
parent d910980ede
commit 487054ff76
11 changed files with 219 additions and 1627 deletions

2
libs/mare/charmap.h Normal file
View File

@@ -0,0 +1,2 @@
#include <pico/stdlib.h>
#include <fonts.h>

View File

@@ -18,12 +18,15 @@ extern "C" {
#include <cstring>
#include <memory>
#include <math.h>
#include <fonts.h>
namespace Render{
typedef int32_t p_t;
struct pos_t {
friend bool operator==(const pos_t a, const pos_t b){ return a.x==b.x && a.y==b.y;};
friend bool operator!=(const pos_t a, const pos_t b){ return a.x!=b.x || a.y!=b.y;};
p_t x;
p_t y;
};
@@ -50,11 +53,24 @@ namespace Render{
Black
};
enum class Font {
Font8,
Font12,
Font16,
Font20,
Font24
};
typedef ScreenRotation TextRotation;
class Page;
class Drawable;
class DrawablePoint;
class DrawableLine;
class DrawableRectangle;
class DrawableCircle;
class DrawableString;
class DrawableCustom;
typedef pos_t dim_t;
typedef std::vector<std::shared_ptr<Page>> Pages;
@@ -244,45 +260,68 @@ namespace Render{
};
class DrawableCircle: public Drawable {
DrawableCircle();
~DrawableCircle();
public:
DrawableCircle(const size_t id, Drawable* parent, Mare* engine);
~DrawableCircle();
public:
void setThickness(uint16_t thickness) { _thickness = thickness; };
const uint16_t getThickness() {return _thickness; };
void setRadius(uint16_t radius) { _radius = radius; };
const uint16_t getRadius() { return _radius; };
void setOutline(bool outline) { _outline = outline; };
const getOutline() { return _outline; };
private:
void render() {};
void render();
// members
private:
uint16_t _thickness;
uint16_t _radius;
bool _outline;
};
class DrawableChar: public Drawable {
DrawableChar();
~DrawableChar();
friend class DrawableString;
public:
DrawableChar(const size_t id, Drawable* parent, Mare* engine, char c, Font f);
~DrawableChar();
public:
private:
void render() {};
void render();
// members
private:
Font _fontEnum;
_tFont _font;
char _char;
};
class DrawableString: public Drawable {
DrawableString();
~DrawableString();
public:
DrawableString(const size_t id, Drawable* parent, Mare* engine, std::string &s, Font f);
~DrawableString();
public:
void setString(std::string& s) { _s = s; };
const std::string& getString() { return &s; };
void setFont(Font f) { _fontEnum = f; };
const Font getFont() { return _fontEnum; };
void setRotation(TextRotation rotation) { _rotation = rotation; };
const TextRotation getRotation() { return _rotation; };
private:
void render() {};
void render();
// members
private:
Font _fontEnum;
TextRotation _rotation;
std::string _s;
std::vector<DrawableChar*> _chars;
};
class DrawableCustom: public Drawable {

View File

@@ -1,4 +1,5 @@
#include "mare.h"
#include "charmap.h"
namespace Render {
@@ -76,11 +77,11 @@ namespace Render {
for (uint16_t t(0); t < _thickness; ++t) {
for (uint16_t xx(0); xx < _length; ++xx) {
if (_rotation == 0 || _rotation == 180){
res.x = _rotation == 0 ? xx : (_length-xx);
res.y = t-(_thickness>>1);
res.x = _rotation == 0 ? xx : -xx;
res.y = t-_ofst;
} else if (_rotation == 90 || _rotation == 270){
res.x = t-(_thickness>>1);
res.y = _rotation == 90 ? xx : _length-xx;
res.x = t-_ofst;
res.y = _rotation == 90 ? xx : -xx;
} else {
res = e->rotateXY(xx,t-_ofst,_rotation/180.0f*M_PI);
}
@@ -172,11 +173,143 @@ namespace Render {
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Circle
DrawableCircle::DrawableCircle(const size_t id, Drawable* parent, Mare* engine):
Drawable(id, parent,engine),
_thickness(1),
_radius(8),
_outline(false)
{
}
DrawableCircle::~DrawableCircle ()
{
}
void DrawableCircle::render()
{
auto e = engine();
uint8_t* buf = getBuffer();
auto tt = _thickness;
auto bm = getBlendMode();
const pos_t o = getOrigin();
const uint8_t t2 = _thickness >> 1;
const pos_t oo = {o.x-t2, o.y-t2};
auto dot = [buf, tt, e, bm] (p_t x, p_t y) {
for (uint16_t xx(0); xx < tt; ++xx) {
for (uint16_t yy(0); yy < tt; ++yy) {
e->setPixel(buf, xx+x, yy+y, bm);
}
}
};
auto pix = [buf,e,bm](p_t x, p_t y) {
e->setPixel(buf, x, y, bm);
};
if (isDirty()){
if (_outline){
const float st = M_PI_2/45;
pos_t pres;
for (float t(0); t <= M_TWOPI; t+=st){
auto res = e->rotateXY(_radius,0,t);
if (res != pres) {
dot(oo.x+res.x, oo.y+res.y);
dot(oo.x-res.x, oo.y+res.y);
dot(oo.x-res.x, oo.y+res.y);
dot(oo.x-res.x, oo.y-res.y);
pres=res;
}
}
}
resetDirty();
}
}
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Character
extern const _tFont* Font8;
extern const _tFont* Font12;
extern const _tFont* Font16;
extern const _tFont* Font20;
extern const _tFont* Font24;
DrawableChar::DrawableChar(const size_t id, Drawable* parent, Mare* engine, char c, Font f) :
Drawable(id, parent,engine),
_fontEnum(f),
_char(c)
{
switch (_fontEnum)
{
case Font::Font8:
_font = *Font8;
break;
case Font::Font12:
_font = *Font12;
break;
case Font::Font16:
_font = *Font16;
break;
case Font::Font20:
_font = *Font20;
break;
case Font::Font24:
_font = *Font24;
break;
default:
break;
}
}
DrawableChar::~DrawableChar() {
}
void DrawableChar::render() {
}
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//String
DrawableString::DrawableString(const size_t id, Drawable* parent, Mare* engine, std::string &s, Font f) :
Drawable(id, parent,engine),
_fontEnum(f),
_s(s)
{
_chars.reserve(s.size());
uint16_t idx(0);
for(auto c : _s){
_chars.push_back(engine->addDrawable<DrawableChar>(c.c_str(),_fontEnum));
}
}
DrawableString::~DrawableString()
{
//free heap of drawable characters
for (auto c : _chars) {
delete c;
}
_chars.clear();
}
void DrawableString::render() {
for (auto c : _chars){
if (c != nullptr){
c->render();
}
}
}
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Custom