cocos2d-x lua 屏幕适配问题(OpenGL调用),版本号(cocos2dx v3.4)

前端之家收集整理的这篇文章主要介绍了cocos2d-x lua 屏幕适配问题(OpenGL调用),版本号(cocos2dx v3.4)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

我们知道,cocos2dx 中屏幕适配的设置方法

  1. Director::getInstance()->getOpenGLView()->setDesignResolutionSize(960,640,kResolutionShowAll);

为了保持我们的游戏不被拉伸,选择showAll方法。但是有一个问题,showAll会留黑边,那么我们只需要在openGL中渲染黑边即可。这样黑边就会被填充为我们自己设置的图片。话不多说,看看下面的代码

MainScene.lua

调用示例如下,只需要在onEnter里面渲染即可

  1. function MainScene:onEnter()
  2.  
  3. local function createSpriteWithPathPosScale(path,pos)
  4. local sprite = cc.Sprite:create(path)
  5. sprite : setAnchorPoint(cc.p(0.5,0.5))
  6. sprite : setPosition(pos)
  7. return sprite
  8. end
  9.  
  10.  
  11. local frameSize = cc.Director:getInstance() : getOpenGLView() : getFrameSize()
  12. local layer = cp.ScreenMatchLayer : create()
  13. layer:retain()
  14.  
  15.  
  16. local sprite = createSpriteWithPathPosScale("ui_img_left.jpg",cc.p(-44,320))
  17. layer : getChildByName("_node") : addChild(sprite)
  18.  
  19.  
  20. sprite = createSpriteWithPathPosScale("ui_img_right.jpg",cc.p(960 + 44,320))
  21. layer : getChildByName("_node") : addChild(sprite)
  22.  
  23.  
  24. sprite = createSpriteWithPathPosScale("ui_img_top.jpg",cc.p(480,640 + 32.5))
  25. layer : getChildByName("_node") : addChild(sprite)
  26.  
  27.  
  28. sprite = createSpriteWithPathPosScale("ui_img_down.jpg",-32.5))
  29. layer : getChildByName("_node") : addChild(sprite)
  30. end


AppDelegate.cpp

这里面对ScreenMatchLayer进行注册以便被lua调用

  1. bool AppDelegate::applicationDidFinishLaunching()
  2. {
  3. // set default FPS
  4. Director::getInstance()->setAnimationInterval(1.0 / 60.0f);
  5. // register lua module
  6. auto engine = LuaEngine::getInstance();
  7. ScriptEngineManager::getInstance()->setScriptEngine(engine);
  8. lua_State* L = engine->getLuaStack()->getLuaState();
  9. lua_module_register(L);
  10. ScreenMatchLayer::lua_bind_AllFunction(L);
  11.  
  12. // If you want to use Quick-Cocos2d-X,please uncomment below code
  13. // register_all_quick_manual(L);
  14.  
  15. LuaStack* stack = engine->getLuaStack();
  16. stack->setXXTEAKeyAndSign("2dxLua",strlen("2dxLua"),"XXTEA",strlen("XXTEA"));
  17. //register custom function
  18. //LuaStack* stack = engine->getLuaStack();
  19. //register_custom_function(stack->getLuaState());
  20. #if (COCOS2D_DEBUG > 0) && (CC_CODE_IDE_DEBUG_SUPPORT > 0)
  21. // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
  22. RuntimeEngine::getInstance()->start();
  23. cocos2d::log("iShow!");
  24. #else
  25. if (engine->executeScriptFile("src/main.lua"))
  26. {
  27. return false;
  28. }
  29. #endif
  30. return true;
  31. }



