Example: GfxSpriteAtlas.cpp
1// This file is generated by WOWCube SDK project wizard2//3// Application UUID: 6EqWQJuAqt4//56#include <stdint.h>7#include <string>8#include <vector>9#include <string>10#include <memory>1112#include "AppManager.h"13#include "GfxSpriteAtlas.h"1415using namespace Cubios;16using namespace Cubios::Math;17using namespace Cubios::Gfx;1819//Applicaton initialization callback. Called once when CUB application starts20void OnInit(Cubios::AppManager& appMgr)21{22 GfxSpriteAtlas* theApp = new GfxSpriteAtlas();2324 appMgr.SetApplication(theApp,"6EqWQJuAqt");2526 theApp->InitializeResources();27}282930GfxSpriteAtlas::GfxSpriteAtlas()31{32}3334GfxSpriteAtlas::~GfxSpriteAtlas()35{36 this->Scene.DisposeAllObjects();37}3839void GfxSpriteAtlas::InitializeResources()40{41 //Create sprite atlas with terrain sprites42 this->terrainAtlas = new Cubios::Gfx::SpriteAtlas<Cubios::Gfx::SpriteAtlasElement>("terrain_atlas.png",&this->Scene);4344 //Define atlas sprites45 this->terrainAtlas->AddSprite(GfxObjects::terrain, Cubios::Math::Rect2(0,0,163,100));46 this->terrainAtlas->AddSprite(GfxObjects::pit, Cubios::Math::Rect2(163,0,163,100));47 this->terrainAtlas->AddSprite(GfxObjects::rock3, Cubios::Math::Rect2(163*2,0,163,100));4849 this->terrainAtlas->AddSprite(GfxObjects::rock2, Cubios::Math::Rect2(0,100,163,100));50 this->terrainAtlas->AddSprite(GfxObjects::rock, Cubios::Math::Rect2(163,100,163,100));51 this->terrainAtlas->AddSprite(GfxObjects::plant4, Cubios::Math::Rect2(163*2,100,163,100));5253 this->terrainAtlas->AddSprite(GfxObjects::plant3, Cubios::Math::Rect2(0,200,163,100));54 this->terrainAtlas->AddSprite(GfxObjects::plant2, Cubios::Math::Rect2(163,200,163,100));55 this->terrainAtlas->AddSprite(GfxObjects::plant, Cubios::Math::Rect2(163*2,200,163,100));5657 //Create background58 this->Scene.CreateObjectWithID(GfxObjects::myBackground,new Background(Cubios::Gfx::Colors::black));5960 //Create offscreen render target61 this->prepareOffscreenResource();6263 //Create an atlas for animated object64 this->fireballAtlas = new Cubios::Gfx::SpriteAtlas<FireballSpriteAtlasElement>("fireball_atlas.png",&this->Scene);6566 //Add dynamic sprite objects67 this->fireballAtlas->AddSprite(GfxObjects::fireball,Cubios::Math::Rect2(0,0,51,38));68 this->fireballAtlas->AddSprite(GfxObjects::fireball+1,Cubios::Math::Rect2(0,0,51,38));69 this->fireballAtlas->AddSprite(GfxObjects::fireball+2,Cubios::Math::Rect2(0,0,51,38));7071 this->fireballAtlas->AddSprite(GfxObjects::fireball+3,Cubios::Math::Rect2(0,38,51,20));72 this->fireballAtlas->AddSprite(GfxObjects::fireball+4,Cubios::Math::Rect2(0,38,51,20));73 this->fireballAtlas->AddSprite(GfxObjects::fireball+5,Cubios::Math::Rect2(0,38,51,20));7475 //Set initial positions76 this->fireballAtlas->Get(GfxObjects::fireball)->SetPosition(-20,100);77 this->fireballAtlas->Get(GfxObjects::fireball+1)->SetPosition(-20,80);78 this->fireballAtlas->Get(GfxObjects::fireball+2)->SetPosition(-20,170);7980 this->fireballAtlas->Get(GfxObjects::fireball+3)->SetPosition(-20,70);81 this->fireballAtlas->Get(GfxObjects::fireball+4)->SetPosition(-20,50);82 this->fireballAtlas->Get(GfxObjects::fireball+5)->SetPosition(-20,190);8384 //Uncomment this line in order to see actuall FPSes85 //this->ShowFPSCounter(true);86}8788void GfxSpriteAtlas::prepareOffscreenResource()89{90 OffscreenRenderTarget* rt = new OffscreenRenderTarget(240,240,Cubios::GFX_PixelFormat_t::FORMAT_RGB565);91 rt->SetPosition(120,120);9293 rt->Begin(true);9495 rt->Add(this->Scene[GfxObjects::myBackground]);9697 rt->Add(terrainAtlas->Get(GfxObjects::terrain))->SetPosition(120,120);98 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120-79,120-47);99 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120+79,120-47);100 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120+79,120+47);101 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120-79,120+47);102103 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120,120-47*2);104 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120,120+47*2);105106 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120-79*2,120-47*2);107 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120-79*2,120+47*2);108109 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120+79*2,120-47*2);110 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120+79*2,120+47*2);111112 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120+79*2,120);113 rt->AddCopy(terrainAtlas->Get(GfxObjects::terrain)->Copy())->SetPosition(120-79*2,120);114115116 rt->Add(terrainAtlas->Get(GfxObjects::pit))->SetPosition(120,120);117118 rt->Add(terrainAtlas->Get(GfxObjects::rock3))->SetPosition(120-79,120-47);119 rt->Add(terrainAtlas->Get(GfxObjects::rock))->SetPosition(120-79,120-47);120 rt->Add(terrainAtlas->Get(GfxObjects::rock2))->SetPosition(120,120-47*2);121122 rt->AddCopy(terrainAtlas->Get(GfxObjects::rock3)->Copy())->SetPosition(120+79,120-47);123 rt->AddCopy(terrainAtlas->Get(GfxObjects::plant3)->Copy())->SetPosition(120+79,120-47);124125 rt->AddCopy(terrainAtlas->Get(GfxObjects::rock3)->Copy())->SetPosition(120-79,120+47);126 rt->AddCopy(terrainAtlas->Get(GfxObjects::rock3)->Copy())->SetPosition(120,120+47*2);127 rt->AddCopy(terrainAtlas->Get(GfxObjects::plant3)->Copy())->SetPosition(120,120+47*2);128129 rt->Add(terrainAtlas->Get(GfxObjects::plant))->SetPosition(120+79,120+47);130 rt->Add(terrainAtlas->Get(GfxObjects::plant4))->SetPosition(120-79,120+77);131132 rt->Add(terrainAtlas->Get(GfxObjects::plant2))->SetPosition(120-39,120+7);133134 rt->End();135136 this->Scene.CreateObjectWithID(GfxObjects::myRT,rt);137}138139//This callback is called every "tick" of cubeapp application loop140void GfxSpriteAtlas::on_Tick(uint32_t currentTime, uint32_t deltaTime)141{142143 dynamic_cast<FireballSpriteAtlasElement*>(this->fireballAtlas->Get(GfxObjects::fireball))->Tick(15);144 dynamic_cast<FireballSpriteAtlasElement*>(this->fireballAtlas->Get(GfxObjects::fireball+1))->Tick(10);145 dynamic_cast<FireballSpriteAtlasElement*>(this->fireballAtlas->Get(GfxObjects::fireball+2))->Tick(12);146147 dynamic_cast<FireballSpriteAtlasElement*>(this->fireballAtlas->Get(GfxObjects::fireball+3))->Tick(35);148 dynamic_cast<FireballSpriteAtlasElement*>(this->fireballAtlas->Get(GfxObjects::fireball+4))->Tick(25);149 dynamic_cast<FireballSpriteAtlasElement*>(this->fireballAtlas->Get(GfxObjects::fireball+5))->Tick(30);150151}152153//This callback is called every time device is about to render visuals. Use it for calling your rendering code.154void GfxSpriteAtlas::on_Render(std::array<Cubios::Screen, 3>& screens)155{156 for(auto it = screens.begin(); it != screens.end(); ++it)157 {158 //Start adding objects to a screen159 it->Begin(TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,true);160161 it->Add(this->Scene[GfxObjects::myRT]);162163 it->Add(this->fireballAtlas->Get(GfxObjects::fireball));164 it->Add(this->fireballAtlas->Get(GfxObjects::fireball+1));165 it->Add(this->fireballAtlas->Get(GfxObjects::fireball+2));166167 it->Add(this->fireballAtlas->Get(GfxObjects::fireball+3));168 it->Add(this->fireballAtlas->Get(GfxObjects::fireball+4));169 it->Add(this->fireballAtlas->Get(GfxObjects::fireball+5));170171 //Finish adding objects172 it->End();173 }174}175176//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.177void GfxSpriteAtlas::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)178{179}180181//This callback is called on programmable timer (if any)182void GfxSpriteAtlas::on_Timer(uint8_t timerID)183{184}185186//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed187void GfxSpriteAtlas::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)188{189}190191//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module192void GfxSpriteAtlas::on_Message(uint32_t type, uint8_t* pkt, u32_t size)193{194}195196//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source197void GfxSpriteAtlas::on_ExternalMessage(uint8_t* pkt, u32_t size)198{199}200201//Screen pat callback202void GfxSpriteAtlas::on_Pat(uint32_t count)203{204}205206//This callback is called before the application quits207void GfxSpriteAtlas::on_Close()208{209}