1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
43
44
48
49
50
51
52
53
54
55
56
57
62
76
77
78
79
80
91
92
93
97
98
99
100
101
102
103
104
105
106
107
108
109
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
175
180
181
182
183
184
185
186
187
188
189
196
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
223
227
228
229
230
231
232
233
234
235
242
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
288
289
290
291
295
299
300
301
302
303
304
305
306
307
308
309
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
343
347
348
349
350
351
352
358
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
383
384
385
389
390
391
395
396
397
398
399
400
404
405
406
407
408
409
410
416
417
428
429
430
436
437
438
441
442
443
444
445
446
447
448
449
450
451
452
453
454
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
514
520
521
522
523
524
525
526
527
528
529
533
534
535
536
537
538
539
540
541
542
543
544
545
/* ... */
#include <gui/animated_graphics_screen/BumpMapImage.hpp>
#include <touchgfx/EasingEquations.hpp>
BumpMapImage::BumpMapImage(BitmapId image_, const unsigned int* bump_map_, const unsigned char* light_source_) :
currentState(ANIMATE_AUTO),
image(image_),
bump(0),
bump_map(bump_map_),
light_source(light_source_),
currentTime(0),
catchTime(25),
lightPositionX(1000),
lightPositionY(1000),
tempPositionX(1000),
tempPositionY(1000)
{
Application::getInstance()->registerTimerWidget(this);
}{ ... }
BumpMapImage::~BumpMapImage()
{
Application::getInstance()->unregisterTimerWidget(this);
}{ ... }
void BumpMapImage::handleTickEvent()
{
currentTime += 2;
tempPositionX = (int)(128.0f * cos((double)currentTime / 64)) - 20 + 160;
tempPositionY = (int)(128.0f * sin((double) - currentTime / 45)) + 20 + 110;
invalidateAccordingToCurrentLightSetting();
if (currentState == ANIMATE_AUTO)
{
lightPositionX = tempPositionX;
lightPositionY = tempPositionY;
}if (currentState == ANIMATE_AUTO) { ... }
else if (currentState == ANIMATE_CATCH)
{
if (catchTime > 0)
{
lightPositionX = lightPositionX + ((tempPositionX - lightPositionX) / catchTime);
lightPositionY = lightPositionY + ((tempPositionY - lightPositionY) / catchTime);
catchTime--;
}if (catchTime > 0) { ... }
else
{
catchTime = 25;
currentState = ANIMATE_AUTO;
}else { ... }
}else if (currentState == ANIMATE_CATCH) { ... }
invalidateAccordingToCurrentLightSetting();
}{ ... }
void BumpMapImage::handleClickEvent(const ClickEvent& evt)
{
if (evt.getType() == ClickEvent::PRESSED)
{
setDragFinish(false);
}if (evt.getType() == ClickEvent::PRESSED) { ... }
else
{
setDragFinish(true);
}else { ... }
}{ ... }
void BumpMapImage::draw(const touchgfx::Rect& r) const
{
drawOptimized(r);
...}
void BumpMapImage::drawOptimized(const touchgfx::Rect& r) const
{
Rect absoluteRect = r;
translateRectToAbsolute(absoluteRect);
/* ... */
int16_t nx;
int16_t ny;
int16_t lightCalculationPositionX;
int16_t lightCalculationPositionY;
uint32_t* fb = (uint32_t*)HAL::getInstance()->lockFrameBuffer();
const uint32_t* imageData = (const uint32_t*)(image.getData());
int offset;
uint16_t invalidatedY = absoluteRect.y;
uint16_t invalidatedX = absoluteRect.x;
uint32_t newValue = 0;
uint16_t imageDataValue = 0;
uint8_t lightDiff = 0;
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
uint32_t colorResult = 0;
int relativeX;
int relativeY;
int bumpMapInfo;
uint32_t pixelColorValues;
for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++)
{
offset = (absoluteY * image.getWidth() / 2) + (invalidatedX - getX()) / 2;
for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX += 2, offset += 1)
{
newValue = 0;
relativeX = absoluteX - getX();
relativeY = absoluteY - getY();
bumpMapInfo = bump_map[offset];
pixelColorValues = *(imageData + offset);
nx = (int8_t)(bumpMapInfo >> 24);
ny = (int8_t)((bumpMapInfo >> 16) & 0xFF);
lightCalculationPositionX = (relativeX - lightPositionX) + (lightSourceWidth / 2) + nx;
lightCalculationPositionY = (relativeY - lightPositionY) + (lightSourceHeight / 2) + ny;
if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight)
{
lightDiff = 0;
}if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { ... }
else
{
lightDiff = light_source[lightCalculationPositionY * lightSourceWidth + lightCalculationPositionX];
}else { ... }
imageDataValue = pixelColorValues & 0xFFFF;
red = (imageDataValue & 0xF800) >> 11;
green = (imageDataValue & 0x07E0) >> 5;
blue = imageDataValue & 0x001F;
if (lightDiff < lightMax / 2)
{
red = red / 4 + (red - red / 4) * lightDiff / (lightMax / 2);
green = green / 4 + (green - green / 4) * lightDiff / (lightMax / 2);
blue = blue / 4 + (blue - blue / 4) * lightDiff / (lightMax / 2);
}if (lightDiff < lightMax / 2) { ... }
else
{
red = red + (31 - red) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
green = green + (63 - green) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
blue = blue + (31 - blue) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
}else { ... }
colorResult = (red << 11) | (green << 5) | blue;
newValue |= colorResult;
nx = (int8_t)((bumpMapInfo >> 8) & 0xFF);
ny = (int8_t)(bumpMapInfo & 0xFF);
lightCalculationPositionX = (relativeX + 1 - lightPositionX) + (lightSourceWidth / 2) + nx;
lightCalculationPositionY = (relativeY + 1 - lightPositionY) + (lightSourceHeight / 2) + ny;
if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight)
{
lightDiff = 0;
}if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { ... }
else
{
lightDiff = light_source[lightCalculationPositionY * lightSourceWidth + lightCalculationPositionX];
}else { ... }
imageDataValue = pixelColorValues >> 16;
red = (imageDataValue & 0xF800) >> 11;
green = (imageDataValue & 0x07E0) >> 5;
blue = imageDataValue & 0x001F;
if (lightDiff < lightMax / 2)
{
red = red / 4 + (red - red / 4) * lightDiff / (lightMax / 2);
green = green / 4 + (green - green / 4) * lightDiff / (lightMax / 2);
blue = blue / 4 + (blue - blue / 4) * lightDiff / (lightMax / 2);
}if (lightDiff < lightMax / 2) { ... }
else
{
red = red + (31 - red) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
green = green + (63 - green) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
blue = blue + (31 - blue) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
}else { ... }
colorResult = (red << 11) | (green << 5) | blue;
newValue |= (colorResult << 16);
*(fb + (absoluteX / 2) + (HAL::DISPLAY_WIDTH / 2 * absoluteY)) = newValue;
}for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX += 2, offset += 1) { ... }
}for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++) { ... }
HAL::getInstance()->unlockFrameBuffer();
...}
void BumpMapImage::drawNormal(const touchgfx::Rect& r) const
{
Rect absoluteRect = r;
translateRectToAbsolute(absoluteRect);
int8_t nx;
int8_t ny;
int16_t lightCalculationPositionX;
int16_t lightCalculationPositionY;
uint16_t* fb = HAL::getInstance()->lockFrameBuffer();
const uint16_t* imageData = (const uint16_t*) image.getData();
const uint16_t* bumpData = (const uint16_t*) bump.getData();
int offset = absoluteRect.x;
if (absoluteRect.y == 0)
{
while (offset < absoluteRect.width)
{
fb[offset++] = 0x0000;
}while (offset < absoluteRect.width) { ... }
offset += (HAL::DISPLAY_WIDTH - absoluteRect.width);
}if (absoluteRect.y == 0) { ... }
uint16_t invalidatedY = absoluteRect.y;
uint16_t invalidatedX = absoluteRect.x;
if (invalidatedX == 0)
{
invalidatedX = 1;
}if (invalidatedX == 0) { ... }
if (invalidatedY == 0)
{
invalidatedY = 1;
}if (invalidatedY == 0) { ... }
int relativeX;
int relativeY;
uint8_t lightDiff;
for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++)
{
offset = (absoluteY * image.getWidth()) + invalidatedX - getX();
if (absoluteRect.x == 0)
{
fb[offset++] = 0;
}if (absoluteRect.x == 0) { ... }
for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX++, offset++)
{
relativeX = absoluteX - getX();
relativeY = absoluteY - getY();
/* ... */
nx = height(bumpData[offset - 1]) - height(bumpData[offset]);
ny = height(bumpData[offset - image.getWidth()]) - height(bumpData[offset]);
lightCalculationPositionX = (relativeX - lightPositionX) + (lightSourceWidth / 2) + nx;
lightCalculationPositionY = (relativeY - lightPositionY) + (lightSourceHeight / 2) + ny;
if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight)
{
lightDiff = 0;
}if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { ... }
else
{
lightDiff = light_source[lightCalculationPositionY * lightSourceWidth + lightCalculationPositionX];
}else { ... }
uint8_t red = (*(imageData + offset) & 0xF800) >> 11;
uint8_t green = (*(imageData + offset) & 0x07E0) >> 5;
uint8_t blue = *(imageData + offset) & 0x001F;
if (lightDiff < lightMax / 2)
{
red = red / 4 + (red - red / 4) * lightDiff / (lightMax / 2);
green = green / 4 + (green - green / 4) * lightDiff / (lightMax / 2);
blue = blue / 4 + (blue - blue / 4) * lightDiff / (lightMax / 2);
}if (lightDiff < lightMax / 2) { ... }
else
{
red = red + (31 - red) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
green = green + (63 - green) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
blue = blue + (31 - blue) * (lightDiff - (lightMax / 2)) / (lightMax / 2);
}else { ... }
*(fb + absoluteX + (HAL::DISPLAY_WIDTH * absoluteY)) = (uint16_t)((red << 11) | (green << 5) | blue);
}for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX++, offset++) { ... }
}for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++) { ... }
HAL::getInstance()->unlockFrameBuffer();
...}
void BumpMapImage::invalidateAccordingToCurrentLightSetting()
{
uint8_t margin = 66;
int16_t x = lightPositionX - (lightSourceWidth / 2) - margin;
if (x < 0)
{
x = 0;
}if (x < 0) { ... }
if (x % 2 == 1)
{
x--;
}if (x % 2 == 1) { ... }
int16_t y = lightPositionY - (lightSourceHeight / 2) - margin;
if (y < 0)
{
y = 0;
}if (y < 0) { ... }
uint16_t width = lightSourceWidth + 2 * margin;
uint16_t height = lightSourceHeight + 2 * margin;
if (width % 2 == 1)
{
width++;
}if (width % 2 == 1) { ... }
Rect newRect = Rect(x, y, width, height);
invalidateRect(newRect);
}{ ... }
void BumpMapImage::setLightPosition(uint16_t x, uint16_t y)
{
lightPositionX = x;
lightPositionY = y;
invalidateAccordingToCurrentLightSetting();
}{ ... }
void BumpMapImage::setDragFinish(bool finish)
{
if (finish)
{
currentState = ANIMATE_CATCH;
}if (finish) { ... }
else
{
currentState = DRAGGING;
}else { ... }
}{ ... }
int BumpMapImage::height(uint16_t value) const
{
uint8_t red = (value & 0xF800) >> 11;
uint8_t green = (value & 0x07E0) >> 5;
uint8_t blue = value & 0x001F;
return red + green + blue;
...}
Rect BumpMapImage::getSolidRect() const
{
return Rect(0, 0, getWidth(), getHeight());
...}
#ifdef SIMULATOR
#include <iostream>
#include <fstream>
void BumpMapImage::printLightTable()
{
std::ofstream outputFile;
outputFile.open("lightTableTemp.txt");
uint32_t result;
outputFile << "const unsigned short _light_source[] = {\n ";
for (int y = 0; y < lightSourceHeight; y++)
{
for (int x = 0; x < lightSourceWidth; x++)
{
result = light(x, y);
outputFile << std::hex << "0x" << result;
if (!((x == lightSourceWidth - 1) && (y == lightSourceHeight - 1)))
{
outputFile << ", ";
}if (!((x == lightSourceWidth - 1) && (y == lightSourceHeight - 1))) { ... }
}for (int x = 0; x < lightSourceWidth; x++) { ... }
}for (int y = 0; y < lightSourceHeight; y++) { ... }
outputFile << "\n};";
outputFile.close();
}printLightTable () { ... }
void BumpMapImage::printBumpMap()
{
std::ofstream outputFile;
outputFile.open("bumpMapTemp.txt");
int8_t nx;
int8_t ny;
const uint16_t* bumpData = (const uint16_t*) bump.getData();
int offset = 0;
while (offset < image.getWidth() / 2)
{
outputFile << std::hex << "0x" << 0 << ", ";
offset++;
}while (offset < image.getWidth() / 2) { ... }
offset = image.getWidth();
uint32_t printValue = 0;
for (int y = 1; y < image.getHeight(); y++)
{
offset++;
for (int x = 1; x < image.getWidth(); x++, offset++)
{
nx = height(bumpData[offset - 1]) - height(bumpData[offset]);
ny = height(bumpData[offset - image.getWidth()]) - height(bumpData[offset]);
if (x % 2 == 0)
{
printValue |= (uint32_t)((((uint8_t)(nx) << 8) | (uint8_t)(ny)) << 16);
}if (x % 2 == 0) { ... }
else
{
printValue |= ((uint8_t)(nx) << 8) | (uint8_t)(ny);
outputFile << std::hex << "0x" << printValue << ", ";
printValue = 0;
}else { ... }
}for (int x = 1; x < image.getWidth(); x++, offset++) { ... }
}for (int y = 1; y < image.getHeight(); y++) { ... }
HAL::getInstance()->unlockFrameBuffer();
outputFile.close();
}printBumpMap () { ... }
uint8_t BumpMapImage::light(int16_t x, int16_t y) const
{
if (x < 0 || y < 0 || x > lightSourceWidth || y > lightSourceHeight)
{
return 0;
}if (x < 0 || y < 0 || x > lightSourceWidth || y > lightSourceHeight) { ... }
uint16_t distToCenter = (uint16_t)(sqrt((float)((x - lightSourceWidth / 2) * (x - lightSourceWidth / 2) + (y - lightSourceHeight / 2) * (y - lightSourceHeight / 2))));
int16_t result = lightMax - distToCenter;
result = (result > 0) ? result : 0;
return (uint8_t) result;
...}
/* ... */
#endif