ScreenMatchLayer.h


  1. #ifndef __SCREENMATCHLAYER_H__
  2. #define __SCREENMATCHLAYER_H__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. namespace cocos2d{
  7.  
  8. class ScreenMatchLayer : public Layer
  9. {
  10. public:
  11. ScreenMatchLayer(void);
  12. ~ScreenMatchLayer(void);
  13. static ScreenMatchLayer* create();
  14. bool init();
  15.  
  16. void visit(Renderer *renderer,const Mat4& parentTransform,uint32_t parentFlags);
  17. void onBeforeVisit();
  18. void onAfterVisit();
  19.  
  20. static int lua_bind_AllFunction(lua_State* tolua_S);
  21. static const std::string classPrefix;
  22. static const std::string fullName;
  23. static const std::string className;
  24. private:
  25. Node* _node;
  26. CustomCommand _beforeVisit;
  27. CustomCommand _afterVisit;
  28. };
  29. }
  30.  
  31. int lua_cocos2dx_ScreenMatchLayer_create(lua_State* tolua_S);
  32. #endif

ScreenMatchLayer.cpp

  1. #include "ScreenMatchLayer.h"
  2. #include "tolua_fix.h"
  3. #include "LuaBasicConversions.h"
  4. using namespace cocos2d;
  5.  
  6. const std::string ScreenMatchLayer::classPrefix = "cp";
  7. const std::string ScreenMatchLayer::className = "ScreenMatchLayer";
  8. const std::string ScreenMatchLayer::fullName = classPrefix + "." + className;
  9.  
  10. ScreenMatchLayer::ScreenMatchLayer(void)
  11. {
  12. }
  13.  
  14.  
  15. ScreenMatchLayer::~ScreenMatchLayer(void)
  16. {
  17. }
  18.  
  19. bool ScreenMatchLayer::init()
  20. {
  21. if ( !Layer::init() ) {
  22. return false;
  23. }
  24. _node = Node::create();
  25. _node->setName("_node");
  26. _node->setAnchorPoint(ccp(0,0));
  27. addChild(_node);
  28.  
  29. GLView* openGLView = this->_director->getOpenGLView();
  30. Size frameSize = openGLView->getFrameSize();
  31. Size designSize = Size(960,640);
  32.  
  33. Size nodeSize = Size(0,0);
  34.  
  35. bool frameSizeWidthLarger = (designSize.width / designSize.height) < (frameSize.width / frameSize.height );
  36. if(frameSizeWidthLarger)
  37. {
  38. nodeSize.height = designSize.height;
  39. nodeSize.width = nodeSize.height * (frameSize.width / frameSize.height);
  40. }
  41. else
  42. {
  43. nodeSize.width = designSize.width;
  44. nodeSize.height = nodeSize.width * (frameSize.height / frameSize.width);
  45. }
  46. _node->setPosition(ccp((nodeSize.width - designSize.width) / 2,(nodeSize.height - designSize.height) / 2));
  47.  
  48. auto _afterDrawListener = EventListenerCustom::create(Director::EVENT_AFTER_VISIT,[this](EventCustom* event) {
  49. this->visit(this->_director->getRenderer(),Mat4::IDENTITY,0);
  50. });
  51. auto eventDispatcher = Director::getInstance()->getEventDispatcher();
  52. eventDispatcher->addEventListenerWithFixedPriority(_afterDrawListener,1);
  53.  
  54. return true;
  55. }
  56.  
  57. void ScreenMatchLayer::onBeforeVisit()
  58. {
  59. GLView* openGLView = this->_director->getOpenGLView();
  60. Size frameSize = openGLView->getFrameSize();
  61. glViewport((GLint)(0),(GLint)(0),(GLsizei)(frameSize.width),(GLsizei)(frameSize.height));
  62. }
  63.  
  64. void ScreenMatchLayer::onAfterVisit()
  65. {
  66. GLView* openGLView = this->_director->getOpenGLView();
  67. Size designSize = openGLView->getDesignResolutionSize();
  68. openGLView->setViewPortInPoints(0,designSize.width,designSize.height);
  69. }
  70.  
  71. void ScreenMatchLayer::visit(Renderer *renderer,uint32_t parentFlags)
  72. {
  73. if(!_visible)
  74. return;
  75.  
  76. _beforeVisit.init(_globalZOrder);
  77. _beforeVisit.func = CC_CALLBACK_0(ScreenMatchLayer::onBeforeVisit,this);
  78. renderer->addCommand(&_beforeVisit);
  79.  
  80. Node::visit(renderer,parentTransform,parentFlags);
  81. _afterVisit.init(_globalZOrder);
  82. _afterVisit.func = CC_CALLBACK_0(ScreenMatchLayer::onAfterVisit,this);
  83. renderer->addCommand(&_afterVisit);
  84. }
  85.  
  86. ScreenMatchLayer * ScreenMatchLayer::create()
  87. {
  88. ScreenMatchLayer *pRet = new ScreenMatchLayer();
  89. if ( pRet && pRet->init() ) {
  90. pRet->autorelease();
  91. return pRet;
  92. }
  93. CC_SAFE_DELETE(pRet);
  94. return nullptr;
  95. }
  96.  
  97. int ScreenMatchLayer::lua_bind_AllFunction(lua_State* tolua_S)
  98. {
  99. lua_getglobal(tolua_S,"_G");
  100. if (lua_istable(tolua_S,-1))//stack:...,_G,{
  101. tolua_open(tolua_S);
  102. tolua_module(tolua_S,classPrefix.c_str(),0);
  103. tolua_beginmodule(tolua_S,classPrefix.c_str());
  104.  
  105. tolua_usertype(tolua_S,ScreenMatchLayer::fullName.c_str());
  106. tolua_cclass(tolua_S,className.c_str(),ScreenMatchLayer::fullName.c_str(),"cc.Layer",nullptr);
  107.  
  108. tolua_beginmodule(tolua_S,className.c_str());
  109. tolua_function(tolua_S,"create",lua_cocos2dx_ScreenMatchLayer_create);
  110. tolua_endmodule(tolua_S);
  111. g_luaType[typeid(ScreenMatchLayer).name()] = ScreenMatchLayer::fullName;
  112. g_typeCast[className] = ScreenMatchLayer::fullName;
  113. tolua_endmodule(tolua_S);
  114. }
  115. lua_pop(tolua_S,1);
  116. return 1;
  117. }
  118.  
  119. int lua_cocos2dx_ScreenMatchLayer_create(lua_State* tolua_S)
  120. {
  121. int argc = 0;
  122. ScreenMatchLayer* parentOfFunction = nullptr;
  123. const std::string &functionString = "'lua_cocos2dx_ScreenMatchLayer_create'";
  124. const std::string &luaFunctionString = ScreenMatchLayer::fullName + ":create";
  125.  
  126. #if COCOS2D_DEBUG >= 1
  127. tolua_Error tolua_err;
  128. if (!tolua_isusertable(tolua_S,1,&tolua_err))
  129. {
  130. tolua_error(tolua_S,("#ferror in function " + functionString).c_str(),&tolua_err);
  131. return 0;
  132. }
  133.  
  134. parentOfFunction = (ScreenMatchLayer*)tolua_tousertype(tolua_S,0);
  135. if (!parentOfFunction)
  136. {
  137. //tolua_error(tolua_S,("invalid 'cobj' in function " + functionString).c_str(),nullptr);
  138. //return 0;
  139. }
  140. #endif
  141.  
  142. argc = lua_gettop(tolua_S) - 1;
  143.  
  144. if (argc == 0)
  145. {
  146. ScreenMatchLayer* ret = ScreenMatchLayer::create();
  147. object_to_luaval<ScreenMatchLayer>(tolua_S,(ScreenMatchLayer*)ret);
  148. return 1;
  149. }
  150. else
  151. {luaL_error(tolua_S,"%s has wrong number of arguments: %d,was expecting %d \n",luaFunctionString.c_str(),argc,1);}
  152. return 0;
  153.  
  154. }

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