2013-07-23 01:58:48 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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 "CCApplication.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <string>
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2013-07-23 17:35:22 +08:00
|
|
|
#include <QGuiApplication>
|
2013-07-23 01:58:48 +08:00
|
|
|
#include <QLocale>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
class Cocos2DQt5MainloopIntegration : public QObject {
|
|
|
|
public:
|
2013-07-23 22:06:48 +08:00
|
|
|
Cocos2DQt5MainloopIntegration()
|
2013-07-23 01:58:48 +08:00
|
|
|
: QObject()
|
2013-07-23 22:06:48 +08:00
|
|
|
, m_timer(0)
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
2013-07-23 22:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setInterval(int interval_ms)
|
|
|
|
{
|
|
|
|
if (m_timer != 0) {
|
|
|
|
killTimer(m_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_timer = startTimer(interval_ms);
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void timerEvent(QTimerEvent *event)
|
|
|
|
{
|
2013-07-26 16:55:46 +08:00
|
|
|
cocos2d::Director::getInstance()->mainLoop();
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
2013-07-23 22:06:48 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_timer;
|
2013-07-23 01:58:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-23 22:06:48 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-23 01:58:48 +08:00
|
|
|
// Application singleton
|
2013-07-26 16:55:46 +08:00
|
|
|
static Application *
|
2013-07-23 01:58:48 +08:00
|
|
|
application = NULL;
|
|
|
|
|
|
|
|
static int
|
|
|
|
global_fake_argc = 1;
|
|
|
|
|
|
|
|
static char *
|
|
|
|
global_fake_argv[1];
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
// @deprecated Use getInstance() instead
|
|
|
|
Application *
|
|
|
|
Application::sharedApplication()
|
|
|
|
{
|
|
|
|
return getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
Application *
|
|
|
|
Application::getInstance()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
CC_ASSERT(application != NULL);
|
|
|
|
return application;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::Application()
|
2013-07-23 01:58:48 +08:00
|
|
|
: m_application(NULL)
|
|
|
|
, m_animationInterval(1000 / 60)
|
|
|
|
, m_resourceRootPath("")
|
2013-07-23 22:06:48 +08:00
|
|
|
, m_mainloop(new Cocos2DQt5MainloopIntegration)
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
// Inject argv[0] by resolving /proc/self/exe
|
|
|
|
QString filename = QDir("/proc/self/exe").canonicalPath();
|
|
|
|
QByteArray utf8 = filename.toUtf8();
|
|
|
|
global_fake_argv[0] = strdup(utf8.data());
|
|
|
|
|
|
|
|
m_application = new QGuiApplication(global_fake_argc, global_fake_argv);
|
|
|
|
|
|
|
|
CC_ASSERT(application == NULL);
|
|
|
|
application = this;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::~Application()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
2013-07-23 22:06:48 +08:00
|
|
|
delete m_mainloop;
|
2013-07-23 01:58:48 +08:00
|
|
|
delete m_application;
|
|
|
|
|
|
|
|
CC_ASSERT(application == this);
|
|
|
|
application = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::run()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
// Initialize instance and cocos2d.
|
|
|
|
if (!applicationDidFinishLaunching()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-23 22:06:48 +08:00
|
|
|
m_mainloop->setInterval(m_animationInterval);
|
2013-07-23 01:58:48 +08:00
|
|
|
|
|
|
|
return m_application->exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::setAnimationInterval(double interval)
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
// Interval is expressed in seconds
|
|
|
|
m_animationInterval = interval * 1000;
|
|
|
|
|
2013-07-23 22:06:48 +08:00
|
|
|
m_mainloop->setInterval(m_animationInterval);
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::setResourceRootPath(const std::string &rootResDir)
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
m_resourceRootPath = rootResDir;
|
|
|
|
if (m_resourceRootPath[m_resourceRootPath.length() - 1] != '/') {
|
|
|
|
m_resourceRootPath += '/';
|
|
|
|
}
|
2013-07-26 16:55:46 +08:00
|
|
|
FileUtils* pFileUtils = FileUtils::getInstance();
|
2013-07-23 01:58:48 +08:00
|
|
|
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
|
|
|
|
searchPaths.insert(searchPaths.begin(), m_resourceRootPath);
|
|
|
|
pFileUtils->setSearchPaths(searchPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::getResourceRootPath()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
return m_resourceRootPath;
|
|
|
|
}
|
|
|
|
|
2013-08-01 15:20:17 +08:00
|
|
|
Application::Platform
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::getTargetPlatform()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
2013-08-01 15:20:17 +08:00
|
|
|
return Platform::OS_LINUX;
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
2013-08-01 15:20:17 +08:00
|
|
|
LanguageType
|
2013-07-26 16:55:46 +08:00
|
|
|
Application::getCurrentLanguage()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
QLocale locale;
|
|
|
|
|
|
|
|
switch (locale.language()) {
|
|
|
|
case QLocale::English:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::ENGLISH;
|
2013-07-23 21:47:38 +08:00
|
|
|
case QLocale::Chinese:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::CHINESE;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::French:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::FRENCH;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Italian:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::ITALIAN;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::German:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::GERMAN;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Spanish:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::SPANISH;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Russian:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::RUSSIAN;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Korean:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::KOREAN;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Japanese:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::JAPANESE;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Hungarian:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::HUNGARIAN;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Portuguese:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::PORTUGUESE;
|
2013-07-23 01:58:48 +08:00
|
|
|
case QLocale::Arabic:
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::ARABIC;
|
2013-07-23 01:58:48 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-01 15:20:17 +08:00
|
|
|
return LanguageType::ENGLISH;
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|