1// This file is generated by WOWCube SDK project wizard
2
3#include <stdint.h>
4#include <string>
5#include <vector>
6#include <string>
7#include <memory>
8
9#include "AppManager.h"
10#include "Rectangles.h"
11
12using namespace Cubios::Gfx;
13
14//Applicaton initialization callback. Called once when CUB application starts
15void OnInit(Cubios::AppManager& appMgr)
16{
17 Rectangles* theApp = new Rectangles();
18
19 appMgr.SetApplication(theApp,"AAbbCCddEE");
20
21 theApp->InitializeResources();
22
23}
24
25void Rectangles::InitializeResources()
26{
27 LOG_i("\n");
28 LOG_i("WOWCube SDK Example \"Simple Shapes - Rectangles\" ***\n");
29
30 LOG_i("Cubeapp is initializing...");
31 LOG_i("Cubeapp is started on module %d",this->Module);
32
33 //Create rects
34 for(int i=0;i<2;i++)
35 {
36 rects[i].dist = 90-40*i;
37 }
38
39 LOG_i("Done.");
40}
41
42
43Rectangles::Rectangles():fParam(0),fOffset(0)
44{
45}
46
47Rectangles::~Rectangles()
48{
49}
50
51//This callback is called every "tick" of cubeapp application loop
52void Rectangles::on_Tick(uint32_t currentTime, uint32_t deltaTime)
53{
54}
55
56//This callback is called every time device is about to render visuals. Use it for calling your rendering code.
57void Rectangles::on_Render(std::array<Cubios::Screen, 3>& screens)
58{
59 for(auto it = screens.begin(); it != screens.end(); ++it)
60 {
61 Cubios::GFX_setRenderTarget(it->ID());
62 Cubios::GFX_clear(Colors::black);
63
64 if(rects[0].dist>rects[1].dist)
65 {
66 for(int i=1;i>=0;i--)
67 {
68 this->drawRect(128, 128, rects[i].dist,rects[i].xoffset);
69
70 rects[i].dist-=5;
71 if(rects[i].dist<=0) rects[i].dist = 90;
72
73 rects[i].xoffset = (int)(fOffset);
74 }
75 }
76 else
77 {
78 for(int i=0;i<2;i++)
79 {
80 this->drawRect(128, 128, rects[i].dist,rects[i].xoffset);
81
82 rects[i].dist-=5;
83 if(rects[i].dist<=0) rects[i].dist = 90;
84
85 rects[i].xoffset = (int)(fOffset);
86 }
87 }
88
89 Cubios::GFX_render();
90 }
91}
92
93//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.
94void Rectangles::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)
95{
96 fOffset = sin(fParam)*30;
97 fParam+=0.085;
98}
99
100//This callback is called on programmable timer (if any)
101void Rectangles::on_Timer(uint8_t timerID)
102{
103}
104
105//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed
106void Rectangles::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)
107{
108}
109
110//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module
111void Rectangles::on_Message(uint32_t type, uint8_t* pkt, u32_t size)
112{
113}
114
115//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source
116void Rectangles::on_ExternalMessage(uint8_t* pkt, u32_t size)
117{
118}
119
120//Screen tap callback
121void Rectangles::on_Tap(uint32_t count)
122{
123}
124
125void Rectangles::drawRect(int cx, int cy, int dist, int xoffset)
126{
127 float d = ((float)dist/100)*128;
128
129 //cacluclate size of a rectange
130 int size = 128-(int)d;
131
132 //calculate colors
133 int color = (size+10)*2;
134 if(color>255) color = 255;
135
136 int bgcolor = color/15;
137 if(bgcolor>100) bgcolor = 100;
138
139 //calculate horizontal offset
140 int xoff = ((int)d*xoffset)/90;
141 cx+=xoff;
142
143 //calculate border width
144 int border = 7-(dist*6)/90;
145
146 //Render rectangles
147
148 int x = cx-size;
149 int y = cy-size;
150 int w = size*2;
151 int h = size*2;
152
153 Cubios::GFX_drawRectangle(x, y, w, h, color|(0<<8)|(0<<16)|(255<<24));
154
155 x = cx-size+border;
156 y = cy-size+border;
157 w = (size-border)*2;
158 h = (size-border)*2;
159
160 Cubios::GFX_drawRectangle(x, y, w, h, bgcolor|(bgcolor<<8)|(bgcolor<<16)|(255<<24));
161}