2015. 2. 14. 15:41
[Cocos2d-x] AppActivity에 Google Play랑 Admob 더하기 DATA_OLD/Cocos2d-x Study2015. 2. 14. 15:41
Cocos2d-x에서 게임 내부적인 구현은 모두 C++에서 이루어지지만,
생계(...)와 경쟁력에 중요한 부분이라 할 수 있는 광고와 구글 플레이와 관련된 것들은 대부분 자바에서 이루어진다.
특히 AppActivity.java파일에서 거의 모든것이 이루어지기 때문에 이부분에 대한 구현이 중요하다.
보통 AdMob관련 글과 Google Play 관련 글, 두 부류로 나뉘어있어서 각각을 보고 한번에 구현하려면 꽤 귀찮은 과정이 소요되기 마련이다.
Google Play는 GameSharing과 GameHelper같은 부가적인 API가 본체이기 때문에 크게 손볼 것은 없고, Admob은 AppActivity에서만 이루어지면 되기 때문에, 이 두가지가 함께 나오기 위해서는 AppActivity.java파일의 구현이 중요하다.
이 두가지를 한번씩 거쳐봄으로써, 두가지를 합쳐 정리한 코드를 올려본다.
다만, 확실한 나의 지식으로 구현했다기보다는 여기서 보고 저기서 본 두가지를 합쳐 짬뽕하여 에러가 나지않게만 구현한 것이기 때문에 향후 생기는 어떠한 문제에 대해서도 책임지지 않음을 일러두는 바이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2011 Zynga Inc. Copyright (c) 2013-2014 Chukong Technologies Inc. Copyright (c) 2014 Adrian Dawid http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ package org.cocos2dx.cpp; import input.your.game.R; //해당 게임의 패키지로 변경 import android.content.Context; import android.content.Intent; import android.os.Bundle; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.annotation.TargetApi; import android.graphics.Color; import android.graphics.Point; import android.os.Build; import android.view.Display; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.games.Games; public class AppActivity extends BaseGameActivity { /////////////////////////////////이하 AdMob관련 부분//////////////////////////////////// private static AppActivity _appActiviy; private AdView adView; private static final String AD_UNIT_ID = "ca-app-pub-0000000000000000/0000000000"; //admob에서 얻은 코드 삽입 // Helper get display screen to avoid deprecated function use private Point getDisplaySize(Display d) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return getDisplaySizeGE11(d); } return getDisplaySizeLT11(d); } @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) private Point getDisplaySizeGE11(Display d) { Point p = new Point(0, 0); d.getSize(p); return p; } private Point getDisplaySizeLT11(Display d) { try { Method getWidth = Display.class.getMethod("getWidth", new Class[] {}); Method getHeight = Display.class.getMethod("getHeight", new Class[] {}); return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(), ((Integer) getHeight.invoke(d, (Object[]) null)).intValue()); } catch (NoSuchMethodException e2) // None of these exceptions should ever occur. { return new Point(-1, -1); } catch (IllegalArgumentException e2) { return new Point(-2, -2); } catch (IllegalAccessException e2) { return new Point(-3, -3); } catch (InvocationTargetException e2) { return new Point(-4, -4); } } public static void hideAd() { _appActiviy.runOnUiThread(new Runnable() { @Override public void run() { if (_appActiviy.adView.isEnabled()) _appActiviy.adView.setEnabled(false); if (_appActiviy.adView.getVisibility() != 4 ) _appActiviy.adView.setVisibility(View.INVISIBLE); } }); } public static void showAd() { _appActiviy.runOnUiThread(new Runnable() { @Override public void run() { if (!_appActiviy.adView.isEnabled()) _appActiviy.adView.setEnabled(true); if (_appActiviy.adView.getVisibility() == 4 ) _appActiviy.adView.setVisibility(View.VISIBLE); } }); } @Override protected void onResume() { super.onResume(); if (adView != null) { adView.resume(); } } @Override protected void onPause() { if (adView != null) { adView.pause(); } super.onPause(); } @Override protected void onDestroy() { adView.destroy(); super.onDestroy(); } /////////////////////////////////이상 AdMob 관련 부분//////////////////////////////////// @Override protected void onCreate(Bundle savedInstanceState) { String leaderboardIdsRaw = getString(R.string.leaderboards); String achievementIdsRaw = getString(R.string.achievements); leaderboardIDs = leaderboardIdsRaw.split(";"); achievementIDs = achievementIdsRaw.split(";"); currentContext = this; super.onCreate(savedInstanceState); ////여기서부터 admob관련 추가 부분//// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); int width = getDisplaySize(getWindowManager().getDefaultDisplay()).x; LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams( width, LinearLayout.LayoutParams.WRAP_CONTENT); adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(AD_UNIT_ID); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("HASH_DEVICE_ID") .build(); adView.loadAd(adRequest); adView.setBackgroundColor(Color.BLACK); adView.setBackgroundColor(0); addContentView(adView,adParams); _appActiviy = this; ////여기까지 admob관련 추가 부분//// } /////////////////////////////////이하 Google Play 관련 부분//////////////////////////////////// static int currentID; static int currentAchievementID; static boolean gpgAvailable; static String[] leaderboardIDs; static String[] achievementIDs; static Context currentContext; @Override public void onSignInSucceeded(){ gpgAvailable = true; } @Override public void onSignInFailed(){ gpgAvailable = false; } /*@brief Changes the actvie leaderboard @param The index of the leaderboard */ static public void openLeaderboard(int leaderboardID){ currentID = leaderboardID; } /*@brief This function opens the leaderboards ui for an leaderboard id*/ static public void openLeaderboardUI(){ if(gpgAvailable){ ((AppActivity)currentContext).runOnUiThread(new Runnable() { public void run() { ((AppActivity)currentContext).startActivityForResult(Games.Leaderboards.getLeaderboardIntent(((AppActivity)currentContext).getGameHelper().getApiClient(), leaderboardIDs[currentID]),2); } }); } } static public boolean isGPGSupported(){ return gpgAvailable; } /*@brief Submits a score to the leaderboard that is currently actvie*/ static public void submitScoreToLeaderboard(int score) { if(gpgAvailable){ Games.Leaderboards.submitScore(((AppActivity)currentContext).getGameHelper().getApiClient(),leaderboardIDs[currentID],score); updateAchievement(100); } } /*@brief Shows the achievements ui*/ static public void showAchievements() { if(gpgAvailable){ ((AppActivity)currentContext).runOnUiThread(new Runnable() { public void run() { ((AppActivity)currentContext).startActivityForResult(Games.Achievements.getAchievementsIntent(((AppActivity)currentContext).getGameHelper().getApiClient()), 5); } }); } } /*@brief Changes the actvie Achievement @param The index of the achievement in the list*/ static public void openAchievement(int achievementID){ currentAchievementID = achievementID; } static public void updateAchievement(int percentage){ if(gpgAvailable){ Games.Achievements.unlock(((AppActivity)currentContext).getGameHelper().getApiClient(), achievementIDs[currentAchievementID]); } } static public void exitGame() { Intent intent = new Intent(currentContext, MainActivity.class); MainActivity.exiting=true; currentContext.startActivity(intent); } /////////////////////////////////이상 Google Play 관련 부분//////////////////////////////////// } | cs |
'DATA_OLD > Cocos2d-x Study' 카테고리의 다른 글
[Cocos2d-x] Google Play 연동법(2) - 나머지 작업 및 GameSharing 추가 (0) | 2015.02.14 |
---|---|
[Cocos2d-x] 안드로이드 포팅 시 Android.mk 파일 수정 관련 (0) | 2015.02.14 |
[Cocos2d-x] Google Play 연동법(1) - 라이브러리 추가 (0) | 2015.02.09 |
[Cocos2d-X] perhaps java_home does not point to the jdk (0) | 2015.01.19 |
[Cocos2d-x] Move액션의 속도, 움직임 관련하여 (0) | 2015.01.19 |