永发信息网

cocos2dx 怎么获得android屏幕分辨率

答案:1  悬赏:70  手机版
解决时间 2021-02-11 11:46
  • 提问者网友:趣果有间
  • 2021-02-11 06:24
cocos2dx 怎么获得android屏幕分辨率
最佳答案
  • 五星知识达人网友:街头电车
  • 2021-02-11 07:21
写一个ViewAutoScale函数,如下:

#include "ViewAutoScale.h"
USING_NS_CC;

bool IsMatchDisplay(int w, int h, CCSize& size )
{
return (w==size.width && h==size.height) || (h==size.width && w==size.height);
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
int ViewAutoScale(cocos2d::CCEGLView* view,
void* title,
int width,
int height,
cocos2d::CCSize* supportDisplay,
int displays,
int defaultWidth,
int defaultHeight)
{
if(view == NULL)
{
return -1;
}
for (int i=0; i < displays; i++)
{
if (IsMatchDisplay(width, height, supportDisplay[i]))
{
view->Create((LPCTSTR)title, width, height);
return i+1;
}
}
view->Create((LPCTSTR)title, defaultWidth, defaultHeight);

view->setScreenScale(min((float)width/ defaultWidth, (float)height/ defaultHeight));
view->resize(width, height);
view->centerWindow();
return 0;
}

#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
int ViewAutoScale(cocos2d::CCEGLView* view,
void* title,
int width,
int height,
cocos2d::CCSize* supportDisplay,
int displays,
int defaultWidth,
int defaultHeight)
{
if(view == NULL)
{
return -1;
}
for (int i=0; i < displays; i++)
{
if (IsMatchDisplay(width, height, supportDisplay[i]))
{
return i+1;
}
}
view->create(defaultWidth, defaultHeight);
return 0;
}
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
int ViewAutoScale(cocos2d::CCEGLView* view,
void* title,
int width,
int height,
cocos2d::CCSize* supportDisplay,
int displays,
int defaultWidth,
int defaultHeight)
{
return 0;
}
#endif

修改jni/helloworld/main.cpp文件:

void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
{
cocos2d::CCSize sSupportDisplay[]={cocos2d::CCSize(480, 320)};
if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView())
{
cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();
view->setFrameWidthAndHeight(w, h);
// if you want to run in WVGA with HVGA resource, set it
ViewAutoScale(view,
NULL,
w,
h,
sSupportDisplay,
sizeof(sSupportDisplay)/sizeof(CCSize),
480, 320);
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