Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix window size #1891

Merged
merged 8 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cocos/platform/CCApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ class CC_DLL Application
@return Current language iso 639-1 code.
*/
std::string getCurrentLanguageCode() const;

const cocos2d::Vec2& getViewSize() const;
void updateViewSize(int width, int height);

/**
@brief Get current display stats.
Expand Down Expand Up @@ -204,6 +207,8 @@ class CC_DLL Application
bool _multiTouch = false;
bool _isStarted = false;
bool _isDownsampleEnabled = false;

cocos2d::Vec2 _viewSize;
};

// end of platform group
Expand Down
23 changes: 23 additions & 0 deletions cocos/platform/android/CCApplication-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ THE SOFTWARE.
#include "platform/CCApplication.h"
#include <EGL/egl.h>
#include <cstring>
#include <jni.h>
#include "platform/android/jni/JniImp.h"
#include "platform/android/CCGL-android.h"
#include "base/CCScheduler.h"
Expand All @@ -52,6 +53,22 @@ PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;

NS_CC_BEGIN

void Application::updateViewSize(int width, int height)
{
_viewSize.x = width;
_viewSize.y = height;
}

extern "C" {
void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeOnSizeChanged(JNIEnv * env, jobject obj, jint width, jint height) {
auto inst = Application::getInstance();
// nativeOnSizeChanged is firstly called before Application initiating.
if (inst != nullptr) {
inst->updateViewSize(width, height);
}
}
}

Application* Application::_instance = nullptr;
std::shared_ptr<Scheduler> Application::_scheduler = nullptr;

Expand All @@ -67,6 +84,7 @@ Application::Application(const std::string& name, int width, int height)
PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArraysOES");

_renderTexture = new RenderTexture(width, height);
updateViewSize(width, height);
}

Application::~Application()
Expand Down Expand Up @@ -266,4 +284,9 @@ std::string Application::getSystemVersion()
return getSystemVersionJNI();
}

const cocos2d::Vec2& Application::getViewSize() const
{
return _viewSize;
}

NS_CC_END
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView {
private Cocos2dxRenderer mCocos2dxRenderer;
private boolean mStopHandleTouchAndKeyEvents = false;

public static native void nativeOnSizeChanged(int width, int height);

// ===========================================================
// Constructors
// ===========================================================
Expand Down Expand Up @@ -250,6 +252,7 @@ public void run() {
protected void onSizeChanged(final int pNewSurfaceWidth, final int pNewSurfaceHeight, final int pOldSurfaceWidth, final int pOldSurfaceHeight) {
if(!this.isInEditMode()) {
this.mCocos2dxRenderer.setScreenWidthAndHeight(pNewSurfaceWidth, pNewSurfaceHeight);
nativeOnSizeChanged(pNewSurfaceWidth, pNewSurfaceHeight);
}
}

Expand Down
2 changes: 2 additions & 0 deletions cocos/platform/desktop/CCGLView-desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ GLView::GLView(Application* application, const std::string& name, int x, int y,
computeScale();

glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_mainFBO);
Application::getInstance()->updateViewSize(width, height);
}

GLView::~GLView()
Expand Down Expand Up @@ -443,6 +444,7 @@ void GLView::onGLFWWindowIconifyCallback(GLFWwindow* /*window*/, int iconified)

void GLView::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
{
Application::getInstance()->updateViewSize(width, height);
EventDispatcher::dispatchResizeEvent(width, height);
}

Expand Down
31 changes: 17 additions & 14 deletions cocos/platform/ios/CCApplication-ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,17 @@ of this software and associated documentation files (the "Software"), to deal

namespace
{
cocos2d::Vec2 getResolution()
{
CGRect bounds = [UIScreen mainScreen].bounds;
float scale = [[UIScreen mainScreen] scale];
float width = bounds.size.width * scale;
float height = bounds.size.height * scale;

return cocos2d::Vec2(width, height);
}

bool setCanvasCallback(se::Object* global)
{
cocos2d::Vec2 resolution = getResolution();
auto &viewSize = cocos2d::Application::getInstance()->getViewSize();
se::ScriptEngine* se = se::ScriptEngine::getInstance();
uint8_t devicePixelRatio = cocos2d::Application::getInstance()->getDevicePixelRatio();
char commandBuf[200] = {0};
sprintf(commandBuf, "window.innerWidth = %d; window.innerHeight = %d;",
(int)(resolution.x / devicePixelRatio),
(int)(resolution.y / devicePixelRatio));
(int)(viewSize.x / devicePixelRatio),
(int)(viewSize.y / devicePixelRatio));
se->evalString(commandBuf);
cocos2d::ccViewport(0, 0, resolution.x / devicePixelRatio, resolution.y / devicePixelRatio);
cocos2d::ccViewport(0, 0, viewSize.x / devicePixelRatio, viewSize.y / devicePixelRatio);
glDepthMask(GL_TRUE);
return true;
}
Expand Down Expand Up @@ -219,6 +209,8 @@ -(void) doCaller: (id) sender
EventDispatcher::init();

_delegate = [[MainLoop alloc] initWithApplication:this];

updateViewSize(width, height);
}

