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 4 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
2 changes: 2 additions & 0 deletions cocos/platform/CCApplication.h
Expand Up @@ -137,6 +137,8 @@ class CC_DLL Application
@return Current language iso 639-1 code.
*/
std::string getCurrentLanguageCode() const;

cocos2d::Vec2 getResolution();

/**
@brief Get current display stats.
Expand Down
22 changes: 22 additions & 0 deletions cocos/platform/android/CCApplication-android.cpp
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

// _resolution can't be private member of Application
// because _resolution updated before initiating Application
cocos2d::Vec2 _resolution;

void updateResolution(int width, int height)
{
_resolution.x = width;
_resolution.y = height;
}

extern "C" {
void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeOnSizeChanged(JNIEnv * env, jobject obj, jint width, jint height) {
updateResolution(width, height);
}
}

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

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

cocos2d::Vec2 Application::getResolution()
{
return _resolution;
}

NS_CC_END
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
5 changes: 5 additions & 0 deletions cocos/platform/desktop/CCGLView-desktop.cpp
Expand Up @@ -215,6 +215,11 @@ float GLView::getScale() const
return _scale;
}

void GLView::getWindowSize(int &width, int &height)
{
glfwGetWindowSize(_mainWindow, &width, &height);
}

GLint GLView::getMainFBO() const
{
return _mainFBO;
Expand Down
1 change: 1 addition & 0 deletions cocos/platform/desktop/CCGLView-desktop.h
Expand Up @@ -65,6 +65,7 @@ class CC_DLL GLView final
void pollEvents();
void swapBuffers();
float getScale() const;
void getWindowSize(int &width, int &height);
GLint getMainFBO() const;
void setIsEditboxEditing(bool value);

Expand Down
22 changes: 11 additions & 11 deletions cocos/platform/ios/CCApplication-ios.mm
Expand Up @@ -39,19 +39,9 @@ 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();
cocos2d::Vec2 resolution = cocos2d::Application::getInstance()->getResolution();
se::ScriptEngine* se = se::ScriptEngine::getInstance();
uint8_t devicePixelRatio = cocos2d::Application::getInstance()->getDevicePixelRatio();
char commandBuf[200] = {0};
Expand Down Expand Up @@ -245,6 +235,16 @@ -(void) doCaller: (id) sender
Application::_instance = nullptr;
}

cocos2d::Vec2 Application::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);
}
minggo marked this conversation as resolved.
Show resolved Hide resolved

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

cocos2d::Vec2 Application::getResolution() {
auto res = Vec2();
int width, height;
CAST_VIEW(_view)->getWindowSize(width, height);
minggo marked this conversation as resolved.
Show resolved Hide resolved
res.x = width;
res.y = height;
return res;
}

void Application::start()
{
if (!_view)
Expand Down
9 changes: 9 additions & 0 deletions cocos/platform/win32/CCApplication-win32.cpp
Expand Up @@ -134,6 +134,15 @@ Application::~Application()
Application::_instance = nullptr;
}

cocos2d::Vec2 Application::getResolution() {
auto res = Vec2();
int width, height;
CAST_VIEW(_view)->getWindowSize(width, height);
res.x = width;
res.y = height;
return res;
}

void Application::start()
{
if (!_view)
Expand Down
12 changes: 7 additions & 5 deletions cocos/renderer/renderer/ForwardRenderer.cpp
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();
Vec2 res = Application::getInstance()->getResolution();
for (auto& camera : cameras)
{
View* view = requestView();
camera->extractView(*view, _width, _height);
camera->extractView(*view, res.x, res.y);
minggo marked this conversation as resolved.
Show resolved Hide resolved
}

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;
Vec2 res = Application::getInstance()->getResolution();
int width = res.x;
int height = res.y;
FrameBuffer* fb = camera->getFrameBuffer();
if (nullptr != fb) {
width = fb->getWidth();
Expand Down
3 changes: 0 additions & 3 deletions cocos/renderer/renderer/ForwardRenderer.h
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