Skip to content

Commit

Permalink
only build with google ads if framework present
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed Dec 30, 2016
1 parent 686570b commit 408c22f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ third_party/freetype-*/*.pdb
third_party/tinymt-*/*.lib
third_party/tinymt-*/*.a

third_party/GoogleMobileAds.framework

tests/*.out
tests/*.err
tests/*.res
Expand Down
13 changes: 10 additions & 3 deletions Common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ ifndef LUAVM
LUAVM = lua51
endif

ifeq (,$(wildcard $(THIRD_PARTY_DIR)/GoogleMobileAds.framework))
GOOGLE_ADS_FRAMEWORK_OPT=
else
GOOGLE_ADS=1
GOOGLE_ADS_FRAMEWORK_OPT=-Wl,-framework,GoogleMobileAds
endif

SDL_ALIB = $(BUILD_LIB_DIR)/libsdl$(ALIB_EXT)
ANGLE_ALIB = $(BUILD_LIB_DIR)/libangle$(ALIB_EXT)
ANGLE_WIN_PREBUILT = $(BUILD_LIB_DIR)/angle-win-prebuilt.date
Expand Down Expand Up @@ -151,7 +158,7 @@ else ifeq ($(TARGET_PLATFORM),ios32)
-Wl,-framework,UIKit -Wl,-framework,QuartzCore -Wl,-framework,SpriteKit -Wl,-framework,StoreKit -Wl,-framework,CoreMedia \
-Wl,-framework,CoreMotion -Wl,-framework,Foundation -Wl,-framework,CoreTelephony \
-Wl,-framework,AVFoundation -Wl,-framework,CoreVideo -Wl,-framework,MessageUI -Wl,-framework,AdSupport \
-Wl,-framework,GLKit -Wl,-framework,GameKit -Wl,-framework,GoogleMobileAds
-Wl,-framework,GLKit -Wl,-framework,GameKit $(GOOGLE_ADS_FRAMEWORK_OPT)
LUA_CFLAGS += -DLUA_USE_POSIX
IOS = 1
else ifeq ($(TARGET_PLATFORM),ios64)
Expand All @@ -169,7 +176,7 @@ else ifeq ($(TARGET_PLATFORM),ios64)
-Wl,-framework,UIKit -Wl,-framework,QuartzCore -Wl,-framework,SpriteKit -Wl,-framework,StoreKit -Wl,-framework,CoreMedia \
-Wl,-framework,CoreMotion -Wl,-framework,Foundation -Wl,-framework,CoreTelephony -Wl,-framework,MessageUI -Wl,-framework,AdSupport \
-Wl,-framework,AVFoundation -Wl,-framework,CoreVideo \
-Wl,-framework,GLKit -Wl,-framework,GameKit -Wl,-framework,GoogleMobileAds
-Wl,-framework,GLKit -Wl,-framework,GameKit $(GOOGLE_ADS_FRAMEWORK_OPT)
LUA_CFLAGS += -DLUA_USE_POSIX
IOS = 1
else ifeq ($(TARGET_PLATFORM),iossim)
Expand All @@ -186,7 +193,7 @@ else ifeq ($(TARGET_PLATFORM),iossim)
-Wl,-framework,CFNetwork -Wl,-framework,CoreGraphics -Wl,-framework,SpriteKit -Wl,-framework,AdSupport \
-Wl,-framework,UIKit -Wl,-framework,QuartzCore -Wl,-framework,SystemConfiguration -Wl,-framework,StoreKit -Wl,-framework,CoreMedia \
-Wl,-framework,CoreMotion -Wl,-framework,Foundation -Wl,-framework,CoreVideo -Wl,-framework,CoreTelephony -Wl,-framework,MessageUI \
-Wl,-framework,GLKit -Wl,-framework,GoogleMobileAds
-Wl,-framework,GLKit $(GOOGLE_ADS_FRAMEWORK_OPT)
LUA_CFLAGS += -DLUA_USE_POSIX
IOS = 1
else ifeq ($(TARGET_PLATFORM),html)
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ else ifeq ($(LUAVM),lua53)
else
$(error invalid LUAVM: $(LUAVM))
endif
ifdef GOOGLE_ADS
AM_DEFS += AM_GOOGLE_ADS
endif

AMULET = $(BUILD_BIN_DIR)/amulet$(EXE_EXT)

Expand Down
8 changes: 8 additions & 0 deletions scripts/install_google_ads.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
mkdir -p google_ads_tmp
curl -o google_ads_tmp/sdk.zip http://dl.google.com/googleadmobadssdk/googlemobileadssdkios.zip
cd google_ads_tmp
unzip sdk.zip
cp -r `find . -type d -name GoogleMobileAds.framework` ../third_party/
cd ..
rm -rf google_ads_tmp
29 changes: 25 additions & 4 deletions src/am_backend_ios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#import <GLKit/GLKit.h>
#import <GameKit/GameKit.h>
#import <StoreKit/StoreKit.h>
#import <GoogleMobileAds/GADBannerView.h>

#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
Expand All @@ -36,9 +35,13 @@ static float *ios_audio_buffer = NULL;
static int ios_audio_offset = 0;
static NSLock *ios_audio_mutex = [[NSLock alloc] init];
static bool ios_done_first_draw = false;
#ifdef AM_GOOGLE_ADS
#import <GoogleMobileAds/GADBannerView.h>
static GADBannerView *banner_ad = nil;
static bool banner_ad_filled = false;
static bool banner_ad_want_visible = false;
static id banner_delegate = nil;
#endif

static GLKView *ios_view = nil;
static GLKViewController *ios_view_controller = nil;
Expand Down Expand Up @@ -525,6 +528,7 @@ static void ios_become_active() {
}
}

#ifdef AM_GOOGLE_ADS
static void hide_banner() {
[UIView animateWithDuration:0.5
animations:^ { banner_ad.frame = CGRectMake(0.0, -banner_ad.frame.size.height,
Expand All @@ -542,6 +546,7 @@ static void show_banner() {
banner_ad.frame.size.height);
}];
}
#endif

static CMMotionManager *motionManager = nil;

Expand Down Expand Up @@ -632,21 +637,25 @@ static BOOL handle_orientation(UIInterfaceOrientation orientation) {
@end


@interface AMAppDelegate : UIResponder <UIApplicationDelegate, SKPaymentTransactionObserver, GLKViewDelegate, GLKViewControllerDelegate, GADAdDelegate>
@interface AMAppDelegate : UIResponder <UIApplicationDelegate, SKPaymentTransactionObserver, GLKViewDelegate, GLKViewControllerDelegate
#ifdef AM_GOOGLE_ADS
, GADAdDelegate
#endif
>

@property (strong, nonatomic) UIWindow *window;

@end

static id banner_delegate = nil;

@implementation AMAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Prevent rotate animation when application launches.
[UIView setAnimationsEnabled:NO];

#ifdef AM_GOOGLE_ADS
banner_delegate = self;
#endif

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

Expand Down Expand Up @@ -791,6 +800,7 @@ static id banner_delegate = nil;
}
}

#ifdef AM_GOOGLE_ADS
- (void)adViewDidReceiveAd:(nonnull GADBannerView *)bannerView
{
banner_ad_filled = true;
Expand All @@ -805,6 +815,7 @@ static id banner_delegate = nil;
banner_ad_filled = false;
//banner_ad.hidden = YES;
}
#endif

- (void)dealloc
{
Expand Down Expand Up @@ -1162,6 +1173,7 @@ const char *am_preferred_language() {
}

static int init_google_banner_ad(lua_State *L) {
#ifdef AM_GOOGLE_ADS
am_check_nargs(L, 1);
if (banner_ad != nil) return luaL_error(L, "google ads already initialized");
if (ios_view == nil) return luaL_error(L, "internal error: ios_view not initialized");
Expand All @@ -1177,10 +1189,12 @@ static int init_google_banner_ad(lua_State *L) {
banner_ad.rootViewController = ios_view_controller;
banner_ad.delegate = banner_delegate;
[ios_view addSubview:banner_ad];
#endif
return 0;
}

static int set_google_banner_ad_visible(lua_State *L) {
#ifdef AM_GOOGLE_ADS
am_check_nargs(L, 1);
if (banner_ad == nil) return luaL_error(L, "please initialse ads first");
bool vis = lua_toboolean(L, 1) ? true : false;
Expand All @@ -1190,19 +1204,26 @@ static int set_google_banner_ad_visible(lua_State *L) {
} else if (!vis) {
hide_banner();
}
#endif
return 0;
}

static int is_google_banner_ad_visible(lua_State *L) {
#ifdef AM_GOOGLE_ADS
if (banner_ad == nil) return luaL_error(L, "please initialse ads first");
lua_pushboolean(L, banner_ad.hidden == YES ? 0 : 1);
#else
lua_pushboolean(L, 0);
#endif
return 1;
}

static int request_google_banner_ad(lua_State *L) {
#ifdef AM_GOOGLE_ADS
if (banner_ad == nil) return luaL_error(L, "please initialse ads first");
[banner_ad loadRequest:[GADRequest request]];
hide_banner();
#endif
return 0;
}

Expand Down

0 comments on commit 408c22f

Please sign in to comment.