fix iOS resolution issue (#3066)

This commit is contained in:
minggo
2020-11-24 19:03:32 +08:00
committed by GitHub
parent f88b8e9c81
commit 8a1a318893
6 changed files with 48 additions and 62 deletions
@@ -856,8 +856,8 @@ static bool js_engine_Device_getDevicePixelRatio(se::State& s)
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 0) {
int result = cc::Device::getDevicePixelRatio();
ok &= int32_to_seval((int)result, &s.rval());
float result = cc::Device::getDevicePixelRatio();
ok &= float_to_seval(result, &s.rval());
SE_PRECONDITION2(ok, false, "js_engine_Device_getDevicePixelRatio : Error processing arguments");
return true;
}
+1 -1
View File
@@ -77,7 +77,7 @@ public:
/**
* Get device pixel ratio.
*/
static int getDevicePixelRatio();
static float getDevicePixelRatio();
/**
* To enable or disable accelerometer.
@@ -134,7 +134,7 @@ cc::Vec4 Device::getSafeAreaEdge()
return cc::Vec4();
}
int Device::getDevicePixelRatio()
float Device::getDevicePixelRatio()
{
return 1;
}
+2 -2
View File
@@ -36,12 +36,12 @@
namespace cc {
int Device::getDevicePixelRatio() {
float Device::getDevicePixelRatio() {
#if (CC_PLATFORM == CC_PLATFORM_MAC_OSX)
return [[[[NSApplication sharedApplication] delegate] getWindow] backingScaleFactor];
;
#else
return [[UIScreen mainScreen] scale];
return [[UIScreen mainScreen] nativeScale];
#endif //CC_USE_METAL
}
+41 -55
View File
@@ -31,8 +31,7 @@
#include "audio/include/AudioEngine.h"
#ifndef CC_USE_METAL
@interface MyTimer : NSObject
{
@interface MyTimer : NSObject {
cc::Application* _app;
CADisplayLink* _displayLink;
int _fps;
@@ -46,10 +45,8 @@
@implementation MyTimer
- (instancetype)initWithApp:(cc::Application*)app fps:(int)fps
{
if (self = [super init])
{
- (instancetype)initWithApp:(cc::Application*)app fps:(int)fps {
if (self = [super init]) {
_fps = fps;
_app = app;
_displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(renderScene:)];
@@ -58,28 +55,23 @@
return self;
}
- (void)start
{
- (void)start {
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)pause
{
- (void)pause {
_displayLink.paused = TRUE;
}
- (void)resume
{
- (void)resume {
_displayLink.paused = FALSE;
}
- (void)changeFPS:(int)fps
{
- (void)changeFPS:(int)fps {
_displayLink.preferredFramesPerSecond = fps;
}
- (void)renderScene:(id)sender
{
- (void)renderScene:(id)sender {
_app->tick();
}
@@ -88,20 +80,29 @@
namespace cc {
namespace
{
bool setCanvasCallback(se::Object* global)
{
namespace {
bool setCanvasCallback(se::Object* global) {
auto viewLogicalSize = cc::Application::getInstance()->getViewLogicalSize();
se::ScriptEngine* se = se::ScriptEngine::getInstance();
CGRect nativeBounds = [[UIScreen mainScreen] nativeBounds];
int nativeWidth = static_cast<int>(nativeBounds.size.width);
int nativeHeight = static_cast<int>(nativeBounds.size.height);
auto rotation = cc::Device::getDeviceRotation();
bool isLandscape = rotation == cc::Device::Rotation::_90 || rotation == cc::Device::Rotation::_270;
if (isLandscape) std::swap(nativeWidth, nativeHeight);
char commandBuf[200] = {0};
// https://stackoverflow.com/questions/5795978/string-format-for-intptr-t-and-uintptr-t/41897226#41897226
// format intptr_t
//set window.innerWidth/innerHeight in css pixel units
sprintf(commandBuf, "window.innerWidth = %d; window.innerHeight = %d; window.windowHandler = 0x%" PRIxPTR ";",
(int)(viewLogicalSize.x),
(int)(viewLogicalSize.y),
(uintptr_t)(UIApplication.sharedApplication.delegate.window.rootViewController.view) );
sprintf(commandBuf, "window.innerWidth = %d; window.innerHeight = %d; window.nativeWidth = %d; window.nativeHeight = %d; window.windowHandler = 0x%" PRIxPTR ";",
static_cast<int>(viewLogicalSize.x),
static_cast<int>(viewLogicalSize.y),
nativeWidth,
nativeHeight,
reinterpret_cast<uintptr_t>(UIApplication.sharedApplication.delegate.window.rootViewController.view));
se::ScriptEngine* se = se::ScriptEngine::getInstance();
se->evalString(commandBuf);
return true;
}
@@ -114,8 +115,7 @@ namespace
Application* Application::_instance = nullptr;
std::shared_ptr<Scheduler> Application::_scheduler = nullptr;
Application::Application(int width, int height)
{
Application::Application(int width, int height) {
Application::_instance = this;
_scheduler = std::make_shared<Scheduler>();
EventDispatcher::init();
@@ -127,8 +127,7 @@ Application::Application(int width, int height)
#endif
}
Application::~Application()
{
Application::~Application() {
#if USE_AUDIO
AudioEngine::end();
#endif
@@ -143,16 +142,14 @@ Application::~Application()
#endif
}
std::string Application::getCurrentLanguageCode() const
{
std::string Application::getCurrentLanguageCode() const {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
return [currentLanguage UTF8String];
}
bool Application::isDisplayStats()
{
bool Application::isDisplayStats() {
se::AutoHandleScope hs;
se::Value ret;
char commandBuf[100] = "cc.debug.isDisplayStats();";
@@ -160,20 +157,17 @@ bool Application::isDisplayStats()
return ret.toBoolean();
}
void Application::setDisplayStats(bool isShow)
{
void Application::setDisplayStats(bool isShow) {
se::AutoHandleScope hs;
char commandBuf[100] = {0};
sprintf(commandBuf, "cc.debug.setDisplayStats(%s);", isShow ? "true" : "false");
se::ScriptEngine::getInstance()->evalString(commandBuf);
}
void Application::setCursorEnabled(bool value)
{
void Application::setCursorEnabled(bool value) {
}
Application::LanguageType Application::getCurrentLanguage() const
{
Application::LanguageType Application::getCurrentLanguage() const {
// get the current language and country config
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
@@ -205,29 +199,25 @@ Application::LanguageType Application::getCurrentLanguage() const
return LanguageType::ENGLISH;
}
Application::Platform Application::getPlatform() const
{
Application::Platform Application::getPlatform() const {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) // idiom for iOS <= 3.2, otherwise: [UIDevice userInterfaceIdiom] is faster.
return Platform::IPAD;
else
return Platform::IPHONE;
}
bool Application::openURL(const std::string &url)
{
bool Application::openURL(const std::string &url) {
NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
NSURL* nsUrl = [NSURL URLWithString:msg];
return [[UIApplication sharedApplication] openURL:nsUrl];
}
void Application::copyTextToClipboard(const std::string &text)
{
void Application::copyTextToClipboard(const std::string &text) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding];
}
bool Application::init()
{
bool Application::init() {
se::ScriptEngine* se = se::ScriptEngine::getInstance();
se->addRegisterCallback(setCanvasCallback);
@@ -238,28 +228,24 @@ bool Application::init()
return true;
}
void Application::onPause()
{
void Application::onPause() {
#ifndef CC_USE_METAL
[_timer pause];
#endif
}
void Application::onResume()
{
void Application::onResume() {
#ifndef CC_USE_METAL
[_timer resume];
#endif
}
std::string Application::getSystemVersion()
{
std::string Application::getSystemVersion() {
NSString* systemVersion = [UIDevice currentDevice].systemVersion;
return [systemVersion UTF8String];
}
void Application::setPreferredFramesPerSecond(int fps)
{
void Application::setPreferredFramesPerSecond(int fps) {
_fps = fps;
#ifndef CC_USE_METAL
[_timer changeFPS:_fps];
+1 -1
View File
@@ -96,7 +96,7 @@ cc::Vec4 Device::getSafeAreaEdge()
return cc::Vec4();
}
int Device::getDevicePixelRatio()
float Device::getDevicePixelRatio()
{
return 1;
}