Cocos2dx 3.0 学习笔记:屏幕适配的方法。

我们都知道现在手机的分辨率越来越杂。480*320  800*480  1280*720 等等分辨率。如果要让游戏运行在所有的屏幕分辨率上,似乎是很困难,美工要为不同的分辨率创建不同分辨率的素材,这样 美工可是要累死的节奏。也是很让人头疼的。 所以coco引进了一个"设计分辨率" 所有素材按照设计分辨率的大小来设计制造。以及在编码时精灵的位置等等 都依据这个"设计分辨率"作为参照。之后如果游戏运行在不同的设备上 coco会自动为目标设备的分辨率进行缩放或者拉伸。这样就做到了屏幕所有的适配。

下面贴一段代码

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto eglView = EGLView::getInstance();
    director->setOpenGLView(eglView);
    eglView->setDesignResolutionSize(480,800,ResolutionPolicy(0)); //设计分辨率为480*800的分辨率 我们的素材也按照480*800作为标准设计。
    // turn on display FPS
   director->setDisplayStats(true);
    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();

    // run
    director->runWithScene(scene);
	
    return true;
}

第三个参数:ResolutionPolicy(0) 因为我这里没有引用这个声明枚举的文件。下面贴一下其声明
enum class ResolutionPolicy
{
    // The entire application is visible in the specified area without trying to preserve the original aspect ratio.
    // Distortion can occur, and the application may appear stretched or compressed.
    EXACT_FIT,//缩放拉伸全屏 
    // The entire application fills the specified area, without distortion but possibly with some cropping,
    // while maintaining the original aspect ratio of the application.
    NO_BORDER, //不显示操作面板
    // The entire application is visible in the specified area without distortion while maintaining the original
    // aspect ratio of the application. Borders can appear on two sides of the application.
    SHOW_ALL,//显示全部,但是保持宽高比
    // The application takes the height of the design resolution size and modifies the width of the internal
    // canvas so that it fits the aspect ratio of the device
    // no distortion will occur however you must make sure your application works on different
    // aspect ratios
    FIXED_HEIGHT,//高度不变
    // The application takes the width of the design resolution size and modifies the height of the internal
    // canvas so that it fits the aspect ratio of the device
    // no distortion will occur however you must make sure your application works on different
    // aspect ratios
    FIXED_WIDTH,//宽度不变

    UNKNOWN,
};

这里面我试过就 EXACT_FIT 和NO_BORDER效果是一样的 SHOW_ALL 是保持素材的宽高比 但是 上下方或者左右方可能出现黑条 最后面两个可以忽略,实用性基本没有。

然后在入口函数设置宽高分辨率,因为我这是在win下 在安卓下入口不一样 并且宽度和高度肯定是要调用某些API获取(还没弄android抱歉)

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // create the application instance
    AppDelegate app;
    EGLView eglView;
    eglView.init("Test",320,480); //设置其分辨率为320*480
    return Application::getInstance()->run();

运行可以看到,适配成功 不管多少分辨率coco会为我们拉伸或者缩放。

下面图素材是480*800,分别使用320*480 和240*320测试。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值