Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enabled content scale mode and support for variable screen aspect ratio on android #538

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions cocos2dx/platform/android/CCEGLView_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void CCEGLView::setFrameWidthAndHeight(int width, int height)
m_sSizeInPixel.height = height;
}

void CCEGLView::create(int width, int height)
void CCEGLView::create(int width, int height, bool auto_adjust)
{
if (width == 0 || height == 0)
{
Expand All @@ -62,6 +62,17 @@ void CCEGLView::create(int width, int height)
(float)m_sSizeInPixel.height / m_sSizeInPoint.height);
int viewPortW = (int)(m_sSizeInPoint.width * m_fScreenScaleFactor);
int viewPortH = (int)(m_sSizeInPoint.height * m_fScreenScaleFactor);

if (auto_adjust) {
// use the real display size
viewPortW = m_sSizeInPixel.width;
viewPortH = m_sSizeInPixel.height;

// increase the "size in points" values to fit the display aspect ratio
m_sSizeInPoint.width = m_sSizeInPixel.width / m_fScreenScaleFactor;
m_sSizeInPoint.height = m_sSizeInPixel.height / m_fScreenScaleFactor;
}

m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2;
m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2;
m_rcViewPort.size.width = viewPortW;
Expand Down Expand Up @@ -118,12 +129,12 @@ void CCEGLView::swapBuffers()
bool CCEGLView::canSetContentScaleFactor()
{
// can scale content?
return false;
return true;
}

void CCEGLView::setContentScaleFactor(float contentScaleFactor)
{
m_fScreenScaleFactor = contentScaleFactor;
//m_fScreenScaleFactor = contentScaleFactor;
}

void CCEGLView::setViewPortInPoints(float x, float y, float w, float h)
Expand Down
4 changes: 3 additions & 1 deletion cocos2dx/platform/android/CCEGLView_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class CC_DLL CCEGLView
/**
* create a drawing rect,
* the width and heiht is the resource size match best
* @param auto_adjust when enabled, the selected resolution will be automatically increased
* to fit the screen aspect ratio of the current android device.
*/
void create(int width, int height);
void create(int width, int height, bool auto_adjust=true);
EGLTouchDelegate* getDelegate(void);

// keep compatible
Expand Down
87 changes: 87 additions & 0 deletions cocos2dx/platform/android/CCFileUtils_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,51 @@ void CCFileUtils::setResourcePath(const char* pszResourcePath)

const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
#if (CC_IS_RETINA_DISPLAY_SUPPORTED)
if (CC_CONTENT_SCALE_FACTOR() > 1.0f)
{
std::string hiRes = pszRelativePath;
std::string::size_type pos = hiRes.find_last_of("/");
std::string::size_type dotPos = hiRes.find_last_of(".");
bool available = false;

if (std::string::npos != dotPos && (dotPos > pos || pos == std::string::npos))
{
hiRes.insert(dotPos, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
}
else
{
hiRes.append(CC_RETINA_DISPLAY_FILENAME_SUFFIX);
}

bool isApkResourcePath = (hiRes[0] != '/');
if (isApkResourcePath) {
// when the resources are within the APK file, open the zip to check if the HD image is available
unzFile pFile = unzOpen(s_strResourcePath.c_str());

if (pFile) {
available = (UNZ_OK == unzLocateFile(pFile, (string("assets/") + hiRes).c_str(), 1));
unzClose(pFile);
}
}
else {
FILE *fp = fopen(hiRes.c_str(), "rb");
if (fp) {
available = true;
fclose(fp);
}
}

// a HD version of this file is available, so return the new name
if (available) {
CCString *pRet = new CCString();
pRet->autorelease();
pRet->m_sString.swap(hiRes);
return pRet->m_sString.c_str();
}
}
#endif

return pszRelativePath;
}

Expand All @@ -59,6 +104,48 @@ const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const
pRet->autorelease();
pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1);
pRet->m_sString += pszFilename;

#if (CC_IS_RETINA_DISPLAY_SUPPORTED)
if (CC_CONTENT_SCALE_FACTOR() > 1.0f)
{
std::string hiRes = pRet->m_sString.c_str();
std::string::size_type pos = hiRes.find_last_of("/");
std::string::size_type dotPos = hiRes.find_last_of(".");
bool available = false;

if (std::string::npos != dotPos && (dotPos > pos || pos == std::string::npos))
{
hiRes.insert(dotPos, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
}
else
{
hiRes.append(CC_RETINA_DISPLAY_FILENAME_SUFFIX);
}

bool isApkResourcePath = (hiRes[0] != '/');
if (isApkResourcePath) {
// when the resources are within the APK file, open the zip to check if the HD image is available
unzFile pFile = unzOpen(s_strResourcePath.c_str());

if (pFile) {
available = (UNZ_OK == unzLocateFile(pFile, (string("assets/") + hiRes).c_str(), 1));
unzClose(pFile);
}
}
else {
FILE *fp = fopen(hiRes.c_str(), "rb");
if (fp) {
available = true;
fclose(fp);
}
}

if (available) {
pRet->m_sString.swap(hiRes);
}
}
#endif

return pRet->m_sString.c_str();
}

Expand Down