render line with rotation method, still some bugs

This commit is contained in:
Emanuele Trabattoni
2021-06-02 18:04:23 +02:00
parent 7116885f2d
commit 60c6ca83c1
6 changed files with 109 additions and 73 deletions

View File

@@ -8,7 +8,7 @@ namespace Render {
_engine(engine),
_origin({0,0}),
_bbox({0,0,0,0}),
_blendMode(Render::BlendMode::Intersect),
_blendMode(Render::BlendMode::Add),
_dirty(false)
{
}
@@ -32,6 +32,7 @@ namespace Render {
void DrawablePoint::render() {
uint8_t *buf;
pos_t o = getOrigin();
if (isDirty()){
buf = getBuffer();
//TODO: implement screen rotation and margin check
@@ -40,7 +41,7 @@ namespace Render {
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);
engine()->setPixel(buf,o.x+xx,o.y+yy,getBlendMode());
}
}
resetDirty();
@@ -54,7 +55,8 @@ namespace Render {
Drawable(id, parent,engine),
_thickness(1),
_lenght(0),
_rotation(0.0f)
_rotation(0.0f),
_outline(false)
{
}
@@ -67,40 +69,55 @@ namespace Render {
uint8_t* buf = getBuffer();
auto tt = _thickness;
auto e = engine();
auto square = [buf,e,tt](uint16_t x, uint16_t y){
uint8_t c = tt > 1 ? tt>>1 : 0; //sprite render
x -= c;
y -= c;
for (auto xx(0); xx <= tt; ++xx)
for (auto yy(0); yy <= tt; ++yy){
e->setPixel(buf,xx+x,yy+y,true);
}
};
// auto square = [buf,e,tt](uint16_t x, uint16_t y){
// uint8_t c = tt > 1 ? tt>>1 : 0; //sprite render
// x -= c;
// y -= c;
// for (auto xx(0); xx <= tt; ++xx)
// for (auto yy(0); yy <= tt; ++yy){
// e->setPixel(buf,xx+x,yy+y,getBlendMode());
// }
// };
if (isDirty()) {
_rotation = fmod(_rotation, M_TWOPI);
float cx,sx;
sincosf(_rotation,&sx,&cx); // use optimized float instructions
uint16_t endX = fabs(round(cx*(float)_lenght));
uint16_t endY = fabs(round(sx*(float)_lenght));
// if (isDirty() && false) {
// _rotation = fmod(_rotation, M_TWOPI);
// float cx,sx;
// sincosf(_rotation,&sx,&cx); // use optimized float instructions
// uint16_t endX = fabs(round(cx*(float)_lenght));
// uint16_t endY = fabs(round(sx*(float)_lenght));
int8_t mult = (_rotation <= 3*M_PI_4 || _rotation >= 7*M_PI_4) ? 1 : -1;
// int8_t mult = (_rotation <= 3*M_PI_4 || _rotation >= 7*M_PI_4) ? 1 : -1;
if ((_rotation >= 7*M_PI_4 || _rotation <= M_PI_4) ||
(_rotation >= 3*M_PI_4 && _rotation <= 5*M_PI_4)) {
// if ((_rotation >= 7*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*mult*dydx;
uint16_t x = getOrigin().x+xx*mult;
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*mult;
uint16_t x = getOrigin().x+yy*mult*dxdy;
square(x,y);
// for(uint16_t xx(0); xx <= endX; ++xx){
// float dydx = cx == 0.0f ? 0 : (sx/cx);
// uint16_t y = getOrigin().y+xx*mult*dydx;
// uint16_t x = getOrigin().x+xx*mult;
// 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*mult;
// uint16_t x = getOrigin().x+yy*mult*dxdy;
// square(x,y);
// }
// }
// resetDirty();
// }
if (isDirty() && true) {
_rotation = fmod(_rotation, M_TWOPI);
pos_t o = getOrigin();
static spos_t res = {0,0};
for (uint16_t t(0); t < _thickness; ++t) {
for (uint16_t xx(0); xx < _lenght; ++xx) {
if (!_outline || (_outline && (xx == 0 || xx == _lenght - 1 || t == 0 || t == _thickness - 1))) {
res = e->rotateXY(xx,t-(_thickness>>1),_rotation);
e->setPixel(buf,res.x+o.x,res.y+o.y,getBlendMode());
}
}
}
resetDirty();