Cocosd-x屏比

前端之家收集整理的这篇文章主要介绍了Cocosd-x屏比前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

完美主义情况下android自适屏,最好的方式当然是做多套不同分辨率屏幕大小的图片资源,但是一般作为我们这样的小团队开发,不会去花费太多的时间去做多套不同的资源,能给你多做几个封面就不错了,所以这个重任又从美术的头上落到我们程序猿的头上,在cocos2d-x中android包默认支持等比例的屏幕缩放,具体实现方式,在jni/helloworld/main.cpp中增加如下代码即可。继续拿HelloWorld开刀!

等比缩放效果图:

这样游戏就已经等比缩放了,不过在大部分android手机上会有黑边,虽然我个人比较喜欢这种方式,因为等比缩放游戏画面不会变形,但是通常情况下我说是没用的,策划说要全屏的,测试说要全屏的,我说让美术做个图,美术说要约会,没时间加班,好吧。又成了程序员的事了。

上面说过cocos2d-x默认支持等比例缩放,从源代码可以看出来,这些源代码在cocos2dx/platform/android下面,在CCEGLView_android.cpp中

@H_502_14@ 19
20
@H_502_14@ 21
( int width height )
{
width == 0 || height 0 )
{
return ;
}
m_sSizeInPoint . ;
;
// calculate the factor and the rect of viewport
// 这里取宽高需要缩放比例中小的那个作为等比缩放的比例,可以保证画面完全显示,但是会有黑边
// !!!需要修改的地方就是这里!!!
m_fScreenScaleFactor MIN float ) m_sSizeInPixel /
;
viewPortW int m_fScreenScaleFactor ;
viewPortH ;
m_rcViewPort origin x - viewPortW ) 2 ;
y viewPortH ;
size ;
;
m_bNotHVGA true ;

在CCEGLView_android中增加宽高不同的缩放参数,并使用这两个参数来对画面进行不等比缩放

CCEGLView_android.h中增加

@H_502_14@ 9
float getScreenScaleFactor ;
// Hxs add for non uniform scale. 2013/05/15
getScreenScaleFactorX ;
getScreenScaleFactorY ;
/*********************/
// 原本只有一个缩放参数,等比缩放会有黑边
// Hxs add for non uniform scale. 2013/05/15
m_fScreenScaleFactorX // X,Y方向的缩放参数
m_fScreenScaleFactorY ;

CCEGLView_android.cpp中

@H_502_14@ 21
22
@H_502_14@ 23
24
@H_502_14@ 25
26
@H_502_14@ 27
28
@H_502_14@ 29
30
@H_502_14@ 31
32
@H_502_14@ 33
34
@H_502_14@ 35
36
@H_502_14@ 37
38
@H_502_14@ 39
40
@H_502_14@ 41
42
@H_502_14@ 43
44
@H_502_14@ 45
46
@H_502_14@ 47
48
@H_502_14@ 49
50
@H_502_14@ 51
52
@H_502_14@ 53
54
@H_502_14@ 55
56
@H_502_14@ 57
58
@H_502_14@ 59
60
@H_502_14@ 61
62
@H_502_14@ 63
64
@H_502_14@ 65
66
@H_502_14@ 67
68
@H_502_14@ 69
70
@H_502_14@ 71
72
@H_502_14@ 73
74
@H_502_14@ 75
76
@H_502_14@ 77
#define NON_UNIFORM_SCALE 1 // 不等比缩放开关 宏定义
/* 修改create(int width,int height) / CCEGLView::setViewPortInPoints(float x,float y,float w,float h) / CCEGLView::setScissorInPoints(float x,float h)方法中关于缩放的处理 */
)
{
)
{
;
}
;
;
// calculate the factor and the rect of viewport
;
#if NON_UNIFORM_SCALE
m_fScreenScaleFactorX // 分别得到X,Y的缩放参数
m_fScreenScaleFactorY ;
#else
// 等比例缩放
;
#endif
;
;
;
;
;
;
;
}
setViewPortInPoints x y )
{
m_bNotHVGA )
{
factorX CC_CONTENT_SCALE_FACTOR ;
factorY ;
glViewport GLint factorX + Crayon-h" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(0,
factorY Crayon-sy" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(51,
w Crayon-h" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(0,
h ;
}
else
{
Crayon-line Crayon-striped-line" id="Crayon-54f2a4f4774dc854808313-42" style="border:0px; padding:0px 5px; margin:0px; height:inherit; background-color:rgb(245,
Crayon-h" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(0,
Crayon-line Crayon-striped-line" id="Crayon-54f2a4f4774dc854808313-44" style="border:0px; padding:0px 5px; margin:0px; height:inherit; background-color:rgb(245,
;
}
}
setScissorInPoints )
{
)
{
;
;
glScissor Crayon-sy" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(51,
Crayon-line Crayon-striped-line" id="Crayon-54f2a4f4774dc854808313-56" style="border:0px; padding:0px 5px; margin:0px; height:inherit; background-color:rgb(245,
Crayon-h" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(0,
;
}
else
{
Crayon-line Crayon-striped-line" id="Crayon-54f2a4f4774dc854808313-62" style="border:0px; padding:0px 5px; margin:0px; height:inherit; background-color:rgb(245,
Crayon-h" style="height:inherit; font-size:inherit!important; line-height:inherit!important; color:rgb(0,
Crayon-line Crayon-striped-line" id="Crayon-54f2a4f4774dc854808313-64" style="border:0px; padding:0px 5px; margin:0px; height:inherit; background-color:rgb(245,
;
}
/* 增加getScreenScaleFactorX/getScreenScaleFactorY的实现 */
)
{
return ;
}
)
{
;
到此为止,画面上的缩放已经完成啦,但是缩放后触摸的位置还需要修正,继续修改cocos2dx/platform/android/jni/TouchesJni.cpp中关于触摸的缩放,此文件中有多处需要修改的敌方,但是修改方式都一样。以Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin为例

Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin jint id jfloat )
{
CCRect rcRect getViewPort ;
//float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
fScreenScaleFactorX ;
fScreenScaleFactorY ;
CCSet set ;
CCTouch pTouch s_pTouches [ ] ;
! pTouch )
{
LOGD ( "Beginning touches with id: %d,x=%f,y=%f" ;
CCTouch
//pTouch->SetTouchInfo(0,(x - rcRect.origin.x) / fScreenScaleFactor,(y - rcRect.origin.y) / fScreenScaleFactor,id);
// 分别对x 和 y进行缩放
pTouch SetTouchInfo rcRect fScreenScaleFactorX fScreenScaleFactorY ;
] ;
addObject ;
getDelegate touchesBegan &set NULL ;
else
{
"Beginnig touches with id: %d error" ;
}
}

猜你在找的Cocos2d-x相关文章