Qt 5: Add CCAccelerometer implementation

[nemo] Add QtSensors-based Accelerometer implementation
This commit is contained in:
Thomas Perl 2013-07-23 13:30:07 +00:00
parent 7f0a05d8c0
commit bcee05d471
7 changed files with 149 additions and 14 deletions

View File

@ -0,0 +1,42 @@
/**
*
* Cocos2D-X Qt 5 Platform
*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Thomas Perl <thomas.perl@jollamobile.com>
*
* 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.
*
**/
#include "AccelerometerListener.h"
#include <QObject>
AccelerometerListener::AccelerometerListener(CCAccelerometer *accelerometer)
: QObject()
, m_accelerometer(accelerometer)
{
}
void
AccelerometerListener::onReadingChanged()
{
m_accelerometer->readingChanged();
}

View File

@ -0,0 +1,50 @@
/**
*
* Cocos2D-X Qt 5 Platform
*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Thomas Perl <thomas.perl@jollamobile.com>
*
* 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.
*
**/
#ifndef COCOS2DX_ACCELEROMETER_LISTENER_QT5_H
#define COCOS2DX_ACCELEROMETER_LISTENER_QT5_H
#include "CCAccelerometer.h"
#include <QObject>
USING_NS_CC;
class AccelerometerListener : public QObject {
Q_OBJECT
public:
AccelerometerListener(CCAccelerometer *accelerometer);
public slots:
void onReadingChanged();
private:
CCAccelerometer *m_accelerometer;
};
#endif /* COCOS2DX_ACCELEROMETER_LISTENER_QT5_H */

View File

@ -26,6 +26,10 @@
**/
#include "CCAccelerometer.h"
#include "AccelerometerListener.h"
#include <QAccelerometer>
#include <QAccelerometerReading>
NS_CC_BEGIN
@ -33,11 +37,18 @@ static CCAccelerometer *
shared_accelerometer = NULL;
CCAccelerometer::CCAccelerometer()
: m_accelerometer(new QAccelerometer)
, m_listener(new AccelerometerListener(this))
, m_delegate(NULL)
{
QObject::connect(m_accelerometer, SIGNAL(readingChanged()),
m_listener, SLOT(onReadingChanged()));
}
CCAccelerometer::~CCAccelerometer()
{
delete m_listener;
delete m_accelerometer;
}
CCAccelerometer *
@ -51,25 +62,39 @@ CCAccelerometer::sharedAccelerometer()
}
void
CCAccelerometer::addDelegate(CCAccelerometerDelegate *pDelegate)
{
}
void
CCAccelerometer::removeDelegate(CCAccelerometerDelegate *pDelegate)
{
}
void
CCAccelerometer::setDelegate(CCAccelerometerDelegate *pDelegate)
{
m_delegate = pDelegate;
}
void
CCAccelerometer::setAccelerometerInterval(float interval)
{
if (interval == 0.0) {
m_accelerometer->setDataRate(0.0);
} else {
// Interval is specified in seconds
m_accelerometer->setDataRate(1.0 / interval);
}
}
void
CCAccelerometer::readingChanged()
{
if (m_delegate == NULL) {
return;
}
QAccelerometerReading *reading = m_accelerometer->reading();
CCAcceleration accel;
accel.x = reading->x();
accel.y = reading->y();
accel.z = reading->z();
accel.timestamp = reading->timestamp();
m_delegate->didAccelerate(&accel);
}
NS_CC_END

View File

@ -32,6 +32,9 @@
#include "platform/CCCommon.h"
#include "platform/CCAccelerometerDelegate.h"
class QAccelerometer;
class AccelerometerListener;
NS_CC_BEGIN
class CCAccelerometer {
@ -41,10 +44,16 @@ class CCAccelerometer {
static CCAccelerometer *sharedAccelerometer();
void removeDelegate(CCAccelerometerDelegate *pDelegate);
void addDelegate(CCAccelerometerDelegate *pDelegate);
void setDelegate(CCAccelerometerDelegate *pDelegate);
void setAccelerometerInterval(float interval);
/* Functions to be called from AccelerometerListener */
void readingChanged();
private:
QAccelerometer *m_accelerometer;
AccelerometerListener *m_listener;
CCAccelerometerDelegate *m_delegate;
};
NS_CC_END

View File

@ -72,6 +72,7 @@ SOURCES += ../actions/CCAction.cpp \
../platform/qt5/CCDevice.cpp \
../platform/qt5/CCApplication.cpp \
../platform/qt5/CCAccelerometer.cpp \
../platform/qt5/AccelerometerListener.cpp \
../platform/linux/CCImage.cpp \
../script_support/CCScriptSupport.cpp \
../sprite_nodes/CCAnimation.cpp \
@ -137,6 +138,9 @@ SOURCES += ../actions/CCAction.cpp \
../ccFPSImages.c \
../cocos2d.cpp
# Headers with QObject subclasses (will be processed by moc)
HEADERS += ../platform/qt5/AccelerometerListener.h
# WebP
INCLUDEPATH += ../platform/third_party/marmalade/libwebp/webp
SOURCES += $$files(../platform/third_party/marmalade/libwebp/dec/*.c)

View File

@ -15,6 +15,7 @@ CONFIG(debug, debug|release) {
}
OBJECTS_DIR = obj/$${BUILD_TYPE}
MOC_DIR = obj/$${BUILD_TYPE}
LIB_OUTPUT_DIR = $${PWD}/../../lib/$${OS_TYPE}/$${BUILD_TYPE}
# Installation location of binaries
@ -32,6 +33,9 @@ COCOS2DX_SYSTEM_LIBS += -lz
COCOS2DX_SYSTEM_LIBS += -ljpeg -ltiff -lpng
COCOS2DX_SYSTEM_LIBS += -lfontconfig -lfreetype
# Sensors module needed for CCAccelerometer
QT += sensors
LINK_AGAINST_COCOS2DX = -L$${LIB_OUTPUT_DIR} -lcocos2d $${COCOS2DX_SYSTEM_LIBS}
# CocosDenshion (audio library)

View File

@ -1,6 +1,6 @@
Name: cocos2d-x
Version: 1.0.0
Release: 1
Release: 2
Summary: Cocos2D-X Cross-Platform 2D Games Framework
Group: System/GUI/Other
@ -12,6 +12,7 @@ BuildRequires: pkgconfig(glesv2)
BuildRequires: pkgconfig(Qt5Core)
BuildRequires: pkgconfig(Qt5Gui)
BuildRequires: pkgconfig(Qt5Multimedia)
BuildRequires: pkgconfig(Qt5Sensors)
BuildRequires: pkgconfig(zlib)
BuildRequires: pkgconfig(fontconfig)
BuildRequires: pkgconfig(freetype2)