cocos2d-x 3.17.1
AndroidStudio 3.4.1
github source
-> imagepicker
- 소스 경로
소스를 다운로드해서 ImagePicker
폴더를 프로젝트의 아래 경로에 넣습니다.
cocos2d/extensions/ImagePicker
- 소스 포함 시키기
CMakeLists.txt 수정
CMakeLists.txt
# add cross-platforms source files and header files
list(APPEND GAME_SOURCE
cocos2d/extensions/ImagePicker/ImagePicker.cpp
cocos2d/extensions/ImagePicker/ImagePickerImpl.cpp
)
list(APPEND GAME_HEADER
cocos2d/extensions/ImagePicker/ImagePicker.h
cocos2d/extensions/ImagePicker/ImagePickerImpl.h
)
- 프로젝트 소스에서 include
include.h
#include "ImagePicker/ImagePicker.h"
- 적용 예
샘플 소스의 HelloWorld.cpp를 참고하여 만들었습니다.
PhotoOpener.h
#ifndef __PHOTHOPENER_SCENE_H__
#define __PHOTHOPENER_SCENE_H__
#include "cocos2d.h"
class PhotoOpener : public cocos2d::Layer, public cocos2d::ImagePickerDelegate
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void didFinishPickingWithResult(cocos2d::Texture2D* result);
// implement the "static create()" method manually
CREATE_FUNC(PhotoOpener);
};
#endif // __PhotoOpener_SCENE_H__
PhotoOpener.cpp
bool PhotoOpener::init()
{
if ( !Layer::init() )
{
return false;
}
ImagePicker::getInstance()->pickImage(this);
return true;
}
// call back
void PhotoOpener::didFinishPickingWithResult(cocos2d::Texture2D* result)
{
if(nullptr == result)
{
log("%s | no photo", __FUNCTION__);
Director::getInstance()->popScene();
return;
}
//
// 540 x 960
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//
// anchor 0.5, 0.5
auto sprite = Sprite::createWithTexture(result);
//
// sprite size
Size sprSize = sprite->getContentSize();
//
// pos
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
//
// scale
float scalfator = visibleSize.width/sprSize.width;
sprite->setScale(scalfator);
//
// to do..
}