Chartboost.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #if IOS
  4. #include "../Platforms/iOS/iOS.h"
  5. static void ChartboostCall(ChartboostClass::RESULT result)
  6. {
  7. if(void (*callback)(ChartboostClass::RESULT)=EE::Chartboost.callback)callback(result);
  8. }
  9. @interface EsenthelChartboostDelegate : NSObject<ChartboostDelegate>
  10. {
  11. // following methods must use 'AppVolume.muteEx' instead of 'PauseSound/ResumeSound' because it's possible that the app is still running while the video is displayed, and new sounds could be started by the app
  12. }
  13. @end
  14. @implementation EsenthelChartboostDelegate
  15. //- (BOOL)shouldRequestInterstitial:(CBLocation)location {return YES;}
  16. //- (BOOL)shouldDisplayInterstitial:(CBLocation)location {return YES;}
  17. - (void)didCacheInterstitial:(CBLocation)location {ChartboostCall(ChartboostClass::INTERSTITIAL_LOADED);}
  18. - (void)didFailToLoadInterstitial:(CBLocation)location withError:(CBLoadError)error {ChartboostCall(ChartboostClass::INTERSTITIAL_LOAD_FAIL);}
  19. - (void)didDisplayInterstitial:(CBLocation)location {ChartboostCall(ChartboostClass::INTERSTITIAL_DISPLAYED);}
  20. - (void)didDismissInterstitial:(CBLocation)location {AppVolume.muteEx(false); ChartboostCall(ChartboostClass::INTERSTITIAL_CLOSED);}
  21. //- (void)didCloseInterstitial:(CBLocation)location {AppVolume.muteEx(false); ChartboostCall(ChartboostClass::INTERSTITIAL_CLOSED);}
  22. - (void)didClickInterstitial:(CBLocation)location {ChartboostCall(ChartboostClass::INTERSTITIAL_CLICKED);}
  23. //- (BOOL)shouldDisplayRewardedVideo:(CBLocation)location {return YES;}
  24. - (void)didCacheRewardedVideo:(CBLocation)location {ChartboostCall(ChartboostClass::REWARDED_VIDEO_LOADED);}
  25. - (void)didFailToLoadRewardedVideo:(CBLocation)location withError:(CBLoadError)error {ChartboostCall(ChartboostClass::REWARDED_VIDEO_LOAD_FAIL);}
  26. - (void)didDisplayRewardedVideo:(CBLocation)location {AppVolume.muteEx(true); ChartboostCall(ChartboostClass::REWARDED_VIDEO_DISPLAYED);}
  27. - (void)willDisplayVideo:(CBLocation)location {AppVolume.muteEx(true); /*ChartboostCall(ChartboostClass::REWARDED_VIDEO_DISPLAYED);*/}
  28. - (void)didDismissRewardedVideo:(CBLocation)location {AppVolume.muteEx(false); ChartboostCall(ChartboostClass::REWARDED_VIDEO_CLOSED);}
  29. //- (void)didCloseRewardedVideo:(CBLocation)location {AppVolume.muteEx(false); ChartboostCall(ChartboostClass::REWARDED_VIDEO_CLOSED);}
  30. - (void)didCompleteRewardedVideo:(CBLocation)location withReward:(int)reward {AppVolume.muteEx(false); ChartboostCall(ChartboostClass::REWARDED_VIDEO_COMPLETED);}
  31. - (void)didClickRewardedVideo:(CBLocation)location {ChartboostCall(ChartboostClass::REWARDED_VIDEO_CLICKED);}
  32. @end
  33. #endif
  34. namespace EE{
  35. /******************************************************************************/
  36. ChartboostClass Chartboost;
  37. /******************************************************************************/
  38. #if IOS
  39. static EsenthelChartboostDelegate *EsenthelChartboostDelegatePtr;
  40. static void InitChartboost()
  41. {
  42. if(NSString *app_id =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"ChartboostAppID" ])if(app_id .length)
  43. if(NSString *app_signature=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"ChartboostAppSignature"])if(app_signature.length)
  44. {
  45. EsenthelChartboostDelegatePtr=[[EsenthelChartboostDelegate alloc] init];
  46. [::Chartboost startWithAppId:app_id
  47. appSignature:app_signature
  48. delegate:EsenthelChartboostDelegatePtr];
  49. }
  50. }
  51. #endif
  52. ChartboostClass::~ChartboostClass()
  53. {
  54. #if IOS
  55. [::Chartboost setDelegate:nil];
  56. [EsenthelChartboostDelegatePtr release]; EsenthelChartboostDelegatePtr=null; // have to release only after we will not use Chartboost anymore, and we've cleared the delegate, if we would release inside 'InitChartboost' then it would get destroyed immediately as chartboost does not increase the ref cound for it
  57. #endif
  58. }
  59. ChartboostClass::ChartboostClass()
  60. {
  61. #if IOS
  62. InitChartboostPtr=InitChartboost;
  63. #endif
  64. }
  65. /******************************************************************************/
  66. Bool ChartboostClass::visible()C // this does not work on Android, most likely it's not compatible with OpenGL and 'Chartboost.setImpressionsUseActivities'
  67. {
  68. #if ANDROID
  69. JNI jni;
  70. if(jni && ActivityClass)
  71. if(JMethodID chartboostVisible=jni->GetStaticMethodID(ActivityClass, "chartboostVisible", "()Z"))
  72. return jni->CallStaticBooleanMethod(ActivityClass, chartboostVisible);
  73. #elif IOS
  74. return [::Chartboost isAnyViewVisible];
  75. #endif
  76. return false;
  77. }
  78. /******************************************************************************/
  79. ChartboostClass& ChartboostClass::interstitialLoad()
  80. {
  81. #if ANDROID
  82. JNI jni;
  83. if(jni && ActivityClass)
  84. if(JMethodID chartboostInterstitialLoad=jni->GetStaticMethodID(ActivityClass, "chartboostInterstitialLoad", "()V"))
  85. jni->CallStaticVoidMethod(ActivityClass, chartboostInterstitialLoad);
  86. #elif IOS
  87. [::Chartboost cacheInterstitial:CBLocationDefault];
  88. #endif
  89. return T;
  90. }
  91. ChartboostClass& ChartboostClass::interstitialShow()
  92. {
  93. #if ANDROID
  94. JNI jni;
  95. if(jni && ActivityClass)
  96. if(JMethodID chartboostInterstitialShow=jni->GetStaticMethodID(ActivityClass, "chartboostInterstitialShow", "()V"))
  97. jni->CallStaticVoidMethod(ActivityClass, chartboostInterstitialShow);
  98. #elif IOS
  99. [::Chartboost showInterstitial:CBLocationDefault];
  100. #endif
  101. return T;
  102. }
  103. Bool ChartboostClass::interstitialAvailable()C
  104. {
  105. #if ANDROID
  106. JNI jni;
  107. if(jni && ActivityClass)
  108. if(JMethodID chartboostInterstitialAvailable=jni->GetStaticMethodID(ActivityClass, "chartboostInterstitialAvailable", "()Z"))
  109. return jni->CallStaticBooleanMethod(ActivityClass, chartboostInterstitialAvailable);
  110. #elif IOS
  111. return [::Chartboost hasInterstitial:CBLocationDefault];
  112. #endif
  113. return false;
  114. }
  115. /******************************************************************************/
  116. ChartboostClass& ChartboostClass::rewardedVideoLoad()
  117. {
  118. #if ANDROID
  119. JNI jni;
  120. if(jni && ActivityClass)
  121. if(JMethodID chartboostRewardedVideoLoad=jni->GetStaticMethodID(ActivityClass, "chartboostRewardedVideoLoad", "()V"))
  122. jni->CallStaticVoidMethod(ActivityClass, chartboostRewardedVideoLoad);
  123. #elif IOS
  124. [::Chartboost cacheRewardedVideo:CBLocationDefault];
  125. #endif
  126. return T;
  127. }
  128. ChartboostClass& ChartboostClass::rewardedVideoShow()
  129. {
  130. #if ANDROID
  131. JNI jni;
  132. if(jni && ActivityClass)
  133. if(JMethodID chartboostRewardedVideoShow=jni->GetStaticMethodID(ActivityClass, "chartboostRewardedVideoShow", "()V"))
  134. jni->CallStaticVoidMethod(ActivityClass, chartboostRewardedVideoShow);
  135. #elif IOS
  136. [::Chartboost showRewardedVideo:CBLocationDefault];
  137. #endif
  138. return T;
  139. }
  140. Bool ChartboostClass::rewardedVideoAvailable()C
  141. {
  142. #if ANDROID
  143. JNI jni;
  144. if(jni && ActivityClass)
  145. if(JMethodID chartboostRewardedVideoAvailable=jni->GetStaticMethodID(ActivityClass, "chartboostRewardedVideoAvailable", "()Z"))
  146. return jni->CallStaticBooleanMethod(ActivityClass, chartboostRewardedVideoAvailable);
  147. #elif IOS
  148. return [::Chartboost hasRewardedVideo:CBLocationDefault];
  149. #endif
  150. return false;
  151. }
  152. /******************************************************************************/
  153. } // namespace EE
  154. /******************************************************************************/
  155. #if ANDROID
  156. extern "C" JNIEXPORT void JNICALL Java_com_esenthel_Native_chartboost(JNIEnv *env, jclass clazz, jint result)
  157. {
  158. if(void (*callback)(ChartboostClass::RESULT)=Chartboost.callback)callback(ChartboostClass::RESULT(result));
  159. }
  160. #endif
  161. /******************************************************************************/