adapt mac resolution (#1248)

This commit is contained in:
minggo
2018-05-18 11:57:12 +08:00
committed by GitHub
parent 7a2a9374ec
commit 8a76997242
13 changed files with 155 additions and 139 deletions
+46 -22
View File
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <cfloat>
#include <cassert>
#include "platform/CCApplication.h"
NS_CC_BEGIN
@@ -36,7 +37,6 @@ NS_CC_BEGIN
// #undef CC_ENABLE_GL_STATE_CACHE
// #define CC_ENABLE_GL_STATE_CACHE 0
#if CC_ENABLE_GL_STATE_CACHE
namespace
{
GLint __currentVertexBuffer = -1;
@@ -46,12 +46,10 @@ namespace
uint32_t __enabledVertexAttribArrayFlag = 0;
VertexAttributePointerInfo __enabledVertexAttribArrayInfo[MAX_ATTRIBUTE_UNIT];
}
#endif // CC_ENABLE_GL_STATE_CACHE
//FIXME: need to consider invoking this after restarting game.
void ccInvalidateStateCache()
{
#if CC_ENABLE_GL_STATE_CACHE
__currentVertexBuffer = -1;
__currentIndexBuffer = -1;
__currentVertexArray = -1;
@@ -59,7 +57,6 @@ void ccInvalidateStateCache()
__enabledVertexAttribArrayFlag = 0;
for (int i = 0; i < MAX_ATTRIBUTE_UNIT; ++i)
__enabledVertexAttribArrayInfo[i] = VertexAttributePointerInfo();
#endif
}
/****************************************************************************************
@@ -90,6 +87,9 @@ void ccBindBuffer(GLenum target, GLuint buffer)
glBindBuffer(target, buffer);
}
#else
// Should cache it, ccVertexAttribPointer depends on it.
__currentVertexBuffer = buffer;
glBindBuffer(target, buffer);
#endif
}
@@ -112,24 +112,12 @@ void ccDeleteBuffers(GLsizei n, const GLuint * buffers)
GLint ccGetBoundVertexBuffer()
{
#if CC_ENABLE_GL_STATE_CACHE
return __currentVertexBuffer;
#else
GLint VBO = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &VBO);
return VBO;
#endif
}
GLint ccGetBoundIndexBuffer()
{
#if CC_ENABLE_GL_STATE_CACHE
return __currentIndexBuffer;
#else
GLint VEO = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &VEO);
return VEO;
#endif
}
void ccBindVertexArray(GLuint VAO)
@@ -141,17 +129,14 @@ void ccBindVertexArray(GLuint VAO)
glBindVertexArray(VAO);
}
#else
__currentVertexArray = VAO;
glBindVertexArray(VAO);
#endif
}
GLint ccGetBoundVertexArray()
{
#if CC_ENABLE_GL_STATE_CACHE
return __currentVertexArray;
#else
return 0;
#endif
}
/****************************************************************************************
@@ -185,7 +170,7 @@ void ccDisableVertexAttribArray(GLuint index)
if (__enabledVertexAttribArrayFlag & flag)
{
glDisableVertexAttribArray(index);
__enabledVertexAttribArrayFlag &= !(1 << index);
__enabledVertexAttribArrayFlag &= ~(1 << index);
}
#else
glDisableVertexAttribArray(index);
@@ -194,6 +179,7 @@ void ccDisableVertexAttribArray(GLuint index)
void ccVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* pointer)
{
#if CC_ENABLE_GL_STATE_CACHE
assert(index < MAX_ATTRIBUTE_UNIT);
if (index >= MAX_ATTRIBUTE_UNIT)
return;
@@ -201,7 +187,7 @@ void ccVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean norm
// The index is not enabled, return.
if (! (__enabledVertexAttribArrayFlag & (1 << index)) )
return;
#endif
__enabledVertexAttribArrayInfo[index] = VertexAttributePointerInfo(__currentVertexBuffer, index, size, type, normalized, stride, pointer);
// FIXME: should check all the values to determine if need to invoke glVertexAttribPointer or not?
@@ -222,4 +208,42 @@ const VertexAttributePointerInfo* getVertexAttribPointerInfo(GLuint index)
return &__enabledVertexAttribArrayInfo[index];
}
/****************************************************************************************
Other functions.
***************************************************************************************/
void ccViewport(GLint x, GLint y, GLsizei width, GLsizei height)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
GLint currentFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFBO);
if (currentFBO == Application::getInstance()->getMainFBO())
{
float scale = Application::getInstance()->getScreenScale();
x *= scale;
y *= scale;
width *= scale;
height *= scale;
}
#endif
glViewport(x, y, width, height);
}
void ccScissor(GLint x, GLint y, GLsizei width, GLsizei height)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
GLint currentFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFBO);
if (currentFBO == Application::getInstance()->getMainFBO())
{
float scale = Application::getInstance()->getScreenScale();
x *= scale;
y *= scale;
width *= scale;
height *= scale;
}
#endif
glScissor(x, y, width, height);
}
NS_CC_END
+3
View File
@@ -38,6 +38,9 @@ GLint ccGetBoundIndexBuffer();
void ccBindVertexArray(GLuint VAO);
GLint ccGetBoundVertexArray();
void ccViewport(GLint x, GLint y, GLsizei width, GLsizei height);
void ccScissor(GLint x, GLint y, GLsizei width, GLsizei height);
struct VertexAttributePointerInfo
{
VertexAttributePointerInfo(GLuint VBO, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* pointer)
+4 -3
View File
@@ -30,6 +30,7 @@
#include "base/ccMacros.h"
#include "base/CCConfiguration.h"
#include "platform/CCPlatformConfig.h"
#include "platform/CCApplication.h"
NS_CC_BEGIN
@@ -98,7 +99,7 @@ void RenderTexture::init(int factor)
void RenderTexture::prepare()
{
glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
glViewport(0, 0, _width, _height);
ccViewport(0, 0, _width, _height);
}
void RenderTexture::draw()
@@ -121,7 +122,7 @@ void RenderTexture::draw()
glGetIntegerv(GL_CURRENT_PROGRAM, &prevProgram);
glBindFramebuffer(GL_FRAMEBUFFER, _mainFBO);
glViewport(0, 0, _deviceResolution.x, _deviceResolution.y);
ccViewport(0, 0, _deviceResolution.x, _deviceResolution.y);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
@@ -424,7 +425,7 @@ void RenderTexture::initVBOAndVAO()
void RenderTexture::initFramebuffer()
{
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_mainFBO);
_mainFBO = Application::getInstance()->getMainFBO();
glGenFramebuffers(1, &_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);
+10 -3
View File
@@ -147,6 +147,12 @@ public:
inline uint8_t getDevicePixelRatio() const { return _devicePixelRatio; }
inline bool isDownsampleEnabled() const { return _isDownsampleEnabled; }
/** The value is (framebuffer size) / (window size), but on iOS, it is special, its value is 1.
*/
float getScreenScale() const;
GLint getMainFBO() const;
/**
@brief Get target platform.
*/
@@ -162,11 +168,10 @@ public:
std::string getSystemVersion();
protected:
virtual void onCreateView(int& x, int& y, int& width, int& height,
PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount);
virtual void onCreateView(PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount);
private:
void createView(const std::string& name);
void createView(const std::string& name, int width, int height);
static Application* _instance;
@@ -181,6 +186,8 @@ private:
// The ratio to downsample, for example, if its value is 2,
// then the rendering size of render texture is device_resolution/2.
uint8_t _devicePixelRatio = 1;
GLint _mainFBO = 0;
};
// end of platform group
@@ -203,8 +203,17 @@ Application::Platform Application::getPlatform() const
return Platform::ANDROIDOS;
}
void Application::onCreateView(int& /*x*/, int& /*y*/, int& /*width*/, int& /*height*/,
PixelFormat& /*pixelformat*/, DepthFormat& /*depthFormat*/, int& /*multisamplingCount*/)
float Application::getScreenScale() const
{
return 1.f;
}
GLint Application::getMainFBO() const
{
return _mainFBO;
}
void Application::onCreateView(PixelFormat& /*pixelformat*/, DepthFormat& /*depthFormat*/, int& /*multisamplingCount*/)
{
}
@@ -179,6 +179,10 @@ GLView::GLView(Application* application, const std::string& name, int x, int y,
if(multisamplingCount > 0)
glEnable(GL_MULTISAMPLE);
computeScale();
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_mainFBO);
}
GLView::~GLView()
@@ -205,15 +209,14 @@ void GLView::swapBuffers()
glfwSwapBuffers(_mainWindow);
}
float GLView::getScaleFactor() const
float GLView::getScale() const
{
int widthInPixel = 0;
glfwGetFramebufferSize(_mainWindow, &widthInPixel, nullptr);
int width = 0;
glfwGetWindowSize(_mainWindow, &width, nullptr);
return float(widthInPixel) / width;
return _scale;
}
GLint GLView::getMainFBO() const
{
return _mainFBO;
}
void GLView::onGLFWError(int errorID, const char* errorDesc)
@@ -366,6 +369,17 @@ static bool glew_dynamic_binding()
}
#endif
void GLView::computeScale()
{
int widthInPixel = 0;
glfwGetFramebufferSize(_mainWindow, &widthInPixel, nullptr);
int width = 0;
glfwGetWindowSize(_mainWindow, &width, nullptr);
_scale = float(widthInPixel) / width;
}
// helper
bool GLView::initGlew()
{
@@ -45,10 +45,12 @@ public:
bool windowShouldClose() const;
void pollEvents();
void swapBuffers();
float getScaleFactor() const;
float getScale() const;
GLint getMainFBO() const;
private:
bool initGlew();
void computeScale();
// GLFW callbacks
void onGLFWError(int errorID, const char* errorDesc);
@@ -62,6 +64,7 @@ private:
GLFWwindow* _mainWindow = nullptr;
GLFWmonitor* _monitor = nullptr;
GLint _mainFBO = -1;
std::string _glfwError;
@@ -69,6 +72,9 @@ private:
float _mouseX = 0;
float _mouseY = 0;
// (framebuffer size) / (window size)
float _scale = 1.0f;
friend class GLFWEventHandler;
};
+20 -24
View File
@@ -24,16 +24,15 @@
****************************************************************************/
#import "CCApplication.h"
#import <UIKit/UIKit.h>
#include "base/CCScheduler.h"
#include "base/CCAutoreleasePool.h"
#include "base/CCGLUtils.h"
#include "renderer/gfx/DeviceGraphics.h"
#include "scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp"
#include "scripting/js-bindings/event/EventDispatcher.h"
#include "CCEAGLView-ios.h"
#import <UIKit/UIKit.h>
#include "base/CCGLUtils.h"
namespace
{
@@ -57,7 +56,7 @@ namespace
(int)(resolution.x / devicePixelRatio),
(int)(resolution.y / devicePixelRatio));
se->evalString(commandBuf);
glViewport(0, 0, resolution.x / devicePixelRatio, resolution.y / devicePixelRatio);
cocos2d::ccViewport(0, 0, resolution.x / devicePixelRatio, resolution.y / devicePixelRatio);
glDepthMask(GL_TRUE);
return true;
}
@@ -198,8 +197,9 @@ Application::Application(const std::string& name, int width, int height)
Application::_instance = this;
_scheduler = new Scheduler();
createView(name);
createView(name, width, height);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_mainFBO);
_renderTexture = new RenderTexture(width, height);
se::ScriptEngine::getInstance();
@@ -298,6 +298,16 @@ Application::Platform Application::getPlatform() const
return Platform::IPHONE;
}
float Application::getScreenScale() const
{
return 1.f;
}
GLint Application::getMainFBO() const
{
return _mainFBO;
}
bool Application::openURL(const std::string &url)
{
NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
@@ -328,14 +338,8 @@ void Application::setMultitouch(bool value)
}
}
void Application::onCreateView(int&x, int& y, int& width, int& height, PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount)
void Application::onCreateView(PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount)
{
CGRect bounds = [UIScreen mainScreen].bounds;
x = bounds.origin.x;
y = bounds.origin.y;
width = bounds.size.width;
height = bounds.size.height;
pixelformat = PixelFormat::RGB565;
depthFormat = DepthFormat::DEPTH24_STENCIL8;
@@ -361,27 +365,19 @@ namespace
}
}
void Application::createView(const std::string& /*name*/)
void Application::createView(const std::string& /*name*/, int width, int height)
{
int x = 0;
int y = 0;
int width = 0;
int height = 0;
PixelFormat pixelFormat = PixelFormat::RGB565;
DepthFormat depthFormat = DepthFormat::DEPTH24_STENCIL8;
int multisamplingCount = 0;
onCreateView(x,
y,
width,
height,
pixelFormat,
onCreateView(pixelFormat,
depthFormat,
multisamplingCount);
CGRect bounds;
bounds.origin.x = x;
bounds.origin.y = y;
bounds.origin.x = 0;
bounds.origin.y = 0;
bounds.size.width = width;
bounds.size.height = height;
+25 -34
View File
@@ -23,19 +23,17 @@ 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 "platform/CCApplication.h"
#import <Cocoa/Cocoa.h>
#import <algorithm>
#import <mutex>
#include <algorithm>
#include <mutex>
#include "base/CCScheduler.h"
#include "base/CCAutoreleasePool.h"
#include "base/CCGLUtils.h"
#include "platform/CCApplication.h"
#include "platform/desktop/CCGLView-desktop.h"
#include "scripting/js-bindings/event/EventDispatcher.h"
#include "renderer/gfx/DeviceGraphics.h"
#include "scripting/js-bindings/jswrapper/SeApi.h"
#include "base/CCGLUtils.h"
NS_CC_BEGIN
@@ -52,7 +50,7 @@ namespace
g_width / devicePixelRatio,
g_height / devicePixelRatio);
se->evalString(commandBuf);
glViewport(0, 0, g_width / devicePixelRatio, g_height / devicePixelRatio);
ccViewport(0, 0, g_width / devicePixelRatio, g_height / devicePixelRatio);
glDepthMask(GL_TRUE);
return true;
}
@@ -66,15 +64,14 @@ Application::Application(const std::string& name, int width, int height)
{
Application::_instance = this;
g_width = width;
g_height = height;
createView(name, width, height);
_renderTexture = new RenderTexture(width, height);
_scheduler = new Scheduler();
createView(name);
float scale = CAST_VIEW(_view)->getScaleFactor();
_renderTexture = new RenderTexture(width * scale, height * scale);
renderer::DeviceGraphics::setScaleFactor(scale);
EventDispatcher::init();
se::ScriptEngine::getInstance();
}
@@ -195,6 +192,16 @@ Application::LanguageType Application::getCurrentLanguage() const
return LanguageType::ENGLISH;
}
float Application::getScreenScale() const
{
return CAST_VIEW(_view)->getScale();
}
GLint Application::getMainFBO() const
{
return CAST_VIEW(_view)->getMainFBO();
}
bool Application::openURL(const std::string &url)
{
NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
@@ -219,41 +226,25 @@ void Application::setMultitouch(bool)
{
}
void Application::onCreateView(int&x, int& y, int& width, int& height, PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount)
void Application::onCreateView(PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount)
{
x = 0;
y = 0;
width = 960;
height = 640;
pixelformat = PixelFormat::RGBA8;
depthFormat = DepthFormat::DEPTH24_STENCIL8;
multisamplingCount = 0;
}
void Application::createView(const std::string& name)
void Application::createView(const std::string& name, int width, int height)
{
int x = 0;
int y = 0;
int width = 0;
int height = 0;
int multisamplingCount = 0;
PixelFormat pixelformat;
DepthFormat depthFormat;
onCreateView(x,
y,
width,
height,
pixelformat,
onCreateView(pixelformat,
depthFormat,
multisamplingCount);
_view = new GLView(this, name, x, y, width, height, pixelformat, depthFormat, multisamplingCount);
g_width = width;
g_height = height;
_view = new GLView(this, name, 0, 0, width, height, pixelformat, depthFormat, multisamplingCount);
}
std::string Application::getSystemVersion()
+2 -15
View File
@@ -55,8 +55,6 @@ namespace
}
} // namespace {
int DeviceGraphics::_scaleFactor = 1.0f;
DeviceGraphics* DeviceGraphics::getInstance()
{
static DeviceGraphics* __instance = nullptr;
@@ -66,11 +64,6 @@ DeviceGraphics* DeviceGraphics::getInstance()
return __instance;
}
void DeviceGraphics::setScaleFactor(float value)
{
_scaleFactor = value;
}
bool DeviceGraphics::supportGLExtension(const std::string& extension) const
{
return (_glExtensions && strstr(_glExtensions, extension.c_str() ) ) ? true : false;
@@ -137,10 +130,7 @@ void DeviceGraphics::setViewport(int x, int y, int w, int h)
_vy = y;
_vw = w;
_vh = h;
GL_CHECK(glViewport(_vx * _scaleFactor,
_vy * _scaleFactor,
_vw * _scaleFactor,
_vh * _scaleFactor));
GL_CHECK(ccViewport(_vx, _vy, _vw, _vh));
}
}
@@ -155,10 +145,7 @@ void DeviceGraphics::setScissor(int x, int y, int w, int h)
_sy = y;
_sw = w;
_sh = h;
glScissor(_sx * _scaleFactor,
_sy * _scaleFactor,
_sw * _scaleFactor,
_sh * _scaleFactor);
ccScissor(_sx, _sy, _sw, _sh);
}
}
+1 -5
View File
@@ -61,9 +61,7 @@ public:
};
static DeviceGraphics* getInstance();
static void setScaleFactor(float value);
inline const Capacity& getCapacity() const { return _caps; }
bool supportGLExtension(const std::string& extension) const;
@@ -163,8 +161,6 @@ private:
inline void commitVertexBuffer();
inline void commitTextures();
static int _scaleFactor;
int _vx;
int _vy;
int _vw;
@@ -1092,23 +1092,6 @@ static bool js_gfx_DeviceGraphics_enableStencilTest(se::State& s)
}
SE_BIND_FUNC(js_gfx_DeviceGraphics_enableStencilTest)
static bool js_gfx_DeviceGraphics_setScaleFactor(se::State& s)
{
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 1) {
float arg0 = 0;
ok &= seval_to_float(args[0], &arg0);
SE_PRECONDITION2(ok, false, "js_gfx_DeviceGraphics_setScaleFactor : Error processing arguments");
cocos2d::renderer::DeviceGraphics::setScaleFactor(arg0);
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
return false;
}
SE_BIND_FUNC(js_gfx_DeviceGraphics_setScaleFactor)
static bool js_gfx_DeviceGraphics_getInstance(se::State& s)
{
const auto& args = s.args();
@@ -1163,7 +1146,6 @@ bool js_register_gfx_DeviceGraphics(se::Object* obj)
cls->defineFunction("supportGLExtension", _SE(js_gfx_DeviceGraphics_supportGLExtension));
cls->defineFunction("setStencilOp", _SE(js_gfx_DeviceGraphics_setStencilOp));
cls->defineFunction("enableStencilTest", _SE(js_gfx_DeviceGraphics_enableStencilTest));
cls->defineStaticFunction("setScaleFactor", _SE(js_gfx_DeviceGraphics_setScaleFactor));
cls->defineStaticFunction("getInstance", _SE(js_gfx_DeviceGraphics_getInstance));
cls->install();
JSBClassType::registerClass<cocos2d::renderer::DeviceGraphics>(cls);
@@ -1983,7 +1983,7 @@ static bool JSB_glScissor(se::State& s) {
ok &= seval_to_int32(args[3], &arg3 );
SE_PRECONDITION2(ok, false, "Error processing arguments");
JSB_GL_CHECK(glScissor((GLint)arg0 , (GLint)arg1 , (GLsizei)arg2 , (GLsizei)arg3 ));
JSB_GL_CHECK(ccScissor((GLint)arg0 , (GLint)arg1 , (GLsizei)arg2 , (GLsizei)arg3 ));
return true;
}
@@ -2917,7 +2917,7 @@ static bool JSB_glViewport(se::State& s) {
ok &= seval_to_int32(args[3], &arg3 );
SE_PRECONDITION2(ok, false, "Error processing arguments");
JSB_GL_CHECK(glViewport((GLint)arg0 , (GLint)arg1, (GLsizei)arg2 , (GLsizei)arg3));
JSB_GL_CHECK(ccViewport((GLint)arg0 , (GLint)arg1, (GLsizei)arg2 , (GLsizei)arg3));
return true;
}
SE_BIND_FUNC(JSB_glViewport)
@@ -4164,7 +4164,7 @@ static bool JSB_glFlushCommand(se::State& s) {
}
else if (commandID == GL_COMMAND_SCISSOR) {
LOG_GL_COMMAND("Flush: SCISSOR\n");
JSB_GL_CHECK_VOID(glScissor((GLint)p[1], (GLint)p[2], (GLsizei)p[3], (GLsizei)p[4]));
JSB_GL_CHECK_VOID(ccScissor((GLint)p[1], (GLint)p[2], (GLsizei)p[3], (GLsizei)p[4]));
p += 5;
}
else if (commandID == GL_COMMAND_STENCIL_FUNC) {
@@ -4398,7 +4398,7 @@ static bool JSB_glFlushCommand(se::State& s) {
}
else if (commandID == GL_COMMAND_VIEW_PORT) {
LOG_GL_COMMAND("Flush: VIEW_PORT\n");
JSB_GL_CHECK_VOID(glViewport((GLint)p[1], (GLint)p[2], (GLsizei)p[3], (GLsizei)p[4]));
JSB_GL_CHECK_VOID(ccViewport((GLint)p[1], (GLint)p[2], (GLsizei)p[3], (GLsizei)p[4]));
p += 5;
}
else {