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