cocos2dx进阶学习之屏幕适配
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了cocos2dx进阶学习之屏幕适配,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5371字,纯文字阅读大概需要8分钟。
内容图文
http://blog.csdn.net/itcastcpp/article/details/18608437 背景 在学习cocos2dx时,我们在main函数中发现一句代码, [cpp] view plaincopyprint? #includemain.h #includeAppDelegate.h #includeCCEGLView.h USING_NS_CC; int APIENTRY_tWinMain( HINSTANCE
http://blog.csdn.net/itcastcpp/article/details/18608437
背景
在学习cocos2dx时,我们在main函数中发现一句代码,
[cpp] view plaincopyprint?
- #include "main.h"
- #include "AppDelegate.h"
- #include "CCEGLView.h"
- USING_NS_CC;
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // create the application instance
- AppDelegate app;
- CCEGLView* eglView = CCEGLView::sharedOpenGLView();
- eglView->setViewName("CrazyMario");
- eglView->setFrameSize(480, 320);
- return CCApplication::sharedApplication()->run();
- }
#include "main.h" #include "AppDelegate.h" #include "CCEGLView.h" USING_NS_CC; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // create the application instance AppDelegate app; CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setViewName("CrazyMario"); eglView->setFrameSize(480, 320); return CCApplication::sharedApplication()->run(); }
那就是eglView->setFrameSize(480,320),这句代码设置了窗口的大小,一般说来手机游戏需要全屏显示,所以对于不同分辨率的手机,setFrameSize要求不一样的。这样是不是很崩溃?因为我们代码里很多地方可能也要改,图片大小可能也要改,那怎么办呢?
其实cocos2dx已经为我们做好了这一切
结局方案
这个方案就是调用eglView->setDesignResolutionSize(480, 320, kResolutionShowAll);来告诉cocos2dx,我的程序是按照480,320来设计的,那么setFrameSize如果不是480,320那么cocos2dx会按照比例给我们做适配,不用我们做别的事情。
在超级马里奥这个游戏里,在AppDelegate中已经调用了setDesignResolutionSize函数设置设计大小为480,320
那么在setFrameSize不同的情况下,也不会引起图片比例不合适的情况,只是窗口大小会发生变化而已
在480*320的情况下
在960*640的情况下,只是界面变大了,图片没有任何的不适合
setDesignResolutionSize的参数
第三个参数的取值范围如下:
[cpp] view plaincopyprint?
- enum 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.
- kResolutionExactFit,
- // The entire application fills the specified area, without distortion but possibly with some cropping,
- // while maintaining the original aspect ratio of the application.
- kResolutionNoBorder,
- // 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.
- kResolutionShowAll,
- // 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
- kResolutionFixedHeight,
- // 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
- kResolutionFixedWidth,
- kResolutionUnKnown,
- };
enum 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. kResolutionExactFit, // The entire application fills the specified area, without distortion but possibly with some cropping, // while maintaining the original aspect ratio of the application. kResolutionNoBorder, // 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. kResolutionShowAll, // 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 kResolutionFixedHeight, // 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 kResolutionFixedWidth, kResolutionUnKnown, };
kResolutionExactFit:会靠拉伸来填满屏幕,本例来说背景图会变形来填充屏幕,因为1024:768=1.3, 480:320=1.5,宽高比不同,图片也就无法等比缩放来填满屏幕,只能变形了。
kResolutionNoBorder: 看不到黑边,实际就是宽高等比缩放,但缩放比例取宽比和高比之中大的那一个。
kResolutionShowAll:全部显示,可以理解为保证内容都显示在屏幕之内,实际也是宽高等比缩放,但缩放比例取宽比和高比之中小的那一个。
内容总结
以上是互联网集市为您收集整理的cocos2dx进阶学习之屏幕适配全部内容,希望文章能够帮你解决cocos2dx进阶学习之屏幕适配所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。