Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Created September 22, 2015 01:57
Show Gist options
  • Select an option

  • Save tklee1975/7b2ace4bee70c0a01823 to your computer and use it in GitHub Desktop.

Select an option

Save tklee1975/7b2ace4bee70c0a01823 to your computer and use it in GitHub Desktop.
Source code for Cocos-2dx Score Sharing (Android Platform)
//
// ShareHelper.cpp
// TapToJump
//
// Created by Ken Lee on 10/9/15.
//
//
#include "ShareHelper.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "AndroidHelper.h"
void ShareHelper::shareScore()
{
AndroidHelper::callVoidStaticMethod("org.cocos2dx.cpp.ShareHelper", "shareScoreImage");
}
#else
void ShareHelper::shareScore()
{
log("showScore: Feature not support!");
}
#endif
//
// ShareHelper.h
// TapToJump
//
// Created by Ken Lee on 10/9/15.
//
//
#ifndef __TapToJump__ShareHelper__
#define __TapToJump__ShareHelper__
#include <stdio.h>
#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;
class ShareHelper {
public:
static void shareScore();
};
#endif /* defined(__TapToJump__ShareHelper__) */
package org.cocos2dx.cpp;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
public class ShareHelper {
private static final String TAG = "ShareHelper";
public static void shareScoreImage()
{
shareImage("share.png", true);
}
public static void shareImage(String imageFile, boolean isInternal) {
String sharePath = DeviceHelper.getExternalPath() + "/" + imageFile; // Other app only can get extern path
// Environment.getDataDirectory().getAbsolutePath()
// : DeviceHelper.getExternalPath();
if(isInternal) {
String src = AppActivity.getAppContext().getFilesDir().getAbsolutePath() + "/" + imageFile;
FileHelper.deleteFile(sharePath);
boolean isOkay = FileHelper.copyFile(src, sharePath);
Log.d("TapMeHigh", "copy " + src + " to " + sharePath);
if(isOkay == false) {
Log.e("TapMeHigh", "Fail to copy image");
} else {
Log.d("TapMeHigh", "success");
}
}
//String externPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//Uri imageUri = Uri.parse("file://" + externPath + "/temp/test.jpg");
Uri imageUri = Uri.parse("file://" + sharePath);
// Uri imageUri = Uri.parse("file://" + path + "/" + imageFile);
Log.d(TAG, "ShareImage: imageUri=" + imageUri);
// This is working on Twitter, Whatsapp but not Facebook
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "I have a new score in TAP ME HIGH!!");
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("image/jpeg");
//
ActivityHelper.startActivityFromMain(sendIntent);
}
}
//
// ShareImage.cpp
// TapToJump
//
// Created by Ken Lee on 9/9/15.
//
//
#include "ShareImage.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
#include "GameData.h"
#include "DeviceHelper.h"
#include "ShareHelper.h"
#define kFilename "/share.png"
using namespace cocostudio::timeline;
// on "init" you need to initialize your instance
bool ShareImage::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
this->setContentSize(Size(240, 240));
// Setup UI
Node *rootNode = CSLoader::createNode("ShareLayer.csb");
addChild(rootNode);
mScoreText = (Text *) rootNode->getChildByName("scoreText");
return true;
}
void ShareImage::setScore(int score)
{
char scoreStr[20];
sprintf(scoreStr, "%d", score);
mScoreText->setString(scoreStr);
}
Sprite *ShareImage::getSprite()
{
RenderTexture *texture = getRenderTexture();
auto sprite = Sprite::createWithTexture(texture->getSprite()->getTexture());
return sprite;
}
RenderTexture *ShareImage::getRenderTexture()
{
Size size = this->getContentSize();
RenderTexture *texture = RenderTexture::create(size.width, size.height,
Texture2D::PixelFormat::RGBA8888);
return texture;
}
void ShareImage::saveImage(const std::string &filename, bool shareAfterSave)
{
RenderTexture *text = getRenderTexture();
std::function<void (RenderTexture*, const std::string&)> callback =
[shareAfterSave](RenderTexture*, const std::string &file) {
log("RenderImage ready!! share=%d file=%s", shareAfterSave,
file.c_str());
if(shareAfterSave) {
ShareHelper::shareScore();
}
};
log("Start saving Image: %s", filename.c_str());
text->saveToFile(filename, true, callback);
}
void ShareImage::saveAndShare()
{
saveImage(kFilename, true);//
// ShareImage.h
// TapToJump
//
// The template to generate Share Image
//
// Created by Ken Lee on 9/9/15.
//
//
#ifndef __TapToJump__ShareImage__
#define __TapToJump__ShareImage__
#include <stdio.h>
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
USING_NS_CC_EXT;
USING_NS_CC;
using namespace cocos2d;
using namespace cocos2d::ui;
class ShareImage : public Layer
{
public:
CREATE_FUNC(ShareImage);
virtual bool init();
void setScore(int score);
RenderTexture *getRenderTexture();
void saveToLocal();
void saveImage(const std::string &filename, bool shareAfterSave);
void saveAndShare();
Sprite *getSprite();
private:
Text *mScoreText;
};
#endif /* defined(__TapToJump__ShareImage__) */
}
//
// ShareImage.h
// TapToJump
//
// The template to generate Share Image
//
// Created by Ken Lee on 9/9/15.
//
//
#ifndef __TapToJump__ShareImage__
#define __TapToJump__ShareImage__
#include <stdio.h>
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
USING_NS_CC_EXT;
USING_NS_CC;
using namespace cocos2d;
using namespace cocos2d::ui;
class ShareImage : public Layer
{
public:
CREATE_FUNC(ShareImage);
virtual bool init();
void setScore(int score);
RenderTexture *getRenderTexture();
void saveImage(const std::string &filename, bool shareAfterSave);
void saveAndShare();
Sprite *getSprite();
private:
Text *mScoreText;
};
#endif /* defined(__TapToJump__ShareImage__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment