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 "Lines.h"
11
12using namespace Cubios::Gfx;
13
14//Applicaton initialization callback. Called once when CUB application starts
15void OnInit(Cubios::AppManager& appMgr)
16{
17 Lines* theApp = new Lines();
18
19 appMgr.SetApplication(theApp,"AAbbCCddEE");
20
21 theApp->InitializeResources();
22
23}
24
25
26Lines::Lines()
27{
28}
29
30Lines::~Lines()
31{
32}
33
34void Lines::InitializeResources()
35{
36 LOG_i("\n");
37 LOG_i("WOWCube SDK Example \"Simple Shapes - Lines\" ***\n");
38
39 LOG_i("Cubeapp is initializing...");
40 LOG_i("Cubeapp is started on module %d",this->Module);
41
42 //Create starts
43 for(int n=0;n<50;n++)
44 {
45 this->addStar();
46 }
47
48 LOG_i("Done.");
49}
50
51//This callback is called every "tick" of cubeapp application loop
52void Lines::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 Lines::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 this->renderStarfield();
65
66 Cubios::GFX_render();
67
68 }
69}
70
71//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.
72void Lines::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)
73{
74}
75
76//This callback is called on programmable timer (if any)
77void Lines::on_Timer(uint8_t timerID)
78{
79}
80
81//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed
82void Lines::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)
83{
84}
85
86//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module
87void Lines::on_Message(uint32_t type, uint8_t* pkt, u32_t size)
88{
89}
90
91//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source
92void Lines::on_ExternalMessage(uint8_t* pkt, u32_t size)
93{
94}
95
96//Screen pat callback
97void Lines::on_Pat(uint32_t count)
98{
99}
100
101//Adds a star object
102void Lines::addStar()
103{
104 //Random probability from 0% to 100%
105 int prob = Cubios::random(0,100);
106
107 //Velocity vector components filled with random values
108 float vx = (float)(400-Cubios::random(0,800))/(float)(Cubios::random(10,100));
109 float vy = (float)(400-Cubios::random(0,800))/(float)(Cubios::random(10,100));
110
111 //Create 50 unique stars that have non-zero velocity
112 if(numStars<50 && prob<50 && vx!=0 && vy!=0)
113 {
114 stars[numStars].x = 0;
115 stars[numStars].y = 0;
116 stars[numStars].prevx = 0;
117 stars[numStars].prevy = 0;
118
119 stars[numStars].vx = vx;
120 stars[numStars].vy = vy;
121
122 numStars++;
123 }
124
125}
126
127//Renders the starfield
128void Lines::renderStarfield()
129{
130 int x_from,x_to,y_from,y_to;
131
132 for(int n=0;n<numStars;n++)
133 {
134 //Move the star
135 stars[n].prevx = stars[n].x;
136 stars[n].prevy = stars[n].y;
137
138 stars[n].x = stars[n].x+(int)stars[n].vx;
139 stars[n].y = stars[n].y+(int)stars[n].vy;
140
141 //If star is out of screen, reset its position
142 if(stars[n].x>120 || stars[n].x<-120 || stars[n].y>120 || stars[n].y<-120)
143 {
144 stars[n].x = 0;
145 stars[n].y = 0;
146 }
147
148 //Calculate star color
149 uint32_t color = (abs(stars[n].x)+abs(stars[n].y))*2;
150 if(color>255) color = 255;
151
152 x_from = DISPLAY_CENTER_X+stars[n].prevx;
153 y_from = DISPLAY_CENTER_Y+stars[n].prevy;
154
155 x_to = DISPLAY_CENTER_X+stars[n].x;
156 y_to = DISPLAY_CENTER_Y+stars[n].y;
157
158 //Draw a line
159 Cubios::GFX_drawLine(x_from,y_from, x_to,y_to, 1, color|(color<<8)|(color<<16)|(255<<24));
160 }
161}
162