Example: GfxOrientation.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 "GfxOrientation.h"1112using namespace Cubios;13using namespace Cubios::Math;14using namespace Cubios::Gfx;1516//Applicaton initialization callback. Called once when CUB application starts17void OnInit(Cubios::AppManager& appMgr)18{19 GfxOrientation* theApp = new GfxOrientation();2021 appMgr.SetApplication(theApp,"AAbbCCddEE");2223 theApp->InitializeResources();2425}262728GfxOrientation::GfxOrientation():currentMode(0)29{30}3132GfxOrientation::~GfxOrientation()33{34 this->Scene.DisposeAllObjects();35}3637void GfxOrientation::InitializeResources()38{39 this->modes.push_back({TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_MENU,false,"MENU"});40 this->modes.push_back({TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_MENU,true,"MENU"});41 this->modes.push_back({TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,false,"GRAVITY"});42 this->modes.push_back({TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,true,"GRAVITY"});43 this->modes.push_back({TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_SPLASH,false,"SPLASH"});44 this->modes.push_back({TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_SPLASH,true,"SPLASH"});4546 //Create a Cubios::Gfx::Background object47 this->Scene.CreateObjectWithID(GfxObjects::myBackground,new Background(0,0,0));4849 //Create animated sprite50 this->Scene.CreateObjectWithID(GfxObjects::mySprite,new AnimatedSprite("torch","png",1,8,Transform(120,120,0)));51 this->torch = dynamic_cast<AnimatedSprite*>(this->Scene[GfxObjects::mySprite]);52 this->torch->SetPlaybackSpeed(100);53 this->torch->SetPlaybackMode(AnimatedSprite::playbackMode_t::Loop);5455 //Create a Cubios::Gfx::Text objects56 this->Scene.CreateObjectWithID(GfxObjects::myText1,new Text("PAT",120,20,9,Colors::gold));57 this->Scene.CreateObjectWithID(GfxObjects::myText2,new Text("TO CHANGE",120,50,9,Colors::gold));58 this->Scene.CreateObjectWithID(GfxObjects::myText3,new Text("ORIENTATION",120,80,9,Colors::gold));59 this->Scene.CreateObjectWithID(GfxObjects::myText4,new Text("MODE",120,110,9,Colors::gold));6061 this->Scene.CreateObjectWithID(GfxObjects::myText5,new Text("-MODE-",120,160,6,Colors::tomato));62 this->Scene.CreateObjectWithID(GfxObjects::myText6,new Text("GRAVITY",120,200,6,Colors::silver));6364 this->SetTimer(1,250);6566}6768//This callback is called every "tick" of cubeapp application loop69void GfxOrientation::on_Tick(uint32_t currentTime, uint32_t deltaTime)70{71 this->torch->Tick(deltaTime);72}7374//This callback is called every time device is about to render visuals. Use it for calling your rendering code.75void GfxOrientation::on_Render(std::array<Cubios::Screen, 3>& screens)76{77 for(auto it = screens.begin(); it != screens.end(); ++it)78 {79 //typically, you don't set orientation directly. But for this sample we have to do it.80 it->SetOrientation(this->modes.at(this->currentMode).mode);8182 //Start adding objects to a screen83 it->Begin(this->modes.at(this->currentMode).mode,this->modes.at(this->currentMode).autorotation);8485 //Add animated sprite on the sides of a cube86 if(it->Direction()!=TOPOLOGY_orientation_t::ORIENTATION_UP && it->Direction()!=TOPOLOGY_orientation_t::ORIENTATION_DOWN)87 {88 if(this->Module==0)89 {90 it->Add(this->Scene[GfxObjects::myBackground]);9192 it->Add(this->Scene[GfxObjects::myText1]);93 it->Add(this->Scene[GfxObjects::myText2]);94 it->Add(this->Scene[GfxObjects::myText3]);95 it->Add(this->Scene[GfxObjects::myText4]);9697 if(this->modes.at(this->currentMode).autorotation)98 dynamic_cast<Text*>(it->Add(this->Scene[GfxObjects::myText5]))->SetContent("AUTOROTATION:ON")99 ->SetColor(Colors::lime);100 else101 dynamic_cast<Text*>(it->Add(this->Scene[GfxObjects::myText5]))->SetContent("AUTOROTATION:OFF")102 ->SetColor(Colors::tomato);103104 dynamic_cast<Text*>(it->Add(this->Scene[GfxObjects::myText6]))->FormatContent("MODE:%s", this->modes.at(this->currentMode).name.c_str());105 }106 else107 {108 it->Add(this->torch);109 }110 }111 else112 {113 //Add background114 it->Add(this->Scene[GfxObjects::myBackground]);115 }116117 //Finish adding objects118 it->End();119 }120}121122//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.123void GfxOrientation::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)124{125}126127//This callback is called on programmable timer (if any)128void GfxOrientation::on_Timer(uint8_t timerID)129{130}131132//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed133void GfxOrientation::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)134{135}136137//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module138void GfxOrientation::on_Message(uint32_t type, uint8_t* pkt, u32_t size)139{140 NetworkMessage nm(pkt,size);141142 if(this->Module!=0)143 {144 //Read incoming mode145 this->currentMode = nm.ReadInt(4);146 }147148}149150//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source151void GfxOrientation::on_ExternalMessage(uint8_t* pkt, u32_t size)152{153}154155//Screen pat callback156void GfxOrientation::on_Pat(uint32_t count)157{158 if(this->Module==0)159 {160 if(this->currentMode<5)161 {162 this->currentMode++;163 }164 else165 {166 this->currentMode = 0;167 }168169 NetworkMessage msg;170 msg.WriteInt(this->currentMode,4); //4 bits is enough171172 //send new mode to all modules173 this->SendNetworkMessage(1,&msg);174 }175}