2014-03-29 03:52:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-10 08:41:49 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-03-29 03:52:16 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "platform/CCDevice.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/winrt/CCFreeTypeFont.h"
|
2014-09-10 08:53:08 +08:00
|
|
|
#include "platform/CCStdC.h"
|
2014-03-29 03:52:16 +08:00
|
|
|
|
|
|
|
using namespace Windows::Graphics::Display;
|
|
|
|
using namespace Windows::Devices::Sensors;
|
|
|
|
using namespace Windows::Foundation;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CCFreeTypeFont sFT;
|
|
|
|
|
|
|
|
int Device::getDPI()
|
|
|
|
{
|
2014-10-28 02:07:00 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
|
2014-03-29 03:52:16 +08:00
|
|
|
static const float dipsPerInch = 96.0f;
|
|
|
|
return floor(DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
|
2014-10-29 12:01:26 +08:00
|
|
|
#elif defined WP8_SHADER_COMPILER
|
|
|
|
return 0;
|
2014-10-28 02:07:00 +08:00
|
|
|
#else
|
|
|
|
return cocos2d::GLViewImpl::sharedOpenGLView()->GetDPI();
|
|
|
|
#endif
|
2014-03-29 03:52:16 +08:00
|
|
|
}
|
|
|
|
|
2014-04-20 00:10:34 +08:00
|
|
|
static Accelerometer^ sAccelerometer = nullptr;
|
|
|
|
|
|
|
|
|
2014-03-29 03:52:16 +08:00
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled)
|
|
|
|
{
|
2014-10-23 23:40:34 +08:00
|
|
|
#ifndef WP8_SHADER_COMPILER
|
2014-03-29 03:52:16 +08:00
|
|
|
static Windows::Foundation::EventRegistrationToken sToken;
|
2014-04-20 00:10:34 +08:00
|
|
|
static bool sEnabled = false;
|
2014-03-29 03:52:16 +08:00
|
|
|
|
2014-09-24 01:01:42 +08:00
|
|
|
// we always need to reset the accelerometer
|
|
|
|
if (sAccelerometer)
|
|
|
|
{
|
|
|
|
sAccelerometer->ReadingChanged -= sToken;
|
|
|
|
sAccelerometer = nullptr;
|
|
|
|
sEnabled = false;
|
|
|
|
}
|
|
|
|
|
2014-04-20 00:10:34 +08:00
|
|
|
if (isEnabled)
|
2014-03-29 03:52:16 +08:00
|
|
|
{
|
2014-04-20 00:10:34 +08:00
|
|
|
sAccelerometer = Accelerometer::GetDefault();
|
|
|
|
|
|
|
|
if(sAccelerometer == nullptr)
|
|
|
|
{
|
2015-04-03 11:51:00 +08:00
|
|
|
// It's not a friendly experience and may cause crash.
|
|
|
|
//MessageBox("This device does not have an accelerometer.","Alert");
|
|
|
|
log("This device does not have an accelerometer.");
|
2014-04-20 00:10:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-29 03:52:16 +08:00
|
|
|
setAccelerometerInterval(0.0f);
|
2014-04-20 00:10:34 +08:00
|
|
|
sEnabled = true;
|
2014-03-29 03:52:16 +08:00
|
|
|
|
2014-04-20 00:10:34 +08:00
|
|
|
sToken = sAccelerometer->ReadingChanged += ref new TypedEventHandler
|
2014-03-29 03:52:16 +08:00
|
|
|
<Accelerometer^,AccelerometerReadingChangedEventArgs^>
|
2014-04-20 00:10:34 +08:00
|
|
|
([](Accelerometer^ a, AccelerometerReadingChangedEventArgs^ e)
|
2014-03-29 03:52:16 +08:00
|
|
|
{
|
2014-04-20 00:10:34 +08:00
|
|
|
if (!sEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccelerometerReading^ reading = e->Reading;
|
2014-03-29 03:52:16 +08:00
|
|
|
cocos2d::Acceleration acc;
|
|
|
|
acc.x = reading->AccelerationX;
|
|
|
|
acc.y = reading->AccelerationY;
|
|
|
|
acc.z = reading->AccelerationZ;
|
|
|
|
acc.timestamp = 0;
|
|
|
|
|
2014-08-04 14:17:14 +08:00
|
|
|
auto orientation = GLViewImpl::sharedOpenGLView()->getDeviceOrientation();
|
2014-03-29 03:52:16 +08:00
|
|
|
|
2014-11-21 01:00:22 +08:00
|
|
|
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
2014-03-29 03:52:16 +08:00
|
|
|
switch (orientation)
|
|
|
|
{
|
|
|
|
case DisplayOrientations::Portrait:
|
|
|
|
acc.x = reading->AccelerationX;
|
|
|
|
acc.y = reading->AccelerationY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisplayOrientations::Landscape:
|
|
|
|
acc.x = -reading->AccelerationY;
|
|
|
|
acc.y = reading->AccelerationX;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisplayOrientations::PortraitFlipped:
|
|
|
|
acc.x = -reading->AccelerationX;
|
|
|
|
acc.y = reading->AccelerationY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisplayOrientations::LandscapeFlipped:
|
|
|
|
acc.x = reading->AccelerationY;
|
2015-01-20 01:38:49 +08:00
|
|
|
acc.y = -reading->AccelerationX;
|
2014-03-29 03:52:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
acc.x = reading->AccelerationX;
|
|
|
|
acc.y = reading->AccelerationY;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-21 01:00:22 +08:00
|
|
|
#else // Windows Store App
|
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/apps/dn440593
|
|
|
|
switch (orientation)
|
|
|
|
{
|
|
|
|
case DisplayOrientations::Portrait:
|
|
|
|
acc.x = reading->AccelerationY;
|
|
|
|
acc.y = -reading->AccelerationX;
|
|
|
|
break;
|
2014-10-15 05:16:52 +08:00
|
|
|
|
2014-11-21 01:00:22 +08:00
|
|
|
case DisplayOrientations::Landscape:
|
|
|
|
acc.x = reading->AccelerationX;
|
|
|
|
acc.y = reading->AccelerationY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisplayOrientations::PortraitFlipped:
|
|
|
|
acc.x = -reading->AccelerationY;
|
|
|
|
acc.y = reading->AccelerationX;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisplayOrientations::LandscapeFlipped:
|
2015-01-20 01:38:49 +08:00
|
|
|
acc.x = -reading->AccelerationX;
|
|
|
|
acc.y = -reading->AccelerationY;
|
2014-11-21 01:00:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
acc.x = reading->AccelerationY;
|
|
|
|
acc.y = -reading->AccelerationX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-20 00:10:34 +08:00
|
|
|
std::shared_ptr<cocos2d::InputEvent> event(new AccelerometerEvent(acc));
|
2014-08-04 14:17:14 +08:00
|
|
|
cocos2d::GLViewImpl::sharedOpenGLView()->QueueEvent(event);
|
2014-03-29 03:52:16 +08:00
|
|
|
});
|
|
|
|
}
|
2014-10-23 23:40:34 +08:00
|
|
|
#endif
|
2014-03-29 03:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::setAccelerometerInterval(float interval)
|
|
|
|
{
|
2014-04-20 00:10:34 +08:00
|
|
|
if (sAccelerometer)
|
2014-03-29 03:52:16 +08:00
|
|
|
{
|
2015-01-17 07:36:16 +08:00
|
|
|
try {
|
|
|
|
int minInterval = sAccelerometer->MinimumReportInterval;
|
|
|
|
int reqInterval = (int) interval;
|
|
|
|
sAccelerometer->ReportInterval = reqInterval < minInterval ? minInterval : reqInterval;
|
|
|
|
}
|
|
|
|
catch (Platform::COMException^)
|
|
|
|
{
|
|
|
|
CCLOG("Device::setAccelerometerInterval not supported on this device");
|
|
|
|
}
|
2014-04-20 00:10:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Device::setAccelerometerInterval: accelerometer not enabled.");
|
2014-03-29 03:52:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-06 10:36:55 +08:00
|
|
|
Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
|
2014-03-29 03:52:16 +08:00
|
|
|
{
|
|
|
|
Data ret;
|
|
|
|
ssize_t dataLen;
|
|
|
|
|
|
|
|
unsigned char* data = sFT.initWithString(text, textDefinition, align, width, height, dataLen);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
ret.fastSet(data, dataLen);
|
2014-05-06 10:36:55 +08:00
|
|
|
hasPremultipliedAlpha = false;
|
2014-03-29 03:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-18 20:41:12 +08:00
|
|
|
void Device::setKeepScreenOn(bool value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-29 03:52:16 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|