Files
EnergyMonitor_Pico/libs/mare/mare_drawables.cpp
Emanuele Trabattoni 539a95df61 even wonkier line...
problems with angles and screen limits,
need to implement screen rotation seriously
2021-05-29 12:55:35 +02:00

120 lines
3.5 KiB
C++

#include "mare.h"
namespace Render {
Drawable::Drawable(const size_t id, Drawable* parent, Mare* engine):
_id(id),
_parent(parent),
_engine(engine),
_origin({0,0}),
_bbox({0,0,0,0}),
_blendMode(Render::BlendMode::Intersect),
_dirty(false)
{
}
Drawable::~Drawable() {
}
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Point
DrawablePoint::DrawablePoint(const size_t id, Drawable* parent, Mare* engine, dim_t size):
Drawable(id, parent,engine),
_size(size)
{
}
DrawablePoint::~DrawablePoint() {
}
void DrawablePoint::render() {
uint8_t *buf;
if (isDirty()){
buf = getBuffer();
//TODO: implement screen rotation and margin check
auto dx = engine()->getSize().x;
auto dy = engine()->getSize().y;
for (uint16_t xx(0); xx< _size.x; xx++){
for (uint16_t yy(0); yy < _size.y; yy++)
{
engine()->setPixel(buf,dx-(getOrigin().x+xx),getOrigin().y+yy,true);
}
}
resetDirty();
}
}
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Line
DrawableLine::DrawableLine(const size_t id, Drawable* parent, Mare* engine):
Drawable(id, parent,engine),
_thickness(1),
_lenght(0),
_rotation(0.0f)
{
}
DrawableLine::~DrawableLine(){
}
void DrawableLine::render(){
uint8_t* buf = getBuffer();
auto tt = _thickness;
auto dx = engine()->getSize().x;
auto dy = engine()->getSize().y;
auto e = engine();
auto square = [tt,dy,buf,e](uint16_t x, uint16_t y){ //sprite render
for (auto xx(0); xx <= tt; ++xx)
for (auto yy(0); yy <= tt; ++yy){
e->setPixel(buf,dy-(yy+y),xx+x,true);
}
};
if (isDirty()) {
_rotation = fmod(_rotation, M_TWOPI);
float cx,sx;
sincosf(_rotation,&sx,&cx); // use optimized float instructions
uint16_t endX = round(cx*(float)_lenght);
uint16_t endY = round(sx*(float)_lenght);
if ((_rotation >= -M_PI_4 && _rotation <= M_PI_4) ||
(_rotation >= 3*M_PI_4 && _rotation <= 5*M_PI_4))
for(uint16_t xx(0); xx <= endX; ++xx){
float dydx = cx == 0.0f ? 0 : (sx/cx);
uint16_t y = getOrigin().y+xx*dydx;
uint16_t x = getOrigin().x+xx;
square(x,y);
}
else
for (uint16_t yy(0); yy <= endY; ++yy){
float dxdy = sx == 0.0f ? 0 : (cx/sx);
uint16_t y = getOrigin().y+yy;
uint16_t x = getOrigin().x+yy*dxdy;
square(x,y);
}
resetDirty();
}
}
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Rectangle
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Circle
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Character
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//String
//--------+--------+--------+--------+--------+--------+--------+--------+--------+--------//
//Custom
}