|
| 1 | +#include "AppDelegate.h" |
| 2 | +#include "HelloWorldScene.h" |
| 3 | + |
| 4 | +// #define USE_AUDIO_ENGINE 1 |
| 5 | +// #define USE_SIMPLE_AUDIO_ENGINE 1 |
| 6 | + |
| 7 | +#if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE |
| 8 | +#error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!" |
| 9 | +#endif |
| 10 | + |
| 11 | +#if USE_AUDIO_ENGINE |
| 12 | +#include "audio/include/AudioEngine.h" |
| 13 | +using namespace cocos2d::experimental; |
| 14 | +#elif USE_SIMPLE_AUDIO_ENGINE |
| 15 | +#include "audio/include/SimpleAudioEngine.h" |
| 16 | +using namespace CocosDenshion; |
| 17 | +#endif |
| 18 | + |
| 19 | +#include "AppsFlyer/AppsFlyerX.h" |
| 20 | +#include "AppsFlyer/AppsFlyerXDeepLinkResult.h" |
| 21 | +#include "../cocos2d/cocos/platform/CCPlatformMacros.h" |
| 22 | + |
| 23 | +USING_NS_CC; |
| 24 | + |
| 25 | +static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); |
| 26 | +static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); |
| 27 | +static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); |
| 28 | +static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); |
| 29 | + |
| 30 | +AppDelegate::AppDelegate() |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +AppDelegate::~AppDelegate() |
| 35 | +{ |
| 36 | +#if USE_AUDIO_ENGINE |
| 37 | + AudioEngine::end(); |
| 38 | +#elif USE_SIMPLE_AUDIO_ENGINE |
| 39 | + SimpleAudioEngine::end(); |
| 40 | +#endif |
| 41 | +} |
| 42 | + |
| 43 | +// if you want a different context, modify the value of glContextAttrs |
| 44 | +// it will affect all platforms |
| 45 | +void AppDelegate::initGLContextAttrs() |
| 46 | +{ |
| 47 | + // set OpenGL context attributes: red,green,blue,alpha,depth,stencil |
| 48 | + GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; |
| 49 | + |
| 50 | + GLView::setGLContextAttrs(glContextAttrs); |
| 51 | + |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +// if you want to use the package manager to install more packages, |
| 56 | +// don't modify or remove this function |
| 57 | +static int register_all_packages() |
| 58 | +{ |
| 59 | + return 0; //flag for packages manager |
| 60 | +} |
| 61 | + |
| 62 | +static void onConversionDataSuccess(cocos2d::ValueMap installData) { |
| 63 | + CCLOG("%s", "AppDelegate.cpp got conversion data!"); |
| 64 | + std::string gcd = "Conversion Data : "; |
| 65 | + |
| 66 | + for (auto &t : installData){ |
| 67 | + |
| 68 | + CCLOG("%s - %s", t.first.c_str(), t.second.asString().c_str()); |
| 69 | + gcd.append(t.first.c_str()); |
| 70 | + gcd.append(" : "); |
| 71 | + gcd.append(t.second.asString().c_str()); |
| 72 | + gcd.append("\n"); |
| 73 | + } |
| 74 | +// auto gcdStr = gcd; |
| 75 | +// auto currentScene = Director::getInstance()->getRunningScene(); |
| 76 | +// auto size = Director::getInstance()->getVisibleSize(); |
| 77 | +// Vec2 origin = Director::getInstance()->getVisibleOrigin(); |
| 78 | +// auto myLabel = Label::createWithSystemFont(gcd.c_str(), "Arial", 6); |
| 79 | +// myLabel->setPosition(Vec2(origin.x + 80, origin.y + size.height - 180)); |
| 80 | +// //myLabel->setHeight( origin.y + size.height - 180); |
| 81 | +// //myLabel->setHorizontalAlignment(cocos2d::TextHAlignment::CENTER); |
| 82 | +// currentScene->addChild(myLabel,1); |
| 83 | + |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +static void onConversionDataFail(cocos2d::ValueMap map) { |
| 88 | + for (auto &t : map){ |
| 89 | + CCLOG("%s - %s", t.first.c_str(), t.second.asString().c_str()); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +static void onAppOpenAttribution(cocos2d::ValueMap map) { |
| 94 | + for (auto &t : map){ |
| 95 | + CCLOG("%s - %s", t.first.c_str(), t.second.asString().c_str()); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +static void onAppOpenAttributionFailure(cocos2d::ValueMap map) { |
| 100 | + for (auto &t : map){ |
| 101 | + CCLOG("%s - %s", t.first.c_str(), t.second.asString().c_str()); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +static void didResolveDeepLink(AppsFlyerXDeepLinkResult result){ |
| 106 | + CCLOG("%s", "AppDelegate.cpp got ddl!"); |
| 107 | + std::string ddl = "Deep link data is \n"; |
| 108 | + switch (result.status) { |
| 109 | + case NOTFOUND: |
| 110 | + CCLOG("deep link not found"); |
| 111 | + break; |
| 112 | + case FOUND: |
| 113 | + if (!result.deepLink.empty()){ |
| 114 | + if (!result.getMediaSource().empty()) { |
| 115 | + CCLOG("Media source is %s", result.getMediaSource().c_str()); |
| 116 | + } |
| 117 | + for (auto &t : result.deepLink){ |
| 118 | + CCLOG("%s - %s", t.first.c_str(), t.second.asString().c_str()); |
| 119 | + ddl.append(t.first.c_str()); |
| 120 | + ddl.append(" : "); |
| 121 | + ddl.append(t.second.asString().c_str()); |
| 122 | + ddl.append("\n"); |
| 123 | + } |
| 124 | + auto currentScene = Director::getInstance()->getRunningScene(); |
| 125 | + auto child = currentScene->getChildByTag(1); |
| 126 | + if (child != NULL) |
| 127 | + currentScene->removeChildByTag(1); |
| 128 | + auto size = Director::getInstance()->getVisibleSize(); |
| 129 | + Vec2 origin = Director::getInstance()->getVisibleOrigin(); |
| 130 | + auto myLabel = Label::createWithSystemFont(ddl.c_str(), "Arial", 6); |
| 131 | + myLabel->setPosition(Vec2(origin.x + 80, origin.y + size.height - 180)); |
| 132 | + //myLabel->setHeight( origin.y + size.height - 180); |
| 133 | + //myLabel->setHorizontalAlignment(cocos2d::TextHAlignment::CENTER); |
| 134 | + currentScene->addChild(myLabel,1, 1); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +bool AppDelegate::applicationDidFinishLaunching() { |
| 141 | + |
| 142 | + AppsFlyerX::stop(false); |
| 143 | +// AppsFlyerX::enableTCFDataCollection(true); |
| 144 | + AppsFlyerX::setIsDebug(true); |
| 145 | +// AppsFlyerX::setConsentData(AppsFlyerXConsent::initNonGDPRUser()); |
| 146 | + // AppsFlyerX::setConsentData(AppsFlyerXConsent::initForGDPRUser(true,true)); |
| 147 | + //AppsFlyerX::setMinTimeBetweenSessions(9); |
| 148 | + AppsFlyerX::setAppsFlyerDevKey("devkey"); |
| 149 | + |
| 150 | +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) |
| 151 | + // // In case you want to use manual mode. |
| 152 | + // AppsFlyerX::setManualStart(true); |
| 153 | + // // |
| 154 | + AppsFlyerX::setAppleAppID("appleAppId"); |
| 155 | + // AppsFlyerX::waitForATTUserAuthorizationWithTimeoutInterval(60); |
| 156 | + |
| 157 | +#endif |
| 158 | +// std::vector<std::string> partners; |
| 159 | +// partners.push_back("facebook_int"); |
| 160 | +// partners.push_back("googleadwords_int"); |
| 161 | +// AppsFlyerX::sharingFilter(partners); |
| 162 | +// AppsFlyerX::sharingFilterForAllPartners(); |
| 163 | + AppsFlyerX::setOnConversionDataSuccess(onConversionDataSuccess); |
| 164 | + AppsFlyerX::setOnConversionDataFail(onConversionDataFail); |
| 165 | + AppsFlyerX::setOnAppOpenAttribution(onAppOpenAttribution); |
| 166 | + AppsFlyerX::setOnAppOpenAttributionFailure(onAppOpenAttributionFailure); |
| 167 | + AppsFlyerX::setDidResolveDeepLink(didResolveDeepLink); |
| 168 | + ValueMap data; |
| 169 | + data["thePartnerId"] = "abcd"; |
| 170 | + data["Item_id"] = 1; |
| 171 | + data["isLegacy"] = false; |
| 172 | + AppsFlyerX::setPartnerData("partnerID", data); |
| 173 | + |
| 174 | +// AppsFlyerX::logEvent(AFEventPurchase, {{ "key1", cocos2d::Value("value1")}, |
| 175 | +// { "key2", cocos2d::Value("value2")}}); |
| 176 | +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) |
| 177 | +// remove this to use manual mode. |
| 178 | + AppsFlyerX::start(); |
| 179 | +// |
| 180 | +#endif |
| 181 | + |
| 182 | + // initialize director |
| 183 | + auto director = Director::getInstance(); |
| 184 | + auto glview = director->getOpenGLView(); |
| 185 | + if(!glview) { |
| 186 | +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) |
| 187 | + glview = GLViewImpl::createWithRect("AppsFlyerCocos2dX", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); |
| 188 | +#else |
| 189 | + glview = GLViewImpl::create("AppsFlyerCocos2dX"); |
| 190 | +#endif |
| 191 | + director->setOpenGLView(glview); |
| 192 | + } |
| 193 | + |
| 194 | + // turn on display FPS |
| 195 | + director->setDisplayStats(true); |
| 196 | + |
| 197 | + // set FPS. the default value is 1.0/60 if you don't call this |
| 198 | + director->setAnimationInterval(1.0f / 60); |
| 199 | + |
| 200 | + // Set the design resolution |
| 201 | + glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); |
| 202 | + auto frameSize = glview->getFrameSize(); |
| 203 | + // if the frame's height is larger than the height of medium size. |
| 204 | + if (frameSize.height > mediumResolutionSize.height) |
| 205 | + { |
| 206 | + director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); |
| 207 | + } |
| 208 | + // if the frame's height is larger than the height of small size. |
| 209 | + else if (frameSize.height > smallResolutionSize.height) |
| 210 | + { |
| 211 | + director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); |
| 212 | + } |
| 213 | + // if the frame's height is smaller than the height of medium size. |
| 214 | + else |
| 215 | + { |
| 216 | + director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); |
| 217 | + } |
| 218 | + |
| 219 | + register_all_packages(); |
| 220 | + |
| 221 | + // create a scene. it's an autorelease object |
| 222 | + auto scene = HelloWorld::createScene(); |
| 223 | + |
| 224 | + // run |
| 225 | + director->runWithScene(scene); |
| 226 | + |
| 227 | + return true; |
| 228 | +} |
| 229 | + |
| 230 | +// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked. |
| 231 | +void AppDelegate::applicationDidEnterBackground() { |
| 232 | + Director::getInstance()->stopAnimation(); |
| 233 | + |
| 234 | + AppsFlyerX::didEnterBackground(); |
| 235 | + |
| 236 | +#if USE_AUDIO_ENGINE |
| 237 | + AudioEngine::pauseAll(); |
| 238 | +#elif USE_SIMPLE_AUDIO_ENGINE |
| 239 | + SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); |
| 240 | + SimpleAudioEngine::getInstance()->pauseAllEffects(); |
| 241 | +#endif |
| 242 | +} |
| 243 | + |
| 244 | +// this function will be called when the app is active again |
| 245 | +void AppDelegate::applicationWillEnterForeground() { |
| 246 | + Director::getInstance()->startAnimation(); |
| 247 | + |
| 248 | + //CCLOG("%s", "~+~+~+~+~ applicationWillEnterForeground ~+~+~+~+~"); |
| 249 | + |
| 250 | +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) |
| 251 | +// remove this to use manual mode. |
| 252 | + AppsFlyerX::start(); |
| 253 | +// |
| 254 | +#endif |
| 255 | + |
| 256 | +#if USE_AUDIO_ENGINE |
| 257 | + AudioEngine::resumeAll(); |
| 258 | +#elif USE_SIMPLE_AUDIO_ENGINE |
| 259 | + SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); |
| 260 | + SimpleAudioEngine::getInstance()->resumeAllEffects(); |
| 261 | +#endif |
| 262 | +} |
0 commit comments