Application::~Application()
Expand All @@ -245,6 +237,17 @@ -(void) doCaller: (id) sender
Application::_instance = nullptr;
}

const cocos2d::Vec2& Application::getViewSize() const
{
return _viewSize;
}

void Application::updateViewSize(int width, int height)
{
_viewSize.x = width;
_viewSize.y = height;
}
minggo marked this conversation as resolved.
Show resolved Hide resolved

void Application::start()
{
if (_delegate)
Expand Down
11 changes: 11 additions & 0 deletions cocos/platform/mac/CCApplication-mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ bool setCanvasCallback(se::Object* global)
Application::_instance = nullptr;
}

const cocos2d::Vec2& Application::getViewSize() const
{
return _viewSize;
}

void Application::updateViewSize(int width, int height)
{
_viewSize.x = width;
_viewSize.y = height;
}

void Application::start()
{
if (!_view)
Expand Down
11 changes: 11 additions & 0 deletions cocos/platform/win32/CCApplication-win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ Application::~Application()
Application::_instance = nullptr;
}

const cocos2d::Vec2& Application::getViewSize() const
{
return _viewSize;
}

void Application::updateViewSize(int width, int height)
{
_viewSize.x = width;
_viewSize.y = height;
}

void Application::start()
{
if (!_view)
Expand Down
12 changes: 7 additions & 5 deletions cocos/renderer/renderer/ForwardRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "Light.h"
#include <algorithm>

#include "CCApplication.h"

#include "math/MathUtil.h"

RENDERER_BEGIN
Expand Down Expand Up @@ -72,8 +74,6 @@ ForwardRenderer::~ForwardRenderer()
bool ForwardRenderer::init(DeviceGraphics* device, std::vector<ProgramLib::Template>& programTemplates, Texture2D* defaultTexture, int width, int height)
{
BaseRenderer::init(device, programTemplates, defaultTexture);
_width = width;
_height = height;
registerStage("opaque", std::bind(&ForwardRenderer::opaqueStage, this, std::placeholders::_1, std::placeholders::_2));
registerStage("shadowcast", std::bind(&ForwardRenderer::shadowStage, this, std::placeholders::_1, std::placeholders::_2));
registerStage("transparent", std::bind(&ForwardRenderer::transparentStage, this, std::placeholders::_1, std::placeholders::_2));
Expand All @@ -92,10 +92,11 @@ void ForwardRenderer::render(Scene* scene)
updateLights(scene);
scene->sortCameras();
auto& cameras = scene->getCameras();
auto &viewSize = Application::getInstance()->getViewSize();
for (auto& camera : cameras)
{
View* view = requestView();
camera->extractView(*view, _width, _height);
camera->extractView(*view, viewSize.x, viewSize.y);
}

for (size_t i = 0, len = _views->getLength(); i < len; ++i) {
Expand All @@ -109,8 +110,9 @@ void ForwardRenderer::render(Scene* scene)
void ForwardRenderer::renderCamera(Camera* camera, Scene* scene)
{
reset();
int width = _width;
int height = _height;
auto &viewSize = Application::getInstance()->getViewSize();
int width = viewSize.x;
int height = viewSize.y;
FrameBuffer* fb = camera->getFrameBuffer();
if (nullptr != fb) {
width = fb->getWidth();
Expand Down
3 changes: 0 additions & 3 deletions cocos/renderer/renderer/ForwardRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ class ForwardRenderer final : public BaseRenderer
Vector<Light*> _ambientLights;

RecyclePool<float>* _arrayPool = nullptr;

int _width = 0;
int _height = 0;
std::size_t _numLights = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,24 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[window makeKeyAndVisible];

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarOrientationChanged:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

//run the cocos2d-x game scene
app->start();

return YES;
}

- (void)statusBarOrientationChanged:(NSNotification *)notification {
CGRect bounds = [UIScreen mainScreen].bounds;
float scale = [[UIScreen mainScreen] scale];
float width = bounds.size.width * scale;
float height = bounds.size.height * scale;
Application::getInstance()->updateViewSize(width, height);
}

- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,24 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[window makeKeyAndVisible];

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarOrientationChanged:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

//run the cocos2d-x game scene
app->start();

return YES;
}

- (void)statusBarOrientationChanged:(NSNotification *)notification {
CGRect bounds = [UIScreen mainScreen].bounds;
float scale = [[UIScreen mainScreen] scale];
float width = bounds.size.width * scale;
float height = bounds.size.height * scale;
Application::getInstance()->updateViewSize(width, height);
}

- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,22 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[window makeKeyAndVisible];

[[UIApplication sharedApplication] setStatusBarHidden: YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarOrientationChanged:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

app->start();
return YES;
}

- (void)statusBarOrientationChanged:(NSNotification *)notification {
CGRect bounds = [UIScreen mainScreen].bounds;
float scale = [[UIScreen mainScreen] scale];
float width = bounds.size.width * scale;
float height = bounds.size.height * scale;
Application::getInstance()->updateViewSize(width, height);
}

minggo marked this conversation as resolved.
Show resolved Hide resolved

- (void)applicationWillResignActive:(UIApplication *)application {
/*
Expand Down