Example: Twinkle.cpp
1// This file is generated by WOWCube SDK project wizard23#include <stdint.h>4#include <string>5#include <vector>6#include <string>7#include <memory>89#include "AppManager.h"10#include "Screen.h"11#include "NetworkMessage.h"12#include "native.h"1314#include "Twinkle.h"1516#include "Star.h"1718using namespace Cubios;19using namespace Cubios::Math;20using namespace Cubios::Gfx;2122//Applicaton initialization callback. Called once when CUB application starts23void OnInit(Cubios::AppManager& appMgr)24{25 Twinkle* app = new Twinkle();26 appMgr.SetApplication(app);2728 app->InitializeResources();29}303132Twinkle::Twinkle()33{34 this->a = 0;35 this->av=0.003;36 this->r = 150;37}3839Twinkle::~Twinkle()40{41 this->Scene.DisposeAllObjects();42}4344void Twinkle::InitializeResources()45{46 //Create background object47 this->Scene.CreateObjectWithID(Sprites::Clear,new Background(255,0,0));4849 //Create stars50 this->Scene.CreateObjectWithID(Sprites::MyStar0,new Star("star.png",Transform(120,120,0)));51 this->stars[0] = dynamic_cast<Star*>(this->Scene[Sprites::MyStar0]);5253 this->Scene.CreateObjectWithID(Sprites::MyStar1,new Star("star.png",Transform(120,120,0)));54 this->stars[1] = dynamic_cast<Star*>(this->Scene[Sprites::MyStar1]);5556 this->Scene.CreateObjectWithID(Sprites::MyStar2,new Star("star.png",Transform(120,120,0)));57 this->stars[2] = dynamic_cast<Star*>(this->Scene[Sprites::MyStar2]);5859 //Set animation types60 this->stars[1]->AnimationType = Star::Animation::UpAndDown;61 this->stars[2]->AnimationType = Star::Animation::LeftAndRight;6263 //Create UFO64 this->Scene.CreateObjectWithID(Sprites::MyUfo,new Star("ufo.png",Transform(0,0,0)));65}6667void Twinkle::on_Timer(uint8_t id)68{6970}71//This callback is called every "tick" of cubeapp application loop72void 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;7778 if(this->Module==0)79 {80 //Calculate the lemniscate81 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));8485 //Set UFO position vector on current module86 this->pos.Set(240+x,240+y*2);8788 a+= av*float(dt);89 if(a>PI_2) a-=PI_2;9091 //Send the position to other modules92 this->timerMessage.Reset(true);93 this->timerMessage.WriteFloat(this->pos.X);94 this->timerMessage.WriteFloat(this->pos.Y);9596 this->SendNetworkMessage(0, &this->timerMessage);97 }9899}100101//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];105106 //Iterate through each screen107 for(auto it = screens.begin(); it != screens.end(); ++it)108 {109 //Begin adding objects110 it->Begin(TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,true);111112 //Add background113 it->Add(bkg->SetColor(0xFF000044));114115 //If the cube is assembled116 if(TOPOLOGY_isAssembled()==1)117 {118 //Add UFO sprite.119 //Depending on screen position on a face, set sprite coordinates120121 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 }136137 }138139 dynamic_cast<Star*>(it->Add(this->stars[it->ID()]))->RenderStep();140141 //All objects are added142 it->End();143 }144}145146//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}150151//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed152void Twinkle::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)153{154}155156//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module157void Twinkle::on_Message(uint32_t type, uint8_t* pkt, u32_t size)158{159 NetworkMessage nm(pkt,size);160161 if(this->Module!=0)162 {163 //Read UFO sprite position data and set position vector164 this->pos.Set(nm.ReadFloat(),nm.ReadFloat());165 }166}167168//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source169void Twinkle::on_ExternalMessage(uint8_t* pkt, u32_t size)170{171}172173//Screen pat callback174void Twinkle::on_Pat(uint32_t count)175{176}