我们知道,cocos2dx 中屏幕适配的设置方法是
Director::getInstance()->getopenGLVIEw()->setDesignResolutionSize(960,640,kResolutionShowAll);
为了保持我们的游戏不被拉伸,选择showAll方法。但是有一个问题,showAll会留黑边,那么我们只需要在openGL中渲染黑边即可。这样黑边就会被填充为我们自己设置的图片。话不多说,看看下面的代码。
MainScene.lua调用示例如下,只需要在onEnter里面渲染即可
function MainScene:onEnter() local function createSpriteWithPathPosScale(path,pos,scale) -- body local sprite = cc.Sprite:create(path) sprite : setAnchorPoint(cc.p(0,0)) sprite : setposition(pos) sprite : setScale(scale) return sprite end local layer = cp.ScreenMatchLayer : create() layer:retain() local sprite = createSpriteWithPathPosScale("ui_img_left.jpg",cc.p(0 - 88,0),1) layer : getChildByname("_node") : addChild(sprite) sprite = createSpriteWithPathPosScale("ui_img_left.jpg",cc.p(960,-1) layer : getChildByname("_node") : addChild(sprite) sprite = createSpriteWithPathPosScale("ui_img_top.jpg",cc.p(480,640 + 32.5),1) layer : getChildByname("_node") : addChild(sprite) sprite = createSpriteWithPathPosScale("ui_img_top.jpg",-480-32.5),-1) layer : getChildByname("_node") : addChild(sprite)end
这里面对ScreenMatchLayer进行注册以便被lua调用
bool AppDelegate::applicationDIDFinishLaunching(){ // set default FPS Director::getInstance()->setAnimationInterval(1.0 / 60.0f); // register lua module auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); lua_State* L = engine->getLuaStack()->getLuaState(); lua_module_register(L); ScreenMatchLayer::lua_bind_AllFunction(L); // If you want to use Quick-Cocos2d-X,please uncomment below code // register_all_quick_manual(L); LuaStack* stack = engine->getLuaStack(); stack->setXXTEAKeyAndSign("2dxLua",strlen("2dxLua"),"XXTEA",strlen("XXTEA")); //register custom function //LuaStack* stack = engine->getLuaStack(); //register_custom_function(stack->getLuaState()); #if (COCOS2D_DEBUG > 0) && (CC_CODE_IDE_DEBUG_SUPPORT > 0) // NOTE:Please don't remove this call if you want to deBUG with Cocos Code IDE RuntimeEngine::getInstance()->start(); cocos2d::log("iShow!");#else if (engine->executeScriptfile("src/main.lua")) { return false; }#endif return true;}
ScreenMatchLayer.h
#ifndef __SCREENMATCHLAYER_H__#define __SCREENMATCHLAYER_H__#include "cocos2d.h"namespace cocos2d{ class ScreenMatchLayer : public Layer { public: ScreenMatchLayer(voID); ~ScreenMatchLayer(voID); static ScreenMatchLayer* create(); bool init(); voID visit(Renderer *renderer,const Mat4& parenttransform,uint32_t parentFlags); voID onBeforeVisit(); voID onAfterVisit(); static int lua_bind_AllFunction(lua_State* tolua_S); static const std::string classprefix; static const std::string fullname; static const std::string classname; private: Node* _node; CustomCommand _beforeVisit; CustomCommand _afterVisit; };}int lua_cocos2dx_ScreenMatchLayer_create(lua_State* tolua_S);#endif
ScreenMatchLayer.cpp
#include "ScreenMatchLayer.h"#include "tolua_fix.h"#include "LuaBasicConversions.h"using namespace cocos2d;const std::string ScreenMatchLayer::classprefix = "cp";const std::string ScreenMatchLayer::classname = "ScreenMatchLayer";const std::string ScreenMatchLayer::fullname = classprefix + "." + classname;ScreenMatchLayer::ScreenMatchLayer(voID){}ScreenMatchLayer::~ScreenMatchLayer(voID){}bool ScreenMatchLayer::init(){ if ( !Layer::init() ) { return false; } _node = Node::create(); _node->setname("_node"); _node->setAnchorPoint(ccp(0,0)); addChild(_node); GLVIEw* openGLVIEw = this->_director->getopenGLVIEw(); Size frameSize = openGLVIEw->getFrameSize(); Size designSize = Size(960,640); Size nodeSize = Size(0,0); bool frameSizeWIDthLarger = (designSize.wIDth / designSize.height) < (frameSize.wIDth / frameSize.height ); if(frameSizeWIDthLarger) { nodeSize.height = designSize.height; nodeSize.wIDth = nodeSize.height * (frameSize.wIDth / frameSize.height); } else { nodeSize.wIDth = designSize.wIDth; nodeSize.height = nodeSize.wIDth * (frameSize.height / frameSize.wIDth); } _node->setposition(ccp((nodeSize.wIDth - designSize.wIDth) / 2,(nodeSize.height - designSize.height) / 2)); auto _afterDrawListener = EventListenerCustom::create(Director::EVENT_AFTER_VISIT,[this](EventCustom* event) { this->visit(this->_director->getRenderer(),Mat4::IDENTITY,0); }); auto eventdispatcher = Director::getInstance()->getEventdispatcher(); eventdispatcher->addEventListenerWithFixedPriority(_afterDrawListener,1); return true;}voID ScreenMatchLayer::onBeforeVisit(){ GLVIEw* openGLVIEw = this->_director->getopenGLVIEw(); Size frameSize = openGLVIEw->getFrameSize(); glVIEwport((Glint)(0),(Glint)(0),(GLsizei)(frameSize.wIDth),(GLsizei)(frameSize.height));}voID ScreenMatchLayer::onAfterVisit(){ GLVIEw* openGLVIEw = this->_director->getopenGLVIEw(); Size designSize = openGLVIEw->getDesignResolutionSize(); openGLVIEw->setVIEwPortInPoints(0,designSize.wIDth,designSize.height);}voID ScreenMatchLayer::visit(Renderer *renderer,uint32_t parentFlags){ if(!_visible) return; _beforeVisit.init(_globalZOrder); _beforeVisit.func = CC_CALLBACK_0(ScreenMatchLayer::onBeforeVisit,this); renderer->addCommand(&_beforeVisit); Node::visit(renderer,parenttransform,parentFlags); _afterVisit.init(_globalZOrder); _afterVisit.func = CC_CALLBACK_0(ScreenMatchLayer::onAfterVisit,this); renderer->addCommand(&_afterVisit);}ScreenMatchLayer * ScreenMatchLayer::create(){ ScreenMatchLayer *pRet = new ScreenMatchLayer(); if ( pRet && pRet->init() ) { pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return nullptr;}int ScreenMatchLayer::lua_bind_AllFunction(lua_State* tolua_S){ lua_getglobal(tolua_S,"_G"); if (lua_istable(tolua_S,-1))//stack:...,_G,{ tolua_open(tolua_S); tolua_module(tolua_S,classprefix.c_str(),0); tolua_beginmodule(tolua_S,classprefix.c_str()); tolua_usertype(tolua_S,ScreenMatchLayer::fullname.c_str()); tolua_cclass(tolua_S,classname.c_str(),ScreenMatchLayer::fullname.c_str(),"cc.Layer",nullptr); tolua_beginmodule(tolua_S,classname.c_str()); tolua_function(tolua_S,"create",lua_cocos2dx_ScreenMatchLayer_create); tolua_endmodule(tolua_S); g_luaType[typeID(ScreenMatchLayer).name()] = ScreenMatchLayer::fullname; g_typeCast[classname] = ScreenMatchLayer::fullname; tolua_endmodule(tolua_S); } lua_pop(tolua_S,1); return 1;}int lua_cocos2dx_ScreenMatchLayer_create(lua_State* tolua_S){ int argc = 0; ScreenMatchLayer* parentOfFunction = nullptr; const std::string &functionString = "'lua_cocos2dx_ScreenMatchLayer_create'"; const std::string &luaFunctionString = ScreenMatchLayer::fullname + ":create";#if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; if (!tolua_isusertable(tolua_S,1,&tolua_err)) { tolua_error(tolua_S,("#ferror in function " + functionString).c_str(),&tolua_err); return 0; } parentOfFunction = (ScreenMatchLayer*)tolua_tousertype(tolua_S,0); if (!parentOfFunction) { //tolua_error(tolua_S,("invalID 'cobj' in function " + functionString).c_str(),nullptr); //return 0; }#endif argc = lua_gettop(tolua_S) - 1; if (argc == 0) { ScreenMatchLayer* ret = ScreenMatchLayer::create(); object_to_luaval<ScreenMatchLayer>(tolua_S,(ScreenMatchLayer*)ret); return 1; } else {luaL_error(tolua_S,"%s has wrong number of arguments: %d,was expecting %d \n",luaFunctionString.c_str(),argc,1);} return 0;}总结
以上是内存溢出为你收集整理的cocos2d-x lua 屏幕适配问题(OpenGL调用),版本号(cocos2dx v3.4)全部内容,希望文章能够帮你解决cocos2d-x lua 屏幕适配问题(OpenGL调用),版本号(cocos2dx v3.4)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)