本篇主要介绍cocos2dx项目开发过程中或者说项目务必遇到的一些知识点(ps.貌似himi博客写的都是务必的 :tx: himi认为写别人没写的才更容易吸引人不是~)
ok,不多说废话,第一个介绍的是修改项目配置让你的android项目支持自适应屏幕;其实关于android项目自适应屏幕这个问题,himi实在不想再多费口舌,一方面因为himi之前博文有说过,另外一方面现在android开源缘故造成分辨率泛滥也成必然。大家注意做项目尽可能使用相对位置,别写死坐标,另外一点就是针对流行分辨率做适应就好了,如果你们公司很有必要铺android市场的量,那么只能一个一个分辨率去搞了=。 = himi身为kjava(j2me)一路走过来的dev来说,我是在是对自适应虐到习惯…..
1. 咳咳,本不想说,回到正题,那么对于cocos2dx中如何设置项目android版自适应,其实很easy,直接在编译好的android项目中如下路径查找:
your project name/jni/helloworld/main.cpp
ok,找到main.cpp后双击打开,然后看到如下代码段:
1
2
// if you want to run in wvga with hvga resource, set it
view->create(480, 320); // please change it to (320, 480);if you're in portrait mode.
view->create(480,320);默认关闭的,这里打开即可;其实himi也是从cocos2dx引擎框架中看到的,打开你的任意一个cocos2dx引擎框架的项目,然后打开appdelegate.cpp 文件,就能看到:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5yMzMzMzMzLcRDMvwlMxAjMvw1ckF2bsBXdvwFduVGdu92YtA3dvwVbvNmLl1WYnlWbphmL3d3dvw1LcpDc0RHaiojIsJye.png)
2. 下面继续介绍如何让你的cocos2dx-android项目设置缩放比例,一样很easy,设置代码如下:
ccdirector::shareddirector()->setcontentscalefactor(2.0);
默认值是1.0,缩放2倍,从下面这两张图可以明显看出设置后的区别:(点击放大图片)
为了便于后续讲解更容易理解,那么这里himi博文讲解使用的两行图片这里先给出,大家先看下:
rect.png 规格: 40*40 | rect-hd.png 规格:80*80
3.下面介绍如何让cocos2dx的android版项目使用ios retina类似@2x的-hd功能也直接使用高清图片,当然cocos2dx引擎默认找的高清图为-hd;但是编译xcode的cocos2dx项目到android版后,android版可不会那么聪明自动使用你的-hd的版图片,所以下面himi来手把手教你设置;具体步骤如下:
3.1 首先在你的项目下找到 cceglview_android.cpp ,双击打开:
找到 void cceglview::create(int width, int height) 函数,然后函数内替换成如下代码:
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
void cceglview::create(int width, int height)
{
if (width == 0 || height == 0)
{
return;
}
m_ssizeinpoint.width = width;
m_ssizeinpoint.height = height;
// calculate the factor and the rect of viewport
m_fscreenscalefactor = min((float)m_ssizeinpixel.width / m_ssizeinpoint.width, (float)m_ssizeinpixel.height / m_ssizeinpoint.height);
cclog("cceglview::create / screen scale factor = %f", m_fscreenscalefactor);
if (m_fscreenscalefactor >= 1.5f)
cclog("cceglview::create / hd scale factor => increase content scale factor");
cocos2d::ccdirector::shareddirector()->setcontentscalefactor(2.0f);
int viewportw = (int)(m_ssizeinpoint.width * m_fscreenscalefactor);
int viewporth = (int)(m_ssizeinpoint.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;
m_rcviewport.size.height = viewporth;
m_bnothvga = true;
}
3.2 继续在你的项目下找到ccfileutils_android.cpp 类,双击打开:
找到 const char* ccfileutils::fullpathfromrelativepath(const char *pszrelativepath) 函数,然后替换如下内容:
29
30
31
32
33
34
35
36
37
38
39
const char* ccfileutils::fullpathfromrelativepath(const char *pszrelativepath)
if (cc_content_scale_factor() == 2.0f)
//cc_retina_display_filename_suffix
// verifier si suffix deja present
std::string path = pszrelativepath;
std::string::size_type pos = path.rfind("/") + 1; // the begin index of last part of path
std::string::size_type suffixpos = path.rfind(cc_retina_display_filename_suffix);
if ((std::string::npos != suffixpos) && (suffixpos > pos))
{
// => if yes, return path directly
}
else
// => if no, add "retina"/hd suffix and test if file exist
ccstring *pret = new ccstring();
pret->autorelease();
pret->m_sstring = path.substr(0, path.rfind(".")) + cc_retina_display_filename_suffix + path.substr(path.rfind("."), path.length());
if (existfiledata(pret->m_sstring.c_str()))
{
// => if yes, return path with suffix
cclog("cocos2d: filepath(%s) with suffix(%s) exist, use it.", pret->m_sstring.c_str(), cc_retina_display_filename_suffix);
return pret->m_sstring.c_str();
}
else
// => if no, return path without suffix
}
return pszrelativepath;
然后接着在本类添加如下两个函数:
40
41
42
43
44
45
46
47
48
49
50
51
52
53
bool ccfileutils::existfiledata(const char* pszfilename)
string fullpath(pszfilename);
if ((! pszfilename))
return false;
if (pszfilename[0] != '/')
// read from apk
fullpath.insert(0, "assets/");
return ccfileutils::existfiledatafromzip(s_strresourcepath.c_str(), fullpath.c_str());
else
do
// read rrom other path than user set it
file *fp = fopen(pszfilename, "rb");
if (fp != null)
fclose(fp);
return true;
while (0);
return false;
bool ccfileutils::existfiledatafromzip(const char* pszzipfilepath, const char* pszfilename)
unzfile pfile = null;
bool res = false;
do
cc_break_if(!pszzipfilepath || !pszfilename);
cc_break_if(strlen(pszzipfilepath) == 0);
pfile = unzopen(pszzipfilepath);
int nret = unzlocatefile(pfile, pszfilename, 1);
res = unz_ok == nret;
} while (0);
if (pfile)
unzclose(pfile);
return res;
最后在ccfileutils.h 中声明两个函数的定义:
static bool existfiledata(const char* pszfilename);
static bool existfiledatafromzip(const char* pszzipfilepath, const char* pszfilename);
3.3 最后记得设置缩放比例的值2.0,那么重新编译你的项目到android运行则如下图所示:
ok,本篇就到这里,himi最近感冒,还没吃晚饭,咳咳,先晚饭去了。。。北京最近下雨天气偏凉~大家多注意身体,