From 13aa3ad2b5b7630f74f0a4961ea8922409df943f Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Sat, 8 Feb 2014 12:35:11 -0800 Subject: [PATCH] Adds new commands to Console * resolution * projection --- cocos/2d/platform/CCGLViewProtocol.cpp | 10 ++- cocos/2d/platform/CCGLViewProtocol.h | 7 ++ cocos/base/CCConsole.cpp | 88 ++++++++++++++++++++++++++ cocos/base/CCConsole.h | 2 + tests/test-cpp/Classes/VisibleRect.cpp | 37 +++++++++-- 5 files changed, 136 insertions(+), 8 deletions(-) diff --git a/cocos/2d/platform/CCGLViewProtocol.cpp b/cocos/2d/platform/CCGLViewProtocol.cpp index 0c0a6277e6..a763e906b8 100644 --- a/cocos/2d/platform/CCGLViewProtocol.cpp +++ b/cocos/2d/platform/CCGLViewProtocol.cpp @@ -151,7 +151,15 @@ void GLViewProtocol::setFrameSize(float width, float height) _designResolutionSize = _screenSize = Size(width, height); } -Size GLViewProtocol::getVisibleSize() const +Rect GLViewProtocol::getVisibleRect() const +{ + Rect ret; + ret.size = getVisibleSize(); + ret.origin = getVisibleOrigin(); + return ret; +} + +Size GLViewProtocol::getVisibleSize() const { if (_resolutionPolicy == ResolutionPolicy::NO_BORDER) { diff --git a/cocos/2d/platform/CCGLViewProtocol.h b/cocos/2d/platform/CCGLViewProtocol.h index 63ab1fa27a..d4dbe92889 100644 --- a/cocos/2d/platform/CCGLViewProtocol.h +++ b/cocos/2d/platform/CCGLViewProtocol.h @@ -117,6 +117,11 @@ public: */ virtual Point getVisibleOrigin() const; + /** + * Get the visible rectangle of opengl viewport. + */ + virtual Rect getVisibleRect() const; + /** * Set the design resolution size. * @param width Design resolution width. @@ -177,6 +182,8 @@ public: */ float getScaleY() const; + /** returns the current Resolution policy */ + ResolutionPolicy getResolutionPolicy() const { return _resolutionPolicy; } protected: void handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[]); diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index fe23b06218..eab3448924 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -56,6 +57,7 @@ #include "platform/CCFileUtils.h" #include "CCConfiguration.h" #include "CCTextureCache.h" +#include "CCGLView.h" NS_CC_BEGIN @@ -246,6 +248,8 @@ Console::Console() } } }, { "help", "Print this message", std::bind(&Console::commandHelp, this, std::placeholders::_1, std::placeholders::_2) }, + { "projection", "[2d | 3d] Changes or print the current projection", std::bind(&Console::commandProjection, this, std::placeholders::_1, std::placeholders::_2) }, + { "resolution", "[width height | ] Changes or print the window resolution", std::bind(&Console::commandResolution, this, std::placeholders::_1, std::placeholders::_2) }, { "scene graph", "Print the scene graph", std::bind(&Console::commandSceneGraph, this, std::placeholders::_1, std::placeholders::_2) }, { "texture", "[flush | ] Flush or print the TextureCache info", std::bind(&Console::commandTextures, this, std::placeholders::_1, std::placeholders::_2) }, }; @@ -422,6 +426,90 @@ void Console::commandConfig(int fd, const std::string& args) ); } +void Console::commandResolution(int fd, const std::string& args) +{ + if(args.length()==0) { + auto director = Director::getInstance(); + Size points = director->getWinSize(); + Size pixels = director->getWinSizeInPixels(); + auto glview = director->getOpenGLView(); + Size design = glview->getDesignResolutionSize(); + ResolutionPolicy res = glview->getResolutionPolicy(); + Rect visibleRect = glview->getVisibleRect(); + + mydprintf(fd, "Window Size:\n" + "\t%d x %d (points)\n" + "\t%d x %d (pixels)\n" + "\t%d x %d (design resolution)\n" + "Resolution Policy: %d\n" + "Visible Rect:\n" + "\torigin: %d x %d\n" + "\tsize: %d x %d", + (int)points.width, (int)points.height, + (int)pixels.width, (int)pixels.height, + (int)design.width, (int)design.height, + (int)res, + (int)visibleRect.origin.x, (int)visibleRect.origin.y, + (int)visibleRect.size.width, (int)visibleRect.size.height + ); + + } else { + int width, height, policy; + + std::istringstream stream( args ); + stream >> width >> height>> policy; + + Scheduler *sched = Director::getInstance()->getScheduler(); + sched->performFunctionInCocosThread( [&](){ + Director::getInstance()->getOpenGLView()->setDesignResolutionSize(width, height, static_cast(policy)); + } ); + } +} + +void Console::commandProjection(int fd, const std::string& args) +{ + auto director = Director::getInstance(); + Scheduler *sched = director->getScheduler(); + + if(args.length()==0) + { + char buf[20]; + auto proj = director->getProjection(); + switch (proj) { + case cocos2d::Director::Projection::_2D: + sprintf(buf,"2d"); + break; + case cocos2d::Director::Projection::_3D: + sprintf(buf,"3d"); + break; + case cocos2d::Director::Projection::CUSTOM: + sprintf(buf,"custom"); + break; + + default: + sprintf(buf,"unknown"); + break; + } + mydprintf(fd, "Current projection: %s", buf); + } + else if( args.compare("2d") == 0) + { + sched->performFunctionInCocosThread( [&](){ + director->setProjection(Director::Projection::_2D); + } ); + } + else if( args.compare("3d") == 0) + { + sched->performFunctionInCocosThread( [&](){ + director->setProjection(Director::Projection::_3D); + } ); + } + else + { + mydprintf(fd, "Unsupported argument: '%s'. Supported arguments: '2d' or '3d'", args.c_str()); + } +} + void Console::commandTextures(int fd, const std::string& args) { Scheduler *sched = Director::getInstance()->getScheduler(); diff --git a/cocos/base/CCConsole.h b/cocos/base/CCConsole.h index 7e9f81c9f6..bb078d076a 100644 --- a/cocos/base/CCConsole.h +++ b/cocos/base/CCConsole.h @@ -112,6 +112,8 @@ protected: void commandFileUtils(int fd, const std::string &args); void commandConfig(int fd, const std::string &args); void commandTextures(int fd, const std::string &args); + void commandResolution(int fd, const std::string &args); + void commandProjection(int fd, const std::string &args); // file descriptor: socket, console, etc. int _listenfd; diff --git a/tests/test-cpp/Classes/VisibleRect.cpp b/tests/test-cpp/Classes/VisibleRect.cpp index 94a494a02e..1add93eaec 100644 --- a/tests/test-cpp/Classes/VisibleRect.cpp +++ b/tests/test-cpp/Classes/VisibleRect.cpp @@ -1,21 +1,44 @@ +/**************************************************************************** + Copyright (c) 2008-2010 Ricardo Quesada + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2011 Zynga Inc. + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + #include "VisibleRect.h" Rect VisibleRect::s_visibleRect; void VisibleRect::lazyInit() { - if (s_visibleRect.size.width == 0.0f && s_visibleRect.size.height == 0.0f) - { - auto glView = Director::getInstance()->getOpenGLView(); - s_visibleRect.origin = glView->getVisibleOrigin(); - s_visibleRect.size = glView->getVisibleSize(); - } +// if (s_visibleRect.size.width == 0.0f && s_visibleRect.size.height == 0.0f) + s_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect(); } Rect VisibleRect::getVisibleRect() { lazyInit(); - return Rect(s_visibleRect.origin.x, s_visibleRect.origin.y, s_visibleRect.size.width, s_visibleRect.size.height); + return s_visibleRect; } Point VisibleRect::left()