2013-04-09 09:21:53 +08:00
|
|
|
#include "CCApplication.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCEGLView.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
|
|
|
|
#define LOGD(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
|
|
|
|
NS_CC_BEGIN;
|
|
|
|
|
|
|
|
// sharedApplication pointer
|
2013-06-20 14:13:12 +08:00
|
|
|
Application * Application::sm_pSharedApplication = 0;
|
|
|
|
long Application::_animationInterval = 1000;
|
2013-04-09 09:21:53 +08:00
|
|
|
|
|
|
|
// convert the timespec into milliseconds
|
|
|
|
static long time2millis(struct timespec *times)
|
|
|
|
{
|
|
|
|
return times->tv_sec*1000 + times->tv_nsec/1000000;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Application::Application()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
CC_ASSERT(! sm_pSharedApplication);
|
|
|
|
sm_pSharedApplication = this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Application::~Application()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
CC_ASSERT(this == sm_pSharedApplication);
|
|
|
|
sm_pSharedApplication = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" void mainLoopIter(void)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLView::sharedOpenGLView()->handleEvents();
|
|
|
|
Director::sharedDirector()->mainLoop();
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Application::run()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
struct timespec time_struct;
|
|
|
|
long update_time;
|
|
|
|
|
|
|
|
// Initialize instance and cocos2d.
|
|
|
|
if (!applicationDidFinishLaunching())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &time_struct);
|
|
|
|
update_time = time2millis(&time_struct);
|
|
|
|
|
|
|
|
// XXX: Set to 1FPS while debugging
|
|
|
|
emscripten_set_main_loop(&mainLoopIter, 0, 1);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Application::setAnimationInterval(double interval)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
// interval in milliseconds
|
2013-06-15 14:03:30 +08:00
|
|
|
_animationInterval = (long)(interval * 1000);
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Application::setResourceRootPath(const std::string& rootResDir)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resourceRootPath = rootResDir;
|
|
|
|
if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resourceRootPath += '/';
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils* pFileUtils = FileUtils::sharedFileUtils();
|
2013-04-09 09:21:53 +08:00
|
|
|
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
|
2013-06-15 14:03:30 +08:00
|
|
|
searchPaths.insert(searchPaths.begin(), _resourceRootPath);
|
2013-04-09 09:21:53 +08:00
|
|
|
pFileUtils->setSearchPaths(searchPaths);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const std::string& Application::getResourceRootPath(void)
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _resourceRootPath;
|
2013-04-09 09:21:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TargetPlatform Application::getTargetPlatform()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
return kTargetEmscripten;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// static member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-06-20 14:13:12 +08:00
|
|
|
Application* Application::sharedApplication()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
CC_ASSERT(sm_pSharedApplication);
|
|
|
|
return sm_pSharedApplication;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ccLanguageType Application::getCurrentLanguage()
|
2013-04-09 09:21:53 +08:00
|
|
|
{
|
|
|
|
return kLanguageEnglish;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END;
|