WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Examples
  4. Twinkle.cpp
Mission NodeSDK 6.2C++GFX SamplesProject Included

Twinkle.cpp

Example: Twinkle.cpp

Examples / SDK 6.2 / C++ / GFX Samples / Twinkle Stars / project / src
Twinkle.cpp
CPP
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 "Screen.h"
11#include "NetworkMessage.h"
12#include "native.h"
13
14#include "Twinkle.h"
15
16#include "Star.h"
17
18using namespace Cubios;
19using namespace Cubios::Math;
20using namespace Cubios::Gfx;
21
22//Applicaton initialization callback. Called once when CUB application starts
23void OnInit(Cubios::AppManager& appMgr)
24{
25 Twinkle* app = new Twinkle();
26 appMgr.SetApplication(app);
27
28 app->InitializeResources();
29}
30
31
32Twinkle::Twinkle()
33{
34 this->a = 0;
35 this->av=0.003;
36 this->r = 150;
37}
38
39Twinkle::~Twinkle()
40{
41 this->Scene.DisposeAllObjects();
42}
43
44void Twinkle::InitializeResources()
45{
46 //Create background object
47 this->Scene.CreateObjectWithID(Sprites::Clear,new Background(255,0,0));
48
49 //Create stars
50 this->Scene.CreateObjectWithID(Sprites::MyStar0,new Star("star.png",Transform(120,120,0)));
51 this->stars[0] = dynamic_cast<Star*>(this->Scene[Sprites::MyStar0]);
52
53 this->Scene.CreateObjectWithID(Sprites::MyStar1,new Star("star.png",Transform(120,120,0)));
54 this->stars[1] = dynamic_cast<Star*>(this->Scene[Sprites::MyStar1]);
55
56 this->Scene.CreateObjectWithID(Sprites::MyStar2,new Star("star.png",Transform(120,120,0)));
57 this->stars[2] = dynamic_cast<Star*>(this->Scene[Sprites::MyStar2]);
58
59 //Set animation types
60 this->stars[1]->AnimationType = Star::Animation::UpAndDown;
61 this->stars[2]->AnimationType = Star::Animation::LeftAndRight;
62
63 //Create UFO
64 this->Scene.CreateObjectWithID(Sprites::MyUfo,new Star("ufo.png",Transform(0,0,0)));
65}
66
67void Twinkle::on_Timer(uint8_t id)
68{
69
70}
71//This callback is called every "tick" of cubeapp application loop
72void Twinkle::on_Tick(uint32_t currentTime, uint32_t dt)
73{
74 this->stars[0]->Tick(dt)->Transform.Rotation+=0.04*dt;
75 this->stars[1]->Tick(dt)->Transform.Rotation-=0.2*dt;
76 this->stars[2]->Tick(dt)->Transform.Rotation+=0.2*dt;
77
78 if(this->Module==0)
79 {
80 //Calculate the lemniscate
81 float c = 120;
82 float x = (c*1.4142*cos(a))/(1.0+sin(a)*sin(a));
83 float y = (c*1.4142*sin(a)*cos(a))/(1.0+sin(a)*sin(a));
84
85 //Set UFO position vector on current module
86 this->pos.Set(240+x,240+y*2);
87
88 a+= av*float(dt);
89 if(a>PI_2) a-=PI_2;
90
91 //Send the position to other modules
92 this->timerMessage.Reset(true);
93 this->timerMessage.WriteFloat(this->pos.X);
94 this->timerMessage.WriteFloat(this->pos.Y);
95
96 this->SendNetworkMessage(0, &this->timerMessage);
97 }
98
99}
100
101//This callback is called every time device is about to render visuals. Use it for calling your rendering code.
102void Twinkle::on_Render(std::array<Cubios::Screen, 3>& screens)
103{
104 Background* bkg = (Background*)this->Scene[Sprites::Clear];
105
106 //Iterate through each screen
107 for(auto it = screens.begin(); it != screens.end(); ++it)
108 {
109 //Begin adding objects
110 it->Begin(TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,true);
111
112 //Add background
113 it->Add(bkg->SetColor(0xFF000044));
114
115 //If the cube is assembled
116 if(TOPOLOGY_isAssembled()==1)
117 {
118 //Add UFO sprite.
119 //Depending on screen position on a face, set sprite coordinates
120
121 switch(it->Position())
122 {
123 case 0:
124 it->Add(this->Scene[Sprites::MyUfo])->SetPosition(this->pos.X-280, this->pos.Y-280);
125 break;
126 case 1:
127 it->Add(this->Scene[Sprites::MyUfo])->SetPosition(this->pos.X, this->pos.Y-280);
128 break;
129 case 2:
130 it->Add(this->Scene[Sprites::MyUfo])->SetPosition(this->pos.X, this->pos.Y);
131 break;
132 case 3:
133 it->Add(this->Scene[Sprites::MyUfo])->SetPosition(this->pos.X-280, this->pos.Y);
134 break;
135 }
136
137 }
138
139 dynamic_cast<Star*>(it->Add(this->stars[it->ID()]))->RenderStep();
140
141 //All objects are added
142 it->End();
143 }
144}
145
146//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.
147void Twinkle::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)
148{
149}
150
151//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed
152void Twinkle::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)
153{
154}
155
156//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module
157void Twinkle::on_Message(uint32_t type, uint8_t* pkt, u32_t size)
158{
159 NetworkMessage nm(pkt,size);
160
161 if(this->Module!=0)
162 {
163 //Read UFO sprite position data and set position vector
164 this->pos.Set(nm.ReadFloat(),nm.ReadFloat());
165 }
166}
167
168//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source
169void Twinkle::on_ExternalMessage(uint8_t* pkt, u32_t size)
170{
171}
172
173//Screen pat callback
174void Twinkle::on_Pat(uint32_t count)
175{
176}
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Project files

Star.cpp
project/src/Star.cpp
Star.h
project/src/Star.h
Twinkle.cpp
project/src/Twinkle.cpp
Twinkle.h
project/src/Twinkle.h
wowcubeapp-build.json
project/wowcubeapp-build.json
Context Rail

Related nodes

Star.cpp
Examples / SDK 6.2 / C++ / GFX Samples / Twinkle Stars / project / src
Star.h
Examples / SDK 6.2 / C++ / GFX Samples / Twinkle Stars / project / src
Twinkle.h
Examples / SDK 6.2 / C++ / GFX Samples / Twinkle Stars / project / src
Twinkle Stars
Examples / SDK 6.2 / C++ / GFX Samples
Previous Node
Star.h
Examples / SDK 6.2 / C++ / GFX Samples / Twinkle Stars / project / src
Next Node
Twinkle.h
Examples / SDK 6.2 / C++ / GFX Samples / Twinkle Stars / project / src