44
55#include " Face.h"
66
7- // TODO(meganetaaan): move to another file
8- void transformSprite (M5Canvas *from, M5Canvas *to, float r, float s) {
9- int width = to->width ();
10- int height = to->height ();
11- int fromWidth = from->width ();
12- int fromHeight = from->height ();
13- int cx = width / 2 ;
14- int cy = height / 2 ;
15- float cosr = cos (r);
16- float sinr = sin (r);
17-
18- for (int y2 = 0 ; y2 < height; y2++) {
19- for (int x2 = 0 ; x2 < width; x2++) {
20- int x1 = ((((x2 - cx) * cosr) - ((y2 - cy) * sinr)) + cx) / s;
21- int y1 = ((((x2 - cx) * sinr) + ((y2 - cy) * cosr)) + cy) / s;
22- if (x1 < 0 || x1 >= fromWidth || y1 < 0 || y1 >= fromHeight) {
23- continue ;
24- }
25- int color = from->readPixel (x1, y1);
26- to->drawPixel (x2, y2, color);
27- }
28- }
29- }
30-
317namespace m5avatar {
328Balloon b;
339Effect h;
@@ -61,8 +37,7 @@ Face::Face(Drawable *mouth, BoundingRect *mouthPos, Drawable *eyeR,
6137 eyeblowRPos{eyeblowRPos},
6238 eyeblowLPos{eyeblowLPos},
6339 boundingRect{new BoundingRect (0 , 0 , 320 , 240 )},
64- sprite{new M5Canvas (&M5 .Lcd )},
65- tmpSpr{new M5Canvas (&M5 .Lcd )} {}
40+ sprite{new M5Canvas (&M5 .Lcd )} {}
6641
6742Face::~Face () {
6843 delete mouth;
@@ -76,7 +51,6 @@ Face::~Face() {
7651 delete eyeblowL;
7752 delete eyeblowLPos;
7853 delete sprite;
79- delete tmpSpr;
8054 delete boundingRect;
8155}
8256
@@ -137,21 +111,7 @@ void Face::draw(DrawContext *ctx) {
137111 float rotation = ctx->getRotation ();
138112
139113 if (scale != 1.0 || rotation != 0 ) {
140- uint16_t w = boundingRect->getWidth ();
141- uint16_t h = boundingRect->getHeight ();
142- uint16_t tmpSprWidth = _min (w * scale, 320 );
143- uint16_t tmpSprHeight = _min (h * scale, 240 );
144- tmpSpr->createSprite (tmpSprWidth, tmpSprHeight);
145- tmpSpr->setColorDepth (ctx->getColorDepth ());
146- tmpSpr->setBitmapColor (ctx->getColorPalette ()->get (COLOR_PRIMARY ),
147- ctx->getColorPalette ()->get (COLOR_BACKGROUND ));
148- if (ctx->getColorDepth () != 1 ) {
149- sprite->fillSprite (ctx->getColorPalette ()->get (COLOR_BACKGROUND ));
150- }
151- transformSprite (sprite, tmpSpr, rotation, scale);
152-
153- tmpSpr->pushSprite (boundingRect->getLeft (), boundingRect->getTop ());
154- tmpSpr->deleteSprite ();
114+ sprite->pushRotateZoom (rotation, scale, scale);
155115 } else {
156116 sprite->pushSprite (boundingRect->getLeft (), boundingRect->getTop ());
157117 }
0 commit comments